From 2f2cc5cc499dc904a356e5c2722a76c35d997fdc Mon Sep 17 00:00:00 2001 From: Darrell Adjei Date: Sat, 21 Dec 2024 13:48:05 +0000 Subject: [PATCH 1/2] Add support for commit hashes along with branches --- src/core/file/gitCommand.ts | 7 +++-- tests/core/file/gitCommand.test.ts | 45 +++++++++++++++++++++++++----- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/core/file/gitCommand.ts b/src/core/file/gitCommand.ts index b8801ee7..fe262815 100644 --- a/src/core/file/gitCommand.ts +++ b/src/core/file/gitCommand.ts @@ -21,10 +21,13 @@ export const isGitInstalled = async ( export const execGitShallowClone = async ( url: string, directory: string, - branch?: string, + branchOrCommit?: string, deps = { execAsync, }, ) => { - await deps.execAsync(`git clone --depth 1 ${branch ? `-b ${branch} ` : ''}${url} ${directory}`); + await deps.execAsync(`git clone --depth 1 ${url} ${directory}`); + if (branchOrCommit) { + await deps.execAsync(`git -C ${directory} checkout ${branchOrCommit}`); + } }; diff --git a/tests/core/file/gitCommand.test.ts b/tests/core/file/gitCommand.test.ts index 1864390b..eff2548d 100644 --- a/tests/core/file/gitCommand.test.ts +++ b/tests/core/file/gitCommand.test.ts @@ -40,7 +40,7 @@ describe('gitCommand', () => { }); describe('execGitShallowClone', () => { - test('should execute git clone with correct parameters', async () => { + test('should execute git clone with correct parameters for branch', async () => { const mockExecAsync = vi.fn().mockResolvedValue({ stdout: '', stderr: '' }); const url = 'https://github.com/user/repo.git'; const directory = '/tmp/repo'; @@ -48,7 +48,31 @@ describe('gitCommand', () => { await execGitShallowClone(url, directory, branch, { execAsync: mockExecAsync }); - expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 -b ${branch} ${url} ${directory}`); + expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 ${url} ${directory}`); + expect(mockExecAsync).toHaveBeenCalledWith(`git -C ${directory} checkout ${branch}`); + }); + + test('should execute git clone and checkout with correct parameters for commit hash', async () => { + const mockExecAsync = vi.fn().mockResolvedValue({ stdout: '', stderr: '' }); + const url = 'https://github.com/user/repo.git'; + const directory = '/tmp/repo'; + const commitHash = 'abc123def456'; + + await execGitShallowClone(url, directory, commitHash, { execAsync: mockExecAsync }); + + expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 ${url} ${directory}`); + expect(mockExecAsync).toHaveBeenCalledWith(`git -C ${directory} checkout ${commitHash}`); + }); + + test('should execute without branchOrCommit option if not specified by user', async () => { + const mockExecAsync = vi.fn().mockResolvedValue({ stdout: '', stderr: '' }); + const url = 'https://github.com/user/repo.git'; + const directory = '/tmp/repo'; + const branchOrCommit = undefined; + + await execGitShallowClone(url, directory, branchOrCommit, { execAsync: mockExecAsync }); + + expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 ${url} ${directory}`); }); test('should throw error when git clone fails', async () => { @@ -61,18 +85,25 @@ describe('gitCommand', () => { 'Authentication failed', ); - expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 -b ${branch} ${url} ${directory}`); + expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 ${url} ${directory}`); }); - test('should execute without branch option if not specified by user', async () => { - const mockExecAsync = vi.fn().mockResolvedValue({ stdout: '', stderr: '' }); + test('should throw error when checkout fails for commit hash', async () => { + const mockExecAsync = vi + .fn() + .mockImplementation((cmd) => + cmd.includes('checkout') ? Promise.reject(new Error('Checkout failed')) : Promise.resolve({ stdout: '', stderr: '' }) + ); const url = 'https://github.com/user/repo.git'; const directory = '/tmp/repo'; - const branch = undefined; + const commitHash = 'abc123def456'; - await execGitShallowClone(url, directory, branch, { execAsync: mockExecAsync }); + await expect(execGitShallowClone(url, directory, commitHash, { execAsync: mockExecAsync })).rejects.toThrow( + 'Checkout failed', + ); expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 ${url} ${directory}`); + expect(mockExecAsync).toHaveBeenCalledWith(`git -C ${directory} checkout ${commitHash}`); }); }); }); From 0943692956b8607d8b4b005d1e642f8f9ade5a11 Mon Sep 17 00:00:00 2001 From: Darrell Adjei Date: Sat, 21 Dec 2024 13:48:05 +0000 Subject: [PATCH 2/2] Add support for commit hashes along with branches --- src/core/file/gitCommand.ts | 7 +++-- tests/core/file/gitCommand.test.ts | 47 +++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/core/file/gitCommand.ts b/src/core/file/gitCommand.ts index b8801ee7..fe262815 100644 --- a/src/core/file/gitCommand.ts +++ b/src/core/file/gitCommand.ts @@ -21,10 +21,13 @@ export const isGitInstalled = async ( export const execGitShallowClone = async ( url: string, directory: string, - branch?: string, + branchOrCommit?: string, deps = { execAsync, }, ) => { - await deps.execAsync(`git clone --depth 1 ${branch ? `-b ${branch} ` : ''}${url} ${directory}`); + await deps.execAsync(`git clone --depth 1 ${url} ${directory}`); + if (branchOrCommit) { + await deps.execAsync(`git -C ${directory} checkout ${branchOrCommit}`); + } }; diff --git a/tests/core/file/gitCommand.test.ts b/tests/core/file/gitCommand.test.ts index 1864390b..a7bf2cc0 100644 --- a/tests/core/file/gitCommand.test.ts +++ b/tests/core/file/gitCommand.test.ts @@ -40,7 +40,7 @@ describe('gitCommand', () => { }); describe('execGitShallowClone', () => { - test('should execute git clone with correct parameters', async () => { + test('should execute git clone with correct parameters for branch', async () => { const mockExecAsync = vi.fn().mockResolvedValue({ stdout: '', stderr: '' }); const url = 'https://github.com/user/repo.git'; const directory = '/tmp/repo'; @@ -48,7 +48,31 @@ describe('gitCommand', () => { await execGitShallowClone(url, directory, branch, { execAsync: mockExecAsync }); - expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 -b ${branch} ${url} ${directory}`); + expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 ${url} ${directory}`); + expect(mockExecAsync).toHaveBeenCalledWith(`git -C ${directory} checkout ${branch}`); + }); + + test('should execute git clone and checkout with correct parameters for commit hash', async () => { + const mockExecAsync = vi.fn().mockResolvedValue({ stdout: '', stderr: '' }); + const url = 'https://github.com/user/repo.git'; + const directory = '/tmp/repo'; + const commitHash = 'abc123def456'; + + await execGitShallowClone(url, directory, commitHash, { execAsync: mockExecAsync }); + + expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 ${url} ${directory}`); + expect(mockExecAsync).toHaveBeenCalledWith(`git -C ${directory} checkout ${commitHash}`); + }); + + test('should execute without branchOrCommit option if not specified by user', async () => { + const mockExecAsync = vi.fn().mockResolvedValue({ stdout: '', stderr: '' }); + const url = 'https://github.com/user/repo.git'; + const directory = '/tmp/repo'; + const branchOrCommit = undefined; + + await execGitShallowClone(url, directory, branchOrCommit, { execAsync: mockExecAsync }); + + expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 ${url} ${directory}`); }); test('should throw error when git clone fails', async () => { @@ -61,18 +85,27 @@ describe('gitCommand', () => { 'Authentication failed', ); - expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 -b ${branch} ${url} ${directory}`); + expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 ${url} ${directory}`); }); - test('should execute without branch option if not specified by user', async () => { - const mockExecAsync = vi.fn().mockResolvedValue({ stdout: '', stderr: '' }); + test('should throw error when checkout fails for commit hash', async () => { + const mockExecAsync = vi + .fn() + .mockImplementation((cmd) => + cmd.includes('checkout') + ? Promise.reject(new Error('Checkout failed')) + : Promise.resolve({ stdout: '', stderr: '' }), + ); const url = 'https://github.com/user/repo.git'; const directory = '/tmp/repo'; - const branch = undefined; + const commitHash = 'abc123def456'; - await execGitShallowClone(url, directory, branch, { execAsync: mockExecAsync }); + await expect(execGitShallowClone(url, directory, commitHash, { execAsync: mockExecAsync })).rejects.toThrow( + 'Checkout failed', + ); expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 ${url} ${directory}`); + expect(mockExecAsync).toHaveBeenCalledWith(`git -C ${directory} checkout ${commitHash}`); }); }); });