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"
39 static int bisect_list = 0;
40 static int verbose_header = 0;
41 static int abbrev = DEFAULT_ABBREV;
42 static int show_timestamp = 0;
43 static int hdr_termination = 0;
44 static const char *commit_prefix = "";
45 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
47 static void show_commit(struct commit *commit)
50 printf("%lu ", commit->date);
52 fputs(commit_prefix, stdout);
53 if (commit->object.flags & BOUNDARY)
55 fputs(sha1_to_hex(commit->object.sha1), stdout);
57 struct commit_list *parents = commit->parents;
59 struct object *o = &(parents->item->object);
60 parents = parents->next;
61 if (o->flags & TMP_MARK)
63 printf(" %s", sha1_to_hex(o->sha1));
66 /* TMP_MARK is a general purpose flag that can
67 * be used locally, but the user should clean
68 * things up after it is done with them.
70 for (parents = commit->parents;
72 parents = parents->next)
73 parents->item->object.flags &= ~TMP_MARK;
75 if (commit_format == CMIT_FMT_ONELINE)
81 static char pretty_header[16384];
82 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), abbrev);
83 printf("%s%c", pretty_header, hdr_termination);
88 static struct object_list **process_blob(struct blob *blob,
89 struct object_list **p,
90 struct name_path *path,
93 struct object *obj = &blob->object;
95 if (!revs.blob_objects)
97 if (obj->flags & (UNINTERESTING | SEEN))
100 return add_object(obj, p, path, name);
103 static struct object_list **process_tree(struct tree *tree,
104 struct object_list **p,
105 struct name_path *path,
108 struct object *obj = &tree->object;
109 struct tree_entry_list *entry;
112 if (!revs.tree_objects)
114 if (obj->flags & (UNINTERESTING | SEEN))
116 if (parse_tree(tree) < 0)
117 die("bad tree object %s", sha1_to_hex(obj->sha1));
119 p = add_object(obj, p, path, name);
122 me.elem_len = strlen(name);
123 entry = tree->entries;
124 tree->entries = NULL;
126 struct tree_entry_list *next = entry->next;
127 if (entry->directory)
128 p = process_tree(entry->item.tree, p, &me, entry->name);
130 p = process_blob(entry->item.blob, p, &me, entry->name);
137 static void show_commit_list(struct rev_info *revs)
139 struct commit *commit;
140 struct object_list *objects = NULL, **p = &objects, *pending;
142 while ((commit = get_revision(revs)) != NULL) {
143 p = process_tree(commit->tree, p, NULL, "");
146 for (pending = revs->pending_objects; pending; pending = pending->next) {
147 struct object *obj = pending->item;
148 const char *name = pending->name;
149 if (obj->flags & (UNINTERESTING | SEEN))
151 if (obj->type == tag_type) {
153 p = add_object(obj, p, NULL, name);
156 if (obj->type == tree_type) {
157 p = process_tree((struct tree *)obj, p, NULL, name);
160 if (obj->type == blob_type) {
161 p = process_blob((struct blob *)obj, p, NULL, name);
164 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
167 /* An object with name "foo\n0000000..." can be used to
168 * confuse downstream git-pack-objects very badly.
170 const char *ep = strchr(objects->name, '\n');
172 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
173 (int) (ep - objects->name),
177 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
178 objects = objects->next;
183 * This is a truly stupid algorithm, but it's only
184 * used for bisection, and we just don't care enough.
186 * We care just barely enough to avoid recursing for
189 static int count_distance(struct commit_list *entry)
194 struct commit *commit = entry->item;
195 struct commit_list *p;
197 if (commit->object.flags & (UNINTERESTING | COUNTED))
199 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
201 commit->object.flags |= COUNTED;
207 nr += count_distance(p);
216 static void clear_distance(struct commit_list *list)
219 struct commit *commit = list->item;
220 commit->object.flags &= ~COUNTED;
225 static struct commit_list *find_bisection(struct commit_list *list)
228 struct commit_list *p, *best;
233 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
240 for (p = list; p; p = p->next) {
243 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
246 distance = count_distance(p);
247 clear_distance(list);
248 if (nr - distance < distance)
249 distance = nr - distance;
250 if (distance > closest) {
260 static void mark_edge_parents_uninteresting(struct commit *commit)
262 struct commit_list *parents;
264 for (parents = commit->parents; parents; parents = parents->next) {
265 struct commit *parent = parents->item;
266 if (!(parent->object.flags & UNINTERESTING))
268 mark_tree_uninteresting(parent->tree);
269 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
270 parent->object.flags |= SHOWN;
271 printf("-%s\n", sha1_to_hex(parent->object.sha1));
276 static void mark_edges_uninteresting(struct commit_list *list)
278 for ( ; list; list = list->next) {
279 struct commit *commit = list->item;
281 if (commit->object.flags & UNINTERESTING) {
282 mark_tree_uninteresting(commit->tree);
285 mark_edge_parents_uninteresting(commit);
289 int main(int argc, const char **argv)
291 struct commit_list *list;
294 argc = setup_revisions(argc, argv, &revs, NULL);
296 for (i = 1 ; i < argc; i++) {
297 const char *arg = argv[i];
299 /* accept -<digit>, like traditilnal "head" */
300 if ((*arg == '-') && isdigit(arg[1])) {
301 revs.max_count = atoi(arg + 1);
304 if (!strcmp(arg, "-n")) {
306 die("-n requires an argument");
307 revs.max_count = atoi(argv[i]);
310 if (!strncmp(arg,"-n",2)) {
311 revs.max_count = atoi(arg + 2);
314 if (!strcmp(arg, "--header")) {
318 if (!strcmp(arg, "--no-abbrev")) {
322 if (!strncmp(arg, "--abbrev=", 9)) {
323 abbrev = strtoul(arg + 9, NULL, 10);
324 if (abbrev && abbrev < MINIMUM_ABBREV)
325 abbrev = MINIMUM_ABBREV;
326 else if (40 < abbrev)
330 if (!strncmp(arg, "--pretty", 8)) {
331 commit_format = get_commit_format(arg+8);
333 hdr_termination = '\n';
334 if (commit_format == CMIT_FMT_ONELINE)
337 commit_prefix = "commit ";
340 if (!strcmp(arg, "--timestamp")) {
344 if (!strcmp(arg, "--bisect")) {
348 usage(rev_list_usage);
355 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && !revs.pending_objects))
356 usage(rev_list_usage);
358 save_commit_buffer = verbose_header;
359 track_object_refs = 0;
361 prepare_revision_walk(&revs);
362 if (revs.tree_objects)
363 mark_edges_uninteresting(revs.commits);
366 revs.commits = find_bisection(revs.commits);
368 show_commit_list(&revs);