From 118fb6cf2f4b7b3ea6b14839f15d054268de229c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 25 Dec 2025 23:20:01 -0600 Subject: [PATCH] git class bugfixes --- src/lib/git.test.ts | 100 ++++++++++++++++++++++++++++++++++++++++++++ src/lib/git.ts | 65 ++++++++++++++++++---------- 2 files changed, 143 insertions(+), 22 deletions(-) diff --git a/src/lib/git.test.ts b/src/lib/git.test.ts index b4c7447e..0ffe12ba 100644 --- a/src/lib/git.test.ts +++ b/src/lib/git.test.ts @@ -441,6 +441,106 @@ describe('Git', () => { }); }); + describe('nostr pull', () => { + it('should pull from Nostr repository and merge remote tracking branch', 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', 'my-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', 'my-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 first + await gitWithMock.clone({ + url: `nostr://${testNpub}/my-repo`, + dir: '/nostr-repo', + singleBranch: true, + depth: 1, + }); + + // Set up tracking branch configuration + await gitWithMock.setConfig({ + dir: '/nostr-repo', + path: 'branch.main.remote', + value: 'origin', + }); + await gitWithMock.setConfig({ + dir: '/nostr-repo', + path: 'branch.main.merge', + value: 'refs/heads/main', + }); + + // Verify README exists after clone + const readmeAfterClone = await fs.stat('/nostr-repo/README.md').then(() => true).catch(() => false); + expect(readmeAfterClone).toBe(true); + + // Make a local change + await fs.mkdir('/nostr-repo/.shakespeare'); + await fs.writeFile('/nostr-repo/.shakespeare/git.json', JSON.stringify({ name: 'Test', email: 'test@test.com' })); + await fs.writeFile('/nostr-repo/local.txt', 'local change'); + await gitWithMock.add({ dir: '/nostr-repo', filepath: 'local.txt' }); + await gitWithMock.commit({ dir: '/nostr-repo', message: 'Local commit' }); + + // Verify README still exists after local commit + const readmeAfterCommit = await fs.stat('/nostr-repo/README.md').then(() => true).catch(() => false); + expect(readmeAfterCommit).toBe(true); + + // Pull should merge the remote tracking branch + await gitWithMock.pull({ + dir: '/nostr-repo', + }); + + // Verify the repository state + const currentBranch = await gitWithMock.currentBranch({ dir: '/nostr-repo' }); + expect(currentBranch).toBe('main'); + + // Verify both files exist (from remote and local) + const readmeExists = await fs.stat('/nostr-repo/README.md').then(() => true).catch(() => false); + const localExists = await fs.stat('/nostr-repo/local.txt').then(() => true).catch(() => false); + expect(readmeExists).toBe(true); + expect(localExists).toBe(true); + }); + }); + describe('nostr push', () => { it('should require signer for Nostr push', async () => { const gitWithoutSigner = new Git({ diff --git a/src/lib/git.ts b/src/lib/git.ts index 9b6d7080..2c5980a7 100644 --- a/src/lib/git.ts +++ b/src/lib/git.ts @@ -58,7 +58,10 @@ export class Git { } } - private httpNoProxy = new GitHttp(undefined, this.customFetch); + // Get HTTP adapter without proxy + private get httpNoProxy(): HttpClient { + return new GitHttp(undefined, this.customFetch); + } // Get HTTP adapter for the given URL private httpForUrl(url: string | null | undefined): HttpClient { @@ -682,6 +685,7 @@ export class Git { } // Try cloning in order until one succeeds + let cloneSucceeded = false; for (const url of cloneUrls) { try { await Promise.race([ @@ -692,33 +696,45 @@ export class Git { http: this.httpNoProxy, url, }), - ]) + ]); + cloneSucceeded = true; + break; } catch { continue; } } - // Fetch from all clone URLs - await this.nostrFetch(nostrURI, { - ...options, - remote: options.remote || 'origin', + if (!cloneSucceeded) { + throw new Error('Failed to clone from any of the provided clone URLs'); + } + + // Update the remote URL to point to the Nostr URL instead of the HTTP clone URL + const remote = options.remote || 'origin'; + await this.setRemoteURL({ + dir: options.dir, + remote, + url: nostrURI.toString(), }); - // Update refs based on fetched state - for (const [name, val] of state.tags) { - if (name === 'HEAD' || name.startsWith('refs/')) { - const symbolic = val.startsWith('ref: '); - const value = symbolic ? val.substring(5) : val; + // Fetch from all clone URLs to update remote tracking branches + await this.nostrFetch(nostrURI, { + ...options, + remote, + }); - await git.writeRef({ - fs: this.fs, - dir: options.dir, - ref: name, - value, - symbolic, - force: true, - }); - } + // The clone already set up local refs correctly, but we need to ensure + // HEAD matches the Nostr state if it's a symbolic ref + const headTag = state.tags.find(([name]) => name === 'HEAD'); + if (headTag && headTag[1].startsWith('ref: ')) { + const headTarget = headTag[1].substring(5); + await git.writeRef({ + fs: this.fs, + dir: options.dir, + ref: 'HEAD', + value: headTarget, + symbolic: true, + force: true, + }); } } @@ -830,12 +846,17 @@ export class Git { throw new Error(`No remote ref configured for branch ${ref}`); } - // Merge the remote branch into the current branch + // Convert the remote ref to the remote tracking branch + // e.g., refs/heads/main -> refs/remotes/origin/main + const remote = options.remote || 'origin'; + const remoteTrackingBranch = this.toRemoteRef(remoteRef, remote); + + // Merge the remote tracking branch into the current branch await git.merge({ fs: this.fs, dir: options.dir, ours: ref, - theirs: remoteRef, + theirs: remoteTrackingBranch, author: options.author, }); }