branch: allow a no-op "branch -M <current-branch> HEAD"
Overwriting the current branch with a different commit is forbidden, as it will make the status recorded in the index and the working tree out of sync with respect to the HEAD. There however is no reason to forbid it if the current branch is renamed to itself, which admittedly is something only an insane user would do, but is handy for scripts. Test script is by Conrad Irwin. Reported-by: Soeren Sonnenburg <sonne@debian.org> Reported-by: Josh Chia (谢任中) Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Conrad Irwin <conrad.irwin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
017d1e1345
commit
3f59481e33
@ -568,6 +568,7 @@ static void rename_branch(const char *oldname, const char *newname, int force)
|
||||
unsigned char sha1[20];
|
||||
struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
|
||||
int recovery = 0;
|
||||
int clobber_head_ok;
|
||||
|
||||
if (!oldname)
|
||||
die(_("cannot rename the current branch while not on any."));
|
||||
@ -583,7 +584,13 @@ static void rename_branch(const char *oldname, const char *newname, int force)
|
||||
die(_("Invalid branch name: '%s'"), oldname);
|
||||
}
|
||||
|
||||
validate_new_branchname(newname, &newref, force, 0);
|
||||
/*
|
||||
* A command like "git branch -M currentbranch currentbranch" cannot
|
||||
* cause the worktree to become inconsistent with HEAD, so allow it.
|
||||
*/
|
||||
clobber_head_ok = !strcmp(oldname, newname);
|
||||
|
||||
validate_new_branchname(newname, &newref, force, clobber_head_ok);
|
||||
|
||||
strbuf_addf(&logmsg, "Branch: renamed %s to %s",
|
||||
oldref.buf, newref.buf);
|
||||
|
@ -115,6 +115,22 @@ test_expect_success 'git branch -M baz bam should succeed when baz is checked ou
|
||||
git branch -M baz bam
|
||||
'
|
||||
|
||||
test_expect_success 'git branch -M master should work when master is checked out' '
|
||||
git checkout master &&
|
||||
git branch -M master
|
||||
'
|
||||
|
||||
test_expect_success 'git branch -M master master should work when master is checked out' '
|
||||
git checkout master &&
|
||||
git branch -M master master
|
||||
'
|
||||
|
||||
test_expect_success 'git branch -M master2 master2 should work when master is checked out' '
|
||||
git checkout master &&
|
||||
git branch master2 &&
|
||||
git branch -M master2 master2
|
||||
'
|
||||
|
||||
test_expect_success 'git branch -v -d t should work' '
|
||||
git branch t &&
|
||||
test_path_is_file .git/refs/heads/t &&
|
||||
|
Loading…
Reference in New Issue
Block a user