Skip to content

Commit

Permalink
Verify minimum Git version for sparse checkout
Browse files Browse the repository at this point in the history
The `git sparse-checkout` command is available only since Git version
v2.25.0. The `actions/checkout` Action actually supports older Git
versions than that; As of time of writing, the minimum version is
v2.18.0.

Instead of raising this minimum version even for users who do not
require a sparse checkout, only check for this minimum version
specifically when a sparse checkout was asked for.

Suggested-by: Tingluo Huang <tingluohuang@github.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
  • Loading branch information
dscho committed Jun 7, 2023
1 parent a241939 commit c4602b4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
6 changes: 4 additions & 2 deletions __test__/git-command-manager.test.ts
Expand Up @@ -39,7 +39,8 @@ describe('git-auth-helper tests', () => {
jest.spyOn(exec, 'exec').mockImplementation(mockExec)
const workingDirectory = 'test'
const lfs = false
git = await commandManager.createCommandManager(workingDirectory, lfs)
const doSparseCheckout = false
git = await commandManager.createCommandManager(workingDirectory, lfs, doSparseCheckout)

let branches = await git.branchList(false)

Expand Down Expand Up @@ -70,7 +71,8 @@ describe('git-auth-helper tests', () => {
jest.spyOn(exec, 'exec').mockImplementation(mockExec)
const workingDirectory = 'test'
const lfs = false
git = await commandManager.createCommandManager(workingDirectory, lfs)
const doSparseCheckout = false
git = await commandManager.createCommandManager(workingDirectory, lfs, doSparseCheckout)

let branches = await git.branchList(false)

Expand Down
23 changes: 16 additions & 7 deletions dist/index.js
Expand Up @@ -481,9 +481,9 @@ const git_version_1 = __nccwpck_require__(3142);
// Auth header not supported before 2.9
// Wire protocol v2 not supported before 2.18
exports.MinimumGitVersion = new git_version_1.GitVersion('2.18');
function createCommandManager(workingDirectory, lfs) {
function createCommandManager(workingDirectory, lfs, doSparseCheckout) {
return __awaiter(this, void 0, void 0, function* () {
return yield GitCommandManager.createCommandManager(workingDirectory, lfs);
return yield GitCommandManager.createCommandManager(workingDirectory, lfs, doSparseCheckout);
});
}
exports.createCommandManager = createCommandManager;
Expand All @@ -496,6 +496,7 @@ class GitCommandManager {
};
this.gitPath = '';
this.lfs = false;
this.doSparseCheckout = false;
this.workingDirectory = '';
}
branchDelete(remote, branch) {
Expand Down Expand Up @@ -841,10 +842,10 @@ class GitCommandManager {
return output.exitCode === 0;
});
}
static createCommandManager(workingDirectory, lfs) {
static createCommandManager(workingDirectory, lfs, sparseCheckout) {
return __awaiter(this, void 0, void 0, function* () {
const result = new GitCommandManager();
yield result.initializeCommandManager(workingDirectory, lfs);
yield result.initializeCommandManager(workingDirectory, lfs, sparseCheckout);
return result;
});
}
Expand Down Expand Up @@ -880,7 +881,7 @@ class GitCommandManager {
return result;
});
}
initializeCommandManager(workingDirectory, lfs) {
initializeCommandManager(workingDirectory, lfs, sparseCheckout) {
return __awaiter(this, void 0, void 0, function* () {
this.workingDirectory = workingDirectory;
// Git-lfs will try to pull down assets if any of the local/user/system setting exist.
Expand Down Expand Up @@ -932,6 +933,14 @@ class GitCommandManager {
throw new Error(`Minimum required git-lfs version is ${minimumGitLfsVersion}. Your git-lfs ('${gitLfsPath}') is ${gitLfsVersion}`);
}
}
this.doSparseCheckout = sparseCheckout;
if (this.doSparseCheckout) {
// The `git sparse-checkout` command was introduced in Git v2.25.0
const minimumGitSparseCheckoutVersion = new git_version_1.GitVersion('2.25');
if (!gitVersion.checkMinimum(minimumGitSparseCheckoutVersion)) {
throw new Error(`Minimum Git version required for sparse checkout is ${minimumGitSparseCheckoutVersion}. Your git ('${this.gitPath}') is ${gitVersion}`);
}
}
// Set the user agent
const gitHttpUserAgent = `git/${gitVersion} (github-actions-checkout)`;
core.debug(`Set git useragent to: ${gitHttpUserAgent}`);
Expand Down Expand Up @@ -1327,7 +1336,7 @@ function cleanup(repositoryPath) {
}
let git;
try {
git = yield gitCommandManager.createCommandManager(repositoryPath, false);
git = yield gitCommandManager.createCommandManager(repositoryPath, false, false);
}
catch (_a) {
return;
Expand Down Expand Up @@ -1358,7 +1367,7 @@ function getGitCommandManager(settings) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Working directory is '${settings.repositoryPath}'`);
try {
return yield gitCommandManager.createCommandManager(settings.repositoryPath, settings.lfs);
return yield gitCommandManager.createCommandManager(settings.repositoryPath, settings.lfs, settings.sparseCheckout != null);
}
catch (err) {
// Git is required for LFS
Expand Down
28 changes: 23 additions & 5 deletions src/git-command-manager.ts
Expand Up @@ -61,9 +61,14 @@ export interface IGitCommandManager {

export async function createCommandManager(
workingDirectory: string,
lfs: boolean
lfs: boolean,
doSparseCheckout: boolean
): Promise<IGitCommandManager> {
return await GitCommandManager.createCommandManager(workingDirectory, lfs)
return await GitCommandManager.createCommandManager(
workingDirectory,
lfs,
doSparseCheckout
)
}

class GitCommandManager {
Expand All @@ -73,6 +78,7 @@ class GitCommandManager {
}
private gitPath = ''
private lfs = false
private doSparseCheckout = false
private workingDirectory = ''

// Private constructor; use createCommandManager()
Expand Down Expand Up @@ -461,10 +467,11 @@ class GitCommandManager {

static async createCommandManager(
workingDirectory: string,
lfs: boolean
lfs: boolean,
sparseCheckout: boolean
): Promise<GitCommandManager> {
const result = new GitCommandManager()
await result.initializeCommandManager(workingDirectory, lfs)
await result.initializeCommandManager(workingDirectory, lfs, sparseCheckout)
return result
}

Expand Down Expand Up @@ -514,7 +521,8 @@ class GitCommandManager {

private async initializeCommandManager(
workingDirectory: string,
lfs: boolean
lfs: boolean,
sparseCheckout: boolean
): Promise<void> {
this.workingDirectory = workingDirectory

Expand Down Expand Up @@ -577,6 +585,16 @@ class GitCommandManager {
}
}

this.doSparseCheckout = sparseCheckout
if (this.doSparseCheckout) {
// The `git sparse-checkout` command was introduced in Git v2.25.0
const minimumGitSparseCheckoutVersion = new GitVersion('2.25')
if (!gitVersion.checkMinimum(minimumGitSparseCheckoutVersion)) {
throw new Error(
`Minimum Git version required for sparse checkout is ${minimumGitSparseCheckoutVersion}. Your git ('${this.gitPath}') is ${gitVersion}`
)
}
}
// Set the user agent
const gitHttpUserAgent = `git/${gitVersion} (github-actions-checkout)`
core.debug(`Set git useragent to: ${gitHttpUserAgent}`)
Expand Down
5 changes: 3 additions & 2 deletions src/git-source-provider.ts
Expand Up @@ -275,7 +275,7 @@ export async function cleanup(repositoryPath: string): Promise<void> {

let git: IGitCommandManager
try {
git = await gitCommandManager.createCommandManager(repositoryPath, false)
git = await gitCommandManager.createCommandManager(repositoryPath, false, false)
} catch {
return
}
Expand Down Expand Up @@ -311,7 +311,8 @@ async function getGitCommandManager(
try {
return await gitCommandManager.createCommandManager(
settings.repositoryPath,
settings.lfs
settings.lfs,
settings.sparseCheckout != null
)
} catch (err) {
// Git is required for LFS
Expand Down

0 comments on commit c4602b4

Please sign in to comment.