11 /* bits #0-15 in revision.h */
13 #define COUNTED (1u<<16)
15 static const char rev_list_usage[] =
16 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
28 " formatting output:\n"
30 " --objects | --objects-edge\n"
32 " --header | --pretty\n"
33 " --abbrev=nr | --no-abbrev\n"
41 static int bisect_list = 0;
42 static int show_timestamp = 0;
43 static int hdr_termination = 0;
44 static const char *header_prefix;
46 static void show_commit(struct commit *commit)
49 printf("%lu ", commit->date);
51 fputs(header_prefix, stdout);
52 if (commit->object.flags & BOUNDARY)
54 if (revs.abbrev_commit && revs.abbrev)
55 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
58 fputs(sha1_to_hex(commit->object.sha1), stdout);
60 struct commit_list *parents = commit->parents;
62 struct object *o = &(parents->item->object);
63 parents = parents->next;
64 if (o->flags & TMP_MARK)
66 printf(" %s", sha1_to_hex(o->sha1));
69 /* TMP_MARK is a general purpose flag that can
70 * be used locally, but the user should clean
71 * things up after it is done with them.
73 for (parents = commit->parents;
75 parents = parents->next)
76 parents->item->object.flags &= ~TMP_MARK;
78 if (revs.commit_format == CMIT_FMT_ONELINE)
83 if (revs.verbose_header) {
84 static char pretty_header[16384];
85 pretty_print_commit(revs.commit_format, commit, ~0,
86 pretty_header, sizeof(pretty_header),
88 printf("%s%c", pretty_header, hdr_termination);
93 static struct object_list **process_blob(struct blob *blob,
94 struct object_list **p,
95 struct name_path *path,
98 struct object *obj = &blob->object;
100 if (!revs.blob_objects)
102 if (obj->flags & (UNINTERESTING | SEEN))
105 return add_object(obj, p, path, name);
108 static struct object_list **process_tree(struct tree *tree,
109 struct object_list **p,
110 struct name_path *path,
113 struct object *obj = &tree->object;
114 struct tree_entry_list *entry;
117 if (!revs.tree_objects)
119 if (obj->flags & (UNINTERESTING | SEEN))
121 if (parse_tree(tree) < 0)
122 die("bad tree object %s", sha1_to_hex(obj->sha1));
124 p = add_object(obj, p, path, name);
127 me.elem_len = strlen(name);
128 entry = tree->entries;
129 tree->entries = NULL;
131 struct tree_entry_list *next = entry->next;
132 if (entry->directory)
133 p = process_tree(entry->item.tree, p, &me, entry->name);
135 p = process_blob(entry->item.blob, p, &me, entry->name);
142 static void show_commit_list(struct rev_info *revs)
144 struct commit *commit;
145 struct object_list *objects = NULL, **p = &objects, *pending;
147 while ((commit = get_revision(revs)) != NULL) {
148 p = process_tree(commit->tree, p, NULL, "");
151 for (pending = revs->pending_objects; pending; pending = pending->next) {
152 struct object *obj = pending->item;
153 const char *name = pending->name;
154 if (obj->flags & (UNINTERESTING | SEEN))
156 if (obj->type == tag_type) {
158 p = add_object(obj, p, NULL, name);
161 if (obj->type == tree_type) {
162 p = process_tree((struct tree *)obj, p, NULL, name);
165 if (obj->type == blob_type) {
166 p = process_blob((struct blob *)obj, p, NULL, name);
169 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
172 /* An object with name "foo\n0000000..." can be used to
173 * confuse downstream git-pack-objects very badly.
175 const char *ep = strchr(objects->name, '\n');
177 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
178 (int) (ep - objects->name),
182 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
183 objects = objects->next;
188 * This is a truly stupid algorithm, but it's only
189 * used for bisection, and we just don't care enough.
191 * We care just barely enough to avoid recursing for
194 static int count_distance(struct commit_list *entry)
199 struct commit *commit = entry->item;
200 struct commit_list *p;
202 if (commit->object.flags & (UNINTERESTING | COUNTED))
204 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
206 commit->object.flags |= COUNTED;
212 nr += count_distance(p);
221 static void clear_distance(struct commit_list *list)
224 struct commit *commit = list->item;
225 commit->object.flags &= ~COUNTED;
230 static struct commit_list *find_bisection(struct commit_list *list)
233 struct commit_list *p, *best;
238 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
245 for (p = list; p; p = p->next) {
248 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
251 distance = count_distance(p);
252 clear_distance(list);
253 if (nr - distance < distance)
254 distance = nr - distance;
255 if (distance > closest) {
265 static void mark_edge_parents_uninteresting(struct commit *commit)
267 struct commit_list *parents;
269 for (parents = commit->parents; parents; parents = parents->next) {
270 struct commit *parent = parents->item;
271 if (!(parent->object.flags & UNINTERESTING))
273 mark_tree_uninteresting(parent->tree);
274 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
275 parent->object.flags |= SHOWN;
276 printf("-%s\n", sha1_to_hex(parent->object.sha1));
281 static void mark_edges_uninteresting(struct commit_list *list)
283 for ( ; list; list = list->next) {
284 struct commit *commit = list->item;
286 if (commit->object.flags & UNINTERESTING) {
287 mark_tree_uninteresting(commit->tree);
290 mark_edge_parents_uninteresting(commit);
294 int main(int argc, const char **argv)
296 struct commit_list *list;
299 init_revisions(&revs);
301 revs.commit_format = CMIT_FMT_UNSPECIFIED;
302 argc = setup_revisions(argc, argv, &revs, NULL);
304 for (i = 1 ; i < argc; i++) {
305 const char *arg = argv[i];
307 if (!strcmp(arg, "--header")) {
308 revs.verbose_header = 1;
311 if (!strcmp(arg, "--timestamp")) {
315 if (!strcmp(arg, "--bisect")) {
319 usage(rev_list_usage);
322 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
323 /* The command line has a --pretty */
324 hdr_termination = '\n';
325 if (revs.commit_format == CMIT_FMT_ONELINE)
328 header_prefix = "commit ";
330 else if (revs.verbose_header)
331 /* Only --header was specified */
332 revs.commit_format = CMIT_FMT_RAW;
337 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
338 !revs.pending_objects)) ||
340 usage(rev_list_usage);
342 save_commit_buffer = revs.verbose_header;
343 track_object_refs = 0;
347 prepare_revision_walk(&revs);
348 if (revs.tree_objects)
349 mark_edges_uninteresting(revs.commits);
352 revs.commits = find_bisection(revs.commits);
354 show_commit_list(&revs);