@catamorphic/git 0.1.0-alpha.43 → 0.1.0-alpha.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/checkout-remote-backend.d.ts.map +1 -1
- package/dist/fs-backend.d.ts +8 -0
- package/dist/fs-backend.d.ts.map +1 -1
- package/dist/fs-remote-backend.d.ts +43 -9
- package/dist/fs-remote-backend.d.ts.map +1 -1
- package/dist/git-objects.d.ts +77 -0
- package/dist/git-objects.d.ts.map +1 -0
- package/dist/git-sync.d.ts +16 -17
- package/dist/git-sync.d.ts.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3824 -3099
- package/dist/index.js.map +23 -22
- package/dist/native-git.d.ts +114 -2
- package/dist/native-git.d.ts.map +1 -1
- package/dist/native-project-repo.d.ts +40 -5
- package/dist/native-project-repo.d.ts.map +1 -1
- package/dist/network-sync.d.ts.map +1 -1
- package/dist/network.d.ts +14 -9
- package/dist/network.d.ts.map +1 -1
- package/dist/object-origin-cache.d.ts +86 -0
- package/dist/object-origin-cache.d.ts.map +1 -0
- package/dist/object-remote-backend.d.ts +64 -10
- package/dist/object-remote-backend.d.ts.map +1 -1
- package/dist/origin-draft.d.ts.map +1 -1
- package/dist/origin-objects.d.ts +7 -2
- package/dist/origin-objects.d.ts.map +1 -1
- package/dist/project-manager.d.ts +22 -0
- package/dist/project-manager.d.ts.map +1 -1
- package/dist/project-paths.d.ts +20 -0
- package/dist/project-paths.d.ts.map +1 -0
- package/dist/ref-names.d.ts +35 -0
- package/dist/ref-names.d.ts.map +1 -1
- package/dist/types.d.ts +37 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/workspace-checkout.d.ts.map +1 -1
- package/dist/workspace-mirror.d.ts +13 -1
- package/dist/workspace-mirror.d.ts.map +1 -1
- package/dist/workspace-save.d.ts +27 -2
- package/dist/workspace-save.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/checkout-remote-backend.ts +77 -29
- package/src/fs-backend.ts +69 -7
- package/src/fs-remote-backend.ts +255 -133
- package/src/git-objects.ts +363 -0
- package/src/git-sync.ts +124 -453
- package/src/index.ts +9 -7
- package/src/native-git.ts +566 -25
- package/src/native-project-repo.ts +318 -99
- package/src/network-sync.ts +43 -35
- package/src/network.ts +177 -153
- package/src/object-origin-cache.ts +689 -0
- package/src/object-remote-backend.ts +277 -20
- package/src/origin-draft.ts +22 -15
- package/src/origin-objects.ts +21 -5
- package/src/project-manager.ts +261 -73
- package/src/project-paths.ts +142 -0
- package/src/ref-names.ts +56 -0
- package/src/types.ts +38 -4
- package/src/workspace-checkout.ts +16 -9
- package/src/workspace-mirror.ts +199 -58
- package/src/workspace-save.ts +99 -30
- package/dist/project-repo.d.ts +0 -81
- package/dist/project-repo.d.ts.map +0 -1
- package/src/project-repo.ts +0 -821
package/src/network-sync.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import nodeFs from "node:fs";
|
|
2
1
|
import { MANAGED_BRANCH_PREFIX } from "@catamorphic/workflow/project-layout";
|
|
3
|
-
import git from "isomorphic-git";
|
|
4
2
|
import { nativeGit } from "./native-git.js";
|
|
5
3
|
import { NativeProjectRepo } from "./native-project-repo.js";
|
|
6
4
|
import {
|
|
@@ -70,7 +68,9 @@ export async function syncWithNetworkRemote(opts: {
|
|
|
70
68
|
const owned = opts.ownership === "owned";
|
|
71
69
|
|
|
72
70
|
const currentBranch = await dev.currentBranch();
|
|
73
|
-
|
|
71
|
+
// A person's own folder syncs the branch it is on with its upstream; a
|
|
72
|
+
// copy Work manages syncs its `main`.
|
|
73
|
+
if (dev instanceof NativeProjectRepo && !dev.managed) {
|
|
74
74
|
if (currentBranch === "HEAD")
|
|
75
75
|
return { status: "no-op", localSha: null, remoteSha: null };
|
|
76
76
|
const localSha = await dev.resolveRef();
|
|
@@ -90,7 +90,7 @@ export async function syncWithNetworkRemote(opts: {
|
|
|
90
90
|
const remote = (
|
|
91
91
|
await nativeGit(
|
|
92
92
|
dir,
|
|
93
|
-
["ls-remote", opts.url, `refs/heads/${target}`],
|
|
93
|
+
["ls-remote", "--end-of-options", opts.url, `refs/heads/${target}`],
|
|
94
94
|
auth,
|
|
95
95
|
)
|
|
96
96
|
).trim();
|
|
@@ -99,7 +99,6 @@ export async function syncWithNetworkRemote(opts: {
|
|
|
99
99
|
if (!owned) return { status: "ahead", localSha, remoteSha };
|
|
100
100
|
await pushToRemote({
|
|
101
101
|
repoPath: dir,
|
|
102
|
-
native: true,
|
|
103
102
|
url: opts.url,
|
|
104
103
|
credentials: opts.credentials,
|
|
105
104
|
ownership: opts.ownership,
|
|
@@ -111,7 +110,11 @@ export async function syncWithNetworkRemote(opts: {
|
|
|
111
110
|
if (!remoteSha) return push();
|
|
112
111
|
if (remoteSha === localSha)
|
|
113
112
|
return { status: "up-to-date", localSha, remoteSha };
|
|
114
|
-
await nativeGit(
|
|
113
|
+
await nativeGit(
|
|
114
|
+
dir,
|
|
115
|
+
["fetch", "--end-of-options", opts.url, `refs/heads/${target}`],
|
|
116
|
+
auth,
|
|
117
|
+
);
|
|
115
118
|
if (
|
|
116
119
|
await nativeGit(dir, [
|
|
117
120
|
"merge-base",
|
|
@@ -175,9 +178,10 @@ export async function syncWithNetworkRemote(opts: {
|
|
|
175
178
|
}
|
|
176
179
|
|
|
177
180
|
const isDescendent = (oid: string, ancestor: string) =>
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
+
nativeGit(dir, ["merge-base", "--is-ancestor", ancestor, oid]).then(
|
|
182
|
+
() => true,
|
|
183
|
+
() => false,
|
|
184
|
+
);
|
|
181
185
|
|
|
182
186
|
if (await isDescendent(localSha, remoteSha)) {
|
|
183
187
|
if (!owned) return { status: "ahead", localSha, remoteSha };
|
|
@@ -192,40 +196,44 @@ export async function syncWithNetworkRemote(opts: {
|
|
|
192
196
|
if (dirty) return { status: "deferred", localSha, remoteSha };
|
|
193
197
|
|
|
194
198
|
if (behind) {
|
|
195
|
-
await
|
|
196
|
-
fs: nodeFs,
|
|
197
|
-
dir,
|
|
198
|
-
ref: "refs/heads/main",
|
|
199
|
-
value: remoteSha,
|
|
200
|
-
force: true,
|
|
201
|
-
});
|
|
202
|
-
await git.checkout({ fs: nodeFs, dir, ref: "main", force: true });
|
|
199
|
+
await nativeGit(dir, ["merge", "--quiet", "--ff-only", remoteSha]);
|
|
203
200
|
return { status: "pulled", localSha: remoteSha, remoteSha };
|
|
204
201
|
}
|
|
205
202
|
|
|
206
203
|
// Diverged histories on an owned remote.
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
204
|
+
const merged = await nativeGit(dir, [
|
|
205
|
+
"-c",
|
|
206
|
+
`user.name=${opts.author.name}`,
|
|
207
|
+
"-c",
|
|
208
|
+
`user.email=${opts.author.email}`,
|
|
209
|
+
"merge",
|
|
210
|
+
"--quiet",
|
|
211
|
+
"--no-edit",
|
|
212
|
+
"-m",
|
|
213
|
+
`Merge remote ${opts.remoteBranch} into main`,
|
|
214
|
+
remoteSha,
|
|
215
|
+
]).then(
|
|
216
|
+
() => true,
|
|
217
|
+
async (error: unknown) => {
|
|
218
|
+
const conflicted = (
|
|
219
|
+
await nativeGit(dir, ["diff", "--name-only", "--diff-filter=U"]).catch(
|
|
220
|
+
() => "",
|
|
221
|
+
)
|
|
222
|
+
).trim();
|
|
223
|
+
// A background sync never leaves conflict markers behind.
|
|
224
|
+
await nativeGit(dir, ["merge", "--abort"]).catch(() => "");
|
|
225
|
+
if (!conflicted) throw error;
|
|
226
|
+
return false;
|
|
227
|
+
},
|
|
228
|
+
);
|
|
229
|
+
if (!merged) {
|
|
225
230
|
const rescueBranch = rescueBranchName(opts.now ?? new Date());
|
|
226
231
|
await push(rescueBranch);
|
|
227
232
|
return { status: "diverged", localSha, remoteSha, rescueBranch };
|
|
228
233
|
}
|
|
234
|
+
const mergedSha = await dev.resolveRef("refs/heads/main");
|
|
235
|
+
await push(opts.remoteBranch);
|
|
236
|
+
return { status: "merged", localSha: mergedSha, remoteSha: mergedSha };
|
|
229
237
|
}
|
|
230
238
|
|
|
231
239
|
/** `work/diverged-YYYY-MM-DD_HH-mm` — groups rescue pushes on the host. */
|
package/src/network.ts
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { getTracer, withSpan } from "@catamorphic/otel";
|
|
2
2
|
import { MANAGED_BRANCH_PREFIX } from "@catamorphic/workflow/project-layout";
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
import {
|
|
4
|
+
gitErrorText,
|
|
5
|
+
type NativeGitAuth,
|
|
6
|
+
nativeGit,
|
|
7
|
+
TRANSFER_TIMEOUT_MS,
|
|
8
|
+
} from "./native-git.js";
|
|
6
9
|
import { isValidRefName } from "./ref-names.js";
|
|
7
10
|
import type { GitCredentials } from "./types.js";
|
|
8
11
|
|
|
12
|
+
const tracer = getTracer("@catamorphic/git");
|
|
13
|
+
|
|
9
14
|
export interface CloneFromRemoteOptions {
|
|
10
|
-
|
|
15
|
+
/**
|
|
16
|
+
* The repository is a copy Work manages, not a person's own folder: one
|
|
17
|
+
* branch arrives, as the local `main`, with no remote of its own.
|
|
18
|
+
*/
|
|
19
|
+
managed?: boolean;
|
|
11
20
|
/** Directory that already holds an initialized (empty) git repo. */
|
|
12
21
|
repoPath: string;
|
|
13
22
|
url: string;
|
|
@@ -16,105 +25,125 @@ export interface CloneFromRemoteOptions {
|
|
|
16
25
|
branch?: string;
|
|
17
26
|
}
|
|
18
27
|
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
})
|
|
25
|
-
: undefined;
|
|
28
|
+
function authFor(opts: {
|
|
29
|
+
url: string;
|
|
30
|
+
credentials?: GitCredentials;
|
|
31
|
+
}): NativeGitAuth | undefined {
|
|
32
|
+
return opts.credentials ? { ...opts.credentials, url: opts.url } : undefined;
|
|
26
33
|
}
|
|
27
34
|
|
|
28
35
|
/**
|
|
29
36
|
* Populate a freshly-initialized repo from a network git remote (e.g. a
|
|
30
|
-
* GitHub repository)
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
37
|
+
* GitHub repository), with the Git program: the history arrives as packs,
|
|
38
|
+
* whatever its size. A person's own folder becomes an ordinary clone on the
|
|
39
|
+
* remote's branch. A copy Work manages receives that one branch as its
|
|
40
|
+
* local `main` regardless of the remote's branch name: Work's internal sync
|
|
41
|
+
* (`git-sync`, drafts, checkout seeding) assumes `main` throughout, so the
|
|
42
|
+
* remote branch name is only remembered by the caller for push-back.
|
|
34
43
|
*
|
|
35
44
|
* Returns the checked-out sha and the remote's branch name.
|
|
36
45
|
*/
|
|
37
46
|
export async function cloneFromRemote(
|
|
38
47
|
opts: CloneFromRemoteOptions,
|
|
39
48
|
): Promise<{ sha: string; remoteBranch: string }> {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
:
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
opts
|
|
48
|
-
|
|
49
|
+
return withSpan(
|
|
50
|
+
{
|
|
51
|
+
tracer,
|
|
52
|
+
name: "git.clone",
|
|
53
|
+
attributes: { "catamorphic.git.managed": opts.managed ?? false },
|
|
54
|
+
},
|
|
55
|
+
async () => {
|
|
56
|
+
const auth = authFor(opts);
|
|
57
|
+
const transfer = { timeoutMs: TRANSFER_TIMEOUT_MS };
|
|
58
|
+
if (!opts.managed) {
|
|
59
|
+
await nativeGit(opts.repoPath, [
|
|
60
|
+
"remote",
|
|
61
|
+
"add",
|
|
62
|
+
"--end-of-options",
|
|
63
|
+
"origin",
|
|
64
|
+
opts.url,
|
|
65
|
+
]);
|
|
49
66
|
await nativeGit(
|
|
50
67
|
opts.repoPath,
|
|
51
|
-
["
|
|
68
|
+
["fetch", "--tags", "origin"],
|
|
52
69
|
auth,
|
|
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
|
-
|
|
70
|
+
transfer,
|
|
71
|
+
);
|
|
72
|
+
const remoteBranch =
|
|
73
|
+
opts.branch ??
|
|
74
|
+
(
|
|
75
|
+
await nativeGit(
|
|
76
|
+
opts.repoPath,
|
|
77
|
+
["ls-remote", "--symref", "origin", "HEAD"],
|
|
78
|
+
auth,
|
|
79
|
+
)
|
|
80
|
+
).match(/ref: refs\/heads\/(.+)\tHEAD/)?.[1];
|
|
81
|
+
if (!remoteBranch)
|
|
82
|
+
throw new Error("The repository has no default branch to check out");
|
|
83
|
+
await nativeGit(opts.repoPath, [
|
|
84
|
+
"checkout",
|
|
85
|
+
"-B",
|
|
86
|
+
remoteBranch,
|
|
87
|
+
"--track",
|
|
88
|
+
`origin/${remoteBranch}`,
|
|
89
|
+
]);
|
|
90
|
+
await nativeGit(opts.repoPath, [
|
|
91
|
+
"symbolic-ref",
|
|
92
|
+
"refs/remotes/origin/HEAD",
|
|
93
|
+
`refs/remotes/origin/${remoteBranch}`,
|
|
94
|
+
]);
|
|
95
|
+
return {
|
|
96
|
+
sha: (await nativeGit(opts.repoPath, ["rev-parse", "HEAD"])).trim(),
|
|
97
|
+
remoteBranch,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const remoteBranch =
|
|
101
|
+
opts.branch ??
|
|
102
|
+
(
|
|
103
|
+
await nativeGit(
|
|
104
|
+
opts.repoPath,
|
|
105
|
+
["ls-remote", "--symref", "--end-of-options", opts.url, "HEAD"],
|
|
106
|
+
auth,
|
|
107
|
+
).catch((error: unknown) => {
|
|
108
|
+
throw new Error(
|
|
109
|
+
`Could not reach the repository to clone it: ${gitErrorText(error)}`,
|
|
110
|
+
);
|
|
111
|
+
})
|
|
112
|
+
).match(/ref: refs\/heads\/(.+)\tHEAD/)?.[1];
|
|
113
|
+
if (!remoteBranch || !isValidRefName(remoteBranch))
|
|
114
|
+
throw new Error(`Remote '${opts.url}' has no commits to clone`);
|
|
115
|
+
await nativeGit(
|
|
116
|
+
opts.repoPath,
|
|
117
|
+
[
|
|
118
|
+
"fetch",
|
|
119
|
+
"--quiet",
|
|
120
|
+
"--no-tags",
|
|
121
|
+
"--no-write-fetch-head",
|
|
122
|
+
// The copy is new and empty: its unborn `main` is what arrives.
|
|
123
|
+
"--update-head-ok",
|
|
124
|
+
"--end-of-options",
|
|
125
|
+
opts.url,
|
|
126
|
+
`+refs/heads/${remoteBranch}:refs/heads/main`,
|
|
127
|
+
],
|
|
128
|
+
auth,
|
|
129
|
+
transfer,
|
|
130
|
+
).catch((error: unknown) => {
|
|
131
|
+
throw new Error(
|
|
132
|
+
`Could not clone '${remoteBranch}' of the repository: ${gitErrorText(error)}`,
|
|
133
|
+
);
|
|
134
|
+
});
|
|
135
|
+
await nativeGit(
|
|
136
|
+
opts.repoPath,
|
|
137
|
+
["reset", "--quiet", "--hard", "refs/heads/main"],
|
|
138
|
+
undefined,
|
|
139
|
+
transfer,
|
|
140
|
+
);
|
|
141
|
+
return {
|
|
142
|
+
sha: (await nativeGit(opts.repoPath, ["rev-parse", "HEAD"])).trim(),
|
|
143
|
+
remoteBranch,
|
|
144
|
+
};
|
|
145
|
+
},
|
|
146
|
+
);
|
|
118
147
|
}
|
|
119
148
|
|
|
120
149
|
/**
|
|
@@ -183,7 +212,6 @@ export function assertPushAllowed(input: {
|
|
|
183
212
|
*/
|
|
184
213
|
export async function pushToRemote(opts: {
|
|
185
214
|
repoPath: string;
|
|
186
|
-
native?: boolean;
|
|
187
215
|
url: string;
|
|
188
216
|
credentials?: GitCredentials;
|
|
189
217
|
/** Who created the remote: attached remotes only accept Work's branches. */
|
|
@@ -195,7 +223,7 @@ export async function pushToRemote(opts: {
|
|
|
195
223
|
force?: boolean;
|
|
196
224
|
/**
|
|
197
225
|
* Replace the branch only while it still holds `expected` (null: only
|
|
198
|
-
* while it does not exist).
|
|
226
|
+
* while it does not exist).
|
|
199
227
|
*/
|
|
200
228
|
lease?: { expected: string | null };
|
|
201
229
|
}): Promise<void> {
|
|
@@ -215,7 +243,7 @@ export async function pushToRemote(opts: {
|
|
|
215
243
|
});
|
|
216
244
|
if (opts.lease) {
|
|
217
245
|
const expected = opts.lease.expected ?? "";
|
|
218
|
-
if (
|
|
246
|
+
if (expected && !/^[0-9a-f]{40,64}$/.test(expected))
|
|
219
247
|
throw new RemotePushRefusedError(
|
|
220
248
|
remoteBranch,
|
|
221
249
|
"a push with a lease names the commit it replaces.",
|
|
@@ -232,29 +260,20 @@ export async function pushToRemote(opts: {
|
|
|
232
260
|
});
|
|
233
261
|
return;
|
|
234
262
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
opts.
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
fs: nodeFs,
|
|
250
|
-
http,
|
|
251
|
-
dir: opts.repoPath,
|
|
252
|
-
url: opts.url,
|
|
253
|
-
ref,
|
|
254
|
-
remoteRef: `refs/heads/${remoteBranch}`,
|
|
255
|
-
force: opts.force ?? false,
|
|
256
|
-
onAuth: onAuthFor(opts.credentials),
|
|
257
|
-
});
|
|
263
|
+
await nativeGit(
|
|
264
|
+
opts.repoPath,
|
|
265
|
+
[
|
|
266
|
+
"push",
|
|
267
|
+
"--quiet",
|
|
268
|
+
// No lease names what a plain force replaces: a remote given by its
|
|
269
|
+
// address has no tracking ref to compare with.
|
|
270
|
+
...(opts.force ? ["--force"] : []),
|
|
271
|
+
"--end-of-options",
|
|
272
|
+
opts.url,
|
|
273
|
+
`${ref}:refs/heads/${remoteBranch}`,
|
|
274
|
+
],
|
|
275
|
+
authFor(opts),
|
|
276
|
+
);
|
|
258
277
|
}
|
|
259
278
|
|
|
260
279
|
function pushWithLease(input: {
|
|
@@ -272,6 +291,7 @@ function pushWithLease(input: {
|
|
|
272
291
|
"--porcelain",
|
|
273
292
|
"--no-follow-tags",
|
|
274
293
|
`--force-with-lease=${input.target}:${input.expected}`,
|
|
294
|
+
"--end-of-options",
|
|
275
295
|
input.url,
|
|
276
296
|
`${input.ref}:${input.target}`,
|
|
277
297
|
],
|
|
@@ -327,7 +347,7 @@ export async function pushOwnBranch(opts: {
|
|
|
327
347
|
const target = `refs/heads/${remoteBranch}`;
|
|
328
348
|
const listed = await nativeGit(
|
|
329
349
|
opts.repoPath,
|
|
330
|
-
["ls-remote", "--symref", opts.url, "HEAD", target],
|
|
350
|
+
["ls-remote", "--symref", "--end-of-options", opts.url, "HEAD", target],
|
|
331
351
|
auth,
|
|
332
352
|
);
|
|
333
353
|
const refs = new Map<string, string>();
|
|
@@ -426,8 +446,8 @@ function isStaleLease(error: unknown): boolean {
|
|
|
426
446
|
}
|
|
427
447
|
|
|
428
448
|
/**
|
|
429
|
-
* Fetch a branch from a network remote
|
|
430
|
-
*
|
|
449
|
+
* Fetch a branch from a network remote without touching the working tree
|
|
450
|
+
* or any branch. Returns the fetched sha (null when the remote branch does
|
|
431
451
|
* not exist).
|
|
432
452
|
*/
|
|
433
453
|
export async function fetchFromRemote(opts: {
|
|
@@ -436,37 +456,41 @@ export async function fetchFromRemote(opts: {
|
|
|
436
456
|
credentials?: GitCredentials;
|
|
437
457
|
branch: string;
|
|
438
458
|
}): Promise<{ sha: string | null }> {
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
const
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
459
|
+
if (!isValidRefName(opts.branch) || opts.branch.startsWith("-"))
|
|
460
|
+
throw new Error(`'${opts.branch}' is not a branch name`);
|
|
461
|
+
const auth = authFor(opts);
|
|
462
|
+
const listed = (
|
|
463
|
+
await nativeGit(
|
|
464
|
+
opts.repoPath,
|
|
465
|
+
["ls-remote", "--end-of-options", opts.url, `refs/heads/${opts.branch}`],
|
|
466
|
+
auth,
|
|
467
|
+
)
|
|
468
|
+
).trim();
|
|
469
|
+
const sha = listed.split(/\s+/)[0];
|
|
470
|
+
if (!sha || !/^[0-9a-f]{40}$|^[0-9a-f]{64}$/.test(sha)) return { sha: null };
|
|
471
|
+
// Kept under a ref of Work's own, apart from the host's internal origin,
|
|
472
|
+
// so what was fetched stays reachable.
|
|
473
|
+
await nativeGit(
|
|
474
|
+
opts.repoPath,
|
|
475
|
+
[
|
|
476
|
+
"fetch",
|
|
477
|
+
"--quiet",
|
|
478
|
+
"--no-tags",
|
|
479
|
+
"--no-write-fetch-head",
|
|
480
|
+
"--end-of-options",
|
|
481
|
+
opts.url,
|
|
482
|
+
`+refs/heads/${opts.branch}:refs/work/network/${opts.branch}`,
|
|
483
|
+
],
|
|
484
|
+
auth,
|
|
485
|
+
{ timeoutMs: TRANSFER_TIMEOUT_MS },
|
|
486
|
+
);
|
|
487
|
+
return {
|
|
488
|
+
sha: (
|
|
489
|
+
await nativeGit(opts.repoPath, [
|
|
490
|
+
"rev-parse",
|
|
491
|
+
"--verify",
|
|
492
|
+
`refs/work/network/${opts.branch}^{commit}`,
|
|
493
|
+
])
|
|
494
|
+
).trim(),
|
|
495
|
+
};
|
|
472
496
|
}
|