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 verbose_header = 0;
43 static int abbrev = DEFAULT_ABBREV;
44 static int abbrev_commit = 0;
45 static int show_timestamp = 0;
46 static int hdr_termination = 0;
47 static const char *commit_prefix = "";
48 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
50 static void show_commit(struct commit *commit)
53 printf("%lu ", commit->date);
55 fputs(commit_prefix, stdout);
56 if (commit->object.flags & BOUNDARY)
58 if (abbrev_commit && abbrev)
59 fputs(find_unique_abbrev(commit->object.sha1, abbrev), stdout);
61 fputs(sha1_to_hex(commit->object.sha1), stdout);
63 struct commit_list *parents = commit->parents;
65 struct object *o = &(parents->item->object);
66 parents = parents->next;
67 if (o->flags & TMP_MARK)
69 printf(" %s", sha1_to_hex(o->sha1));
72 /* TMP_MARK is a general purpose flag that can
73 * be used locally, but the user should clean
74 * things up after it is done with them.
76 for (parents = commit->parents;
78 parents = parents->next)
79 parents->item->object.flags &= ~TMP_MARK;
81 if (commit_format == CMIT_FMT_ONELINE)
87 static char pretty_header[16384];
88 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), abbrev);
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))
106 return add_object(obj, p, path, name);
109 static struct object_list **process_tree(struct tree *tree,
110 struct object_list **p,
111 struct name_path *path,
114 struct object *obj = &tree->object;
115 struct tree_entry_list *entry;
118 if (!revs.tree_objects)
120 if (obj->flags & (UNINTERESTING | SEEN))
122 if (parse_tree(tree) < 0)
123 die("bad tree object %s", sha1_to_hex(obj->sha1));
125 p = add_object(obj, p, path, name);
128 me.elem_len = strlen(name);
129 entry = tree->entries;
130 tree->entries = NULL;
132 struct tree_entry_list *next = entry->next;
133 if (entry->directory)
134 p = process_tree(entry->item.tree, p, &me, entry->name);
136 p = process_blob(entry->item.blob, p, &me, entry->name);
143 static void show_commit_list(struct rev_info *revs)
145 struct commit *commit;
146 struct object_list *objects = NULL, **p = &objects, *pending;
148 while ((commit = get_revision(revs)) != NULL) {
149 p = process_tree(commit->tree, p, NULL, "");
152 for (pending = revs->pending_objects; pending; pending = pending->next) {
153 struct object *obj = pending->item;
154 const char *name = pending->name;
155 if (obj->flags & (UNINTERESTING | SEEN))
157 if (obj->type == tag_type) {
159 p = add_object(obj, p, NULL, name);
162 if (obj->type == tree_type) {
163 p = process_tree((struct tree *)obj, p, NULL, name);
166 if (obj->type == blob_type) {
167 p = process_blob((struct blob *)obj, p, NULL, name);
170 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
173 /* An object with name "foo\n0000000..." can be used to
174 * confuse downstream git-pack-objects very badly.
176 const char *ep = strchr(objects->name, '\n');
178 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
179 (int) (ep - objects->name),
183 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
184 objects = objects->next;
189 * This is a truly stupid algorithm, but it's only
190 * used for bisection, and we just don't care enough.
192 * We care just barely enough to avoid recursing for
195 static int count_distance(struct commit_list *entry)
200 struct commit *commit = entry->item;
201 struct commit_list *p;
203 if (commit->object.flags & (UNINTERESTING | COUNTED))
205 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
207 commit->object.flags |= COUNTED;
213 nr += count_distance(p);
222 static void clear_distance(struct commit_list *list)
225 struct commit *commit = list->item;
226 commit->object.flags &= ~COUNTED;
231 static struct commit_list *find_bisection(struct commit_list *list)
234 struct commit_list *p, *best;
239 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
246 for (p = list; p; p = p->next) {
249 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
252 distance = count_distance(p);
253 clear_distance(list);
254 if (nr - distance < distance)
255 distance = nr - distance;
256 if (distance > closest) {
266 static void mark_edge_parents_uninteresting(struct commit *commit)
268 struct commit_list *parents;
270 for (parents = commit->parents; parents; parents = parents->next) {
271 struct commit *parent = parents->item;
272 if (!(parent->object.flags & UNINTERESTING))
274 mark_tree_uninteresting(parent->tree);
275 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
276 parent->object.flags |= SHOWN;
277 printf("-%s\n", sha1_to_hex(parent->object.sha1));
282 static void mark_edges_uninteresting(struct commit_list *list)
284 for ( ; list; list = list->next) {
285 struct commit *commit = list->item;
287 if (commit->object.flags & UNINTERESTING) {
288 mark_tree_uninteresting(commit->tree);
291 mark_edge_parents_uninteresting(commit);
295 int main(int argc, const char **argv)
297 struct commit_list *list;
300 argc = setup_revisions(argc, argv, &revs, NULL);
302 for (i = 1 ; i < argc; i++) {
303 const char *arg = argv[i];
305 /* accept -<digit>, like traditilnal "head" */
306 if ((*arg == '-') && isdigit(arg[1])) {
307 revs.max_count = atoi(arg + 1);
310 if (!strcmp(arg, "-n")) {
312 die("-n requires an argument");
313 revs.max_count = atoi(argv[i]);
316 if (!strncmp(arg,"-n",2)) {
317 revs.max_count = atoi(arg + 2);
320 if (!strcmp(arg, "--header")) {
324 if (!strcmp(arg, "--no-abbrev")) {
328 if (!strcmp(arg, "--abbrev")) {
329 abbrev = DEFAULT_ABBREV;
332 if (!strcmp(arg, "--abbrev-commit")) {
336 if (!strncmp(arg, "--abbrev=", 9)) {
337 abbrev = strtoul(arg + 9, NULL, 10);
338 if (abbrev && abbrev < MINIMUM_ABBREV)
339 abbrev = MINIMUM_ABBREV;
340 else if (40 < abbrev)
344 if (!strncmp(arg, "--pretty", 8)) {
345 commit_format = get_commit_format(arg+8);
347 hdr_termination = '\n';
348 if (commit_format == CMIT_FMT_ONELINE)
351 commit_prefix = "commit ";
354 if (!strcmp(arg, "--timestamp")) {
358 if (!strcmp(arg, "--bisect")) {
362 usage(rev_list_usage);
369 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && !revs.pending_objects))
370 usage(rev_list_usage);
372 save_commit_buffer = verbose_header;
373 track_object_refs = 0;
377 prepare_revision_walk(&revs);
378 if (revs.tree_objects)
379 mark_edges_uninteresting(revs.commits);
382 revs.commits = find_bisection(revs.commits);
384 show_commit_list(&revs);