Skip to content

Commit 4dbc59a

Browse files
stefanbellergitster
authored andcommitted
builtin/describe.c: factor out describe_commit
Factor out describing commits into its own function `describe_commit`, which will put any output to stdout into a strbuf, to be printed afterwards. As the next patch will teach Git to describe blobs using a commit and path, this refactor will make it easy to reuse the code describing commits. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cdaed0c commit 4dbc59a

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

builtin/describe.c

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ static unsigned long finish_depth_computation(
256256
return seen_commits;
257257
}
258258

259-
static void display_name(struct commit_name *n)
259+
static void append_name(struct commit_name *n, struct strbuf *dst)
260260
{
261261
if (n->prio == 2 && !n->tag) {
262262
n->tag = lookup_tag(&n->oid);
@@ -272,19 +272,18 @@ static void display_name(struct commit_name *n)
272272
}
273273

274274
if (n->tag)
275-
printf("%s", n->tag->tag);
275+
strbuf_addstr(dst, n->tag->tag);
276276
else
277-
printf("%s", n->path);
277+
strbuf_addstr(dst, n->path);
278278
}
279279

280-
static void show_suffix(int depth, const struct object_id *oid)
280+
static void append_suffix(int depth, const struct object_id *oid, struct strbuf *dst)
281281
{
282-
printf("-%d-g%s", depth, find_unique_abbrev(oid->hash, abbrev));
282+
strbuf_addf(dst, "-%d-g%s", depth, find_unique_abbrev(oid->hash, abbrev));
283283
}
284284

285-
static void describe(const char *arg, int last_one)
285+
static void describe_commit(struct object_id *oid, struct strbuf *dst)
286286
{
287-
struct object_id oid;
288287
struct commit *cmit, *gave_up_on = NULL;
289288
struct commit_list *list;
290289
struct commit_name *n;
@@ -293,26 +292,18 @@ static void describe(const char *arg, int last_one)
293292
unsigned long seen_commits = 0;
294293
unsigned int unannotated_cnt = 0;
295294

296-
if (debug)
297-
fprintf(stderr, _("describe %s\n"), arg);
298-
299-
if (get_oid(arg, &oid))
300-
die(_("Not a valid object name %s"), arg);
301-
cmit = lookup_commit_reference(&oid);
302-
if (!cmit)
303-
die(_("%s is not a valid '%s' object"), arg, commit_type);
295+
cmit = lookup_commit_reference(oid);
304296

305297
n = find_commit_name(&cmit->object.oid);
306298
if (n && (tags || all || n->prio == 2)) {
307299
/*
308300
* Exact match to an existing ref.
309301
*/
310-
display_name(n);
302+
append_name(n, dst);
311303
if (longformat)
312-
show_suffix(0, n->tag ? &n->tag->tagged->oid : &oid);
304+
append_suffix(0, n->tag ? &n->tag->tagged->oid : oid, dst);
313305
if (suffix)
314-
printf("%s", suffix);
315-
printf("\n");
306+
strbuf_addstr(dst, suffix);
316307
return;
317308
}
318309

@@ -386,10 +377,9 @@ static void describe(const char *arg, int last_one)
386377
if (!match_cnt) {
387378
struct object_id *cmit_oid = &cmit->object.oid;
388379
if (always) {
389-
printf("%s", find_unique_abbrev(cmit_oid->hash, abbrev));
380+
strbuf_addstr(dst, find_unique_abbrev(cmit_oid->hash, abbrev));
390381
if (suffix)
391-
printf("%s", suffix);
392-
printf("\n");
382+
strbuf_addstr(dst, suffix);
393383
return;
394384
}
395385
if (unannotated_cnt)
@@ -437,15 +427,36 @@ static void describe(const char *arg, int last_one)
437427
}
438428
}
439429

440-
display_name(all_matches[0].name);
430+
append_name(all_matches[0].name, dst);
441431
if (abbrev)
442-
show_suffix(all_matches[0].depth, &cmit->object.oid);
432+
append_suffix(all_matches[0].depth, &cmit->object.oid, dst);
443433
if (suffix)
444-
printf("%s", suffix);
445-
printf("\n");
434+
strbuf_addstr(dst, suffix);
435+
}
436+
437+
static void describe(const char *arg, int last_one)
438+
{
439+
struct object_id oid;
440+
struct commit *cmit;
441+
struct strbuf sb = STRBUF_INIT;
442+
443+
if (debug)
444+
fprintf(stderr, _("describe %s\n"), arg);
445+
446+
if (get_oid(arg, &oid))
447+
die(_("Not a valid object name %s"), arg);
448+
cmit = lookup_commit_reference(&oid);
449+
if (!cmit)
450+
die(_("%s is not a valid '%s' object"), arg, commit_type);
451+
452+
describe_commit(&oid, &sb);
453+
454+
puts(sb.buf);
446455

447456
if (!last_one)
448457
clear_commit_marks(cmit, -1);
458+
459+
strbuf_release(&sb);
449460
}
450461

451462
int cmd_describe(int argc, const char **argv, const char *prefix)

0 commit comments

Comments
 (0)