From 07454e2bf2d676e16f2fb000202f9d2635cd7aec Mon Sep 17 00:00:00 2001 From: Huy Tran Date: Mon, 9 Dec 2024 13:48:42 +0700 Subject: [PATCH 1/5] Add remote branch option --- src/cli/actions/remoteAction.ts | 5 +++-- src/cli/cliRun.ts | 2 ++ src/core/file/gitCommand.ts | 3 ++- tests/core/file/gitCommand.test.ts | 19 ++++++++++++++++--- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/cli/actions/remoteAction.ts b/src/cli/actions/remoteAction.ts index 992d4c95f..9390b37c6 100644 --- a/src/cli/actions/remoteAction.ts +++ b/src/cli/actions/remoteAction.ts @@ -29,7 +29,7 @@ export const runRemoteAction = async ( spinner.start(); // Clone the repository - await cloneRepository(formatGitUrl(repoUrl), tempDirPath, { + await cloneRepository(formatGitUrl(repoUrl), tempDirPath, options.branch, { execGitShallowClone: deps.execGitShallowClone, }); @@ -73,6 +73,7 @@ export const createTempDirectory = async (): Promise => { export const cloneRepository = async ( url: string, directory: string, + branch?:string, deps = { execGitShallowClone: execGitShallowClone, }, @@ -81,7 +82,7 @@ export const cloneRepository = async ( logger.log(''); try { - await deps.execGitShallowClone(url, directory); + await deps.execGitShallowClone(url, directory, branch); } catch (error) { throw new RepomixError(`Failed to clone repository: ${(error as Error).message}`); } diff --git a/src/cli/cliRun.ts b/src/cli/cliRun.ts index 0693016bb..806ad91bd 100644 --- a/src/cli/cliRun.ts +++ b/src/cli/cliRun.ts @@ -24,6 +24,7 @@ export interface CliOptions extends OptionValues { init?: boolean; global?: boolean; remote?: string; + branch?:string; } export const run = async () => { @@ -44,6 +45,7 @@ export const run = async () => { .option('--init', 'initialize a new repomix.config.json file') .option('--global', 'use global configuration (only applicable with --init)') .option('--remote ', 'process a remote Git repository') + .option('--branch ', 'select a specific branch or commit id') .action((directory = '.', options: CliOptions = {}) => executeAction(directory, process.cwd(), options)); await program.parseAsync(process.argv); diff --git a/src/core/file/gitCommand.ts b/src/core/file/gitCommand.ts index c243f7e92..fc0e2ed0a 100644 --- a/src/core/file/gitCommand.ts +++ b/src/core/file/gitCommand.ts @@ -21,9 +21,10 @@ export const isGitInstalled = async ( export const execGitShallowClone = async ( url: string, directory: string, + branch?:string, deps = { execAsync, }, ) => { - await deps.execAsync(`git clone --depth 1 ${url} ${directory}`); + await deps.execAsync(`git clone --depth 1 ${branch ? `-b ${branch} `:""}${url} ${directory}`); }; diff --git a/tests/core/file/gitCommand.test.ts b/tests/core/file/gitCommand.test.ts index cc5c790c8..1864390bd 100644 --- a/tests/core/file/gitCommand.test.ts +++ b/tests/core/file/gitCommand.test.ts @@ -44,21 +44,34 @@ describe('gitCommand', () => { 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, { execAsync: mockExecAsync }); + await execGitShallowClone(url, directory, branch, { execAsync: mockExecAsync }); - expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 ${url} ${directory}`); + expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 -b ${branch} ${url} ${directory}`); }); test('should throw error when git clone fails', async () => { const mockExecAsync = vi.fn().mockRejectedValue(new Error('Authentication failed')); const url = 'https://github.com/user/repo.git'; const directory = '/tmp/repo'; + const branch = 'master'; - await expect(execGitShallowClone(url, directory, { execAsync: mockExecAsync })).rejects.toThrow( + await expect(execGitShallowClone(url, directory, branch, { execAsync: mockExecAsync })).rejects.toThrow( 'Authentication failed', ); + expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 -b ${branch} ${url} ${directory}`); + }); + + test('should execute without branch 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 branch = undefined; + + await execGitShallowClone(url, directory, branch, { execAsync: mockExecAsync }); + expect(mockExecAsync).toHaveBeenCalledWith(`git clone --depth 1 ${url} ${directory}`); }); }); From 75e3043445f72db8479a8804483f9dfaaab57b74 Mon Sep 17 00:00:00 2001 From: Huy Tran Date: Tue, 10 Dec 2024 02:42:14 +0700 Subject: [PATCH 2/5] Change --branch option to --remote-branch option --- src/cli/cliRun.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/cliRun.ts b/src/cli/cliRun.ts index 806ad91bd..4bc7f3e25 100644 --- a/src/cli/cliRun.ts +++ b/src/cli/cliRun.ts @@ -45,7 +45,7 @@ export const run = async () => { .option('--init', 'initialize a new repomix.config.json file') .option('--global', 'use global configuration (only applicable with --init)') .option('--remote ', 'process a remote Git repository') - .option('--branch ', 'select a specific branch or commit id') + .option('--remote-branch ', 'select a specific branch or commit id') .action((directory = '.', options: CliOptions = {}) => executeAction(directory, process.cwd(), options)); await program.parseAsync(process.argv); From 74218bebf3362bf3b10d26d9e1b13a86ccd4e8e7 Mon Sep 17 00:00:00 2001 From: tranquochuy645 Date: Tue, 10 Dec 2024 21:32:17 +0700 Subject: [PATCH 3/5] Run biome --- src/cli/actions/remoteAction.ts | 2 +- src/cli/cliRun.ts | 2 +- src/core/file/gitCommand.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cli/actions/remoteAction.ts b/src/cli/actions/remoteAction.ts index 9390b37c6..6a47cbbc7 100644 --- a/src/cli/actions/remoteAction.ts +++ b/src/cli/actions/remoteAction.ts @@ -73,7 +73,7 @@ export const createTempDirectory = async (): Promise => { export const cloneRepository = async ( url: string, directory: string, - branch?:string, + branch?: string, deps = { execGitShallowClone: execGitShallowClone, }, diff --git a/src/cli/cliRun.ts b/src/cli/cliRun.ts index 4bc7f3e25..736b0de44 100644 --- a/src/cli/cliRun.ts +++ b/src/cli/cliRun.ts @@ -24,7 +24,7 @@ export interface CliOptions extends OptionValues { init?: boolean; global?: boolean; remote?: string; - branch?:string; + branch?: string; } export const run = async () => { diff --git a/src/core/file/gitCommand.ts b/src/core/file/gitCommand.ts index fc0e2ed0a..b8801ee7b 100644 --- a/src/core/file/gitCommand.ts +++ b/src/core/file/gitCommand.ts @@ -21,10 +21,10 @@ export const isGitInstalled = async ( export const execGitShallowClone = async ( url: string, directory: string, - branch?:string, + branch?: string, deps = { execAsync, }, ) => { - await deps.execAsync(`git clone --depth 1 ${branch ? `-b ${branch} `:""}${url} ${directory}`); + await deps.execAsync(`git clone --depth 1 ${branch ? `-b ${branch} ` : ''}${url} ${directory}`); }; From 5662450f4f8184f88fc4d9921a93e6a8a0fd0911 Mon Sep 17 00:00:00 2001 From: Kazuki Yamada Date: Tue, 10 Dec 2024 06:45:41 -0800 Subject: [PATCH 4/5] fix(cli): Fix CliOptions prop name --- src/cli/actions/remoteAction.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/actions/remoteAction.ts b/src/cli/actions/remoteAction.ts index 6a47cbbc7..1cfbcf719 100644 --- a/src/cli/actions/remoteAction.ts +++ b/src/cli/actions/remoteAction.ts @@ -29,7 +29,7 @@ export const runRemoteAction = async ( spinner.start(); // Clone the repository - await cloneRepository(formatGitUrl(repoUrl), tempDirPath, options.branch, { + await cloneRepository(formatGitUrl(repoUrl), tempDirPath, options.remoteBranch, { execGitShallowClone: deps.execGitShallowClone, }); From 66b6e73dd567d36ded6735d6f018a42ce0e63214 Mon Sep 17 00:00:00 2001 From: Kazuki Yamada Date: Tue, 10 Dec 2024 06:45:50 -0800 Subject: [PATCH 5/5] fix(cli): Fix CliOptions prop name --- src/cli/cliRun.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/cliRun.ts b/src/cli/cliRun.ts index 736b0de44..775ba5d9b 100644 --- a/src/cli/cliRun.ts +++ b/src/cli/cliRun.ts @@ -24,7 +24,7 @@ export interface CliOptions extends OptionValues { init?: boolean; global?: boolean; remote?: string; - branch?: string; + remoteBranch?: string; } export const run = async () => {