Merge branch 'jc/web'
[git/debian.git] / builtin-rev-list.c
blobfb7fc92145b6a8baec340176abf40c6b37f45a8e
1 #include "cache.h"
2 #include "refs.h"
3 #include "tag.h"
4 #include "commit.h"
5 #include "tree.h"
6 #include "blob.h"
7 #include "tree-walk.h"
8 #include "diff.h"
9 #include "revision.h"
10 #include "list-objects.h"
11 #include "builtin.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"
19 " limiting output:\n"
20 " --max-count=nr\n"
21 " --max-age=epoch\n"
22 " --min-age=epoch\n"
23 " --sparse\n"
24 " --no-merges\n"
25 " --remove-empty\n"
26 " --all\n"
27 " --stdin\n"
28 " ordering output:\n"
29 " --topo-order\n"
30 " --date-order\n"
31 " formatting output:\n"
32 " --parents\n"
33 " --objects | --objects-edge\n"
34 " --unpacked\n"
35 " --header | --pretty\n"
36 " --abbrev=nr | --no-abbrev\n"
37 " --abbrev-commit\n"
38 " special purpose:\n"
39 " --bisect"
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)
51 if (show_timestamp)
52 printf("%lu ", commit->date);
53 if (header_prefix)
54 fputs(header_prefix, stdout);
55 if (commit->object.flags & BOUNDARY)
56 putchar('-');
57 if (revs.abbrev_commit && revs.abbrev)
58 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
59 stdout);
60 else
61 fputs(sha1_to_hex(commit->object.sha1), stdout);
62 if (revs.parents) {
63 struct commit_list *parents = commit->parents;
64 while (parents) {
65 struct object *o = &(parents->item->object);
66 parents = parents->next;
67 if (o->flags & TMP_MARK)
68 continue;
69 printf(" %s", sha1_to_hex(o->sha1));
70 o->flags |= TMP_MARK;
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;
77 parents;
78 parents = parents->next)
79 parents->item->object.flags &= ~TMP_MARK;
81 if (revs.commit_format == CMIT_FMT_ONELINE)
82 putchar(' ');
83 else
84 putchar('\n');
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);
93 fflush(stdout);
94 if (commit->parents) {
95 free_commit_list(commit->parents);
96 commit->parents = NULL;
98 free(commit->buffer);
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');
108 if (ep) {
109 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
110 (int) (ep - p->name),
111 p->name);
113 else
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
127 * non-merge entries.
129 static int count_distance(struct commit_list *entry)
131 int nr = 0;
133 while (entry) {
134 struct commit *commit = entry->item;
135 struct commit_list *p;
137 if (commit->object.flags & (UNINTERESTING | COUNTED))
138 break;
139 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
140 nr++;
141 commit->object.flags |= COUNTED;
142 p = commit->parents;
143 entry = p;
144 if (p) {
145 p = p->next;
146 while (p) {
147 nr += count_distance(p);
148 p = p->next;
153 return nr;
156 static void clear_distance(struct commit_list *list)
158 while (list) {
159 struct commit *commit = list->item;
160 commit->object.flags &= ~COUNTED;
161 list = list->next;
165 static struct commit_list *find_bisection(struct commit_list *list)
167 int nr, closest;
168 struct commit_list *p, *best;
170 nr = 0;
171 p = list;
172 while (p) {
173 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
174 nr++;
175 p = p->next;
177 closest = 0;
178 best = list;
180 for (p = list; p; p = p->next) {
181 int distance;
183 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
184 continue;
186 distance = count_distance(p);
187 clear_distance(list);
188 if (nr - distance < distance)
189 distance = nr - distance;
190 if (distance > closest) {
191 best = p;
192 closest = distance;
195 if (best)
196 best->next = NULL;
197 return best;
200 static void read_revisions_from_stdin(struct rev_info *revs)
202 char line[1000];
204 while (fgets(line, sizeof(line), stdin) != NULL) {
205 int len = strlen(line);
206 if (line[len - 1] == '\n')
207 line[--len] = 0;
208 if (!len)
209 break;
210 if (line[0] == '-')
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;
220 int i;
221 int read_from_stdin = 0;
223 init_revisions(&revs, prefix);
224 revs.abbrev = 0;
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;
233 continue;
235 if (!strcmp(arg, "--timestamp")) {
236 show_timestamp = 1;
237 continue;
239 if (!strcmp(arg, "--bisect")) {
240 bisect_list = 1;
241 continue;
243 if (!strcmp(arg, "--stdin")) {
244 if (read_from_stdin++)
245 die("--stdin given twice?");
246 read_revisions_from_stdin(&revs);
247 continue;
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)
256 header_prefix = "";
257 else
258 header_prefix = "commit ";
260 else if (revs.verbose_header)
261 /* Only --header was specified */
262 revs.commit_format = CMIT_FMT_RAW;
264 list = revs.commits;
266 if ((!list &&
267 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
268 !revs.pending.nr)) ||
269 revs.diff)
270 usage(rev_list_usage);
272 save_commit_buffer = revs.verbose_header || revs.grep_filter;
273 track_object_refs = 0;
274 if (bisect_list)
275 revs.limited = 1;
277 prepare_revision_walk(&revs);
278 if (revs.tree_objects)
279 mark_edges_uninteresting(revs.commits, &revs, show_edge);
281 if (bisect_list)
282 revs.commits = find_bisection(revs.commits);
284 traverse_commit_list(&revs, show_commit, show_object);
286 return 0;