Merge branch 'js/branch-symref'

* js/branch-symref:
  add basic branch display tests
  branch: clean up repeated strlen
  Avoid segfault with 'git branch' when the HEAD is detached
  builtin-branch: improve output when displaying remote branches

Conflicts:
	builtin-branch.c
This commit is contained in:
Junio C Hamano
2009-03-05 15:41:35 -08:00
2 changed files with 160 additions and 32 deletions

View File

@ -188,7 +188,8 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
struct ref_item { struct ref_item {
char *name; char *name;
unsigned int kind; char *dest;
unsigned int kind, len;
struct commit *commit; struct commit *commit;
}; };
@ -200,22 +201,47 @@ struct ref_list {
int kinds; int kinds;
}; };
static char *resolve_symref(const char *src, const char *prefix)
{
unsigned char sha1[20];
int flag;
const char *dst, *cp;
dst = resolve_ref(src, sha1, 0, &flag);
if (!(dst && (flag & REF_ISSYMREF)))
return NULL;
if (prefix && (cp = skip_prefix(dst, prefix)))
dst = cp;
return xstrdup(dst);
}
static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data) static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
{ {
struct ref_list *ref_list = (struct ref_list*)(cb_data); struct ref_list *ref_list = (struct ref_list*)(cb_data);
struct ref_item *newitem; struct ref_item *newitem;
struct commit *commit; struct commit *commit;
int kind; int kind, i;
int len; const char *prefix, *orig_refname = refname;
static struct {
int kind;
const char *prefix;
int pfxlen;
} ref_kind[] = {
{ REF_LOCAL_BRANCH, "refs/heads/", 11 },
{ REF_REMOTE_BRANCH, "refs/remotes/", 13 },
};
/* Detect kind */ /* Detect kind */
if (!prefixcmp(refname, "refs/heads/")) { for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
kind = REF_LOCAL_BRANCH; prefix = ref_kind[i].prefix;
refname += 11; if (strncmp(refname, prefix, ref_kind[i].pfxlen))
} else if (!prefixcmp(refname, "refs/remotes/")) { continue;
kind = REF_REMOTE_BRANCH; kind = ref_kind[i].kind;
refname += 13; refname += ref_kind[i].pfxlen;
} else break;
}
if (ARRAY_SIZE(ref_kind) <= i)
return 0; return 0;
commit = lookup_commit_reference_gently(sha1, 1); commit = lookup_commit_reference_gently(sha1, 1);
@ -246,9 +272,14 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
newitem->name = xstrdup(refname); newitem->name = xstrdup(refname);
newitem->kind = kind; newitem->kind = kind;
newitem->commit = commit; newitem->commit = commit;
len = strlen(newitem->name); newitem->len = strlen(refname);
if (len > ref_list->maxwidth) newitem->dest = resolve_symref(orig_refname, prefix);
ref_list->maxwidth = len; /* adjust for "remotes/" */
if (newitem->kind == REF_REMOTE_BRANCH &&
ref_list->kinds != REF_REMOTE_BRANCH)
newitem->len += 8;
if (newitem->len > ref_list->maxwidth)
ref_list->maxwidth = newitem->len;
return 0; return 0;
} }
@ -257,8 +288,10 @@ static void free_ref_list(struct ref_list *ref_list)
{ {
int i; int i;
for (i = 0; i < ref_list->index; i++) for (i = 0; i < ref_list->index; i++) {
free(ref_list->list[i].name); free(ref_list->list[i].name);
free(ref_list->list[i].dest);
}
free(ref_list->list); free(ref_list->list);
} }
@ -299,11 +332,12 @@ static int matches_merge_filter(struct commit *commit)
} }
static void print_ref_item(struct ref_item *item, int maxwidth, int verbose, static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
int abbrev, int current) int abbrev, int current, char *prefix)
{ {
char c; char c;
int color; int color;
struct commit *commit = item->commit; struct commit *commit = item->commit;
struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
if (!matches_merge_filter(commit)) if (!matches_merge_filter(commit))
return; return;
@ -326,7 +360,18 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
color = BRANCH_COLOR_CURRENT; color = BRANCH_COLOR_CURRENT;
} }
if (verbose) { strbuf_addf(&name, "%s%s", prefix, item->name);
if (verbose)
strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color),
maxwidth, 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));
if (item->dest)
strbuf_addf(&out, " -> %s", item->dest);
else if (verbose) {
struct strbuf subject = STRBUF_INIT, stat = STRBUF_INIT; struct strbuf subject = STRBUF_INIT, stat = STRBUF_INIT;
const char *sub = " **** invalid ref ****"; const char *sub = " **** invalid ref ****";
@ -340,28 +385,25 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
if (item->kind == REF_LOCAL_BRANCH) if (item->kind == REF_LOCAL_BRANCH)
fill_tracking_info(&stat, item->name); fill_tracking_info(&stat, item->name);
printf("%c %s%-*s%s %s %s%s\n", c, branch_get_color(color), strbuf_addf(&out, " %s %s%s",
maxwidth, item->name, find_unique_abbrev(item->commit->object.sha1, abbrev),
branch_get_color(BRANCH_COLOR_RESET), stat.buf, sub);
find_unique_abbrev(item->commit->object.sha1, abbrev),
stat.buf, sub);
strbuf_release(&stat); strbuf_release(&stat);
strbuf_release(&subject); strbuf_release(&subject);
} else {
printf("%c %s%s%s\n", c, branch_get_color(color), item->name,
branch_get_color(BRANCH_COLOR_RESET));
} }
printf("%s\n", out.buf);
strbuf_release(&name);
strbuf_release(&out);
} }
static int calc_maxwidth(struct ref_list *refs) static int calc_maxwidth(struct ref_list *refs)
{ {
int i, l, w = 0; int i, w = 0;
for (i = 0; i < refs->index; i++) { for (i = 0; i < refs->index; i++) {
if (!matches_merge_filter(refs->list[i].commit)) if (!matches_merge_filter(refs->list[i].commit))
continue; continue;
l = strlen(refs->list[i].name); if (refs->list[i].len > w)
if (l > w) w = refs->list[i].len;
w = l;
} }
return w; return w;
} }
@ -397,11 +439,13 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
is_descendant_of(head_commit, with_commit)) { is_descendant_of(head_commit, with_commit)) {
struct ref_item item; struct ref_item item;
item.name = xstrdup("(no branch)"); item.name = xstrdup("(no branch)");
item.len = strlen(item.name);
item.kind = REF_LOCAL_BRANCH; item.kind = REF_LOCAL_BRANCH;
item.dest = NULL;
item.commit = head_commit; item.commit = head_commit;
if (strlen(item.name) > ref_list.maxwidth) if (item.len > ref_list.maxwidth)
ref_list.maxwidth = strlen(item.name); ref_list.maxwidth = item.len;
print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1); print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1, "");
free(item.name); free(item.name);
} }
@ -409,8 +453,11 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
int current = !detached && int current = !detached &&
(ref_list.list[i].kind == REF_LOCAL_BRANCH) && (ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
!strcmp(ref_list.list[i].name, head); !strcmp(ref_list.list[i].name, 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, print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
abbrev, current); abbrev, current, prefix);
} }
free_ref_list(&ref_list); free_ref_list(&ref_list);

81
t/t3203-branch-output.sh Executable file
View File

@ -0,0 +1,81 @@
#!/bin/sh
test_description='git branch display tests'
. ./test-lib.sh
test_expect_success 'make commits' '
echo content >file &&
git add file &&
git commit -m one &&
echo content >>file &&
git commit -a -m two
'
test_expect_success 'make branches' '
git branch branch-one
git branch branch-two HEAD^
'
test_expect_success 'make remote branches' '
git update-ref refs/remotes/origin/branch-one branch-one
git update-ref refs/remotes/origin/branch-two branch-two
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/branch-one
'
cat >expect <<'EOF'
branch-one
branch-two
* master
EOF
test_expect_success 'git branch shows local branches' '
git branch >actual &&
test_cmp expect actual
'
cat >expect <<'EOF'
origin/HEAD -> origin/branch-one
origin/branch-one
origin/branch-two
EOF
test_expect_success 'git branch -r shows remote branches' '
git branch -r >actual &&
test_cmp expect actual
'
cat >expect <<'EOF'
branch-one
branch-two
* master
remotes/origin/HEAD -> origin/branch-one
remotes/origin/branch-one
remotes/origin/branch-two
EOF
test_expect_success 'git branch -a shows local and remote branches' '
git branch -a >actual &&
test_cmp expect actual
'
cat >expect <<'EOF'
two
one
two
EOF
test_expect_success 'git branch -v shows branch summaries' '
git branch -v >tmp &&
awk "{print \$NF}" <tmp >actual &&
test_cmp expect actual
'
cat >expect <<'EOF'
* (no branch)
branch-one
branch-two
master
EOF
test_expect_success 'git branch shows detached HEAD properly' '
git checkout HEAD^0 &&
git branch >actual &&
test_cmp expect actual
'
test_done