-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit.js
More file actions
201 lines (168 loc) · 5.16 KB
/
Copy pathgit.js
File metadata and controls
201 lines (168 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const { exec } = require('electron').remote.require('child_process')
const execPromise = (command, options = {}) => new Promise((resolve, reject) => {
exec(command, options, (error, stdout, stderr) => {
if (error) {
reject(error)
} else {
resolve({ stdout, stderr })
}
})
})
class GitPlugin {
constructor() {
this.id = 'git'
this.name = 'Git'
this.description = 'Makes it easy to store version history and sync your vault across computers with Git.'
this.defaultOn = false
this.app = null
this.instance = null
this.cwd = null
this.dirtyState = {
localDirty: false,
remoteDirty: false,
untracked: false
}
this.actionState = null
this.isRepo = true
}
init(app, instance) {
this.app = app
this.instance = instance
this.instance.registerStatusBarItem()
this.instance.registerGlobalCommand({
id: 'git-sync',
name: 'Git: sync changes',
callback: () => this.sync()
})
const style = `
.status-bar-item {
overflow: hidden;
}
.git-status-bar-button {
color: inherit;
font: inherit;
padding: 0 4px;
background: inherit;
border: none;
border-radius: 0;
}
`
const styleEl = document.createElement('style')
styleEl.innerText = style
document.head.appendChild(styleEl)
}
onEnable() {
this.cwd = this.app.vault.adapter.basePath
setInterval(() => {
if (this.actionState) return
this.updateDirty()
}, 10000)
setTimeout(() => {
this.updateStatusBar()
this.updateDirty()
}, 0)
const forceUntracked = () => {
this.dirtyState.untracked = true
this.updateStatusBar()
this.updateDirty()
}
this.instance.registerEvent(this.app.vault.on('create', forceUntracked))
this.instance.registerEvent(this.app.vault.on('modify', forceUntracked))
this.instance.registerEvent(this.app.vault.on('delete', forceUntracked))
this.instance.registerEvent(this.app.vault.on('rename', forceUntracked))
}
async updateStatusBar() {
this.instance.statusBarEl.empty()
if (!this.isRepo) {
this.instance.statusBarEl.createEl('span', {
cls: 'status-bar-item-segment',
text: 'Not a repository'
})
} else if (this.actionState) {
this.instance.statusBarEl.createEl('span', {
cls: 'status-bar-item-segment',
text: this.actionState
})
} else if (this.dirtyState.localDirty || this.dirtyState.remoteDirty || this.dirtyState.untracked) {
const el = this.instance.statusBarEl.createEl('button', {
cls: 'status-bar-item-segment git-status-bar-button',
text: 'Click to sync'
})
el.addEventListener('click', () => this.sync())
} else {
this.instance.statusBarEl.createEl('span', {
cls: 'status-bar-item-segment',
text: 'All up-to-date'
})
}
}
async updateDirty() {
const { cwd } = this
try {
await execPromise('git fetch', { cwd })
} catch (error) {
console.error(error)
if (error.message.includes('not a git repository')) {
this.isRepo = false
}
this.dirtyState = {
localDirty: false,
remoteDirty: false,
untracked: false
}
this.actionState = null
this.updateStatusBar()
return
}
let untracked = false
let remoteDirty = false
let localDirty = false
const { stdout: status } = await execPromise('git status --porcelain', { cwd })
untracked = status.length !== 0
try {
const { stdout: local } = await execPromise('git rev-parse @', { cwd })
const { stdout: remote } = await execPromise('git rev-parse @{u}', { cwd })
const { stdout: base } = await execPromise('git merge-base @ @{u}', { cwd })
console.log(local, remote, base)
const synced = local === remote
const diverged = remote !== base && local !== base
remoteDirty = (local === base || diverged) && !synced
localDirty = (remote === base || diverged) && !synced
} catch {}
this.dirtyState = { remoteDirty, localDirty, untracked }
this.updateStatusBar()
}
async sync() {
const { cwd } = this
try {
this.setActionState('Syncing...')
await this.updateDirty()
if (this.dirtyState.untracked) {
this.setActionState('Committing...')
const formattedDate = window.moment().format('MMMM Do, YYYY h:mma')
await execPromise('git add .', { cwd })
await execPromise(`git commit -m "${formattedDate}"`, { cwd })
await this.updateDirty()
}
if (this.dirtyState.remoteDirty) {
this.setActionState('Pulling...')
await execPromise('git pull', { cwd })
await this.updateDirty()
}
if (this.dirtyState.localDirty) {
this.setActionState('Pushing...')
await execPromise('git push', { cwd })
await this.updateDirty()
}
this.setActionState(null)
} catch (error) {
console.error(error)
this.setActionState('Error!')
}
}
setActionState(actionState) {
this.actionState = actionState
this.updateStatusBar()
}
}
module.exports = () => new GitPlugin()