git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Rafael Ascensão" <rafa.almas@gmail.com>
To: daniels@umanovskis.se
Cc: git@vger.kernel.org, "Rafael Ascensão" <rafa.almas@gmail.com>
Subject: [PATCH] branch: make --show-current use already resolved HEAD
Date: Wed,  7 Nov 2018 22:56:18 +0000	[thread overview]
Message-ID: <20181107225619.6683-1-rafa.almas@gmail.com> (raw)
In-Reply-To: <20181025190421.15022-1-daniels@umanovskis.se>

print_current_branch_name() tries to resolve HEAD and die() when it
doesn't resolve it successfully. But the conditions being tested are
always unreachable because early in branch:cmd_branch() the same logic
is performed.

Eliminate the duplicate and unreachable code, and update the current
logic to the more reliable check for the detached head.

Signed-off-by: Rafael Ascensão <rafa.almas@gmail.com>
---

This patch is meant to be either applied or squashed on top of the
current series.

I am basing the claims of it being more reliable of what Junio suggested
on a previous iteration of this series:
https://public-inbox.org/git/xmqq4ldtgehs.fsf@gitster-ct.c.googlers.com/

But the main goal of this patch is to just bring some attention to this,
as I mentioned it in a previous thread but it got lost. After asking on
#git-devel, the suggestion was to send it as an incremental patch. So
here it is. :)

I still think the mention about scripting should be removed from the
original commit message, leaving it open to being taught other tricks
like --verbose that aren't necessarily script-friendly.

Cheers

 builtin/branch.c | 23 +++++------------------
 1 file changed, 5 insertions(+), 18 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 46f91dc06d..1c51d0a8ca 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -38,6 +38,7 @@ static const char * const builtin_branch_usage[] = {
 
 static const char *head;
 static struct object_id head_oid;
+static int head_flags = 0;
 
 static int branch_use_color = -1;
 static char branch_colors[][COLOR_MAXLEN] = {
@@ -443,21 +444,6 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
 	free(to_free);
 }
 
-static void print_current_branch_name(void)
-{
-	int flags;
-	const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
-	const char *shortname;
-	if (!refname)
-		die(_("could not resolve HEAD"));
-	else if (!(flags & REF_ISSYMREF))
-		return;
-	else if (skip_prefix(refname, "refs/heads/", &shortname))
-		puts(shortname);
-	else
-		die(_("HEAD (%s) points outside of refs/heads/"), refname);
-}
-
 static void reject_rebase_or_bisect_branch(const char *target)
 {
 	struct worktree **worktrees = get_worktrees(0);
@@ -668,10 +654,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 
 	track = git_branch_track;
 
-	head = resolve_refdup("HEAD", 0, &head_oid, NULL);
+	head = resolve_refdup("HEAD", 0, &head_oid, &head_flags);
 	if (!head)
 		die(_("Failed to resolve HEAD as a valid ref."));
-	if (!strcmp(head, "HEAD"))
+	if (!(head_flags & REF_ISSYMREF))
 		filter.detached = 1;
 	else if (!skip_prefix(head, "refs/heads/", &head))
 		die(_("HEAD not found below refs/heads!"));
@@ -716,7 +702,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 			die(_("branch name required"));
 		return delete_branches(argc, argv, delete > 1, filter.kind, quiet);
 	} else if (show_current) {
-		print_current_branch_name();
+		if (!filter.detached)
+			puts(head);
 		return 0;
 	} else if (list) {
 		/*  git branch --local also shows HEAD when it is detached */
-- 
2.19.1


  parent reply	other threads:[~2018-11-07 22:57 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-25 19:04 [PATCH v5] branch: introduce --show-current display option Daniels Umanovskis
2018-10-25 19:30 ` Eric Sunshine
2018-10-26  0:52   ` Junio C Hamano
2018-11-01 22:01     ` Jeff King
2018-10-26  1:53   ` Junio C Hamano
2018-11-07 22:56 ` Rafael Ascensão [this message]
2018-11-08  1:11   ` [PATCH] branch: make --show-current use already resolved HEAD Junio C Hamano
2018-11-08  4:36     ` Rafael Ascensão

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=20181107225619.6683-1-rafa.almas@gmail.com \
    --to=rafa.almas@gmail.com \
    --cc=daniels@umanovskis.se \
    --cc=git@vger.kernel.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).