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>
This commit is contained in:
parent
cdaed0cf02
commit
4dbc59a4cc
@ -256,7 +256,7 @@ static unsigned long finish_depth_computation(
|
|||||||
return seen_commits;
|
return seen_commits;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void display_name(struct commit_name *n)
|
static void append_name(struct commit_name *n, struct strbuf *dst)
|
||||||
{
|
{
|
||||||
if (n->prio == 2 && !n->tag) {
|
if (n->prio == 2 && !n->tag) {
|
||||||
n->tag = lookup_tag(&n->oid);
|
n->tag = lookup_tag(&n->oid);
|
||||||
@ -272,19 +272,18 @@ static void display_name(struct commit_name *n)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (n->tag)
|
if (n->tag)
|
||||||
printf("%s", n->tag->tag);
|
strbuf_addstr(dst, n->tag->tag);
|
||||||
else
|
else
|
||||||
printf("%s", n->path);
|
strbuf_addstr(dst, n->path);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void show_suffix(int depth, const struct object_id *oid)
|
static void append_suffix(int depth, const struct object_id *oid, struct strbuf *dst)
|
||||||
{
|
{
|
||||||
printf("-%d-g%s", depth, find_unique_abbrev(oid->hash, abbrev));
|
strbuf_addf(dst, "-%d-g%s", depth, find_unique_abbrev(oid->hash, abbrev));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void describe(const char *arg, int last_one)
|
static void describe_commit(struct object_id *oid, struct strbuf *dst)
|
||||||
{
|
{
|
||||||
struct object_id oid;
|
|
||||||
struct commit *cmit, *gave_up_on = NULL;
|
struct commit *cmit, *gave_up_on = NULL;
|
||||||
struct commit_list *list;
|
struct commit_list *list;
|
||||||
struct commit_name *n;
|
struct commit_name *n;
|
||||||
@ -293,26 +292,18 @@ static void describe(const char *arg, int last_one)
|
|||||||
unsigned long seen_commits = 0;
|
unsigned long seen_commits = 0;
|
||||||
unsigned int unannotated_cnt = 0;
|
unsigned int unannotated_cnt = 0;
|
||||||
|
|
||||||
if (debug)
|
cmit = lookup_commit_reference(oid);
|
||||||
fprintf(stderr, _("describe %s\n"), arg);
|
|
||||||
|
|
||||||
if (get_oid(arg, &oid))
|
|
||||||
die(_("Not a valid object name %s"), arg);
|
|
||||||
cmit = lookup_commit_reference(&oid);
|
|
||||||
if (!cmit)
|
|
||||||
die(_("%s is not a valid '%s' object"), arg, commit_type);
|
|
||||||
|
|
||||||
n = find_commit_name(&cmit->object.oid);
|
n = find_commit_name(&cmit->object.oid);
|
||||||
if (n && (tags || all || n->prio == 2)) {
|
if (n && (tags || all || n->prio == 2)) {
|
||||||
/*
|
/*
|
||||||
* Exact match to an existing ref.
|
* Exact match to an existing ref.
|
||||||
*/
|
*/
|
||||||
display_name(n);
|
append_name(n, dst);
|
||||||
if (longformat)
|
if (longformat)
|
||||||
show_suffix(0, n->tag ? &n->tag->tagged->oid : &oid);
|
append_suffix(0, n->tag ? &n->tag->tagged->oid : oid, dst);
|
||||||
if (suffix)
|
if (suffix)
|
||||||
printf("%s", suffix);
|
strbuf_addstr(dst, suffix);
|
||||||
printf("\n");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -386,10 +377,9 @@ static void describe(const char *arg, int last_one)
|
|||||||
if (!match_cnt) {
|
if (!match_cnt) {
|
||||||
struct object_id *cmit_oid = &cmit->object.oid;
|
struct object_id *cmit_oid = &cmit->object.oid;
|
||||||
if (always) {
|
if (always) {
|
||||||
printf("%s", find_unique_abbrev(cmit_oid->hash, abbrev));
|
strbuf_addstr(dst, find_unique_abbrev(cmit_oid->hash, abbrev));
|
||||||
if (suffix)
|
if (suffix)
|
||||||
printf("%s", suffix);
|
strbuf_addstr(dst, suffix);
|
||||||
printf("\n");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (unannotated_cnt)
|
if (unannotated_cnt)
|
||||||
@ -437,15 +427,36 @@ static void describe(const char *arg, int last_one)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
display_name(all_matches[0].name);
|
append_name(all_matches[0].name, dst);
|
||||||
if (abbrev)
|
if (abbrev)
|
||||||
show_suffix(all_matches[0].depth, &cmit->object.oid);
|
append_suffix(all_matches[0].depth, &cmit->object.oid, dst);
|
||||||
if (suffix)
|
if (suffix)
|
||||||
printf("%s", suffix);
|
strbuf_addstr(dst, suffix);
|
||||||
printf("\n");
|
}
|
||||||
|
|
||||||
|
static void describe(const char *arg, int last_one)
|
||||||
|
{
|
||||||
|
struct object_id oid;
|
||||||
|
struct commit *cmit;
|
||||||
|
struct strbuf sb = STRBUF_INIT;
|
||||||
|
|
||||||
|
if (debug)
|
||||||
|
fprintf(stderr, _("describe %s\n"), arg);
|
||||||
|
|
||||||
|
if (get_oid(arg, &oid))
|
||||||
|
die(_("Not a valid object name %s"), arg);
|
||||||
|
cmit = lookup_commit_reference(&oid);
|
||||||
|
if (!cmit)
|
||||||
|
die(_("%s is not a valid '%s' object"), arg, commit_type);
|
||||||
|
|
||||||
|
describe_commit(&oid, &sb);
|
||||||
|
|
||||||
|
puts(sb.buf);
|
||||||
|
|
||||||
if (!last_one)
|
if (!last_one)
|
||||||
clear_commit_marks(cmit, -1);
|
clear_commit_marks(cmit, -1);
|
||||||
|
|
||||||
|
strbuf_release(&sb);
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmd_describe(int argc, const char **argv, const char *prefix)
|
int cmd_describe(int argc, const char **argv, const char *prefix)
|
||||||
|
Loading…
Reference in New Issue
Block a user