10 /* bits #0-5 in revision.h */
12 #define COUNTED (1u<<6)
14 static const char rev_list_usage[] =
15 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
27 " formatting output:\n"
29 " --objects | --objects-edge\n"
31 " --header | --pretty\n"
32 " --abbrev=nr | --no-abbrev\n"
40 static int bisect_list = 0;
41 static int verbose_header = 0;
42 static int abbrev = DEFAULT_ABBREV;
43 static int abbrev_commit = 0;
44 static int show_timestamp = 0;
45 static int hdr_termination = 0;
46 static const char *commit_prefix = "";
47 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
49 static void show_commit(struct commit *commit)
52 printf("%lu ", commit->date);
54 fputs(commit_prefix, stdout);
55 if (commit->object.flags & BOUNDARY)
57 if (abbrev_commit && abbrev)
58 fputs(find_unique_abbrev(commit->object.sha1, abbrev), stdout);
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 (commit_format == CMIT_FMT_ONELINE)
86 static char pretty_header[16384];
87 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), abbrev);
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 argc = setup_revisions(argc, argv, &revs, NULL);
301 for (i = 1 ; i < argc; i++) {
302 const char *arg = argv[i];
304 /* accept -<digit>, like traditilnal "head" */
305 if ((*arg == '-') && isdigit(arg[1])) {
306 revs.max_count = atoi(arg + 1);
309 if (!strcmp(arg, "-n")) {
311 die("-n requires an argument");
312 revs.max_count = atoi(argv[i]);
315 if (!strncmp(arg,"-n",2)) {
316 revs.max_count = atoi(arg + 2);
319 if (!strcmp(arg, "--header")) {
323 if (!strcmp(arg, "--no-abbrev")) {
327 if (!strcmp(arg, "--abbrev")) {
328 abbrev = DEFAULT_ABBREV;
331 if (!strcmp(arg, "--abbrev-commit")) {
335 if (!strncmp(arg, "--abbrev=", 9)) {
336 abbrev = strtoul(arg + 9, NULL, 10);
337 if (abbrev && abbrev < MINIMUM_ABBREV)
338 abbrev = MINIMUM_ABBREV;
339 else if (40 < abbrev)
343 if (!strncmp(arg, "--pretty", 8)) {
344 commit_format = get_commit_format(arg+8);
346 hdr_termination = '\n';
347 if (commit_format == CMIT_FMT_ONELINE)
350 commit_prefix = "commit ";
353 if (!strcmp(arg, "--timestamp")) {
357 if (!strcmp(arg, "--bisect")) {
361 usage(rev_list_usage);
368 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && !revs.pending_objects))
369 usage(rev_list_usage);
371 save_commit_buffer = verbose_header;
372 track_object_refs = 0;
374 prepare_revision_walk(&revs);
375 if (revs.tree_objects)
376 mark_edges_uninteresting(revs.commits);
379 revs.commits = find_bisection(revs.commits);
381 show_commit_list(&revs);