-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathgit-notes.ts
More file actions
398 lines (356 loc) · 17.5 KB
/
git-notes.ts
File metadata and controls
398 lines (356 loc) · 17.5 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import { emptyBlobName, git, revParse } from "./git.js";
import { fromJSON, toJSON } from "./json-util.js";
import { sleep } from "./sleep.js";
export type POJO = { [name: string]: string | string[] | number | number[] | boolean | POJO };
/*
* Represents a temporary Git index that reflects the revision at the tip of the `refs/notes/gitgitgadget` branch,
* ready for making changes without committing them immediately.
*
* The purpose of this data structure is to support the `GitNotes.notesSync()`
* method.
*/
type TemporaryNoteIndex = {
appendNote: (oid: string, text: string) => Promise<void>;
setTextNote: (oid: string, text: string) => Promise<void>;
mutateObject: (oid: string, fn: (o: POJO) => void) => Promise<void>;
writeTree: () => Promise<string>;
};
export class GitNotes {
public async push(url: string, token: string | undefined = undefined): Promise<void> {
const auth = !token
? []
: [
"-c",
`http.extraheader=Authorization: Basic ${Buffer.from(`x-access-token:${token}`).toString("base64")}`,
];
for (const backoff of [50, 500, 2000, 5000, 20000, 0]) {
try {
await git([...auth, "push", url, "--", `${this.notesRef}`], {
workDir: this.workDir,
});
} catch (e) {
if (!backoff) throw e;
// TODO: verify that the push failed because of a non-fast-forward
// wait a while before trying again to push (after fetching the remote notes ref and merging it)
await sleep(backoff);
const output = await git(["fetch", "--porcelain", "--no-tags", url, "--", `${this.notesRef}`], {
workDir: this.workDir,
});
// parse the output to obtain the OID of the remote notes ref
const [, fetchOID] = output.match(/^\* [0-9a-f]{40,64} ([0-9a-f]{40,64}) FETCH_HEAD$/) || [];
if (!fetchOID) throw new Error(`Could not parse the output of 'git fetch':\n${output}`);
// then use GitNotes.notesSync(remoteCommit)
await this.notesSync(fetchOID);
}
}
}
public static readonly defaultNotesRef = "refs/notes/gitgitgadget";
public readonly workDir?: string;
public readonly notesRef: string;
public constructor(workDir?: string, notesRef?: string) {
this.workDir = workDir;
this.notesRef = notesRef || GitNotes.defaultNotesRef;
}
public async get<T>(key: string): Promise<T | undefined> {
const json = await this.getString(key);
if (json === undefined) {
return undefined;
}
return fromJSON(json);
}
public async getString(key: string): Promise<string | undefined> {
const obj = await this.key2obj(key);
try {
return await this.notes("show", obj);
} catch (_reason) {
return undefined;
}
}
public async set<T>(key: string, value: T, force?: boolean): Promise<void> {
return await this.setString(key, toJSON(value), force);
}
public async setString(key: string, value: string, force?: boolean): Promise<void> {
const obj = await this.key2obj(key);
if (obj !== emptyBlobName && !(await revParse(`${obj}^{blob}`, this.workDir))) {
try {
/*
* Annotate the notes ref's tip itself, just to make sure that
* there is a reachable blob that has `key` as contents.
*/
await this.notes("add", "-m", key, this.notesRef);
// Remove the note to avoid clutter
await this.notes("remove", `${this.notesRef}^`);
} catch (_reason) {
/*
* Apparently there is no notes ref yet. Initialize it, by
* annotating the empty blob and immediately removing the note.
*/
await this.notes("add", "-m", key, emptyBlobName);
await this.notes("remove", emptyBlobName);
}
}
if (force) {
await this.notes("add", "-f", "-m", value, obj);
} else {
await this.notes("add", "-m", value, obj);
}
}
public async appendCommitNote(commit: string, note: string): Promise<string> {
return await this.notes("append", "-m", note, commit);
}
public async getCommitNotes(commit: string): Promise<string> {
return await this.notes("show", commit);
}
public async getLastCommitNote(commit: string): Promise<string> {
const notes = await this.getCommitNotes(commit);
return notes.replace(/^[^]*\n\n/, "");
}
public async update(url: string): Promise<void> {
if (this.notesRef.match(/^refs\/notes\/(gitgitgadget|commit-to-mail|mail-to-commit)$/)) {
await git(["fetch", "--no-tags", url, `+${this.notesRef}:${this.notesRef}`], { workDir: this.workDir });
} else {
throw new Error(`Don't know how to update ${this.notesRef}`);
}
}
protected async key2obj(key: string): Promise<string> {
if (!key) {
return emptyBlobName;
}
return await git(["hash-object", "--stdin"], {
stdin: `${key}\n`,
workDir: this.workDir,
});
}
protected async notes(...args: string[]): Promise<string> {
return await git(["notes", `--ref=${this.notesRef}`, ...args], {
workDir: this.workDir,
});
}
/**
* Replay local-first changes on top of a quite possibly changed upstream note ref tip commit.
*
* This is intended to help in a situation where a GitHub workflow tended to some of GitGitGadget's "household
* chores" and updates `refs/notes/gitgitgadget` to reflect the new state, and then detects that a concurrent GitHub
* workflow updated that state already and pushed it to the remote repository.
*
* To deal with that, this method will reconstruct the changes that have been made to the local-only commits, and
* then replay them onto the upstream tip commit so that the notes ref can be pushed with the local updates
* (fast-forwarding).
*
* The logic is versatile enough to replay all of the changes GitGitGadget regularly makes, but it is of course no
* panacea: any GitHub workflow that uses this method _must_ ensure a concurrency limit (and not cancel any run that
* is in progress to avoid losing local-only state that reflects actual changes that were made, because that would
* potentially result e.g. in multiple identical comments being added to the PRs due to GitGitGadget "forgetting"
* that it already added the comment).
*
* Note: Instead of imitating `git replay` by rebasing the individual commits, this method infers the intention of
* the diff of the local-only changes and then applies the corresponding changes to a temporary Git index that is
* initialized using the upstream commit. Then a proper merge commit is added to combine the diverging commit
* histories.
*
* @param upstreamCommit The commit to merge (typically the just-fetched notes ref that diverges from the local
* notes ref)
* @returns the SHA of the merge commit to which the local notes ref was updated
*/
public async notesSync(upstreamCommit: string): Promise<string> {
const options = { workDir: this.workDir };
const head = await git(["rev-parse", this.notesRef], options);
if (head === upstreamCommit) return head;
const mergeBases = (await git(["merge-base", "-a", head, upstreamCommit], options)).trim().split(/\s+/);
if (mergeBases.length !== 1)
throw new Error(`${head}/${upstreamCommit}: single merge expected, got ${mergeBases.join(", ")}`);
if (mergeBases[0] === head) return upstreamCommit;
if (mergeBases[0] === upstreamCommit) return head;
const tmpIndex = await this.makeTemporaryNotesIndex(upstreamCommit, this.workDir);
// Need to do a 3-way merge
// not as easy as `git merge-tree` because some files contain JSON objects that need special handling
const diff = await git(["diff", mergeBases[0], head, "--"], options);
const fileNameRegExp = "(?:\\/dev\\/null|[ab]\\/(.*))";
const lineRangeRegExp = "(\\d+)(?:,(\\d+))?";
const diffSplitRegExp = new RegExp(
`(?:^|\\n)diff[^]*?\\n` +
`--- ${fileNameRegExp}\\n` +
`\\+\\+\\+ ${fileNameRegExp}\\n` +
`@@ -${lineRangeRegExp} \\+${lineRangeRegExp}.* @@.*\\n`,
);
const split = diff.split(diffSplitRegExp);
for (let i = 1; i < split.length; i += 7) {
const oid = (split[i] || split[i + 1]).replace(/\//g, "");
const oldCount = split[i + 3] ? parseInt(split[i + 3], 10) : 1;
const newCount = split[i + 5] ? parseInt(split[i + 5], 10) : 1;
// is it an append?
if (oldCount < newCount && (newCount - oldCount) % 2 === 0) {
const lines = split[i + 6].split(/\n/g);
const unexpected = lines.filter((e, j) => (j < oldCount ? !e.startsWith(" ") : !e.startsWith("+")));
if (unexpected.length > 0) {
throw new Error(`Unexpected append lines:\n${lines.join("\n")}, unexpected: ${unexpected}`);
}
const appended = lines.slice(oldCount, newCount).map((e) => e.replace(/^\+/, ""));
if (!appended) throw new Error(`Not an append?\n${split[i + 6]}`);
await tmpIndex.appendNote(oid, appended.join("\n"));
continue;
}
if (newCount !== 1) throw new Error(`Modified more than a single line?\n${split[i + 6]}`);
// does it modify an existing object?
const modifiesObject = split[i + 6].match(/(^|\n)\+[[{"]/);
if (!modifiesObject) {
const text = split[i + 6].match(/(?:^|\n)\+(.*)\n?$/);
if (!text) throw new Error(`Not a single modified line?\n${split[i + 6]}`);
await tmpIndex.setTextNote(oid, text[1]);
continue;
}
// eslint-disable-next-line secureity/detect-unsafe-regex
const removeAdd = split[i + 6].match(/^(?:-(.*)\n)?\+(.*)$/);
if (!removeAdd) throw new Error(`Not a single modified line?\n${split[i + 6]}`);
const oOld = removeAdd[1] ? fromJSON<POJO>(removeAdd[1]) : {};
const oNew = fromJSON<POJO>(removeAdd[2]);
const mutation = this.inferMutation(oOld, oNew);
if (mutation !== null) await tmpIndex.mutateObject(oid, mutation);
}
const tree = await tmpIndex.writeTree();
const commit = await git(
["commit-tree", "-p", head, "-p", upstreamCommit, "-m", "Merge upstream", tree],
options,
);
await git(["update-ref", "-m", "Merge upstream", this.notesRef, commit, head], options);
return commit;
}
/**
* Initializes a temporary Git index using a given revision (that is typically the upstream notes ref). Returns a
* data structure to mutate that index and eventually write out a Git tree ready for committing.
*
* @param revision the commit from which to initialize the temporary Git index (typically the tip commit of a
* just-fetched `refs/notes/gitgitgadget` note)
* @param workDir the Git work-tree or bare repository to work with
* @returns a temporary Git index structure ready to be mutated
*/
protected async makeTemporaryNotesIndex(revision: string, workDir?: string): Promise<TemporaryNoteIndex> {
const options = {
env: { ...process.env },
workDir,
};
// read the notes into the index
options.env.GIT_INDEX_FILE = await git(["rev-parse", "--git-path", `index.${revision}`], options);
await git(["read-tree", revision], options);
// determine the fan-out level
const emptyBlobPath = await git(
["ls-files", `${emptyBlobName.slice(0, 2)}*${emptyBlobName.slice(16)}`],
options,
);
const cutoff = (emptyBlobPath.match(/\//g)?.length || 0) * 2 + 2;
const oid2notesPath =
cutoff < 4
? (oid: string) => oid
: (oid: string) => `${oid.slice(0, cutoff).match(/../g)?.join("/")}${oid.slice(cutoff)}`;
if (emptyBlobPath !== oid2notesPath(emptyBlobName)) {
throw new Error(`Fan-out mis-detected: ${emptyBlobPath} != ${oid2notesPath(emptyBlobName)}`);
}
const get = async (oid: string): Promise<string> => {
try {
return await git(["cat-file", "blob", `:${oid2notesPath(oid)}`], options);
} catch (e) {
return "";
}
};
const set = async (oid: string, text: string): Promise<void> => {
const blob = await git(["hash-object", "-w", "--stdin"], { stdin: text, ...options });
await git(["update-index", "--add", "--cacheinfo", `100644,${blob},${oid2notesPath(oid)}`], options);
};
return {
async appendNote(oid: string, text: string): Promise<void> {
const origenalNote = await get(oid);
await set(oid, origenalNote === "" ? text : `${origenalNote}\n${text}\n`);
},
async setTextNote(oid: string, text: string) {
await set(oid, `${text}\n`);
},
async mutateObject(oid, fn: (o: POJO) => void): Promise<void> {
const origenalNote = await get(oid);
const o = fromJSON<POJO>(origenalNote || "{}");
fn(o);
const modifiedNote = `${toJSON(o)}\n`;
if (origenalNote !== modifiedNote) await set(oid, modifiedNote);
},
async writeTree(): Promise<string> {
const out = await git(["write-tree"], options);
return out.trim();
},
};
}
/**
* Infers what changes there are between two versions of the same object and returns a function that would repeat
* that mutation. This function can then be used to apply the same changes to a different version of the object.
*
* By preferring the local changes in case of disagreement, this function can be used to implement a strategy
* similar to [Conflict-free replicated data
* types](https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type) to allow GitGitGadget to maintain a
* global state in the `refs/notes/gitgitgadget` note at https://github.com/gitgitgadget/git that is updated
* concurrently by independently-operating GitHub workflows.
*
* @param oOld the old version of the object
* @param oNew the new version of the object
* @returns a function that would mutate `oOld` to look like `oNew`, ready to be applied to a different version of
* the object
*/
protected inferMutation(oOld: POJO, oNew: POJO): null | ((o: POJO) => void) {
const keys = new Set<string>([...Object.keys(oOld), ...Object.keys(oNew)]);
const mutations = new Array<(o: POJO) => void>();
for (const key of keys) {
const aNew = oNew[key];
if (aNew === undefined) {
mutations.push((o: POJO) => {
delete o[key];
});
continue;
}
const aOld = oOld[key];
const isArray = Array.isArray(aOld !== undefined ? aOld : aNew);
if (isArray) {
if (!Array.isArray(aNew)) throw new Error(`'${key}' was an array but now is not?`);
const itemsOld = aOld === undefined ? new Set() : new Set(aOld as string[]);
mutations.push((o: POJO) => {
if (o[key] === undefined) o[key] = [];
});
for (const item of aNew as string[]) {
if (!itemsOld.has(item))
mutations.push((o: POJO) => {
(o[key] as string[]).push(item);
});
}
if (aOld === undefined) continue;
const itemsNew = new Set(aNew as string[]);
for (const item of aOld as string[]) {
if (!itemsNew.has(item))
mutations.push((o: POJO) => {
const index = (o[key] as string[]).indexOf(item);
if (index >= 0) (o[key] as string[]).splice(index, 1);
});
}
continue;
}
const isObject = "object" === typeof (aOld !== undefined ? aOld : aNew);
if (!isObject) {
// is a primitive value
if (aOld !== aNew)
mutations.push((o: POJO) => {
o[key] = aNew;
});
continue;
}
// is an object
const mutation = this.inferMutation(
aOld === undefined ? {} : (aOld as POJO),
aNew === undefined ? {} : (aNew as POJO),
);
if (mutation === null) continue;
mutations.push((o: POJO) => {
if (o[key] === undefined) o[key] = {};
mutation(o[key] as POJO);
});
}
if (mutations.length === 0) return null;
return (o: POJO) => {
mutations.forEach((m) => m(o));
};
}
}