From 21e522cba7b98e03047c808bbd4b3a13e9dc6daf Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 12 Nov 2025 15:27:43 +0000 Subject: [PATCH] Fix git fetch handle deteled refs --- src/hooks/useGitFetch.ts | 2 +- src/lib/git.ts | 47 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/hooks/useGitFetch.ts b/src/hooks/useGitFetch.ts index 31931976..596097fb 100644 --- a/src/hooks/useGitFetch.ts +++ b/src/hooks/useGitFetch.ts @@ -346,7 +346,7 @@ export function useGitFetch(projectId: string | null) { if (currentBranchChanged && query.data.currentBranch) { toast({ title: 'Remote changes detected', - description: `The ${query.data.currentBranch} branch has new commits available.`, + description: `The ${query.data.currentBranch} branch has ${query.data.currentBranchOid === null ? 'been deleted' : 'new commits available'}.`, variant: 'default', }); diff --git a/src/lib/git.ts b/src/lib/git.ts index 4d5c0181..79a9d6af 100644 --- a/src/lib/git.ts +++ b/src/lib/git.ts @@ -1388,7 +1388,7 @@ export class Git { /** * Update remote tracking refs to match the Nostr state if git data present locally - * @returns refs with missing data including HEAD + * @returns refs with missing data including HEAD, updated refs, and deleted stale refs */ private async updateRemoteRefsToNostrState( state: RespositoryState, @@ -1396,16 +1396,18 @@ export class Git { ): Promise<{ updated?: Record, refsWithMissingData?: Record, + deleted?: string[], }> { // Get the remote name from options (passed from the fetch call) const remoteName = options.remote || 'origin'; const refsWithMissingData: Record = {}; const updated: Record = {}; - for (const [refName, entry] of [ + const entries = [ ...Object.entries(state.refs), ...(state.HEAD ? [["HEAD", state.HEAD]] : []) - ]) { + ]; + for (const [refName, entry] of entries) { // update symbolic ref to use remote ref const value = entry.startsWith("ref: ") ? `ref: ${this.localRefToRemoteRef(remoteName, entry.slice(5))}` : entry; const isSymbolic = value.startsWith("ref: "); @@ -1456,9 +1458,48 @@ export class Git { } } + // Delete local remote refs that don't exist in Repository State + const deleted: string[] = []; + try { + // Get all local remote tracking refs for this remote + const localRemoteRefs = await git.listRefs({ + fs: this.fs, + dir: options.dir, + filepath: `refs/remotes/${remoteName}` + }); + + // Build set of expected remote refs based on Repository State + // This should match the same set of refs we iterated over above + const expectedRemoteRefs = new Set(); + for (const [refName] of entries) { + expectedRemoteRefs.add(this.localRefToRemoteRef(remoteName, refName)); + } + // Delete any local remote refs that don't exist in the Repository State + for (const localRemoteRef of localRemoteRefs) { + const fullRef = this.localRefToRemoteRef(remoteName, localRemoteRef); + + if (!expectedRemoteRefs.has(fullRef)) { + try { + await git.deleteRef({ + fs: this.fs, + dir: options.dir, + ref: fullRef, + }); + console.log(`✓ Deleted stale remote ref ${localRemoteRef}`); + deleted.push(localRemoteRef); + } catch (error) { + console.warn(`Failed to delete stale remote ref ${localRemoteRef}:`, error); + } + } + } + } catch (error) { + console.warn('Failed to clean up stale remote refs:', error); + } + return { refsWithMissingData: Object.keys(refsWithMissingData).length > 0 ? refsWithMissingData : undefined, updated: Object.keys(updated).length > 0 ? updated : undefined, + deleted: deleted.length > 0 ? deleted : undefined, } }