Skip to content

Commit

Permalink
builtin-branch: use strbuf in fill_tracking_info()
Browse files Browse the repository at this point in the history
This is just about using the API, though in case of ~ 10^100 commits,
this would fix the problem of writing to unallocated memory as well. ;-)

Signed-off-by: Miklos Vajna <vmiklos@frugalware.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Miklos Vajna authored and Junio C Hamano committed Nov 18, 2008
1 parent 3c59c50 commit d3f9f9a
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions builtin-branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,19 +279,19 @@ static int ref_cmp(const void *r1, const void *r2)
return strcmp(c1->name, c2->name);
}

static void fill_tracking_info(char *stat, const char *branch_name)
static void fill_tracking_info(struct strbuf *stat, const char *branch_name)
{
int ours, theirs;
struct branch *branch = branch_get(branch_name);

if (!stat_tracking_info(branch, &ours, &theirs) || (!ours && !theirs))
return;
if (!ours)
sprintf(stat, "[behind %d] ", theirs);
strbuf_addf(stat, "[behind %d] ", theirs);
else if (!theirs)
sprintf(stat, "[ahead %d] ", ours);
strbuf_addf(stat, "[ahead %d] ", ours);
else
sprintf(stat, "[ahead %d, behind %d] ", ours, theirs);
strbuf_addf(stat, "[ahead %d, behind %d] ", ours, theirs);
}

static int matches_merge_filter(struct commit *commit)
Expand Down Expand Up @@ -334,11 +334,8 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
}

if (verbose) {
struct strbuf subject = STRBUF_INIT;
struct strbuf subject = STRBUF_INIT, stat = STRBUF_INIT;
const char *sub = " **** invalid ref ****";
char stat[128];

stat[0] = '\0';

commit = item->commit;
if (commit && !parse_commit(commit)) {
Expand All @@ -348,13 +345,14 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
}

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),
maxwidth, item->name,
branch_get_color(COLOR_BRANCH_RESET),
find_unique_abbrev(item->commit->object.sha1, abbrev),
stat, sub);
stat.buf, sub);
strbuf_release(&stat);
strbuf_release(&subject);
} else {
printf("%c %s%s%s\n", c, branch_get_color(color), item->name,
Expand Down

0 comments on commit d3f9f9a

Please sign in to comment.