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_parents = 0;
43 static int show_timestamp = 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)
51 printf("%lu ", commit->date);
53 fputs(commit_prefix, stdout);
54 if (commit->object.flags & BOUNDARY)
56 fputs(sha1_to_hex(commit->object.sha1), stdout);
58 struct commit_list *parents = commit->parents;
60 struct object *o = &(parents->item->object);
61 parents = parents->next;
62 if (o->flags & TMP_MARK)
64 printf(" %s", sha1_to_hex(o->sha1));
67 /* TMP_MARK is a general purpose flag that can
68 * be used locally, but the user should clean
69 * things up after it is done with them.
71 for (parents = commit->parents;
73 parents = parents->next)
74 parents->item->object.flags &= ~TMP_MARK;
76 if (commit_format == CMIT_FMT_ONELINE)
82 static char pretty_header[16384];
83 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), abbrev);
84 printf("%s%c", pretty_header, hdr_termination);
89 static struct object_list **process_blob(struct blob *blob,
90 struct object_list **p,
91 struct name_path *path,
94 struct object *obj = &blob->object;
96 if (!revs.blob_objects)
98 if (obj->flags & (UNINTERESTING | SEEN))
101 return add_object(obj, p, path, name);
104 static struct object_list **process_tree(struct tree *tree,
105 struct object_list **p,
106 struct name_path *path,
109 struct object *obj = &tree->object;
110 struct tree_entry_list *entry;
113 if (!revs.tree_objects)
115 if (obj->flags & (UNINTERESTING | SEEN))
117 if (parse_tree(tree) < 0)
118 die("bad tree object %s", sha1_to_hex(obj->sha1));
120 p = add_object(obj, p, path, name);
123 me.elem_len = strlen(name);
124 entry = tree->entries;
125 tree->entries = NULL;
127 struct tree_entry_list *next = entry->next;
128 if (entry->directory)
129 p = process_tree(entry->item.tree, p, &me, entry->name);
131 p = process_blob(entry->item.blob, p, &me, entry->name);
138 static void show_commit_list(struct rev_info *revs)
140 struct commit *commit;
141 struct object_list *objects = NULL, **p = &objects, *pending;
143 while ((commit = get_revision(revs)) != NULL) {
144 p = process_tree(commit->tree, p, NULL, "");
147 for (pending = revs->pending_objects; pending; pending = pending->next) {
148 struct object *obj = pending->item;
149 const char *name = pending->name;
150 if (obj->flags & (UNINTERESTING | SEEN))
152 if (obj->type == tag_type) {
154 p = add_object(obj, p, NULL, name);
157 if (obj->type == tree_type) {
158 p = process_tree((struct tree *)obj, p, NULL, name);
161 if (obj->type == blob_type) {
162 p = process_blob((struct blob *)obj, p, NULL, name);
165 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
168 /* An object with name "foo\n0000000..." can be used to
169 * confuse downstream git-pack-objects very badly.
171 const char *ep = strchr(objects->name, '\n');
173 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
174 (int) (ep - objects->name),
178 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
179 objects = objects->next;
184 * This is a truly stupid algorithm, but it's only
185 * used for bisection, and we just don't care enough.
187 * We care just barely enough to avoid recursing for
190 static int count_distance(struct commit_list *entry)
195 struct commit *commit = entry->item;
196 struct commit_list *p;
198 if (commit->object.flags & (UNINTERESTING | COUNTED))
200 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
202 commit->object.flags |= COUNTED;
208 nr += count_distance(p);
217 static void clear_distance(struct commit_list *list)
220 struct commit *commit = list->item;
221 commit->object.flags &= ~COUNTED;
226 static struct commit_list *find_bisection(struct commit_list *list)
229 struct commit_list *p, *best;
234 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
241 for (p = list; p; p = p->next) {
244 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
247 distance = count_distance(p);
248 clear_distance(list);
249 if (nr - distance < distance)
250 distance = nr - distance;
251 if (distance > closest) {
261 static void mark_edge_parents_uninteresting(struct commit *commit)
263 struct commit_list *parents;
265 for (parents = commit->parents; parents; parents = parents->next) {
266 struct commit *parent = parents->item;
267 if (!(parent->object.flags & UNINTERESTING))
269 mark_tree_uninteresting(parent->tree);
270 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
271 parent->object.flags |= SHOWN;
272 printf("-%s\n", sha1_to_hex(parent->object.sha1));
277 static void mark_edges_uninteresting(struct commit_list *list)
279 for ( ; list; list = list->next) {
280 struct commit *commit = list->item;
282 if (commit->object.flags & UNINTERESTING) {
283 mark_tree_uninteresting(commit->tree);
286 mark_edge_parents_uninteresting(commit);
290 int main(int argc, const char **argv)
292 struct commit_list *list;
295 argc = setup_revisions(argc, argv, &revs, NULL);
297 for (i = 1 ; i < argc; i++) {
298 const char *arg = argv[i];
300 /* accept -<digit>, like traditilnal "head" */
301 if ((*arg == '-') && isdigit(arg[1])) {
302 revs.max_count = atoi(arg + 1);
305 if (!strcmp(arg, "-n")) {
307 die("-n requires an argument");
308 revs.max_count = atoi(argv[i]);
311 if (!strncmp(arg,"-n",2)) {
312 revs.max_count = atoi(arg + 2);
315 if (!strcmp(arg, "--header")) {
319 if (!strcmp(arg, "--no-abbrev")) {
323 if (!strncmp(arg, "--abbrev=", 9)) {
324 abbrev = strtoul(arg + 9, NULL, 10);
325 if (abbrev && abbrev < MINIMUM_ABBREV)
326 abbrev = MINIMUM_ABBREV;
327 else if (40 < abbrev)
331 if (!strncmp(arg, "--pretty", 8)) {
332 commit_format = get_commit_format(arg+8);
334 hdr_termination = '\n';
335 if (commit_format == CMIT_FMT_ONELINE)
338 commit_prefix = "commit ";
341 if (!strcmp(arg, "--parents")) {
345 if (!strcmp(arg, "--timestamp")) {
349 if (!strcmp(arg, "--bisect")) {
353 usage(rev_list_usage);
360 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && !revs.pending_objects))
361 usage(rev_list_usage);
363 save_commit_buffer = verbose_header;
364 track_object_refs = 0;
366 prepare_revision_walk(&revs);
367 if (revs.tree_objects)
368 mark_edges_uninteresting(revs.commits);
371 revs.commits = find_bisection(revs.commits);
373 show_commit_list(&revs);