diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -1319,12 +1319,19 @@ def commit(ui, repo, *pats, **opts): raise util.Abort(_('cannot commit an interrupted graft operation'), hint=_('use "hg graft -c" to continue graft')) + branch = repo[None].branch() + bheads = repo.branchheads(branch) + extra = {} if opts.get('close_branch'): extra['close'] = 1 - branch = repo[None].branch() - bheads = repo.branchheads(branch) + if not bheads: + raise util.Abort(_('can only close branch heads')) + elif opts.get('amend'): + if repo.parents()[0].p1().branch() != branch and \ + repo.parents()[0].p2().branch() != branch: + raise util.Abort(_('can only close branch heads')) if opts.get('amend'): if ui.configbool('ui', 'commitsubrepos'): diff --git a/tests/test-branches.t b/tests/test-branches.t --- a/tests/test-branches.t +++ b/tests/test-branches.t @@ -267,14 +267,16 @@ verify update will accept invalid legacy $ hg up -C b 1 files updated, 0 files merged, 0 files removed, 0 files unresolved $ hg commit -d '9 0' --close-branch -m 'close this part branch too' + $ hg commit -d '9 0' --close-branch -m 're-closing this branch' + abort: can only close branch heads + [255] - $ hg commit -d '9 0' --close-branch -m 're-closing this branch' $ hg log -r tip --debug - changeset: 13:c2601d54b1427e99506bee25a566ef3a5963af0b + changeset: 12:e3d49c0575d8fc2cb1cd6859c747c14f5f6d499f branch: b tag: tip phase: draft - parent: 12:e3d49c0575d8fc2cb1cd6859c747c14f5f6d499f + parent: 8:eebb944467c9fb9651ed232aeaf31b3c0a7fc6c1 parent: -1:0000000000000000000000000000000000000000 manifest: 8:6f9ed32d2b310e391a4f107d5f0f071df785bfee user: test @@ -282,13 +284,9 @@ verify update will accept invalid legacy extra: branch=b extra: close=1 description: - re-closing this branch + close this part branch too - $ hg rollback - repository tip rolled back to revision 12 (undo commit) - working directory now based on revision 12 - --- b branch should be inactive $ hg branches diff --git a/tests/test-commit-amend.t b/tests/test-commit-amend.t --- a/tests/test-commit-amend.t +++ b/tests/test-commit-amend.t @@ -743,3 +743,27 @@ Amend a merge changeset (with manifest-l -aa -aa +Issue 3445: amending with --close-branch a commit that created a new head should fail +This shouldn't be possible: + + $ hg up -q default + $ hg branch closewithamend + marked working directory as branch closewithamend + (branches are permanent and global, did you want a bookmark?) + $ hg ci -Am.. + adding cc.orig + adding obs.py + adding obs.pyc + $ hg ci --amend --close-branch -m 'closing' + abort: can only close branch heads + [255] + +This silliness fails: + + $ hg branch silliness + marked working directory as branch silliness + (branches are permanent and global, did you want a bookmark?) + $ echo b >> b + $ hg ci --close-branch -m'open and close' + abort: can only close branch heads + [255]