Changes from fix/list-branch-issue-on-fresh-repo

This commit is contained in:
Kacper
2025-12-23 20:46:10 +01:00
parent 629b7e7433
commit 4958ee1dda
15 changed files with 386 additions and 47 deletions

View File

@@ -1,6 +1,6 @@
import { useState, useCallback } from 'react';
import { getElectronAPI } from '@/lib/electron';
import type { BranchInfo } from '../types';
import type { BranchInfo, GitRepoStatus } from '../types';
export function useBranches() {
const [branches, setBranches] = useState<BranchInfo[]>([]);
@@ -8,6 +8,10 @@ export function useBranches() {
const [behindCount, setBehindCount] = useState(0);
const [isLoadingBranches, setIsLoadingBranches] = useState(false);
const [branchFilter, setBranchFilter] = useState('');
const [gitRepoStatus, setGitRepoStatus] = useState<GitRepoStatus>({
isGitRepo: true,
hasCommits: true,
});
const fetchBranches = useCallback(async (worktreePath: string) => {
setIsLoadingBranches(true);
@@ -22,9 +26,31 @@ export function useBranches() {
setBranches(result.result.branches);
setAheadCount(result.result.aheadCount || 0);
setBehindCount(result.result.behindCount || 0);
setGitRepoStatus({ isGitRepo: true, hasCommits: true });
} else if (result.code === 'NOT_GIT_REPO') {
// Not a git repository - clear branches silently without logging an error
setBranches([]);
setAheadCount(0);
setBehindCount(0);
setGitRepoStatus({ isGitRepo: false, hasCommits: false });
} else if (result.code === 'NO_COMMITS') {
// Git repo but no commits yet - clear branches silently without logging an error
setBranches([]);
setAheadCount(0);
setBehindCount(0);
setGitRepoStatus({ isGitRepo: true, hasCommits: false });
} else if (!result.success) {
// Other errors - log them
console.warn('Failed to fetch branches:', result.error);
setBranches([]);
setAheadCount(0);
setBehindCount(0);
}
} catch (error) {
console.error('Failed to fetch branches:', error);
setBranches([]);
setAheadCount(0);
setBehindCount(0);
} finally {
setIsLoadingBranches(false);
}
@@ -48,5 +74,6 @@ export function useBranches() {
setBranchFilter,
resetBranchFilter,
fetchBranches,
gitRepoStatus,
};
}

View File

@@ -3,6 +3,15 @@ import { getElectronAPI } from '@/lib/electron';
import { toast } from 'sonner';
import type { WorktreeInfo } from '../types';
// Error codes that need special user-friendly handling
const GIT_STATUS_ERROR_CODES = ['NOT_GIT_REPO', 'NO_COMMITS'] as const;
// User-friendly messages for git status errors
const GIT_STATUS_ERROR_MESSAGES: Record<string, string> = {
NOT_GIT_REPO: 'This directory is not a git repository',
NO_COMMITS: 'Repository has no commits yet. Create an initial commit first.',
};
interface UseWorktreeActionsOptions {
fetchWorktrees: () => Promise<Array<{ path: string; branch: string }> | undefined>;
fetchBranches: (worktreePath: string) => Promise<void>;
@@ -29,6 +38,15 @@ export function useWorktreeActions({ fetchWorktrees, fetchBranches }: UseWorktre
toast.success(result.result.message);
fetchWorktrees();
} else {
// Handle git status errors with informative messages
const errorCode = (result as { code?: string }).code;
if (
errorCode &&
GIT_STATUS_ERROR_CODES.includes(errorCode as (typeof GIT_STATUS_ERROR_CODES)[number])
) {
toast.info(GIT_STATUS_ERROR_MESSAGES[errorCode] || result.error);
return;
}
toast.error(result.error || 'Failed to switch branch');
}
} catch (error) {
@@ -56,6 +74,15 @@ export function useWorktreeActions({ fetchWorktrees, fetchBranches }: UseWorktre
toast.success(result.result.message);
fetchWorktrees();
} else {
// Handle git status errors with informative messages
const errorCode = (result as { code?: string }).code;
if (
errorCode &&
GIT_STATUS_ERROR_CODES.includes(errorCode as (typeof GIT_STATUS_ERROR_CODES)[number])
) {
toast.info(GIT_STATUS_ERROR_MESSAGES[errorCode] || result.error);
return;
}
toast.error(result.error || 'Failed to pull latest changes');
}
} catch (error) {
@@ -84,6 +111,15 @@ export function useWorktreeActions({ fetchWorktrees, fetchBranches }: UseWorktre
fetchBranches(worktree.path);
fetchWorktrees();
} else {
// Handle git status errors with informative messages
const errorCode = (result as { code?: string }).code;
if (
errorCode &&
GIT_STATUS_ERROR_CODES.includes(errorCode as (typeof GIT_STATUS_ERROR_CODES)[number])
) {
toast.info(GIT_STATUS_ERROR_MESSAGES[errorCode] || result.error);
return;
}
toast.error(result.error || 'Failed to push changes');
}
} catch (error) {