Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for commit hashes along with branches #213

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/core/file/gitCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
yamadashy marked this conversation as resolved.
Show resolved Hide resolved
};
47 changes: 40 additions & 7 deletions tests/core/file/gitCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,39 @@ 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';
const branch = 'master';

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 () => {
Expand All @@ -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}`);
});
});
});