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 <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Stefan Beller committed
Nov 15, 2017 at 18:00 UTC
4dbc59a4cce418ff8428a9d2ecd67c34ca50db56
1 file changed
+37
-26
builtin/describe.c
+37
-26
@@ -256,7 +256,7 @@ static unsigned long finish_depth_computation(
256
return seen_commits;
257
}
258
259
-static void display_name(struct commit_name *n)
259
+static void append_name(struct commit_name *n, struct strbuf *dst)
260
{
261
if (n->prio == 2 && !n->tag) {
262
n->tag = lookup_tag(&n->oid);
@@ -272,19 +272,18 @@ static void display_name(struct commit_name *n)
272
}
273
274
if (n->tag)
275
- printf("%s", n->tag->tag);
275
+ strbuf_addstr(dst, n->tag->tag);
276
else
277
- printf("%s", n->path);
277
+ strbuf_addstr(dst, n->path);
278
}
279
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)
281
{
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));
283
}
284
285
-static void describe(const char *arg, int last_one)
285
+static void describe_commit(struct object_id *oid, struct strbuf *dst)
286
{
287
- struct object_id oid;
287
struct commit *cmit, *gave_up_on = NULL;
288
struct commit_list *list;
289
struct commit_name *n;
@@ -293,26 +292,18 @@ static void describe(const char *arg, int last_one)
292
unsigned long seen_commits = 0;
293
unsigned int unannotated_cnt = 0;
294
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);
296
297
n = find_commit_name(&cmit->object.oid);
298
if (n && (tags || all || n->prio == 2)) {
299
/*
300
* Exact match to an existing ref.
301
*/
310
- display_name(n);
302
+ append_name(n, dst);
303
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);
305
if (suffix)
314
- printf("%s", suffix);
315
- printf("\n");
306
+ strbuf_addstr(dst, suffix);
307
return;
308
}
309
@@ -386,10 +377,9 @@ static void describe(const char *arg, int last_one)
377
if (!match_cnt) {
378
struct object_id *cmit_oid = &cmit->object.oid;
379
if (always) {
389
- printf("%s", find_unique_abbrev(cmit_oid->hash, abbrev));
380
+ strbuf_addstr(dst, find_unique_abbrev(cmit_oid->hash, abbrev));
381
if (suffix)
391
- printf("%s", suffix);
392
- printf("\n");
382
+ strbuf_addstr(dst, suffix);
383
return;
384
}
385
if (unannotated_cnt)
@@ -437,15 +427,36 @@ static void describe(const char *arg, int last_one)
427
}
428
}
429
440
- display_name(all_matches[0].name);
430
+ append_name(all_matches[0].name, dst);
431
if (abbrev)
442
- show_suffix(all_matches[0].depth, &cmit->object.oid);
432
+ append_suffix(all_matches[0].depth, &cmit->object.oid, dst);
433
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);
455
456
if (!last_one)
457
clear_commit_marks(cmit, -1);
458
+
459
+ strbuf_release(&sb);
460
}
461
462
int cmd_describe(int argc, const char **argv, const char *prefix)