Restore full history after a filtered checkout

This commit is contained in:
xunguangzlj-cloud 2026-11-08 16:35:37 +08:00
parent f548e57e54
commit 44c2bc1922
3 changed files with 174 additions and 0 deletions

49
dist/index.js vendored
View file

@ -35597,6 +35597,8 @@ class GitVersion {
// sparse-checkout not [well-]supported before 2.28 (see https://github.com/actions/checkout/issues/1386)
const MinimumGitVersion = new GitVersion('2.18');
const MinimumGitSparseCheckoutVersion = new GitVersion('2.28');
const MinimumGitRefetchVersion = new GitVersion('2.36');
const MinimumGitFetchWithoutWritingHeadVersion = new GitVersion('2.29');
async function createCommandManager(workingDirectory, lfs, doSparseCheckout) {
return await GitCommandManager.createCommandManager(workingDirectory, lfs, doSparseCheckout);
}
@ -35749,9 +35751,19 @@ class GitCommandManager {
if (options.showProgress) {
args.push('--progress');
}
const inheritedFilter = !options.filter &&
(await this.configExists('remote.origin.partialclonefilter'));
const restoreFullHistory = inheritedFilter && !(options.fetchDepth && options.fetchDepth > 0);
if (options.filter) {
args.push(`--filter=${options.filter}`);
}
else if (inheritedFilter) {
args.push('--no-filter');
if (restoreFullHistory &&
this.gitVersion.checkMinimum(MinimumGitRefetchVersion)) {
args.push('--refetch');
}
}
if (options.fetchDepth && options.fetchDepth > 0) {
args.push(`--depth=${options.fetchDepth}`);
}
@ -35766,6 +35778,43 @@ class GitCommandManager {
await execute(async () => {
await that.execGit(args);
});
if (restoreFullHistory) {
const canSkipFetchHead = this.gitVersion.checkMinimum(MinimumGitFetchWithoutWritingHeadVersion);
let fetchedMissingObjects = false;
let previousMissing = new Set();
while (true) {
const output = await this.execGit(['rev-list', '--objects', '--all', '--quiet', '--missing=print'], false, true);
const missing = [
...output.stdout.matchAll(/^\?([0-9a-f]{40}|[0-9a-f]{64})\r?$/gm)
].map(match => match[1]);
if (!missing.length) {
break;
}
if (missing.some(oid => previousMissing.has(oid))) {
throw new Error('Unable to fetch all objects from the previous partial checkout.');
}
previousMissing = new Set(missing);
for (let i = 0; i < missing.length; i += 100) {
await this.execGit([
'-c',
'protocol.version=2',
'fetch',
'--no-tags',
'--no-recurse-submodules',
'--no-filter',
canSkipFetchHead ? '--no-write-fetch-head' : '--append',
'origin',
...missing.slice(i, i + 100)
]);
}
fetchedMissingObjects = true;
}
if (fetchedMissingObjects && !canSkipFetchHead) {
await this.execGit(args.filter(arg => arg !== '--unshallow'));
}
await this.tryConfigUnset('remote.origin.partialclonefilter');
await this.tryConfigUnset('remote.origin.promisor');
}
}
async getDefaultBranch(repositoryUrl) {
let output;