From 3f59481e338c2d3167b9654a4289a9d1201f7944 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Fri, 25 Nov 2011 20:30:02 -0600 Subject: branch: allow a no-op "branch -M HEAD" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reported-by: Josh Chia (谢任中) Signed-off-by: Jonathan Nieder Signed-off-by: Conrad Irwin Signed-off-by: Junio C Hamano --- builtin/branch.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'builtin/branch.c') diff --git a/builtin/branch.c b/builtin/branch.c index 51ca6a02d..24f33b241 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -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); -- cgit v1.2.1 From 39bd6f726109942c6f77f59638f6763c9f00706c Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Sat, 26 Nov 2011 02:54:55 -0600 Subject: Allow checkout -B to update the current branch When on master, "git checkout -B master " is a more natural way to say "git reset --keep ", which was originally invented for the exact purpose of moving to the named commit while keeping the local changes around. Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- builtin/branch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'builtin/branch.c') diff --git a/builtin/branch.c b/builtin/branch.c index 24f33b241..823789fc9 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -737,7 +737,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) if (kinds != REF_LOCAL_BRANCH) die(_("-a and -r options to 'git branch' do not make sense with a branch name")); create_branch(head, argv[0], (argc == 2) ? argv[1] : head, - force_create, reflog, track); + force_create, reflog, 0, track); } else usage_with_options(builtin_branch_usage, options); -- cgit v1.2.1