10 /* bits #0-3 in revision.h */
12 #define COUNTED (1u << 4)
13 #define TMP_MARK (1u << 5) /* for isolated cases; clean after use */
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"
40 static int bisect_list = 0;
41 static int verbose_header = 0;
42 static int abbrev = DEFAULT_ABBREV;
43 static int show_parents = 0;
44 static int hdr_termination = 0;
45 static const char *commit_prefix = "";
46 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
48 static void show_commit(struct commit *commit)
50 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
52 struct commit_list *parents = commit->parents;
54 struct object *o = &(parents->item->object);
55 parents = parents->next;
56 if (o->flags & TMP_MARK)
58 printf(" %s", sha1_to_hex(o->sha1));
61 /* TMP_MARK is a general purpose flag that can
62 * be used locally, but the user should clean
63 * things up after it is done with them.
65 for (parents = commit->parents;
67 parents = parents->next)
68 parents->item->object.flags &= ~TMP_MARK;
70 if (commit_format == CMIT_FMT_ONELINE)
76 static char pretty_header[16384];
77 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), abbrev);
78 printf("%s%c", pretty_header, hdr_termination);
83 static struct object_list **process_blob(struct blob *blob,
84 struct object_list **p,
85 struct name_path *path,
88 struct object *obj = &blob->object;
90 if (!revs.blob_objects)
92 if (obj->flags & (UNINTERESTING | SEEN))
95 return add_object(obj, p, path, name);
98 static struct object_list **process_tree(struct tree *tree,
99 struct object_list **p,
100 struct name_path *path,
103 struct object *obj = &tree->object;
104 struct tree_entry_list *entry;
107 if (!revs.tree_objects)
109 if (obj->flags & (UNINTERESTING | SEEN))
111 if (parse_tree(tree) < 0)
112 die("bad tree object %s", sha1_to_hex(obj->sha1));
114 p = add_object(obj, p, path, name);
117 me.elem_len = strlen(name);
118 entry = tree->entries;
119 tree->entries = NULL;
121 struct tree_entry_list *next = entry->next;
122 if (entry->directory)
123 p = process_tree(entry->item.tree, p, &me, entry->name);
125 p = process_blob(entry->item.blob, p, &me, entry->name);
132 static void show_commit_list(struct rev_info *revs)
134 struct commit *commit;
135 struct object_list *objects = NULL, **p = &objects, *pending;
137 while ((commit = get_revision(revs)) != NULL) {
138 p = process_tree(commit->tree, p, NULL, "");
141 for (pending = revs->pending_objects; pending; pending = pending->next) {
142 struct object *obj = pending->item;
143 const char *name = pending->name;
144 if (obj->flags & (UNINTERESTING | SEEN))
146 if (obj->type == tag_type) {
148 p = add_object(obj, p, NULL, name);
151 if (obj->type == tree_type) {
152 p = process_tree((struct tree *)obj, p, NULL, name);
155 if (obj->type == blob_type) {
156 p = process_blob((struct blob *)obj, p, NULL, name);
159 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
162 /* An object with name "foo\n0000000..." can be used to
163 * confuse downstream git-pack-objects very badly.
165 const char *ep = strchr(objects->name, '\n');
167 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
168 (int) (ep - objects->name),
172 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
173 objects = objects->next;
178 * This is a truly stupid algorithm, but it's only
179 * used for bisection, and we just don't care enough.
181 * We care just barely enough to avoid recursing for
184 static int count_distance(struct commit_list *entry)
189 struct commit *commit = entry->item;
190 struct commit_list *p;
192 if (commit->object.flags & (UNINTERESTING | COUNTED))
194 if (!revs.paths || (commit->object.flags & TREECHANGE))
196 commit->object.flags |= COUNTED;
202 nr += count_distance(p);
211 static void clear_distance(struct commit_list *list)
214 struct commit *commit = list->item;
215 commit->object.flags &= ~COUNTED;
220 static struct commit_list *find_bisection(struct commit_list *list)
223 struct commit_list *p, *best;
228 if (!revs.paths || (p->item->object.flags & TREECHANGE))
235 for (p = list; p; p = p->next) {
238 if (revs.paths && !(p->item->object.flags & TREECHANGE))
241 distance = count_distance(p);
242 clear_distance(list);
243 if (nr - distance < distance)
244 distance = nr - distance;
245 if (distance > closest) {
255 static void mark_edge_parents_uninteresting(struct commit *commit)
257 struct commit_list *parents;
259 for (parents = commit->parents; parents; parents = parents->next) {
260 struct commit *parent = parents->item;
261 if (!(parent->object.flags & UNINTERESTING))
263 mark_tree_uninteresting(parent->tree);
264 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
265 parent->object.flags |= SHOWN;
266 printf("-%s\n", sha1_to_hex(parent->object.sha1));
271 static void mark_edges_uninteresting(struct commit_list *list)
273 for ( ; list; list = list->next) {
274 struct commit *commit = list->item;
276 if (commit->object.flags & UNINTERESTING) {
277 mark_tree_uninteresting(commit->tree);
280 mark_edge_parents_uninteresting(commit);
284 int main(int argc, const char **argv)
286 struct commit_list *list;
289 argc = setup_revisions(argc, argv, &revs, NULL);
291 for (i = 1 ; i < argc; i++) {
292 const char *arg = argv[i];
294 /* accept -<digit>, like traditilnal "head" */
295 if ((*arg == '-') && isdigit(arg[1])) {
296 revs.max_count = atoi(arg + 1);
299 if (!strcmp(arg, "-n")) {
301 die("-n requires an argument");
302 revs.max_count = atoi(argv[i]);
305 if (!strncmp(arg,"-n",2)) {
306 revs.max_count = atoi(arg + 2);
309 if (!strcmp(arg, "--header")) {
313 if (!strcmp(arg, "--no-abbrev")) {
317 if (!strncmp(arg, "--abbrev=", 9)) {
318 abbrev = strtoul(arg + 9, NULL, 10);
319 if (abbrev && abbrev < MINIMUM_ABBREV)
320 abbrev = MINIMUM_ABBREV;
321 else if (40 < abbrev)
325 if (!strncmp(arg, "--pretty", 8)) {
326 commit_format = get_commit_format(arg+8);
328 hdr_termination = '\n';
329 if (commit_format == CMIT_FMT_ONELINE)
332 commit_prefix = "commit ";
335 if (!strcmp(arg, "--parents")) {
339 if (!strcmp(arg, "--bisect")) {
343 usage(rev_list_usage);
350 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && !revs.pending_objects))
351 usage(rev_list_usage);
353 prepare_revision_walk(&revs);
354 if (revs.tree_objects)
355 mark_edges_uninteresting(revs.commits);
358 revs.commits = find_bisection(revs.commits);
360 save_commit_buffer = verbose_header;
361 track_object_refs = 0;
363 show_commit_list(&revs);