diff --git a/src/lib/git.test.ts b/src/lib/git.test.ts index 0e0875ba..856c19aa 100644 --- a/src/lib/git.test.ts +++ b/src/lib/git.test.ts @@ -454,6 +454,79 @@ describe('Git', () => { const currentBranch = await gitWithMock.currentBranch({ dir: '/nostr-repo-fallback' }); expect(currentBranch).toBe('main'); }); + + it('should set up branch tracking configuration after clone', async () => { + // Create a mock Git HTTP server + const mockServer = new MockGitHttpServer(); + const mockRepo = createMockRepository(); + mockServer.addRepository('https://git.example.com/user/repo', mockRepo); + + // Create Nostr repository events + const repoEvent: NostrEvent = { + id: 'repo-event-id', + pubkey: testPubkey, + created_at: Math.floor(Date.now() / 1000), + kind: 30617, + tags: [ + ['d', 'tracking-test-repo'], + ['clone', 'https://git.example.com/user/repo.git'], + ], + content: '', + sig: 'test-sig', + }; + + const stateEvent: NostrEvent = { + id: 'state-event-id', + pubkey: testPubkey, + created_at: Math.floor(Date.now() / 1000), + kind: 30618, + tags: [ + ['d', 'tracking-test-repo'], + ['HEAD', 'ref: refs/heads/main'], + ['refs/heads/main', '2e538da98e06b91e70268817b0fa3f01aeeb003e'], + ], + content: '', + sig: 'test-sig', + }; + + vi.mocked(nostr.group).mockReturnValue({ + query: vi.fn(async () => [repoEvent, stateEvent]), + } as unknown as ReturnType); + + const gitWithMock = new Git({ + fs, + nostr, + signer, + systemAuthor: { name: 'Test User', email: 'test@example.com' }, + relayList: [{ url: new URL('wss://relay.example.com'), read: true, write: true }], + fetch: mockServer.fetch, + }); + + // Clone the repository + await gitWithMock.clone({ + url: `nostr://${testNpub}/tracking-test-repo`, + dir: '/tracking-test-repo', + singleBranch: true, + depth: 1, + }); + + // Verify branch tracking configuration was set up + const branchRemote = await gitWithMock.getConfig({ + dir: '/tracking-test-repo', + path: 'branch.main.remote', + }); + expect(branchRemote).toBe('origin'); + + const branchMerge = await gitWithMock.getConfig({ + dir: '/tracking-test-repo', + path: 'branch.main.merge', + }); + expect(branchMerge).toBe('refs/heads/main'); + + // Verify that pull works without needing explicit remoteRef parameter + // This is the real test - pull should work because tracking is configured + await expect(gitWithMock.pull({ dir: '/tracking-test-repo' })).resolves.not.toThrow(); + }); }); describe('nostr fetch', () => { diff --git a/src/lib/git.ts b/src/lib/git.ts index 907acce6..5e7473b7 100644 --- a/src/lib/git.ts +++ b/src/lib/git.ts @@ -716,32 +716,24 @@ export class Git { }); // Fetch from clone URLs to get the repository objects + // This creates remote tracking branches (refs/remotes/origin/*) await this.nostrFetch(nostrURI, { ...options, remote, }); - // The NIP-34 state event is the source of truth for repository state - // Set local branch refs to match the state event - for (const [name, value] of state.tags) { - if (name.startsWith('refs/heads/')) { - // Set local branch refs to match state event - await git.writeRef({ - fs: this.fs, - dir: options.dir, - ref: name, - value, - force: true, - }); - } - } - // Checkout working directory if requested (extracts files from objects) + // When checking out a branch name that doesn't exist locally but exists as a remote tracking branch, + // checkout will automatically create the local branch and set up tracking configuration if (!options.noCheckout) { + // Extract the branch name from the ref (e.g., "refs/heads/main" -> "main") + const checkoutRef = headRef.startsWith('refs/heads/') ? headRef.substring(11) : headRef; + await git.checkout({ fs: this.fs, dir: options.dir, - ref: headRef, + ref: checkoutRef, + remote, // This tells checkout which remote to track }); } @@ -862,11 +854,7 @@ export class Git { fs: this.fs, dir: options.dir, path: `branch.${ref}.merge`, - }); - - if (!remoteRef) { - throw new Error(`No remote ref configured for branch ${ref}`); - } + }) || `refs/heads/${ref}`; // Convert the remote ref to the remote tracking branch // e.g., refs/heads/main -> refs/remotes/origin/main