All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Cc: git@vger.kernel.org, Matthieu Moy <Matthieu.Moy@grenoble-inp.fr>,
	Jonathan Niedier <jrnieder@gmail.com>
Subject: Re: [PATCH v3] branch: show rebase/bisect info when possible instead of "(no branch)"
Date: Fri, 08 Feb 2013 10:35:49 -0800	[thread overview]
Message-ID: <7v1ucq3b7e.fsf@alter.siamese.dyndns.org> (raw)
In-Reply-To: <1360318171-17614-1-git-send-email-pclouds@gmail.com> (=?utf-8?B?Ik5ndXnhu4VuCVRow6FpIE5n4buNYw==?= Duy"'s message of "Fri, 8 Feb 2013 17:09:31 +0700")

Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:

> This prints more helpful info when HEAD is detached: is it detached
> because of bisect or rebase? What is the original branch name in those
> cases?
>
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
>  Keep "no branch" in all cases. Just append "rebasing/bisecting [%s]"
>  when applicable.
>
>  builtin/branch.c            | 44 +++++++++++++++++++++++++++++++++++++++++++-
>  t/t6030-bisect-porcelain.sh |  2 +-
>  2 files changed, 44 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/branch.c b/builtin/branch.c
> index 6371bf9..26c0c3d 100644
> --- a/builtin/branch.c
> +++ b/builtin/branch.c
> @@ -550,6 +550,48 @@ static int calc_maxwidth(struct ref_list *refs)
>  	return w;
>  }
>  
> +static char *get_head_description()
> +{
> +	struct stat st;
> +	struct strbuf sb = STRBUF_INIT;
> +	struct strbuf result = STRBUF_INIT;
> +	int bisect = 0;
> +	int ret;
> +	if (!stat(git_path("rebase-merge"), &st) && S_ISDIR(st.st_mode))
> +		ret = strbuf_read_file(&sb, git_path("rebase-merge/head-name"), 0);
> +	else if (!access(git_path("rebase-apply/rebasing"), F_OK))
> +		ret = strbuf_read_file(&sb, git_path("rebase-apply/head-name"), 0);
> +	else if (!access(git_path("BISECT_LOG"), F_OK)) {
> +		ret = strbuf_read_file(&sb, git_path("BISECT_START"), 0);
> +		bisect = 1;
> +	} else
> +		return xstrdup(_("(no branch)"));
> +
> +	if (ret <= 0) {

Doesn't the general "negative signals an error" apply here?

The end result may be the same, as the later part of this function
that uses sb with len==0 ends up showing the same "bisecting" (or
"rebasing") without any other information, but the logic to reach
that outcome looks wrong.

> +		if (bisect)
> +			return xstrdup(_("(no branch, bisecting)"));
> +		else
> +			return xstrdup(_("_(no branch, rebasing)"));
> +	}
> +
> +	while (sb.len && sb.buf[sb.len - 1] == '\n')
> +		strbuf_setlen(&sb, sb.len - 1);
> +
> +	if (bisect) {
> +		unsigned char sha1[20];
> +		if (!get_sha1_hex(sb.buf, sha1))
> +			strbuf_addstr(&result, _("(no branch, bisecting)"));
> +		else
> +			strbuf_addf(&result, _("(no branch, bisecting %s)"), sb.buf);
> +	} else {
> +		if (!prefixcmp(sb.buf, "refs/heads/"))
> +			strbuf_addf(&result, _("(no branch, rebasing %s)"), sb.buf + 11);
> +		else
> +			strbuf_addstr(&result, _("(no branch, rebasing)"));
> +	}
> +	strbuf_release(&sb);
> +	return strbuf_detach(&result, NULL);
> +}

We may want to refactor wt_status_print_state() and its callee a bit
so that it and this part can share the logic without duplication and
risk implementing subtly different decision.  wt_status used to have
clean separation between collection phase and presentation phase,
but the wall between the phases seems deteriorated over time as more
"in progress" crufts have been piled on top.

Such a refactoring may look larger than necessary, but on the other
hand, I do not see this feature very useful if it can over time
drift away from what we will see in "git status", so...

>  
>  static void show_detached(struct ref_list *ref_list)
>  {
> @@ -557,7 +599,7 @@ static void show_detached(struct ref_list *ref_list)
>  
>  	if (head_commit && is_descendant_of(head_commit, ref_list->with_commit)) {
>  		struct ref_item item;
> -		item.name = xstrdup(_("(no branch)"));
> +		item.name = get_head_description();
>  		item.width = utf8_strwidth(item.name);
>  		item.kind = REF_LOCAL_BRANCH;
>  		item.dest = NULL;
> diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh
> index 3e0e15f..9b6f0d0 100755
> --- a/t/t6030-bisect-porcelain.sh
> +++ b/t/t6030-bisect-porcelain.sh
> @@ -164,7 +164,7 @@ test_expect_success 'bisect start: existing ".git/BISECT_START" not modified if
>  	cp .git/BISECT_START saved &&
>  	test_must_fail git bisect start $HASH4 foo -- &&
>  	git branch > branch.output &&
> -	test_i18ngrep "* (no branch)" branch.output > /dev/null &&
> +	test_i18ngrep "* (no branch, bisecting other)" branch.output > /dev/null &&
>  	test_cmp saved .git/BISECT_START
>  '
>  test_expect_success 'bisect start: no ".git/BISECT_START" if mistaken rev' '

  reply	other threads:[~2013-02-08 18:36 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-29 12:12 [PATCH] branch: show (rebasing) or (bisecting) instead of (no branch) when possible Nguyễn Thái Ngọc Duy
2013-01-29 19:13 ` Jonathan Nieder
2013-02-03  5:48 ` [PATCH v2] branch: show rebase/bisect info when possible instead of "(no branch)" Nguyễn Thái Ngọc Duy
2013-02-03 21:23   ` Matthieu Moy
2013-02-03 21:58     ` Junio C Hamano
2013-02-03 22:00       ` Junio C Hamano
2013-02-04 13:13       ` Matthieu Moy
2013-02-04 16:18         ` Junio C Hamano
2013-02-04  7:14     ` Duy Nguyen
2013-02-04  7:17       ` Duy Nguyen
2013-02-08 10:09   ` [PATCH v3] " Nguyễn Thái Ngọc Duy
2013-02-08 18:35     ` Junio C Hamano [this message]
2013-02-14  9:46       ` Duy Nguyen
2013-02-11 19:13     ` 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=7v1ucq3b7e.fsf@alter.siamese.dyndns.org \
    --to=gitster@pobox.com \
    --cc=Matthieu.Moy@grenoble-inp.fr \
    --cc=git@vger.kernel.org \
    --cc=jrnieder@gmail.com \
    --cc=pclouds@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.