Skip to content

Commit 1f41018

Browse files
committed
Merge branch 'sb/describe-blob' into pu
"git describe" was taught to dig trees deeper to find a <commit-ish>:<path> that refers to a given blob object. * sb/describe-blob: builtin/describe.c: describe a blob builtin/describe.c: factor out describe_commit builtin/describe.c: print debug statements earlier builtin/describe.c: rename `oid` to avoid variable shadowing revision.h: introduce blob/tree walking in order of the commits list-objects.c: factor out traverse_trees_and_blobs t6120: fix typo in test name
2 parents 6d1fbc2 + 9264b03 commit 1f41018

File tree

8 files changed

+220
-50
lines changed

8 files changed

+220
-50
lines changed

Documentation/git-describe.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ git-describe(1)
33

44
NAME
55
----
6-
git-describe - Describe a commit using the most recent tag reachable from it
6+
git-describe - Describe a commit or blob using the graph relations
77

88

99
SYNOPSIS
1010
--------
1111
[verse]
1212
'git describe' [--all] [--tags] [--contains] [--abbrev=<n>] [<commit-ish>...]
1313
'git describe' [--all] [--tags] [--contains] [--abbrev=<n>] --dirty[=<mark>]
14+
'git describe' [<options>] <blob>
1415

1516
DESCRIPTION
1617
-----------
@@ -24,6 +25,16 @@ By default (without --all or --tags) `git describe` only shows
2425
annotated tags. For more information about creating annotated tags
2526
see the -a and -s options to linkgit:git-tag[1].
2627

28+
If the given object refers to a blob, it will be described
29+
as `<commit-ish>:<path>`, such that the blob can be found
30+
at `<path>` in the `<commit-ish>`. Note, that the commit is likely
31+
not the commit that introduced the blob, but the one that was found
32+
first; to find the commit that introduced the blob, you need to find
33+
the commit that last touched the path, e.g.
34+
`git log <commit-description> -- <path>`.
35+
As blobs do not point at the commits they are contained in,
36+
describing blobs is slow as we have to walk the whole graph.
37+
2738
OPTIONS
2839
-------
2940
<commit-ish>...::

Documentation/rev-list-options.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,11 @@ ifdef::git-rev-list[]
686686
all object IDs which I need to download if I have the commit
687687
object _bar_ but not _foo_''.
688688

689+
--in-commit-order::
690+
Print tree and blob ids in order of the commits. The tree
691+
and blob ids are printed after they are first referenced
692+
by a commit.
693+
689694
--objects-edge::
690695
Similar to `--objects`, but also print the IDs of excluded
691696
commits prefixed with a ``-'' character. This is used by

builtin/describe.c

Lines changed: 97 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "lockfile.h"
44
#include "commit.h"
55
#include "tag.h"
6+
#include "blob.h"
67
#include "refs.h"
78
#include "builtin.h"
89
#include "exec_cmd.h"
@@ -12,6 +13,8 @@
1213
#include "hashmap.h"
1314
#include "argv-array.h"
1415
#include "run-command.h"
16+
#include "revision.h"
17+
#include "list-objects.h"
1518

1619
#define MAX_TAGS (FLAG_BITS - 1)
1720

@@ -256,7 +259,7 @@ static unsigned long finish_depth_computation(
256259
return seen_commits;
257260
}
258261

259-
static void display_name(struct commit_name *n)
262+
static void append_name(struct commit_name *n, struct strbuf *dst)
260263
{
261264
if (n->prio == 2 && !n->tag) {
262265
n->tag = lookup_tag(&n->oid);
@@ -272,19 +275,18 @@ static void display_name(struct commit_name *n)
272275
}
273276

274277
if (n->tag)
275-
printf("%s", n->tag->tag);
278+
strbuf_addstr(dst, n->tag->tag);
276279
else
277-
printf("%s", n->path);
280+
strbuf_addstr(dst, n->path);
278281
}
279282

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

285-
static void describe(const char *arg, int last_one)
288+
static void describe_commit(struct object_id *oid, struct strbuf *dst)
286289
{
287-
struct object_id oid;
288290
struct commit *cmit, *gave_up_on = NULL;
289291
struct commit_list *list;
290292
struct commit_name *n;
@@ -293,30 +295,25 @@ static void describe(const char *arg, int last_one)
293295
unsigned long seen_commits = 0;
294296
unsigned int unannotated_cnt = 0;
295297

296-
if (get_oid(arg, &oid))
297-
die(_("Not a valid object name %s"), arg);
298-
cmit = lookup_commit_reference(&oid);
299-
if (!cmit)
300-
die(_("%s is not a valid '%s' object"), arg, commit_type);
298+
cmit = lookup_commit_reference(oid);
301299

302300
n = find_commit_name(&cmit->object.oid);
303301
if (n && (tags || all || n->prio == 2)) {
304302
/*
305303
* Exact match to an existing ref.
306304
*/
307-
display_name(n);
305+
append_name(n, dst);
308306
if (longformat)
309-
show_suffix(0, n->tag ? &n->tag->tagged->oid : &oid);
307+
append_suffix(0, n->tag ? &n->tag->tagged->oid : oid, dst);
310308
if (suffix)
311-
printf("%s", suffix);
312-
printf("\n");
309+
strbuf_addstr(dst, suffix);
313310
return;
314311
}
315312

316313
if (!max_candidates)
317314
die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit->object.oid));
318315
if (debug)
319-
fprintf(stderr, _("searching to describe %s\n"), arg);
316+
fprintf(stderr, _("No exact match on refs or tags, searching to describe\n"));
320317

321318
if (!have_util) {
322319
struct hashmap_iter iter;
@@ -381,22 +378,21 @@ static void describe(const char *arg, int last_one)
381378
}
382379

383380
if (!match_cnt) {
384-
struct object_id *oid = &cmit->object.oid;
381+
struct object_id *cmit_oid = &cmit->object.oid;
385382
if (always) {
386-
printf("%s", find_unique_abbrev(oid->hash, abbrev));
383+
strbuf_addstr(dst, find_unique_abbrev(cmit_oid->hash, abbrev));
387384
if (suffix)
388-
printf("%s", suffix);
389-
printf("\n");
385+
strbuf_addstr(dst, suffix);
390386
return;
391387
}
392388
if (unannotated_cnt)
393389
die(_("No annotated tags can describe '%s'.\n"
394390
"However, there were unannotated tags: try --tags."),
395-
oid_to_hex(oid));
391+
oid_to_hex(cmit_oid));
396392
else
397393
die(_("No tags can describe '%s'.\n"
398394
"Try --always, or create some tags."),
399-
oid_to_hex(oid));
395+
oid_to_hex(cmit_oid));
400396
}
401397

402398
QSORT(all_matches, match_cnt, compare_pt);
@@ -434,15 +430,89 @@ static void describe(const char *arg, int last_one)
434430
}
435431
}
436432

437-
display_name(all_matches[0].name);
433+
append_name(all_matches[0].name, dst);
438434
if (abbrev)
439-
show_suffix(all_matches[0].depth, &cmit->object.oid);
435+
append_suffix(all_matches[0].depth, &cmit->object.oid, dst);
440436
if (suffix)
441-
printf("%s", suffix);
442-
printf("\n");
437+
strbuf_addstr(dst, suffix);
438+
}
439+
440+
struct process_commit_data {
441+
struct object_id current_commit;
442+
struct object_id looking_for;
443+
struct strbuf *dst;
444+
struct rev_info *revs;
445+
};
446+
447+
static void process_commit(struct commit *commit, void *data)
448+
{
449+
struct process_commit_data *pcd = data;
450+
pcd->current_commit = commit->object.oid;
451+
}
452+
453+
static void process_object(struct object *obj, const char *path, void *data)
454+
{
455+
struct process_commit_data *pcd = data;
456+
457+
if (!oidcmp(&pcd->looking_for, &obj->oid) && !pcd->dst->len) {
458+
reset_revision_walk();
459+
describe_commit(&pcd->current_commit, pcd->dst);
460+
strbuf_addf(pcd->dst, ":%s", path);
461+
pcd->revs->max_count = 0;
462+
}
463+
}
464+
465+
static void describe_blob(struct object_id oid, struct strbuf *dst)
466+
{
467+
struct rev_info revs;
468+
struct argv_array args = ARGV_ARRAY_INIT;
469+
struct process_commit_data pcd = { null_oid, oid, dst, &revs};
470+
471+
argv_array_pushl(&args, "internal: The first arg is not parsed",
472+
"--all", "--reflog", /* as many starting points as possible */
473+
/* NEEDSWORK: --all is incompatible with worktrees for now: */
474+
"--single-worktree",
475+
"--objects",
476+
"--in-commit-order",
477+
NULL);
478+
479+
init_revisions(&revs, NULL);
480+
if (setup_revisions(args.argc, args.argv, &revs, NULL) > 1)
481+
BUG("setup_revisions could not handle all args?");
482+
483+
if (prepare_revision_walk(&revs))
484+
die("revision walk setup failed");
485+
486+
traverse_commit_list(&revs, process_commit, process_object, &pcd);
487+
reset_revision_walk();
488+
}
489+
490+
static void describe(const char *arg, int last_one)
491+
{
492+
struct object_id oid;
493+
struct commit *cmit;
494+
struct strbuf sb = STRBUF_INIT;
495+
496+
if (debug)
497+
fprintf(stderr, _("describe %s\n"), arg);
498+
499+
if (get_oid(arg, &oid))
500+
die(_("Not a valid object name %s"), arg);
501+
cmit = lookup_commit_reference_gently(&oid, 1);
502+
503+
if (cmit)
504+
describe_commit(&oid, &sb);
505+
else if (lookup_blob(&oid))
506+
describe_blob(oid, &sb);
507+
else
508+
die(_("%s is neither a commit nor blob"), arg);
509+
510+
puts(sb.buf);
443511

444512
if (!last_one)
445513
clear_commit_marks(cmit, -1);
514+
515+
strbuf_release(&sb);
446516
}
447517

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

list-objects.c

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -220,32 +220,22 @@ static void add_pending_tree(struct rev_info *revs, struct tree *tree)
220220
add_pending_object(revs, &tree->object, "");
221221
}
222222

223-
static void do_traverse(struct rev_info *revs,
224-
show_commit_fn show_commit,
225-
show_object_fn show_object,
226-
void *show_data,
227-
filter_object_fn filter_fn,
228-
void *filter_data)
223+
static void traverse_trees_and_blobs(struct rev_info *revs,
224+
struct strbuf *base,
225+
show_object_fn show_object,
226+
void *show_data,
227+
filter_object_fn filter_fn,
228+
void *filter_data)
229229
{
230230
int i;
231-
struct commit *commit;
232-
struct strbuf base;
233231

234-
strbuf_init(&base, PATH_MAX);
235-
while ((commit = get_revision(revs)) != NULL) {
236-
/*
237-
* an uninteresting boundary commit may not have its tree
238-
* parsed yet, but we are not going to show them anyway
239-
*/
240-
if (commit->tree)
241-
add_pending_tree(revs, commit->tree);
242-
show_commit(commit, show_data);
243-
}
232+
assert(!base->len);
244233
for (i = 0; i < revs->pending.nr; i++) {
245234
struct object_array_entry *pending = revs->pending.objects + i;
246235
struct object *obj = pending->item;
247236
const char *name = pending->name;
248237
const char *path = pending->path;
238+
249239
if (obj->flags & (UNINTERESTING | SEEN))
250240
continue;
251241
if (obj->type == OBJ_TAG) {
@@ -257,21 +247,50 @@ static void do_traverse(struct rev_info *revs,
257247
path = "";
258248
if (obj->type == OBJ_TREE) {
259249
process_tree(revs, (struct tree *)obj, show_object,
260-
&base, path, show_data,
250+
base, path, show_data,
261251
filter_fn, filter_data);
262252
continue;
263253
}
264254
if (obj->type == OBJ_BLOB) {
265255
process_blob(revs, (struct blob *)obj, show_object,
266-
&base, path, show_data,
256+
base, path, show_data,
267257
filter_fn, filter_data);
268258
continue;
269259
}
270260
die("unknown pending object %s (%s)",
271261
oid_to_hex(&obj->oid), name);
272262
}
273263
object_array_clear(&revs->pending);
274-
strbuf_release(&base);
264+
}
265+
266+
static void do_traverse(struct rev_info *revs,
267+
show_commit_fn show_commit,
268+
show_object_fn show_object,
269+
void *show_data,
270+
filter_object_fn filter_fn,
271+
void *filter_data)
272+
{
273+
struct commit *commit;
274+
struct strbuf csp; /* callee's scratch pad */
275+
276+
strbuf_init(&csp, PATH_MAX);
277+
while ((commit = get_revision(revs)) != NULL) {
278+
/*
279+
* an uninteresting boundary commit may not have its tree
280+
* parsed yet, but we are not going to show them anyway
281+
*/
282+
if (commit->tree)
283+
add_pending_tree(revs, commit->tree);
284+
show_commit(commit, show_data);
285+
if (revs->tree_blobs_in_commit_order)
286+
traverse_trees_and_blobs(revs, &csp,
287+
show_object, show_data,
288+
filter_fn, filter_data);
289+
}
290+
traverse_trees_and_blobs(revs, &csp,
291+
show_object, show_data,
292+
filter_fn, filter_data);
293+
strbuf_release(&csp);
275294
}
276295

277296
void traverse_commit_list(struct rev_info *revs,

revision.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
18661866
revs->dense = 0;
18671867
} else if (!strcmp(arg, "--show-all")) {
18681868
revs->show_all = 1;
1869+
} else if (!strcmp(arg, "--in-commit-order")) {
1870+
revs->tree_blobs_in_commit_order = 1;
18691871
} else if (!strcmp(arg, "--remove-empty")) {
18701872
revs->remove_empty_trees = 1;
18711873
} else if (!strcmp(arg, "--merges")) {

revision.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ struct rev_info {
122122
ancestry_path:1,
123123
first_parent_only:1,
124124
line_level_traverse:1,
125+
tree_blobs_in_commit_order:1,
125126

126127
/* for internal use only */
127128
exclude_promisor_objects:1;

0 commit comments

Comments
 (0)