10 #include "list-objects.h"
13 /* bits #0-15 in revision.h */
15 #define COUNTED (1u<<16)
17 static const char rev_list_usage[] =
18 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
31 " formatting output:\n"
33 " --objects | --objects-edge\n"
35 " --header | --pretty\n"
36 " --abbrev=nr | --no-abbrev\n"
42 static struct rev_info revs;
44 static int bisect_list;
45 static int show_timestamp;
46 static int hdr_termination;
47 static const char *header_prefix;
49 static void show_commit(struct commit *commit)
52 printf("%lu ", commit->date);
54 fputs(header_prefix, stdout);
55 if (commit->object.flags & BOUNDARY)
57 if (revs.abbrev_commit && revs.abbrev)
58 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
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 (revs.commit_format == CMIT_FMT_ONELINE)
86 if (revs.verbose_header) {
87 static char pretty_header[16384];
88 pretty_print_commit(revs.commit_format, commit, ~0,
89 pretty_header, sizeof(pretty_header),
90 revs.abbrev, NULL, NULL, revs.relative_date);
91 printf("%s%c", pretty_header, hdr_termination);
94 if (commit->parents) {
95 free_commit_list(commit->parents);
96 commit->parents = NULL;
99 commit->buffer = NULL;
102 static void show_object(struct object_array_entry *p)
104 /* An object with name "foo\n0000000..." can be used to
105 * confuse downstream git-pack-objects very badly.
107 const char *ep = strchr(p->name, '\n');
109 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
110 (int) (ep - p->name),
114 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
117 static void show_edge(struct commit *commit)
119 printf("-%s\n", sha1_to_hex(commit->object.sha1));
123 * This is a truly stupid algorithm, but it's only
124 * used for bisection, and we just don't care enough.
126 * We care just barely enough to avoid recursing for
129 static int count_distance(struct commit_list *entry)
134 struct commit *commit = entry->item;
135 struct commit_list *p;
137 if (commit->object.flags & (UNINTERESTING | COUNTED))
139 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
141 commit->object.flags |= COUNTED;
147 nr += count_distance(p);
156 static void clear_distance(struct commit_list *list)
159 struct commit *commit = list->item;
160 commit->object.flags &= ~COUNTED;
165 static struct commit_list *find_bisection(struct commit_list *list)
168 struct commit_list *p, *best;
173 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
180 for (p = list; p; p = p->next) {
183 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
186 distance = count_distance(p);
187 clear_distance(list);
188 if (nr - distance < distance)
189 distance = nr - distance;
190 if (distance > closest) {
200 static void read_revisions_from_stdin(struct rev_info *revs)
204 while (fgets(line, sizeof(line), stdin) != NULL) {
205 int len = strlen(line);
206 if (line[len - 1] == '\n')
211 die("options not supported in --stdin mode");
212 if (handle_revision_arg(line, revs, 0, 1))
213 die("bad revision '%s'", line);
217 int cmd_rev_list(int argc, const char **argv, const char *prefix)
219 struct commit_list *list;
221 int read_from_stdin = 0;
223 init_revisions(&revs, prefix);
225 revs.commit_format = CMIT_FMT_UNSPECIFIED;
226 argc = setup_revisions(argc, argv, &revs, NULL);
228 for (i = 1 ; i < argc; i++) {
229 const char *arg = argv[i];
231 if (!strcmp(arg, "--header")) {
232 revs.verbose_header = 1;
235 if (!strcmp(arg, "--timestamp")) {
239 if (!strcmp(arg, "--bisect")) {
243 if (!strcmp(arg, "--stdin")) {
244 if (read_from_stdin++)
245 die("--stdin given twice?");
246 read_revisions_from_stdin(&revs);
249 usage(rev_list_usage);
252 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
253 /* The command line has a --pretty */
254 hdr_termination = '\n';
255 if (revs.commit_format == CMIT_FMT_ONELINE)
258 header_prefix = "commit ";
260 else if (revs.verbose_header)
261 /* Only --header was specified */
262 revs.commit_format = CMIT_FMT_RAW;
267 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
268 !revs.pending.nr)) ||
270 usage(rev_list_usage);
272 save_commit_buffer = revs.verbose_header || revs.grep_filter;
273 track_object_refs = 0;
277 prepare_revision_walk(&revs);
278 if (revs.tree_objects)
279 mark_edges_uninteresting(revs.commits, &revs, show_edge);
282 revs.commits = find_bisection(revs.commits);
284 traverse_commit_list(&revs, show_commit, show_object);