From 3eb156e9834506e0acf3d73d0bb63011d606788f Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 22 Dec 2025 21:16:18 -0600 Subject: [PATCH] Pass onAuth into the git constructor instead of to push method --- .../git-sync/steps/ConfigureRepoStep.tsx | 9 --------- src/components/git-sync/steps/SyncStep.tsx | 18 ------------------ src/hooks/useGit.test.ts | 3 +++ src/hooks/useGit.ts | 13 ++++++++++--- src/lib/commands/git/push.ts | 5 ----- src/lib/git.ts | 8 ++++++-- 6 files changed, 19 insertions(+), 37 deletions(-) diff --git a/src/components/git-sync/steps/ConfigureRepoStep.tsx b/src/components/git-sync/steps/ConfigureRepoStep.tsx index 02c935c7..ca1f2753 100644 --- a/src/components/git-sync/steps/ConfigureRepoStep.tsx +++ b/src/components/git-sync/steps/ConfigureRepoStep.tsx @@ -64,15 +64,6 @@ export function ConfigureRepoStep({ dir, remote: 'origin', ref: gitStatus?.currentBranch || 'main', - onAuth: () => { - if (selectedProvider.credential) { - return { - username: selectedProvider.credential.username, - password: selectedProvider.credential.password, - }; - } - return undefined; - }, }); } diff --git a/src/components/git-sync/steps/SyncStep.tsx b/src/components/git-sync/steps/SyncStep.tsx index a74d7ce4..72413188 100644 --- a/src/components/git-sync/steps/SyncStep.tsx +++ b/src/components/git-sync/steps/SyncStep.tsx @@ -41,15 +41,6 @@ export function SyncStep({ projectId }: StepProps) { dir, ref: gitStatus?.currentBranch || 'main', singleBranch: true, - onAuth: () => { - if (matchedCredential) { - return { - username: matchedCredential.username, - password: matchedCredential.password, - }; - } - return undefined; - }, }); } catch (err) { const message = err instanceof Error ? err.message : 'Failed to pull'; @@ -72,15 +63,6 @@ export function SyncStep({ projectId }: StepProps) { dir, remote: 'origin', ref: gitStatus?.currentBranch || 'main', - onAuth: () => { - if (matchedCredential) { - return { - username: matchedCredential.username, - password: matchedCredential.password, - }; - } - return undefined; - }, }); } catch (err) { const message = err instanceof Error ? err.message : 'Failed to push'; diff --git a/src/hooks/useGit.test.ts b/src/hooks/useGit.test.ts index e70e3a80..836e12a1 100644 --- a/src/hooks/useGit.test.ts +++ b/src/hooks/useGit.test.ts @@ -93,6 +93,7 @@ describe('useGit', () => { nostr: mockNostr, corsProxy: 'https://cors.isomorphic-git.org', ngitServers: ['git.shakespeare.diy', 'relay.ngit.dev'], + onAuth: expect.any(Function), }); expect(result.current).toEqual({ git: expect.any(Object) }); }); @@ -107,6 +108,7 @@ describe('useGit', () => { nostr: mockNostr, corsProxy: 'https://custom-cors.example.com', ngitServers: ['git.shakespeare.diy', 'relay.ngit.dev'], + onAuth: expect.any(Function), }); expect(result.current).toEqual({ git: expect.any(Object) }); }); @@ -121,6 +123,7 @@ describe('useGit', () => { nostr: mockNostr, corsProxy: 'https://cors.isomorphic-git.org', ngitServers: ['git.shakespeare.diy', 'relay.ngit.dev'], + onAuth: expect.any(Function), }); // Rerender with a different wrapper (different CORS proxy) diff --git a/src/hooks/useGit.ts b/src/hooks/useGit.ts index c18ee714..9e746e6e 100644 --- a/src/hooks/useGit.ts +++ b/src/hooks/useGit.ts @@ -1,8 +1,10 @@ -import { useMemo } from 'react'; +import { useCallback, useMemo } from 'react'; import { Git } from '@/lib/git'; import { useFS } from '@/hooks/useFS'; import { useNostr } from '@nostrify/react'; import { useAppContext } from '@/hooks/useAppContext'; +import { findCredentialsForRepo } from '@/lib/gitCredentials'; +import { useGitSettings } from './useGitSettings'; /** * Hook that provides a Git instance configured with the virtual filesystem @@ -12,6 +14,7 @@ export function useGit(): { git: Git } { const { fs } = useFS(); const { nostr } = useNostr(); const { config } = useAppContext(); + const { settings } = useGitSettings(); const { corsProxy, graspMetadata } = config; const ngitServers = useMemo(() => { @@ -26,9 +29,13 @@ export function useGit(): { git: Git } { }).filter((hostname): hostname is string => Boolean(hostname)); }, [graspMetadata.relays]); + const onAuth = useCallback((url: string) => { + return findCredentialsForRepo(url, settings.credentials); + }, [settings.credentials]); + const git = useMemo(() => { - return new Git({ fs, nostr, corsProxy, ngitServers }); - }, [fs, nostr, corsProxy, ngitServers]); + return new Git({ fs, nostr, corsProxy, ngitServers, onAuth }); + }, [fs, nostr, corsProxy, ngitServers, onAuth]); 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 3e7f3450..25c6ede5 100644 --- a/src/lib/commands/git/push.ts +++ b/src/lib/commands/git/push.ts @@ -4,8 +4,6 @@ import type { NostrSigner } from "@nostrify/nostrify"; import { createSuccessResult, createErrorResult } from "../ShellCommand"; import type { GitSubcommand, GitSubcommandOptions } from "../git"; import type { Git } from "../../git"; -import { findCredentialsForRepo } from "@/lib/gitCredentials"; -import { readGitSettings } from "@/lib/configUtils"; export class GitPushCommand implements GitSubcommand { name = 'push'; @@ -64,8 +62,6 @@ export class GitPushCommand implements GitSubcommand { return createErrorResult(`fatal: '${remote}' does not appear to be a git repository`); } - const settings = await readGitSettings(this.fs); - try { await this.git.push({ dir: cwd, @@ -73,7 +69,6 @@ export class GitPushCommand implements GitSubcommand { ref: targetBranch, remoteRef: targetBranch, force: options.force, - onAuth: (url) => findCredentialsForRepo(url, settings.credentials), signer: this.signer, }); diff --git a/src/lib/git.ts b/src/lib/git.ts index ea9e3a37..82aaa33b 100644 --- a/src/lib/git.ts +++ b/src/lib/git.ts @@ -1,4 +1,4 @@ -import git, { FetchResult, GitHttpRequest, GitHttpResponse, HttpClient } from 'isomorphic-git'; +import git, { AuthCallback, FetchResult, GitHttpRequest, GitHttpResponse, HttpClient } from 'isomorphic-git'; import { NIP05 } from '@nostrify/nostrify'; import { nip19 } from 'nostr-tools'; import { proxyUrl } from './proxyUrl'; @@ -13,6 +13,7 @@ export interface GitOptions { corsProxy?: string; ngitServers?: string[]; systemAuthor?: { name: string; email: string }; + onAuth?: AuthCallback; } interface NostrCloneURI { @@ -33,6 +34,7 @@ export class Git { private nostr: NPool; private ngitServers: string[]; private systemAuthor: { name: string; email: string }; + private onAuth?: AuthCallback; constructor(options: GitOptions) { this.fs = options.fs; @@ -43,6 +45,7 @@ export class Git { name: 'shakespeare.diy', email: 'assistant@shakespeare.diy', }; + this.onAuth = options.onAuth; } // Repository initialization and configuration @@ -241,7 +244,7 @@ export class Git { }); } - async push(options: Omit[0], 'fs' | 'http' | 'corsProxy'> & { signer?: NostrSigner }) { + async push(options: Omit[0], 'fs' | 'http' | 'onAuth' | 'corsProxy'> & { signer?: NostrSigner }) { // Check if this is a Nostr repository by looking at the remote URL const remote = options.remote || 'origin'; const dir = options.dir || '.'; @@ -257,6 +260,7 @@ export class Git { return git.push({ fs: this.fs, http: this.http, + onAuth: this.onAuth, ...options, }); }