git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Deskin Miller <deskinm@gmail.com>
Cc: Johannes Schindelin <Johannes.Schindelin@gmx.de>,
	git@vger.kernel.org, Jay Soffian <jaysoffian@gmail.com>
Subject: [PATCH] git-branch: a detached HEAD and branch called HEAD are different
Date: Thu, 26 Feb 2009 22:10:13 -0800	[thread overview]
Message-ID: <7vvdqwo1fe.fsf_-_@gitster.siamese.dyndns.org> (raw)
In-Reply-To: <7veixkpi4a.fsf@gitster.siamese.dyndns.org> (Junio C. Hamano's message of "Thu, 26 Feb 2009 21:24:21 -0800")

While on a detached HEAD, builtin-branch.c passed string "HEAD" around in
the varilable "head" that held the current branch name.  This did not
allow you to distinguish between a local branch whose name is "HEAD" and a
detached HEAD.

Allow the variable to contain NULL when the HEAD is detached.  The change
is primarily to protect !strcmp(head, name) to see if we are try to
manipulate the current branch with "head &&"; we won't be futzing with the
current branch when the HEAD is detached by definition.

Creating a local branch "HEAD" is not useful at all, but if you managed to
create one by mistake, "git branch -d HEAD" couldn't delete it while your
HEAD is detached, due to this bug.

One of the functions, rename_branch(), even expected NULL to signal this
situation, to prevent "git branch -m othername" while on detached HEAD,
and this change incidentally fixes this unrelated bug.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---

 * I wasn't personally very much interested in these "C-rewrites" of
   Porcelains, and I have never looked at the implementation very
   carefully, but some parts of them are disgustingly crappy, and I am
   finding these inconsistencies after looking at them only for 5
   minutes.  Oh well...

   Anyway, this is a preparatory clean-up.

 branch.c         |    2 +-
 builtin-branch.c |    8 +++++---
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/branch.c b/branch.c
index 1f00e44..e287eab 100644
--- a/branch.c
+++ b/branch.c
@@ -121,7 +121,7 @@ void create_branch(const char *head,
 	if (resolve_ref(ref.buf, sha1, 1, NULL)) {
 		if (!force)
 			die("A branch named '%s' already exists.", name);
-		else if (!is_bare_repository() && !strcmp(head, name))
+		else if (!is_bare_repository() && head && !strcmp(head, name))
 			die("Cannot force update the current branch.");
 		forcing = 1;
 	}
diff --git a/builtin-branch.c b/builtin-branch.c
index 504a981..c34af27 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -126,7 +126,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
 		if (interpret_nth_last_branch(argv[i], &bname) != len)
 			strbuf_add(&bname, argv[i], len);
 
-		if (kinds == REF_LOCAL_BRANCH && !strcmp(head, bname.buf)) {
+		if (kinds == REF_LOCAL_BRANCH && head && !strcmp(head, bname.buf)) {
 			error("Cannot delete the branch '%s' "
 			      "which you are currently on.", bname.buf);
 			ret = 1;
@@ -408,7 +408,7 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
 	for (i = 0; i < ref_list.index; i++) {
 		int current = !detached &&
 			(ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
-			!strcmp(ref_list.list[i].name, head);
+			head && !strcmp(ref_list.list[i].name, head);
 		print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
 			       abbrev, current);
 	}
@@ -541,6 +541,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	head = xstrdup(head);
 	if (!strcmp(head, "HEAD")) {
 		detached = 1;
+		head = NULL;
 	} else {
 		if (prefixcmp(head, "refs/heads/"))
 			die("HEAD not found below refs/heads!");
@@ -561,7 +562,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	else if (rename && (argc == 2))
 		rename_branch(argv[0], argv[1], rename > 1);
 	else if (argc <= 2)
-		create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
+		create_branch(head, argv[0],
+			      (argc == 2) ? argv[1] : head ? head : "HEAD",
 			      force_create, reflog, track);
 	else
 		usage_with_options(builtin_branch_usage, options);

  reply	other threads:[~2009-02-27  6:12 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-14  7:08 [PATCH] teach the new "@{-1} syntax to "git branch" Junio C Hamano
2009-02-14  7:11 ` Jay Soffian
2009-02-14  7:21   ` Junio C Hamano
2009-02-14  9:37     ` [PATCH v2 1/2] Teach the " Junio C Hamano
2009-02-14  9:39     ` [PATCH v2 2/2] Teach @{-1} to git merge Junio C Hamano
2009-02-14 22:52       ` Johannes Schindelin
2009-02-15  2:05         ` Junio C Hamano
2009-02-15  2:32           ` Junio C Hamano
2009-02-15  9:24           ` Nanako Shiraishi
2009-02-15 10:02             ` Junio C Hamano
2009-02-15 11:21           ` Johannes Schindelin
2009-02-16  4:16             ` Junio C Hamano
2009-02-16 10:41               ` Johannes Schindelin
2009-02-16 16:40                 ` Jay Soffian
2009-02-26  0:11                 ` Junio C Hamano
2009-02-26 10:18                   ` Johannes Schindelin
2009-02-27  4:27         ` Deskin Miller
2009-02-27  5:24           ` Junio C Hamano
2009-02-27  6:10             ` Junio C Hamano [this message]
2009-02-27  6:12             ` [PATCH] "git branch -M @{-1}" Junio C Hamano
2009-02-27  6:49               ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7vvdqwo1fe.fsf_-_@gitster.siamese.dyndns.org \
    --to=gitster@pobox.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=deskinm@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=jaysoffian@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).