From b7e94ff67cd448df878d415c2cf29b937e074584 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 30 Sep 2025 12:42:57 -0500 Subject: [PATCH] Start fixing "git pull" --- src/lib/commands/git/pull.test.ts | 84 +++++++++++++++++++++++++++++++ src/lib/commands/git/pull.ts | 29 +++++------ 2 files changed, 96 insertions(+), 17 deletions(-) create mode 100644 src/lib/commands/git/pull.test.ts diff --git a/src/lib/commands/git/pull.test.ts b/src/lib/commands/git/pull.test.ts new file mode 100644 index 00000000..d4273608 --- /dev/null +++ b/src/lib/commands/git/pull.test.ts @@ -0,0 +1,84 @@ +import { describe, it, expect, beforeEach } from 'vitest'; +import { GitPullCommand } from './pull'; +import type { GitSubcommandOptions } from '../git'; +import type { Git } from '../../git'; +import type { JSRuntimeFS } from '../../JSRuntime'; + +// Mock implementations +const mockGit = { + currentBranch: async () => 'main', + listRemotes: async () => [{ remote: 'origin', url: 'https://github.com/user/repo.git' }], + statusMatrix: async () => [], + fetch: async () => {}, + resolveRef: async () => 'abc123', + merge: async () => {}, +} as unknown as Git; + +const mockFS = { + stat: async () => ({ isDirectory: () => true }), +} as unknown as JSRuntimeFS; + +const mockOptions: GitSubcommandOptions = { + git: mockGit, + fs: mockFS, + pwd: '/test/repo', +}; + +describe('GitPullCommand', () => { + let command: GitPullCommand; + + beforeEach(() => { + command = new GitPullCommand(mockOptions); + }); + + describe('parseArgs', () => { + it('should parse no arguments correctly', () => { + // Access the private method for testing + const result = (command as unknown as { parseArgs: (args: string[]) => { remote: string; branch?: string } }).parseArgs([]); + expect(result).toEqual({ + remote: 'origin', + branch: undefined, + }); + }); + + it('should parse remote only', () => { + const result = (command as unknown as { parseArgs: (args: string[]) => { remote: string; branch?: string } }).parseArgs(['upstream']); + expect(result).toEqual({ + remote: 'upstream', + branch: undefined, + }); + }); + + it('should parse remote and branch correctly', () => { + const result = (command as unknown as { parseArgs: (args: string[]) => { remote: string; branch?: string } }).parseArgs(['origin', 'main']); + expect(result).toEqual({ + remote: 'origin', + branch: 'main', + }); + }); + + it('should parse different remote and branch', () => { + const result = (command as unknown as { parseArgs: (args: string[]) => { remote: string; branch?: string } }).parseArgs(['upstream', 'develop']); + expect(result).toEqual({ + remote: 'upstream', + branch: 'develop', + }); + }); + + it('should ignore option flags', () => { + const result = (command as unknown as { parseArgs: (args: string[]) => { remote: string; branch?: string } }).parseArgs(['--rebase', 'origin', '--verbose', 'main']); + expect(result).toEqual({ + remote: 'origin', + branch: 'main', + }); + }); + + it('should handle extra arguments beyond remote and branch', () => { + const result = (command as unknown as { parseArgs: (args: string[]) => { remote: string; branch?: string } }).parseArgs(['origin', 'main', 'extra', 'args']); + expect(result).toEqual({ + remote: 'origin', + branch: 'main', + }); + }); + }); +}); \ No newline at end of file diff --git a/src/lib/commands/git/pull.ts b/src/lib/commands/git/pull.ts index 159e0c48..deee289f 100644 --- a/src/lib/commands/git/pull.ts +++ b/src/lib/commands/git/pull.ts @@ -35,7 +35,7 @@ export class GitPullCommand implements GitSubcommand { let currentBranch: string | null = null; try { currentBranch = await this.git.currentBranch({ - + dir: this.pwd, }) || null; if (!targetBranch) { @@ -51,7 +51,7 @@ export class GitPullCommand implements GitSubcommand { let remoteUrl: string; try { const remotes = await this.git.listRemotes({ - + dir: this.pwd, }); @@ -90,7 +90,7 @@ export class GitPullCommand implements GitSubcommand { let remoteOid: string; try { remoteOid = await this.git.resolveRef({ - + dir: this.pwd, ref: remoteRef, }); @@ -102,14 +102,14 @@ export class GitPullCommand implements GitSubcommand { let currentOid: string; try { currentOid = await this.git.resolveRef({ - + dir: this.pwd, ref: 'HEAD', }); } catch { // Empty repository, just checkout the remote branch await this.git.checkout({ - + dir: this.pwd, ref: targetBranch, }); @@ -123,7 +123,7 @@ export class GitPullCommand implements GitSubcommand { // Perform fast-forward merge (simplified) await this.git.merge({ - + dir: this.pwd, ours: currentBranch || 'HEAD', theirs: remoteRef, @@ -157,18 +157,13 @@ export class GitPullCommand implements GitSubcommand { let remote = 'origin'; // Default remote let branch: string | undefined; - for (let i = 0; i < args.length; i++) { - const arg = args[i]; + const nonOptionArgs = args.filter(arg => !arg.startsWith('-')); - if (!arg.startsWith('-')) { - if (remote === 'origin' && !branch) { - // First non-option argument is remote - remote = arg; - } else if (!branch) { - // Second non-option argument is branch - branch = arg; - } - } + if (nonOptionArgs.length >= 1) { + remote = nonOptionArgs[0]; + } + if (nonOptionArgs.length >= 2) { + branch = nonOptionArgs[1]; } return { remote, branch };