-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdeepenHelper.ts
More file actions
284 lines (260 loc) · 10.8 KB
/
deepenHelper.ts
File metadata and controls
284 lines (260 loc) · 10.8 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
import * as path from 'path';
import { promises as fs } from 'fs';
import { window, ProgressLocation } from 'vscode';
import { Repository } from './git/git';
import { Repository as GitAPIRepository } from './typings/git';
import { getAbsGitCommonDir } from './gitHelper';
type LogFn = (msg: string) => void;
interface FetchTarget {
remote: string;
branch: string;
estimatedDepth: number;
}
const DEPTH_FLOOR = 1024;
const DEPTH_STEPS = 4;
const DEPTH_CAP = 1_000_000;
/**
* If a merge base could not be determined and the repository is a shallow clone,
* offers the user the option to fetch more history (and as a last resort, unshallow)
* to resolve the merge base.
*
* Returns the discovered merge base, or undefined if the repository is not shallow,
* the user declined, the user cancelled, or all attempts failed.
*/
export async function tryDeepenForMergeBase(
repository: Repository,
gitApiRepo: GitAPIRepository,
headRef: string,
headBranchName: string | undefined,
baseRef: string,
log: LogFn,
): Promise<string | undefined> {
const commonDir = await getAbsGitCommonDir(repository);
const shallowBoundary = await readShallowBoundary(commonDir);
if (!shallowBoundary) {
// not a shallow clone; deepening cannot help
return undefined;
}
log(`Repository is a shallow clone (${shallowBoundary.length} boundary commit(s))`);
const baseTarget = await resolveFetchTarget(repository, baseRef, log);
if (baseTarget) {
log(`Base ref "${baseRef}" resolved to fetchable target: ${baseTarget.remote}/${baseTarget.branch}`);
} else {
log(`Base ref "${baseRef}" could not be resolved to a fetchable target`);
}
const headTarget = headBranchName
? await resolveFetchTarget(repository, headBranchName, log)
: undefined;
if (headBranchName) {
if (headTarget) {
log(`HEAD ref "${headBranchName}" resolved to fetchable target: ${headTarget.remote}/${headTarget.branch}`);
} else {
log(`HEAD ref "${headBranchName}" could not be resolved to a fetchable target`);
}
} else {
log('HEAD is detached, skipping HEAD target resolution');
}
if (!baseTarget && !headTarget) {
log('Neither base nor HEAD ref maps to a fetchable remote; cannot deepen.');
return undefined;
}
const targets: FetchTarget[] = [];
if (baseTarget) targets.push(baseTarget);
if (headTarget && !sameTarget(headTarget, baseTarget)) targets.push(headTarget);
const action = 'Fetch more history';
const choice = await window.showErrorMessage(
`No merge base could be found between "${headRef}" and "${baseRef}". ` +
`The repository is a shallow clone — fetching more history may resolve this.`,
action);
if (choice !== action) {
return undefined;
}
const headDepth = await estimateTargetDepths(repository, shallowBoundary, targets, log);
// Sort shallowest first — that's the side most likely to need deepening.
targets.sort((a, b) => a.estimatedDepth - b.estimatedDepth);
// Schedule is based on the shallowest depth across all sides (including HEAD,
// even if it's not a fetchable target), because the merge base can't be found
// until the shallow side has enough history.
const minDepth = Math.min(headDepth, ...targets.map(t => t.estimatedDepth));
const schedule = buildSchedule(minDepth);
log(`HEAD depth: ${headDepth}`);
log(`Target depths: ${targets.map(t => `${t.remote}/${t.branch}=${t.estimatedDepth}`).join(', ')}`);
log(`Min depth (for schedule): ${minDepth}`);
log(`Deepening schedule: [${schedule.join(', ')}]`);
const found = await window.withProgress({
location: ProgressLocation.Notification,
title: 'Fetching more history',
cancellable: true,
}, async (progress, token) => {
for (const depth of schedule) {
if (token.isCancellationRequested) return undefined;
for (const target of targets) {
if (token.isCancellationRequested) return undefined;
if (target.estimatedDepth >= depth) {
log(`Skipping ${target.remote}/${target.branch} (estimated depth ${target.estimatedDepth} >= ${depth})`);
continue;
}
progress.report({ message: `Fetching ${target.remote}/${target.branch} at depth ${depth}...` });
try {
log(`Fetching ${target.remote} ${target.branch} --depth=${depth}`);
await gitApiRepo.fetch(target.remote, target.branch, depth);
} catch (e: any) {
log(`Fetch failed: ${e.message || e}`);
// continue with the next target / depth
}
}
if (token.isCancellationRequested) return undefined;
const mb = await tryGetMergeBase(repository, headRef, baseRef, log);
if (mb) return mb;
}
return undefined;
});
if (found) return found;
// Last resort: offer to unshallow.
const unshallow = 'Unshallow';
const finalChoice = await window.showErrorMessage(
`Still no merge base found between "${headRef}" and "${baseRef}". ` +
`Fetch the full repository history?`,
unshallow);
if (finalChoice !== unshallow) {
return undefined;
}
return await window.withProgress({
location: ProgressLocation.Notification,
title: 'Unshallowing repository',
cancellable: true,
}, async (progress, token) => {
progress.report({ message: 'Fetching full history...' });
try {
log('Unshallowing repository (git pull --unshallow)');
await gitApiRepo.pull(true);
} catch (e: any) {
log(`Unshallow failed: ${e.message || e}`);
return undefined;
}
if (token.isCancellationRequested) return undefined;
return await tryGetMergeBase(repository, headRef, baseRef, log);
});
}
async function tryGetMergeBase(repository: Repository, ref1: string, ref2: string, log: LogFn): Promise<string | undefined> {
try {
const mb = await repository.getMergeBase(ref1, ref2);
if (mb) {
log(`Merge base found after deepening: ${mb}`);
return mb;
}
} catch (e: any) {
log(`getMergeBase still failing: ${e.message || e}`);
}
return undefined;
}
/**
* Reads .git/shallow (in the common gitdir) and returns the boundary commit hashes,
* or undefined if the repository is not shallow.
*/
async function readShallowBoundary(commonDir: string): Promise<string[] | undefined> {
const shallowPath = path.join(commonDir, 'shallow');
let content: string;
try {
content = await fs.readFile(shallowPath, 'utf8');
} catch (e: any) {
if (e.code === 'ENOENT') return undefined;
throw e;
}
const lines = content.split('\n').map(l => l.trim()).filter(l => l.length > 0);
if (lines.length === 0) return undefined;
return lines;
}
/**
* Resolves a ref to a (remote, branch) pair suitable for `git fetch <remote> <branch>`.
* Handles:
* - remote-tracking refs like "origin/main" or "origin/feature/foo"
* - local branches with a configured upstream
* Returns undefined for detached HEAD, local branches without upstream, or unknown refs.
*/
async function resolveFetchTarget(repository: Repository, ref: string, log: LogFn): Promise<FetchTarget | undefined> {
let remotes: { name: string }[];
try {
remotes = await repository.getRemotes();
} catch (e: any) {
log(`Could not list remotes: ${e.message || e}`);
return undefined;
}
if (remotes.length === 0) return undefined;
// Match against remote-tracking ref pattern: <remote>/<branch...>
// Sort by name length descending so a remote named "origin/foo" would be matched
// before "origin", although such names are unusual.
const sorted = [...remotes].sort((a, b) => b.name.length - a.name.length);
for (const r of sorted) {
const prefix = r.name + '/';
if (ref.startsWith(prefix)) {
const branch = ref.substring(prefix.length);
if (branch.length > 0) {
return { remote: r.name, branch, estimatedDepth: 0 };
}
}
}
// Try as local branch with upstream
try {
const branch = await repository.getBranch(ref);
if (branch.upstream && branch.upstream.remote && branch.upstream.name) {
return { remote: branch.upstream.remote, branch: branch.upstream.name, estimatedDepth: 0 };
}
} catch (e: any) {
// not a branch, or no upstream; ignore
}
// Last resort: speculatively assume the branch exists on a remote.
// A failed fetch is handled gracefully (caught and logged).
if (sorted.length > 0) {
return { remote: sorted[sorted.length - 1].name, branch: ref, estimatedDepth: 0 };
}
return undefined;
}
function sameTarget(a: FetchTarget, b: FetchTarget | undefined): boolean {
return !!b && a.remote === b.remote && a.branch === b.branch;
}
/**
* Estimates the current shallow depth for each fetch target and stores it
* on the target's `estimatedDepth` field. Also probes HEAD.
* Best-effort: targets default to 0 on failure.
* Returns the estimated HEAD depth.
*/
async function estimateTargetDepths(
repository: Repository,
boundary: string[],
targets: FetchTarget[],
log: LogFn,
): Promise<number> {
// Also probe HEAD to get the best estimate for the head-side target.
const headDepth = await countCommitsToBoundary(repository, 'HEAD', boundary, log) ?? 0;
for (const t of targets) {
const refDepth = await countCommitsToBoundary(
repository, `refs/remotes/${t.remote}/${t.branch}`, boundary, log) ?? 0;
// For the head-side target, the remote-tracking ref may not exist yet
// (e.g. local branch). Use HEAD depth as a better proxy in that case.
t.estimatedDepth = Math.max(refDepth, refDepth === 0 ? headDepth : 0);
}
return headDepth;
}
async function countCommitsToBoundary(repository: Repository, ref: string, boundary: string[], log: LogFn): Promise<number | undefined> {
const args = ['rev-list', '--count', '--first-parent', ref, ...boundary.map(s => '^' + s)];
try {
const result = await repository.exec(args);
const n = parseInt(result.stdout.trim(), 10);
if (!isNaN(n)) return n;
} catch (e: any) {
log(`Could not count commits for ${ref}: ${e.message || e}`);
}
return undefined;
}
function buildSchedule(startDepth: number): number[] {
const schedule: number[] = [];
let depth = Math.max(DEPTH_FLOOR, startDepth * 2);
for (let i = 0; i < DEPTH_STEPS; i++) {
depth = Math.min(depth, DEPTH_CAP);
schedule.push(depth);
if (depth >= DEPTH_CAP) break;
depth *= 2;
}
return schedule;
}