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"
29 " formatting output:\n"
31 " --objects | --objects-edge\n"
33 " --header | --pretty\n"
34 " --abbrev=nr | --no-abbrev\n"
40 static struct rev_info revs;
42 static int bisect_list = 0;
43 static int show_timestamp = 0;
44 static int hdr_termination = 0;
45 static const char *header_prefix;
47 static void show_commit(struct commit *commit)
50 printf("%lu ", commit->date);
52 fputs(header_prefix, stdout);
53 if (commit->object.flags & BOUNDARY)
55 if (revs.abbrev_commit && revs.abbrev)
56 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
59 fputs(sha1_to_hex(commit->object.sha1), stdout);
61 struct commit_list *parents = commit->parents;
63 struct object *o = &(parents->item->object);
64 parents = parents->next;
65 if (o->flags & TMP_MARK)
67 printf(" %s", sha1_to_hex(o->sha1));
70 /* TMP_MARK is a general purpose flag that can
71 * be used locally, but the user should clean
72 * things up after it is done with them.
74 for (parents = commit->parents;
76 parents = parents->next)
77 parents->item->object.flags &= ~TMP_MARK;
79 if (revs.commit_format == CMIT_FMT_ONELINE)
84 if (revs.verbose_header) {
85 static char pretty_header[16384];
86 pretty_print_commit(revs.commit_format, commit, ~0,
87 pretty_header, sizeof(pretty_header),
88 revs.abbrev, NULL, NULL);
89 printf("%s%c", pretty_header, hdr_termination);
94 static struct object_list **process_blob(struct blob *blob,
95 struct object_list **p,
96 struct name_path *path,
99 struct object *obj = &blob->object;
101 if (!revs.blob_objects)
103 if (obj->flags & (UNINTERESTING | SEEN))
107 return add_object(obj, p, path, name);
110 static struct object_list **process_tree(struct tree *tree,
111 struct object_list **p,
112 struct name_path *path,
115 struct object *obj = &tree->object;
116 struct tree_desc desc;
119 if (!revs.tree_objects)
121 if (obj->flags & (UNINTERESTING | SEEN))
123 if (parse_tree(tree) < 0)
124 die("bad tree object %s", sha1_to_hex(obj->sha1));
127 p = add_object(obj, p, path, name);
130 me.elem_len = strlen(name);
132 desc.buf = tree->buffer;
133 desc.size = tree->size;
138 const unsigned char *sha1;
140 sha1 = tree_entry_extract(&desc, &name, &mode);
141 update_tree_entry(&desc);
144 p = process_tree(lookup_tree(sha1), p, &me, name);
146 p = process_blob(lookup_blob(sha1), p, &me, name);
153 static void show_commit_list(struct rev_info *revs)
155 struct commit *commit;
156 struct object_list *objects = NULL, **p = &objects, *pending;
158 while ((commit = get_revision(revs)) != NULL) {
159 p = process_tree(commit->tree, p, NULL, "");
162 for (pending = revs->pending_objects; pending; pending = pending->next) {
163 struct object *obj = pending->item;
164 const char *name = pending->name;
165 if (obj->flags & (UNINTERESTING | SEEN))
167 if (obj->type == tag_type) {
169 p = add_object(obj, p, NULL, name);
172 if (obj->type == tree_type) {
173 p = process_tree((struct tree *)obj, p, NULL, name);
176 if (obj->type == blob_type) {
177 p = process_blob((struct blob *)obj, p, NULL, name);
180 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
183 /* An object with name "foo\n0000000..." can be used to
184 * confuse downstream git-pack-objects very badly.
186 const char *ep = strchr(objects->name, '\n');
188 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
189 (int) (ep - objects->name),
193 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
194 objects = objects->next;
199 * This is a truly stupid algorithm, but it's only
200 * used for bisection, and we just don't care enough.
202 * We care just barely enough to avoid recursing for
205 static int count_distance(struct commit_list *entry)
210 struct commit *commit = entry->item;
211 struct commit_list *p;
213 if (commit->object.flags & (UNINTERESTING | COUNTED))
215 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
217 commit->object.flags |= COUNTED;
223 nr += count_distance(p);
232 static void clear_distance(struct commit_list *list)
235 struct commit *commit = list->item;
236 commit->object.flags &= ~COUNTED;
241 static struct commit_list *find_bisection(struct commit_list *list)
244 struct commit_list *p, *best;
249 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
256 for (p = list; p; p = p->next) {
259 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
262 distance = count_distance(p);
263 clear_distance(list);
264 if (nr - distance < distance)
265 distance = nr - distance;
266 if (distance > closest) {
276 static void mark_edge_parents_uninteresting(struct commit *commit)
278 struct commit_list *parents;
280 for (parents = commit->parents; parents; parents = parents->next) {
281 struct commit *parent = parents->item;
282 if (!(parent->object.flags & UNINTERESTING))
284 mark_tree_uninteresting(parent->tree);
285 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
286 parent->object.flags |= SHOWN;
287 printf("-%s\n", sha1_to_hex(parent->object.sha1));
292 static void mark_edges_uninteresting(struct commit_list *list)
294 for ( ; list; list = list->next) {
295 struct commit *commit = list->item;
297 if (commit->object.flags & UNINTERESTING) {
298 mark_tree_uninteresting(commit->tree);
301 mark_edge_parents_uninteresting(commit);
305 int cmd_rev_list(int argc, const char **argv, char **envp)
307 struct commit_list *list;
310 init_revisions(&revs);
312 revs.commit_format = CMIT_FMT_UNSPECIFIED;
313 argc = setup_revisions(argc, argv, &revs, NULL);
315 for (i = 1 ; i < argc; i++) {
316 const char *arg = argv[i];
318 if (!strcmp(arg, "--header")) {
319 revs.verbose_header = 1;
322 if (!strcmp(arg, "--timestamp")) {
326 if (!strcmp(arg, "--bisect")) {
330 usage(rev_list_usage);
333 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
334 /* The command line has a --pretty */
335 hdr_termination = '\n';
336 if (revs.commit_format == CMIT_FMT_ONELINE)
339 header_prefix = "commit ";
341 else if (revs.verbose_header)
342 /* Only --header was specified */
343 revs.commit_format = CMIT_FMT_RAW;
348 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
349 !revs.pending_objects)) ||
351 usage(rev_list_usage);
353 save_commit_buffer = revs.verbose_header;
354 track_object_refs = 0;
358 prepare_revision_walk(&revs);
359 if (revs.tree_objects)
360 mark_edges_uninteresting(revs.commits);
363 revs.commits = find_bisection(revs.commits);
365 show_commit_list(&revs);