From 6739fea580024bb290133b896b72b0488f614368 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 22 Dec 2025 22:42:03 -0600 Subject: [PATCH] Pass signer into Git constructor --- src/hooks/useGit.ts | 13 +++++++++++-- src/lib/commands/git/push.ts | 4 ---- src/lib/git.test.ts | 26 ++++++++++++-------------- src/lib/git.ts | 23 +++++++++++++---------- 4 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/hooks/useGit.ts b/src/hooks/useGit.ts index 9e746e6e..7ec69574 100644 --- a/src/hooks/useGit.ts +++ b/src/hooks/useGit.ts @@ -5,6 +5,7 @@ import { useNostr } from '@nostrify/react'; import { useAppContext } from '@/hooks/useAppContext'; import { findCredentialsForRepo } from '@/lib/gitCredentials'; import { useGitSettings } from './useGitSettings'; +import { useCurrentUser } from './useCurrentUser'; /** * Hook that provides a Git instance configured with the virtual filesystem @@ -12,6 +13,7 @@ import { useGitSettings } from './useGitSettings'; */ export function useGit(): { git: Git } { const { fs } = useFS(); + const { user } = useCurrentUser(); const { nostr } = useNostr(); const { config } = useAppContext(); const { settings } = useGitSettings(); @@ -34,8 +36,15 @@ export function useGit(): { git: Git } { }, [settings.credentials]); const git = useMemo(() => { - return new Git({ fs, nostr, corsProxy, ngitServers, onAuth }); - }, [fs, nostr, corsProxy, ngitServers, onAuth]); + return new Git({ + fs, + nostr, + corsProxy, + ngitServers, + onAuth, + signer: user?.signer, + }); + }, [fs, nostr, corsProxy, ngitServers, onAuth, user?.signer]); return { git }; } \ No newline at end of file diff --git a/src/lib/commands/git/push.ts b/src/lib/commands/git/push.ts index 25c6ede5..5376c95b 100644 --- a/src/lib/commands/git/push.ts +++ b/src/lib/commands/git/push.ts @@ -1,6 +1,5 @@ import type { JSRuntimeFS } from "../../JSRuntime"; import type { ShellCommandResult } from "../ShellCommand"; -import type { NostrSigner } from "@nostrify/nostrify"; import { createSuccessResult, createErrorResult } from "../ShellCommand"; import type { GitSubcommand, GitSubcommandOptions } from "../git"; import type { Git } from "../../git"; @@ -12,12 +11,10 @@ export class GitPushCommand implements GitSubcommand { private git: Git; private fs: JSRuntimeFS; - private signer?: NostrSigner; constructor(options: GitSubcommandOptions) { this.git = options.git; this.fs = options.fs; - this.signer = options.signer; } async execute(args: string[], cwd: string): Promise { @@ -69,7 +66,6 @@ export class GitPushCommand implements GitSubcommand { ref: targetBranch, remoteRef: targetBranch, force: options.force, - signer: this.signer, }); return createSuccessResult(`To ${remoteUrl}\n ${targetBranch} -> ${targetBranch}\n`); diff --git a/src/lib/git.test.ts b/src/lib/git.test.ts index 86db8a21..cf792277 100644 --- a/src/lib/git.test.ts +++ b/src/lib/git.test.ts @@ -390,10 +390,15 @@ describe('Git', () => { getPublicKey: vi.fn().mockResolvedValue('abcd1234567890abcdef1234567890abcdef1234567890abcdef1234567890ab'), }; + let gitInstanceWithSigner: Git; + beforeEach(() => { // Mock timers to avoid waiting 5 seconds vi.useFakeTimers(); + // Create a Git instance with a signer + gitInstanceWithSigner = new Git({ fs, nostr, corsProxy, signer: mockSigner }); + mockGit.push = vi.fn().mockResolvedValue(undefined); mockGit.listRemotes = vi.fn().mockResolvedValue([ { remote: 'origin', url: 'nostr://npub1abc123/my-repo' } @@ -459,12 +464,11 @@ describe('Git', () => { mockGit.isDescendent = vi.fn().mockResolvedValue(false); await expect( - gitInstance.push({ + gitInstanceWithSigner.push({ dir: '/test', remote: 'origin', ref: 'main', force: false, - signer: mockSigner, }) ).rejects.toThrow('Updates were rejected'); }); @@ -474,12 +478,11 @@ describe('Git', () => { mockGit.isDescendent = vi.fn().mockResolvedValue(false); // Should not throw with force flag - const pushPromise = gitInstance.push({ + const pushPromise = gitInstanceWithSigner.push({ dir: '/test', remote: 'origin', ref: 'main', force: true, - signer: mockSigner, }); // Advance timers to skip the 5 second wait @@ -496,12 +499,11 @@ describe('Git', () => { mockGit.isDescendent = vi.fn().mockResolvedValue(true); // Should not throw - const pushPromise = gitInstance.push({ + const pushPromise = gitInstanceWithSigner.push({ dir: '/test', remote: 'origin', ref: 'main', force: false, - signer: mockSigner, }); // Advance timers to skip the 5 second wait @@ -517,12 +519,11 @@ describe('Git', () => { // Mock resolveRef to return the same commit as remote mockGit.resolveRef = vi.fn().mockResolvedValue('remotecommit456'); - await gitInstance.push({ + await gitInstanceWithSigner.push({ dir: '/test', remote: 'origin', ref: 'main', force: false, - signer: mockSigner, }); // Should not have attempted to push (already up-to-date) @@ -552,12 +553,11 @@ describe('Git', () => { nostr.relay = vi.fn().mockReturnValue(mockRelay); // Should not throw even if not a fast-forward (no state to check against) - const pushPromise = gitInstance.push({ + const pushPromise = gitInstanceWithSigner.push({ dir: '/test', remote: 'origin', ref: 'main', force: false, - signer: mockSigner, }); // Advance timers to skip the 5 second wait @@ -605,12 +605,11 @@ describe('Git', () => { nostr.relay = vi.fn().mockReturnValue(mockRelay); // Should not throw (no matching ref in state) - const pushPromise = gitInstance.push({ + const pushPromise = gitInstanceWithSigner.push({ dir: '/test', remote: 'origin', ref: 'main', force: false, - signer: mockSigner, }); // Advance timers to skip the 5 second wait @@ -658,12 +657,11 @@ describe('Git', () => { nostr.group = vi.fn().mockReturnValue(mockGroupRelay); - const pushPromise = gitInstance.push({ + const pushPromise = gitInstanceWithSigner.push({ dir: '/test', remote: 'origin', ref: 'main', force: false, - signer: mockSigner, }); // Advance timers to skip the 5 second wait diff --git a/src/lib/git.ts b/src/lib/git.ts index c799fab9..f776d4c9 100644 --- a/src/lib/git.ts +++ b/src/lib/git.ts @@ -14,6 +14,7 @@ export interface GitOptions { ngitServers?: string[]; systemAuthor?: { name: string; email: string }; onAuth?: AuthCallback; + signer?: NostrSigner; } interface NostrCloneURI { @@ -35,6 +36,7 @@ export class Git { private ngitServers: string[]; private systemAuthor: { name: string; email: string }; private onAuth?: AuthCallback; + private signer?: NostrSigner; constructor(options: GitOptions) { this.fs = options.fs; @@ -46,6 +48,7 @@ export class Git { email: 'assistant@shakespeare.diy', }; this.onAuth = options.onAuth; + this.signer = options.signer; } // Repository initialization and configuration @@ -248,17 +251,14 @@ export class Git { }); } - async push(options: Omit[0], 'fs' | 'http' | 'onAuth' | 'corsProxy'> & { signer?: NostrSigner }) { + async push(options: Omit[0], 'fs' | 'http' | 'onAuth' | 'corsProxy'>) { // Check if this is a Nostr repository by looking at the remote URL const remote = options.remote || 'origin'; const dir = options.dir || '.'; const remoteUrl = await this.getRemoteURL(dir, remote); if (remoteUrl && remoteUrl.startsWith('nostr://')) { - if (!options.signer) { - throw new Error('Nostr signer is required for pushing to Nostr repositories'); - } - return this.nostrPush(remoteUrl, { ...options, remote, dir: dir, signer: options.signer }); + return this.nostrPush(remoteUrl, { ...options, remote, dir: dir }); } return git.push({ @@ -1853,8 +1853,11 @@ export class Git { } } - private async nostrPush(nostrUrl: string, options: Omit[0], 'fs' | 'http' | 'onAuth' | 'corsProxy'> & { signer: NostrSigner }) { - const { signer, ...gitPushOptions } = options; + private async nostrPush(nostrUrl: string, options: Omit[0], 'fs' | 'http' | 'onAuth' | 'corsProxy'>) { + if (!this.signer) { + throw new Error('Nostr signer is required for pushing to Nostr repositories'); + } + const dir = options.dir || '.'; const ref = options.ref || 'HEAD'; const remoteRef = options.remoteRef || ref; @@ -1999,10 +2002,10 @@ export class Git { } // Apply push changes and create updated event - stateEvent = await this.applyPushAndUpdateStateEvent(state, remoteRef, localCommit, signer); + stateEvent = await this.applyPushAndUpdateStateEvent(state, remoteRef, localCommit, this.signer); } else { // No existing state event - create initial state from repository - stateEvent = await this.createInitialRepositoryState(dir, nostrURI.d, signer); + stateEvent = await this.createInitialRepositoryState(dir, nostrURI.d, this.signer); } // Publish the state event to Nostr relays @@ -2041,7 +2044,7 @@ export class Git { onAuth: this.onAuth, dir, url: cloneUrl, - ...gitPushOptions, + ...options, }); pushSuccessful = true;