From: Johan Herland <johan@herland.net>
To: git@vger.kernel.org
Cc: johan@herland.net, gitster@pobox.com, jrnieder@gmail.com
Subject: [PATCHv2 09/10] builtin/branch.c: Refactor "remotes/" prepending to remote-tracking branches
Date: Sat, 11 May 2013 18:21:19 +0200 [thread overview]
Message-ID: <1368289280-30337-10-git-send-email-johan@herland.net> (raw)
In-Reply-To: <1368289280-30337-1-git-send-email-johan@herland.net>
When running "git branch -a" (instead of "git branch -r"), we prefix the
remote-tracking branches with "remotes/" to allow the user to more easily
tell them apart from the local branches.
The code that prepended "remotes/" to remote-tracking branches was
located in print_ref_item(), while the code that adjusted the
ref_item.width (to account for the prepended "remotes/") was located in
append_ref().
This code moves the prepending of "remotes/" up into append_ref(), which
is a nice cleanup of the code, as well as a preparation for changing the
logic when remote-tracking branches start showing up in refs/peers/*.
Signed-off-by: Johan Herland <johan@herland.net>
---
builtin/branch.c | 23 +++++++++--------------
1 file changed, 9 insertions(+), 14 deletions(-)
diff --git a/builtin/branch.c b/builtin/branch.c
index c8b49e3..4480be2 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -384,7 +384,6 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
strbuf_addstr(&newitem->name, refname);
newitem->kind = kind;
newitem->commit = commit;
- newitem->width = utf8_strwidth(refname);
strbuf_init(&newitem->dest, 0);
orig_refname = resolve_symref(orig_refname, prefix);
if (orig_refname)
@@ -392,7 +391,8 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
/* adjust for "remotes/" */
if (newitem->kind == REF_REMOTE_BRANCH &&
ref_list->kinds != REF_REMOTE_BRANCH)
- newitem->width += 8;
+ strbuf_insert(&newitem->name, 0, "remotes/", 8);
+ newitem->width = utf8_strwidth(newitem->name.buf);
if (newitem->width > ref_list->maxwidth)
ref_list->maxwidth = newitem->width;
@@ -510,12 +510,12 @@ static void add_verbose_info(struct strbuf *out, struct ref_item *item,
}
static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
- int abbrev, int current, char *prefix)
+ int abbrev, int current)
{
char c;
int color;
struct commit *commit = item->commit;
- struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
+ struct strbuf out = STRBUF_INIT;
if (!matches_merge_filter(commit))
return;
@@ -538,15 +538,14 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
color = BRANCH_COLOR_CURRENT;
}
- strbuf_addf(&name, "%s%s", prefix, item->name.buf);
if (verbose) {
- int utf8_compensation = strlen(name.buf) - utf8_strwidth(name.buf);
+ int utf8_compensation = strlen(item->name.buf) - utf8_strwidth(item->name.buf);
strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color),
- maxwidth + utf8_compensation, name.buf,
+ maxwidth + utf8_compensation, item->name.buf,
branch_get_color(BRANCH_COLOR_RESET));
} else
strbuf_addf(&out, "%c %s%s%s", c, branch_get_color(color),
- name.buf, branch_get_color(BRANCH_COLOR_RESET));
+ item->name.buf, branch_get_color(BRANCH_COLOR_RESET));
if (item->dest.len)
strbuf_addf(&out, " -> %s", item->dest.buf);
@@ -559,7 +558,6 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
} else {
printf("%s\n", out.buf);
}
- strbuf_release(&name);
strbuf_release(&out);
}
@@ -613,7 +611,7 @@ static void show_detached(struct ref_list *ref_list)
item.commit = head_commit;
if (item.width > ref_list->maxwidth)
ref_list->maxwidth = item.width;
- print_ref_item(&item, ref_list->maxwidth, ref_list->verbose, ref_list->abbrev, 1, "");
+ print_ref_item(&item, ref_list->maxwidth, ref_list->verbose, ref_list->abbrev, 1);
strbuf_release(&item.name);
}
}
@@ -661,11 +659,8 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
int current = !detached &&
(ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
!strcmp(ref_list.list[i].name.buf, head);
- char *prefix = (kinds != REF_REMOTE_BRANCH &&
- ref_list.list[i].kind == REF_REMOTE_BRANCH)
- ? "remotes/" : "";
print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
- abbrev, current, prefix);
+ abbrev, current);
}
free_ref_list(&ref_list);
--
1.8.1.3.704.g33f7d4f
next prev parent reply other threads:[~2013-05-11 16:21 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-05-11 16:21 [PATCHv2 00/10] Prepare for alternative remote-tracking branch location Johan Herland
2013-05-11 16:21 ` [PATCHv2 01/10] t7900: Start testing usability of namespaced remote refs Johan Herland
2013-05-11 16:21 ` [PATCHv2 02/10] t7900: Demonstrate failure to expand "$peer/$branch" according to refspecs Johan Herland
2013-05-11 16:21 ` [PATCHv2 03/10] refs.c: Refactor code for mapping between shorthand names and full refnames Johan Herland
2013-05-13 4:56 ` Junio C Hamano
2013-05-13 6:31 ` Johan Herland
2013-05-13 6:45 ` Junio C Hamano
2013-05-13 20:34 ` Junio C Hamano
2013-05-14 14:24 ` Johan Herland
2013-05-14 17:50 ` Junio C Hamano
2013-05-15 6:45 ` Michael Haggerty
2013-05-15 7:39 ` Johan Herland
2013-05-15 13:53 ` Johan Herland
2013-05-15 15:14 ` Junio C Hamano
2013-05-15 19:49 ` Eric Wong
2013-05-11 16:21 ` [PATCHv2 04/10] remote: Reject remote names containing '/' Johan Herland
2013-05-13 4:48 ` Eric Sunshine
2013-05-13 6:32 ` Johan Herland
2013-05-13 4:54 ` Junio C Hamano
2013-05-13 6:53 ` Johan Herland
2013-05-16 9:48 ` Ramkumar Ramachandra
2013-05-16 11:17 ` Johan Herland
2013-05-16 12:14 ` Ramkumar Ramachandra
2013-05-11 16:21 ` [PATCHv2 05/10] refs.c: Add support for expanding/shortening refs in refs/peers/* Johan Herland
2013-05-11 16:21 ` [PATCHv2 06/10] t7900: Test git branch -r/-a output w/remote-tracking branches " Johan Herland
2013-05-11 16:21 ` [PATCHv2 07/10] t3203: Add testcase for fix in 1603ade81352a526ccb206f41ff81ecbc855df2d Johan Herland
2013-05-11 16:21 ` [PATCHv2 08/10] builtin/branch.c: Refactor ref_item.name and .dest into strbufs Johan Herland
2013-05-11 16:21 ` Johan Herland [this message]
2013-05-11 16:21 ` [PATCHv2 10/10] branch: Fix display of remote branches in refs/peers/* Johan Herland
2013-05-13 5:19 ` Eric Sunshine
2013-05-13 6:55 ` Johan Herland
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=1368289280-30337-10-git-send-email-johan@herland.net \
--to=johan@herland.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jrnieder@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).