10 /* bits #0-4 in revision.h */
12 #define COUNTED (1u<<5)
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_parents = 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)
49 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
51 struct commit_list *parents = commit->parents;
53 struct object *o = &(parents->item->object);
54 parents = parents->next;
55 if (o->flags & TMP_MARK)
57 printf(" %s", sha1_to_hex(o->sha1));
60 /* TMP_MARK is a general purpose flag that can
61 * be used locally, but the user should clean
62 * things up after it is done with them.
64 for (parents = commit->parents;
66 parents = parents->next)
67 parents->item->object.flags &= ~TMP_MARK;
69 if (commit_format == CMIT_FMT_ONELINE)
75 static char pretty_header[16384];
76 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), abbrev);
77 printf("%s%c", pretty_header, hdr_termination);
82 static struct object_list **process_blob(struct blob *blob,
83 struct object_list **p,
84 struct name_path *path,
87 struct object *obj = &blob->object;
89 if (!revs.blob_objects)
91 if (obj->flags & (UNINTERESTING | SEEN))
94 return add_object(obj, p, path, name);
97 static struct object_list **process_tree(struct tree *tree,
98 struct object_list **p,
99 struct name_path *path,
102 struct object *obj = &tree->object;
103 struct tree_entry_list *entry;
106 if (!revs.tree_objects)
108 if (obj->flags & (UNINTERESTING | SEEN))
110 if (parse_tree(tree) < 0)
111 die("bad tree object %s", sha1_to_hex(obj->sha1));
113 p = add_object(obj, p, path, name);
116 me.elem_len = strlen(name);
117 entry = tree->entries;
118 tree->entries = NULL;
120 struct tree_entry_list *next = entry->next;
121 if (entry->directory)
122 p = process_tree(entry->item.tree, p, &me, entry->name);
124 p = process_blob(entry->item.blob, p, &me, entry->name);
131 static void show_commit_list(struct rev_info *revs)
133 struct commit *commit;
134 struct object_list *objects = NULL, **p = &objects, *pending;
136 while ((commit = get_revision(revs)) != NULL) {
137 p = process_tree(commit->tree, p, NULL, "");
140 for (pending = revs->pending_objects; pending; pending = pending->next) {
141 struct object *obj = pending->item;
142 const char *name = pending->name;
143 if (obj->flags & (UNINTERESTING | SEEN))
145 if (obj->type == tag_type) {
147 p = add_object(obj, p, NULL, name);
150 if (obj->type == tree_type) {
151 p = process_tree((struct tree *)obj, p, NULL, name);
154 if (obj->type == blob_type) {
155 p = process_blob((struct blob *)obj, p, NULL, name);
158 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
161 /* An object with name "foo\n0000000..." can be used to
162 * confuse downstream git-pack-objects very badly.
164 const char *ep = strchr(objects->name, '\n');
166 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
167 (int) (ep - objects->name),
171 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
172 objects = objects->next;
177 * This is a truly stupid algorithm, but it's only
178 * used for bisection, and we just don't care enough.
180 * We care just barely enough to avoid recursing for
183 static int count_distance(struct commit_list *entry)
188 struct commit *commit = entry->item;
189 struct commit_list *p;
191 if (commit->object.flags & (UNINTERESTING | COUNTED))
193 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
195 commit->object.flags |= COUNTED;
201 nr += count_distance(p);
210 static void clear_distance(struct commit_list *list)
213 struct commit *commit = list->item;
214 commit->object.flags &= ~COUNTED;
219 static struct commit_list *find_bisection(struct commit_list *list)
222 struct commit_list *p, *best;
227 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
234 for (p = list; p; p = p->next) {
237 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
240 distance = count_distance(p);
241 clear_distance(list);
242 if (nr - distance < distance)
243 distance = nr - distance;
244 if (distance > closest) {
254 static void mark_edge_parents_uninteresting(struct commit *commit)
256 struct commit_list *parents;
258 for (parents = commit->parents; parents; parents = parents->next) {
259 struct commit *parent = parents->item;
260 if (!(parent->object.flags & UNINTERESTING))
262 mark_tree_uninteresting(parent->tree);
263 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
264 parent->object.flags |= SHOWN;
265 printf("-%s\n", sha1_to_hex(parent->object.sha1));
270 static void mark_edges_uninteresting(struct commit_list *list)
272 for ( ; list; list = list->next) {
273 struct commit *commit = list->item;
275 if (commit->object.flags & UNINTERESTING) {
276 mark_tree_uninteresting(commit->tree);
279 mark_edge_parents_uninteresting(commit);
283 int main(int argc, const char **argv)
285 struct commit_list *list;
288 argc = setup_revisions(argc, argv, &revs, NULL);
290 for (i = 1 ; i < argc; i++) {
291 const char *arg = argv[i];
293 /* accept -<digit>, like traditilnal "head" */
294 if ((*arg == '-') && isdigit(arg[1])) {
295 revs.max_count = atoi(arg + 1);
298 if (!strcmp(arg, "-n")) {
300 die("-n requires an argument");
301 revs.max_count = atoi(argv[i]);
304 if (!strncmp(arg,"-n",2)) {
305 revs.max_count = atoi(arg + 2);
308 if (!strcmp(arg, "--header")) {
312 if (!strcmp(arg, "--no-abbrev")) {
316 if (!strncmp(arg, "--abbrev=", 9)) {
317 abbrev = strtoul(arg + 9, NULL, 10);
318 if (abbrev && abbrev < MINIMUM_ABBREV)
319 abbrev = MINIMUM_ABBREV;
320 else if (40 < abbrev)
324 if (!strncmp(arg, "--pretty", 8)) {
325 commit_format = get_commit_format(arg+8);
327 hdr_termination = '\n';
328 if (commit_format == CMIT_FMT_ONELINE)
331 commit_prefix = "commit ";
334 if (!strcmp(arg, "--parents")) {
338 if (!strcmp(arg, "--bisect")) {
342 usage(rev_list_usage);
349 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && !revs.pending_objects))
350 usage(rev_list_usage);
352 prepare_revision_walk(&revs);
353 if (revs.tree_objects)
354 mark_edges_uninteresting(revs.commits);
357 revs.commits = find_bisection(revs.commits);
359 save_commit_buffer = verbose_header;
360 track_object_refs = 0;
362 show_commit_list(&revs);