12 /* bits #0-15 in revision.h */
14 #define COUNTED (1u<<16)
16 static const char rev_list_usage[] =
17 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
30 " formatting output:\n"
32 " --objects | --objects-edge\n"
34 " --header | --pretty\n"
35 " --abbrev=nr | --no-abbrev\n"
41 static struct rev_info revs;
43 static int bisect_list;
44 static int show_timestamp;
45 static int hdr_termination;
46 static const char *header_prefix;
48 static void show_commit(struct commit *commit)
51 printf("%lu ", commit->date);
53 fputs(header_prefix, stdout);
54 if (commit->object.flags & BOUNDARY)
56 if (revs.abbrev_commit && revs.abbrev)
57 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
60 fputs(sha1_to_hex(commit->object.sha1), stdout);
62 struct commit_list *parents = commit->parents;
64 struct object *o = &(parents->item->object);
65 parents = parents->next;
66 if (o->flags & TMP_MARK)
68 printf(" %s", sha1_to_hex(o->sha1));
71 /* TMP_MARK is a general purpose flag that can
72 * be used locally, but the user should clean
73 * things up after it is done with them.
75 for (parents = commit->parents;
77 parents = parents->next)
78 parents->item->object.flags &= ~TMP_MARK;
80 if (revs.commit_format == CMIT_FMT_ONELINE)
85 if (revs.verbose_header) {
86 static char pretty_header[16384];
87 pretty_print_commit(revs.commit_format, commit, ~0,
88 pretty_header, sizeof(pretty_header),
89 revs.abbrev, NULL, NULL, revs.relative_date);
90 printf("%s%c", pretty_header, hdr_termination);
93 if (commit->parents) {
94 free_commit_list(commit->parents);
95 commit->parents = NULL;
98 commit->buffer = NULL;
101 static void process_blob(struct blob *blob,
102 struct object_array *p,
103 struct name_path *path,
106 struct object *obj = &blob->object;
108 if (!revs.blob_objects)
110 if (obj->flags & (UNINTERESTING | SEEN))
113 name = xstrdup(name);
114 add_object(obj, p, path, name);
117 static void process_tree(struct tree *tree,
118 struct object_array *p,
119 struct name_path *path,
122 struct object *obj = &tree->object;
123 struct tree_desc desc;
124 struct name_entry entry;
127 if (!revs.tree_objects)
129 if (obj->flags & (UNINTERESTING | SEEN))
131 if (parse_tree(tree) < 0)
132 die("bad tree object %s", sha1_to_hex(obj->sha1));
134 name = xstrdup(name);
135 add_object(obj, p, path, name);
138 me.elem_len = strlen(name);
140 desc.buf = tree->buffer;
141 desc.size = tree->size;
143 while (tree_entry(&desc, &entry)) {
144 if (S_ISDIR(entry.mode))
145 process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
147 process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
153 static void show_commit_list(struct rev_info *revs)
156 struct commit *commit;
157 struct object_array objects = { 0, 0, NULL };
159 while ((commit = get_revision(revs)) != NULL) {
160 process_tree(commit->tree, &objects, NULL, "");
163 for (i = 0; i < revs->pending.nr; i++) {
164 struct object_array_entry *pending = revs->pending.objects + i;
165 struct object *obj = pending->item;
166 const char *name = pending->name;
167 if (obj->flags & (UNINTERESTING | SEEN))
169 if (obj->type == OBJ_TAG) {
171 add_object_array(obj, name, &objects);
174 if (obj->type == OBJ_TREE) {
175 process_tree((struct tree *)obj, &objects, NULL, name);
178 if (obj->type == OBJ_BLOB) {
179 process_blob((struct blob *)obj, &objects, NULL, name);
182 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
184 for (i = 0; i < objects.nr; i++) {
185 struct object_array_entry *p = objects.objects + i;
187 /* An object with name "foo\n0000000..." can be used to
188 * confuse downstream git-pack-objects very badly.
190 const char *ep = strchr(p->name, '\n');
192 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
193 (int) (ep - p->name),
197 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
202 * This is a truly stupid algorithm, but it's only
203 * used for bisection, and we just don't care enough.
205 * We care just barely enough to avoid recursing for
208 static int count_distance(struct commit_list *entry)
213 struct commit *commit = entry->item;
214 struct commit_list *p;
216 if (commit->object.flags & (UNINTERESTING | COUNTED))
218 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
220 commit->object.flags |= COUNTED;
226 nr += count_distance(p);
235 static void clear_distance(struct commit_list *list)
238 struct commit *commit = list->item;
239 commit->object.flags &= ~COUNTED;
244 static struct commit_list *find_bisection(struct commit_list *list)
247 struct commit_list *p, *best;
252 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
259 for (p = list; p; p = p->next) {
262 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
265 distance = count_distance(p);
266 clear_distance(list);
267 if (nr - distance < distance)
268 distance = nr - distance;
269 if (distance > closest) {
279 static void mark_edge_parents_uninteresting(struct commit *commit)
281 struct commit_list *parents;
283 for (parents = commit->parents; parents; parents = parents->next) {
284 struct commit *parent = parents->item;
285 if (!(parent->object.flags & UNINTERESTING))
287 mark_tree_uninteresting(parent->tree);
288 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
289 parent->object.flags |= SHOWN;
290 printf("-%s\n", sha1_to_hex(parent->object.sha1));
295 static void mark_edges_uninteresting(struct commit_list *list)
297 for ( ; list; list = list->next) {
298 struct commit *commit = list->item;
300 if (commit->object.flags & UNINTERESTING) {
301 mark_tree_uninteresting(commit->tree);
304 mark_edge_parents_uninteresting(commit);
308 static void read_revisions_from_stdin(struct rev_info *revs)
312 while (fgets(line, sizeof(line), stdin) != NULL) {
313 int len = strlen(line);
314 if (line[len - 1] == '\n')
319 die("options not supported in --stdin mode");
320 if (handle_revision_arg(line, revs, 0, 1))
321 die("bad revision '%s'", line);
325 int cmd_rev_list(int argc, const char **argv, const char *prefix)
327 struct commit_list *list;
329 int read_from_stdin = 0;
331 init_revisions(&revs, prefix);
333 revs.commit_format = CMIT_FMT_UNSPECIFIED;
334 argc = setup_revisions(argc, argv, &revs, NULL);
336 for (i = 1 ; i < argc; i++) {
337 const char *arg = argv[i];
339 if (!strcmp(arg, "--header")) {
340 revs.verbose_header = 1;
343 if (!strcmp(arg, "--timestamp")) {
347 if (!strcmp(arg, "--bisect")) {
351 if (!strcmp(arg, "--stdin")) {
352 if (read_from_stdin++)
353 die("--stdin given twice?");
354 read_revisions_from_stdin(&revs);
357 usage(rev_list_usage);
360 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
361 /* The command line has a --pretty */
362 hdr_termination = '\n';
363 if (revs.commit_format == CMIT_FMT_ONELINE)
366 header_prefix = "commit ";
368 else if (revs.verbose_header)
369 /* Only --header was specified */
370 revs.commit_format = CMIT_FMT_RAW;
375 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
376 !revs.pending.nr)) ||
378 usage(rev_list_usage);
380 save_commit_buffer = revs.verbose_header;
381 track_object_refs = 0;
385 prepare_revision_walk(&revs);
386 if (revs.tree_objects)
387 mark_edges_uninteresting(revs.commits);
390 revs.commits = find_bisection(revs.commits);
392 show_commit_list(&revs);