Pass onAuth into the git constructor instead of to push method

This commit is contained in:
Alex Gleason committed 2025-12-22 21:16:18 -06:00
1 parent a915b9121f
commit 3eb156e983
6 files changed
+19 -37

No files matched your search

@@ -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;
},
});
}
@@ -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';
+3
View File
@@ -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)
+10 -3
View File
@@ -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 };
}
-5
View File
@@ -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,
});
+6 -2
View File
@@ -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: '[email protected]',
};
this.onAuth = options.onAuth;
}
// Repository initialization and configuration
@@ -241,7 +244,7 @@ export class Git {
});
}
async push(options: Omit<Parameters<typeof git.push>[0], 'fs' | 'http' | 'corsProxy'> & { signer?: NostrSigner }) {
async push(options: Omit<Parameters<typeof git.push>[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,
});
}