Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 53 additions & 14 deletions src/components/FinishDialog.client.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -681,20 +681,6 @@ describe('FinishDialog merge blocker', () => {
);
});

it('says next to the disabled merge button why it is disabled', async () => {
mockInvoke((channel) => {
if (channel === IPC.GetBranchLog) return '';
if (channel === IPC.CheckMergeStatus) return { conflicting_files: [], main_ahead_count: 0 };
return { current_branch: 'task/parent', has_committed_changes: false };
});
mount();
await flush();
expect(confirmButton()?.disabled).toBe(true);
expect(document.querySelector('[data-testid="footer-note"]')?.textContent).toBe(
'No commits on this branch yet.',
);
});

it('asks for a commit when the changes are not committed yet', async () => {
mockInvoke((channel) => {
if (channel === IPC.GetBranchLog) return '';
Expand Down Expand Up @@ -750,6 +736,59 @@ describe('FinishDialog with the branch already on the base', () => {
});
});

describe('FinishDialog with no work on the branch', () => {
it('says nothing is left to merge and offers to close the task', async () => {
mockInvoke((channel) => {
if (channel === IPC.GetBranchLog) return '';
if (channel === IPC.CheckMergeStatus)
return {
conflicting_files: [],
main_ahead_count: 3,
remote: 'unpushed',
target_uncommitted: { path: '/repo', files: ['a.ts'], overlap: ['a.ts'] },
};
return {
current_branch: 'task/parent',
has_committed_changes: false,
has_uncommitted_changes: false,
};
});
const handlers = mount();
await flush();
expect(document.body.textContent).toContain(
'Nothing on this branch: it has no commits and no uncommitted changes, so nothing is left to merge.',
);
expect(document.querySelector('[data-testid="target-uncommitted"]')).toBeNull();
// The quiet notice says it all; no warning that the merge has nothing to bring in.
expect(document.body.textContent).not.toContain('Nothing to merge:');
// An empty branch has nothing to push or to open a PR with.
expect(document.querySelector('section[aria-label="Pull request"]')).toBeNull();
expect(
[...document.querySelectorAll('button')].some((b) => b.textContent?.trim() === 'Push'),
).toBe(false);
expect(document.querySelector('[data-testid="footer-note"]')?.textContent).toBe('');
expect(confirmButton()?.textContent).toBe('Close task');
expect(confirmButton()?.disabled).toBe(false);
confirmButton()?.click();
expect(mergeTask).not.toHaveBeenCalled();
expect(handlers.onCloseTask).toHaveBeenCalled();
});

it("does not offer to close while the worktree is off the task's branch", async () => {
mockInvoke((channel) => {
if (channel === IPC.GetBranchLog) return '';
if (channel === IPC.CheckMergeStatus) return { conflicting_files: [], main_ahead_count: 0 };
// The other branch may hold the task's work.
return { current_branch: 'task/other', has_committed_changes: false };
});
mount();
await flush();
expect(document.body.textContent).not.toContain('nothing is left to merge');
expect(confirmButton()?.textContent).not.toBe('Close task');
expect(confirmButton()?.disabled).toBe(true);
});
});

describe('FinishDialog with landed commits on another branch', () => {
it("does not call the task landed while the worktree is off the task's branch", async () => {
mockInvoke((channel) => {
Expand Down
19 changes: 10 additions & 9 deletions src/components/FinishDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,12 @@ export function FinishDialog(props: FinishDialogProps) {
headSha: () => (merge.worktreeStatus.error ? null : merge.worktreeStatus()?.head_sha),
});
const prMerging = () => pr.busy() === 'merge';
// Merged on GitHub, or the commits reached the base another way, such as a
// cherry-pick: either way closing the task is all that is left.
const landed = () => pr.mode() === 'merged' || (pr.mode() === 'local' && merge.alreadyLanded());
// The commits reached the base another way, such as a cherry-pick, or the
// branch holds no work, such as when it went out on another branch.
const nothingLeft = () => merge.alreadyLanded() || merge.nothingToMerge();
// Merged on GitHub, or nothing left to merge locally: either way closing the
// task is all that is left.
const landed = () => pr.mode() === 'merged' || (pr.mode() === 'local' && nothingLeft());
// A PR merges on GitHub, so a base that moved on locally matters only where
// it conflicts or GitHub asks for the branch to be updated.
const showBaseSync = () => {
Expand Down Expand Up @@ -174,9 +177,7 @@ export function FinishDialog(props: FinishDialogProps) {
const pushFirst = () => pr.mode() === 'pr' && pr.headDiffers();
// A local merge uses local commits, so pushing is only on offer, never required.
const offerPush = () =>
pr.mode() === 'local' &&
!merge.alreadyLanded() &&
(remote() === 'unpushed' || remote() === 'differs');
pr.mode() === 'local' && !nothingLeft() && (remote() === 'unpushed' || remote() === 'differs');
// Undefined while loading; a loaded status without a remote means there is no origin.
const createPrRemote = () =>
merge.mergeStatus.loading || merge.mergeStatus.error || !merge.mergeStatus()
Expand Down Expand Up @@ -441,11 +442,11 @@ export function FinishDialog(props: FinishDialogProps) {
{retroBox()}
</FollowUps>
{/* Without a PR the section only offers one, where the PR shows once it exists;
without an origin, or with the work on the base already, it needs no word on PRs. */}
without an origin, or with nothing left to merge, it needs no word on PRs. */}
<Show
when={taskPrUrl(props.task)}
fallback={
<Show when={remote() && !merge.alreadyLanded()}>
<Show when={remote() && !nothingLeft()}>
<CreatePrEntry
disabled={push.pushing() || merge.merging()}
onCreate={() => setCreatePrOpen(true)}
Expand All @@ -470,7 +471,7 @@ export function FinishDialog(props: FinishDialogProps) {
<Show
when={
pr.mode() === 'local' &&
!merge.alreadyLanded() &&
!nothingLeft() &&
!(merge.worktreeStatus() && !merge.hasCommittedChangesToMerge())
}
>
Expand Down
35 changes: 32 additions & 3 deletions src/components/MergeSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ export function MergeBlockers(props: MergeSectionProps & { showBaseSync: boolean
nothing is left to merge.
</div>
</Show>
<Show when={props.state.nothingToMerge()}>
<div
role="status"
style={{
// Nothing needs attention: with no work on the branch, closing the task is all that is left.
padding: '8px 12px',
'border-radius': 'var(--radius-md)',
border: `1px solid ${theme.border}`,
'margin-bottom': '12px',
'font-size': '13px',
}}
>
Nothing on this branch: it has no commits and no uncommitted changes, so nothing is left
to merge.
</div>
</Show>
<Show when={props.state.worktreeStatus()?.has_uncommitted_changes}>
<div
style={{
Expand All @@ -129,8 +145,14 @@ export function MergeBlockers(props: MergeSectionProps & { showBaseSync: boolean
Uncommitted changes in the worktree are not part of this merge.
</div>
</Show>
{/* No merge runs once the branch has landed, so the base checkout's changes do not matter. */}
<Show when={!props.state.alreadyLanded() && props.state.targetUncommitted()}>
{/* No merge runs once the branch has landed or holds nothing, so the base checkout's changes do not matter. */}
<Show
when={
!props.state.alreadyLanded() &&
!props.state.nothingToMerge() &&
props.state.targetUncommitted()
}
>
{(target) => (
<div
data-testid="target-uncommitted"
Expand All @@ -156,7 +178,14 @@ export function MergeBlockers(props: MergeSectionProps & { showBaseSync: boolean
</div>
)}
</Show>
<Show when={!props.state.worktreeStatus.loading && !props.state.hasCommittedChangesToMerge()}>
{/* A branch with no work at all has the quiet notice above: closing is the way out, nothing needs attention. */}
<Show
when={
!props.state.worktreeStatus.loading &&
!props.state.hasCommittedChangesToMerge() &&
!props.state.nothingToMerge()
}
>
<div
style={{
...bannerStyle(theme.warning),
Expand Down
19 changes: 15 additions & 4 deletions src/components/merge-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ export function createMergeState(props: MergeStateProps) {
* The check ran on the worktree's HEAD, which says nothing about the task's
* own branch while another one is checked out. */
const alreadyLanded = () => mergeStatus()?.already_landed === true && !hasBranchMismatch();
/** The task's branch holds no work at all, committed or not, such as when
* that work went out on another branch. A merge commit counts as work: it
* can carry changes of its own. */
const nothingToMerge = () => {
const status = worktreeStatus();
return (
!!status &&
!status.has_committed_changes &&
!status.has_uncommitted_changes &&
!hasBranchMismatch()
);
};
/** Uncommitted changes in the base checkout; the merge sets them aside. */
const targetUncommitted = () => mergeStatus()?.target_uncommitted;
/** The task's uncommitted files the base changed too: they block bringing it in. */
Expand Down Expand Up @@ -151,10 +163,8 @@ export function createMergeState(props: MergeStateProps) {
const mergeBlocker = (): string | undefined => {
if (!worktreeStatus()) return 'Checking the branch…';
if (hasBranchMismatch()) return "The worktree is not on this task's branch.";
if (!hasCommittedChangesToMerge())
return worktreeStatus()?.has_uncommitted_changes
? 'Commit the changes to merge them.'
: 'No commits on this branch yet.';
// A branch with no work at all is nothingToMerge, which offers Close task instead.
if (!hasCommittedChangesToMerge()) return 'Commit the changes to merge them.';
if (hasConflicts()) return `Resolve the conflicts with ${baseBranchName()} first.`;
if (targetUncommitted()?.overlap.length)
return `Commit or stash the changes in ${baseBranchName()} that this merge touches.`;
Expand Down Expand Up @@ -271,6 +281,7 @@ export function createMergeState(props: MergeStateProps) {
commitCount,
hasConflicts,
alreadyLanded,
nothingToMerge,
targetUncommitted,
uncommittedOverlap,
hasCommittedChangesToMerge,
Expand Down