diff --git a/src/components/GitDialog.test.tsx b/src/components/GitDialog.test.tsx index ab2a5081..de33cd8b 100644 --- a/src/components/GitDialog.test.tsx +++ b/src/components/GitDialog.test.tsx @@ -25,6 +25,7 @@ const baseGitStatus = { }, ahead: 0, behind: 0, + remoteBranchExists: false, }; vi.mock('@/hooks/useGitStatus', () => ({ @@ -82,7 +83,16 @@ describe('GitDialog', () => { expect(screen.queryByText('Authentication')).not.toBeInTheDocument(); // Authentication section is removed }); - it('shows sync status', () => { + it('shows sync status when branch is published', () => { + // Mock git status with published branch + mockUseGitStatus.mockReturnValue({ + data: { + ...baseGitStatus, + remoteBranchExists: true, + }, + refetch: mockRefetch, + }); + render( {}}> @@ -133,7 +143,17 @@ describe('GitDialog', () => { expect(pushButton.closest('button')).not.toBeDisabled(); }); - it('disables push button when no commits ahead', () => { + it('disables push button when no commits ahead and branch is published', () => { + // Mock git status with published branch and no commits ahead + mockUseGitStatus.mockReturnValue({ + data: { + ...baseGitStatus, + ahead: 0, + remoteBranchExists: true, + }, + refetch: mockRefetch, + }); + render( {}}> @@ -147,6 +167,38 @@ describe('GitDialog', () => { expect(pushButton.closest('button')).toBeDisabled(); }); + it('enables push and disables pull when branch is not published', () => { + // Mock git status with unpublished branch (has commits but no remote branch) + mockUseGitStatus.mockReturnValue({ + data: { + ...baseGitStatus, + ahead: 0, + remoteBranchExists: false, + totalCommits: 5, + }, + refetch: mockRefetch, + }); + + render( + + {}}> + + + + ); + + expect(screen.getByText('Sync Status')).toBeInTheDocument(); + expect(screen.getByText('Branch not published')).toBeInTheDocument(); + + const pushButton = screen.getByText('Push'); + expect(pushButton).toBeInTheDocument(); + expect(pushButton.closest('button')).not.toBeDisabled(); + + const pullButton = screen.getByText('Pull'); + expect(pullButton).toBeInTheDocument(); + expect(pullButton.closest('button')).toBeDisabled(); + }); + it('does not show credentials warning when credentials are configured', () => { render( diff --git a/src/components/GitDialog.tsx b/src/components/GitDialog.tsx index 343f935d..0e2421cc 100644 --- a/src/components/GitDialog.tsx +++ b/src/components/GitDialog.tsx @@ -559,7 +559,8 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog return; } - if (gitStatus.ahead === 0) { + // Allow push if there are commits ahead OR if the remote branch doesn't exist (publishing new branch) + if (gitStatus.ahead === 0 && gitStatus.remoteBranchExists) { toast({ title: "Nothing to push", description: "No commits ahead of remote. Commit your changes first.", @@ -568,6 +569,16 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog return; } + // Ensure we have commits to push when publishing a new branch + if (!gitStatus.remoteBranchExists && gitStatus.totalCommits === 0) { + toast({ + title: "Cannot publish branch", + description: "No commits to push. Commit your changes first.", + variant: "destructive", + }); + return; + } + setIsPushing(true); try { @@ -581,10 +592,25 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog signer: user?.signer, // For signing if pushing to Nostr }); + // Set up upstream tracking if this is the first push for this branch + if (!gitStatus.remoteBranchExists) { + await git.setConfig({ + dir: projectPath, + path: `branch.${gitStatus.currentBranch}.remote`, + value: remote.name, + }); + await git.setConfig({ + dir: projectPath, + path: `branch.${gitStatus.currentBranch}.merge`, + value: `refs/heads/${gitStatus.currentBranch}`, + }); + } + const commitCount = gitStatus.remoteBranchExists ? gitStatus.ahead : gitStatus.totalCommits; + const action = gitStatus.remoteBranchExists ? 'pushed to' : 'published to'; toast({ title: "Push successful", - description: `${gitStatus.ahead} commit${gitStatus.ahead !== 1 ? 's' : ''} pushed to ${remote.name}/${gitStatus.currentBranch}`, + description: `${commitCount} commit${commitCount !== 1 ? 's' : ''} ${action} ${remote.name}/${gitStatus.currentBranch}`, }); // Refresh git status after push @@ -724,6 +750,11 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog return { status: 'no-remote', text: 'No remote configured', icon: AlertCircle, color: 'text-yellow-600' }; } + // Check if remote branch doesn't exist + if (!gitStatus.remoteBranchExists && gitStatus.currentBranch) { + return { status: 'not-published', text: 'Branch not published', icon: AlertCircle, color: 'text-yellow-600' }; + } + if (gitStatus.ahead > 0 && gitStatus.behind > 0) { return { status: 'diverged', text: `${gitStatus.ahead} ahead, ${gitStatus.behind} behind`, icon: AlertCircle, color: 'text-red-600' }; } @@ -1095,7 +1126,7 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog