Skip to content

Commit a64044a

Browse files
committed
feat!: support git 2.37.1+
1 parent 64f1d57 commit a64044a

File tree

1 file changed

+56
-25
lines changed

1 file changed

+56
-25
lines changed

src/utils.ts

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const getInstallDirectory = (): string =>
2121
const getPackageJsonPath = (): string =>
2222
resolve(getInstallDirectory(), '..', 'package.json');
2323

24-
const isGitInstallValid = async (): Promise<boolean> => {
24+
const gitVersionSatisfies = async (semver: string): Promise<boolean> => {
2525
try {
2626
const [version] = await executeCommand("git -v | cut -d' ' -f3");
2727

@@ -30,12 +30,15 @@ const isGitInstallValid = async (): Promise<boolean> => {
3030
return false;
3131
}
3232

33-
return satisfies(gitVersion, '>=2.25.0');
33+
return satisfies(gitVersion, semver);
3434
} catch {
3535
return false;
3636
}
3737
};
3838

39+
const isGitVersionMinimum = async () => gitVersionSatisfies('>=2.25.0');
40+
const useNewCommandSyntax = async () => gitVersionSatisfies('>=2.37.1');
41+
3942
const executeCommand = async (
4043
command: string,
4144
cwd: string = './',
@@ -53,11 +56,14 @@ const executeGitCommand = (cwd: string, args: string[] = []): Promise<void> =>
5356
new Promise(
5457
(resolve: () => void, reject: (reason?: string | Error) => void) => {
5558
const stderr = [];
56-
const process = spawn('git', args, { cwd, stdio: [0, 0, 'pipe'] });
59+
const process = spawn('git', args, { cwd });
5760

5861
process.stderr.setEncoding('utf-8');
5962
process.stderr.on('data', (chunk) => stderr.push(chunk));
60-
process.on('error', () => reject(`Error occurred during execution!`));
63+
process.on('error', (error) => {
64+
console.error(error);
65+
reject(`Error occurred during execution!`);
66+
});
6167
process.on('close', (code: number) =>
6268
code === 0
6369
? resolve()
@@ -80,7 +86,7 @@ export async function cloneSparse(
8086
throw new Error('Invalid arguments supplied!');
8187
}
8288

83-
if (!isGitInstallValid()) {
89+
if (!(await isGitVersionMinimum())) {
8490
throw new Error('Git >= 2.25.0 was not found!');
8591
}
8692

@@ -110,32 +116,57 @@ export async function cloneSparse(
110116
throw new Error(`${cwd} already exists, refusing to overwrite it!`);
111117
}
112118

119+
const newSyntax = await useNewCommandSyntax();
120+
113121
console.log('Pre-flight checks passed, performing bare clone...');
114-
await executeGitCommand(workingCopyParent, [
115-
'clone',
116-
'-n',
117-
'--depth',
118-
'1',
119-
'--filter=tree:0',
120-
repoUrl,
121-
workingCopyDir
122-
]);
122+
123+
if (newSyntax) {
124+
await executeGitCommand(workingCopyParent, [
125+
'clone',
126+
'--filter=blob:none',
127+
'--no-checkout',
128+
'--depth',
129+
'1',
130+
'--sparse',
131+
repoUrl,
132+
workingCopyDir
133+
]);
134+
} else {
135+
await executeGitCommand(workingCopyParent, [
136+
'clone',
137+
'-n',
138+
'--depth',
139+
'1',
140+
'--filter=tree:0',
141+
repoUrl,
142+
workingCopyDir
143+
]);
144+
}
123145
console.log('Adding desired paths to sparse-checkout...');
124146
paths = paths.map((path) => path.replace(/"/g, '').replace(/^\./, ''));
125-
if (!globs) {
126-
await executeGitCommand(cwd, [
127-
'sparse-checkout',
128-
'set',
129-
'--no-cone',
130-
...paths
131-
]);
147+
148+
if (newSyntax) {
149+
await executeGitCommand(cwd, ['sparse-checkout', 'add', ...paths]);
132150
} else {
133-
await executeGitCommand(cwd, ['sparse-checkout', 'init']);
134-
await executeCommand(`echo !/* > .git/info/sparse-checkout`, cwd);
135-
for (const path of paths) {
136-
await executeCommand(`echo ${path} >> .git/info/sparse-checkout`, cwd);
151+
if (!globs) {
152+
await executeGitCommand(cwd, [
153+
'sparse-checkout',
154+
'set',
155+
'--no-cone',
156+
...paths
157+
]);
158+
} else {
159+
await executeGitCommand(cwd, ['sparse-checkout', 'init']);
160+
await executeCommand(`echo !/* > .git/info/sparse-checkout`, cwd);
161+
for (const path of paths) {
162+
await executeCommand(
163+
`echo ${path} >> .git/info/sparse-checkout`,
164+
cwd
165+
);
166+
}
137167
}
138168
}
169+
139170
console.log('Performing final checkout...');
140171
// todo: add progress bar
141172
await executeGitCommand(cwd, ['checkout']);

0 commit comments

Comments
 (0)