Merge branch 'jc/thinpack'
[git/trast.git] / rev-list.c
blob13015026276029bc213972cae234981ee14b06b7
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 "revision.h"
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"
16 " limiting output:\n"
17 " --max-count=nr\n"
18 " --max-age=epoch\n"
19 " --min-age=epoch\n"
20 " --sparse\n"
21 " --no-merges\n"
22 " --remove-empty\n"
23 " --all\n"
24 " ordering output:\n"
25 " --topo-order\n"
26 " --date-order\n"
27 " formatting output:\n"
28 " --parents\n"
29 " --objects | --objects-edge\n"
30 " --unpacked\n"
31 " --header | --pretty\n"
32 " --abbrev=nr | --no-abbrev\n"
33 " --abbrev-commit\n"
34 " special purpose:\n"
35 " --bisect"
38 struct rev_info revs;
40 static int bisect_list = 0;
41 static int verbose_header = 0;
42 static int abbrev = DEFAULT_ABBREV;
43 static int abbrev_commit = 0;
44 static int show_timestamp = 0;
45 static int hdr_termination = 0;
46 static const char *commit_prefix = "";
47 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
49 static void show_commit(struct commit *commit)
51 if (show_timestamp)
52 printf("%lu ", commit->date);
53 if (commit_prefix[0])
54 fputs(commit_prefix, stdout);
55 if (commit->object.flags & BOUNDARY)
56 putchar('-');
57 if (abbrev_commit && abbrev)
58 fputs(find_unique_abbrev(commit->object.sha1, abbrev), stdout);
59 else
60 fputs(sha1_to_hex(commit->object.sha1), stdout);
61 if (revs.parents) {
62 struct commit_list *parents = commit->parents;
63 while (parents) {
64 struct object *o = &(parents->item->object);
65 parents = parents->next;
66 if (o->flags & TMP_MARK)
67 continue;
68 printf(" %s", sha1_to_hex(o->sha1));
69 o->flags |= TMP_MARK;
71 /* TMP_MARK is a general purpose flag that can
72 * be used locally, but the user should clean
73 * things up after it is done with them.
75 for (parents = commit->parents;
76 parents;
77 parents = parents->next)
78 parents->item->object.flags &= ~TMP_MARK;
80 if (commit_format == CMIT_FMT_ONELINE)
81 putchar(' ');
82 else
83 putchar('\n');
85 if (verbose_header) {
86 static char pretty_header[16384];
87 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), abbrev);
88 printf("%s%c", pretty_header, hdr_termination);
90 fflush(stdout);
93 static struct object_list **process_blob(struct blob *blob,
94 struct object_list **p,
95 struct name_path *path,
96 const char *name)
98 struct object *obj = &blob->object;
100 if (!revs.blob_objects)
101 return p;
102 if (obj->flags & (UNINTERESTING | SEEN))
103 return p;
104 obj->flags |= SEEN;
105 return add_object(obj, p, path, name);
108 static struct object_list **process_tree(struct tree *tree,
109 struct object_list **p,
110 struct name_path *path,
111 const char *name)
113 struct object *obj = &tree->object;
114 struct tree_entry_list *entry;
115 struct name_path me;
117 if (!revs.tree_objects)
118 return p;
119 if (obj->flags & (UNINTERESTING | SEEN))
120 return p;
121 if (parse_tree(tree) < 0)
122 die("bad tree object %s", sha1_to_hex(obj->sha1));
123 obj->flags |= SEEN;
124 p = add_object(obj, p, path, name);
125 me.up = path;
126 me.elem = name;
127 me.elem_len = strlen(name);
128 entry = tree->entries;
129 tree->entries = NULL;
130 while (entry) {
131 struct tree_entry_list *next = entry->next;
132 if (entry->directory)
133 p = process_tree(entry->item.tree, p, &me, entry->name);
134 else
135 p = process_blob(entry->item.blob, p, &me, entry->name);
136 free(entry);
137 entry = next;
139 return p;
142 static void show_commit_list(struct rev_info *revs)
144 struct commit *commit;
145 struct object_list *objects = NULL, **p = &objects, *pending;
147 while ((commit = get_revision(revs)) != NULL) {
148 p = process_tree(commit->tree, p, NULL, "");
149 show_commit(commit);
151 for (pending = revs->pending_objects; pending; pending = pending->next) {
152 struct object *obj = pending->item;
153 const char *name = pending->name;
154 if (obj->flags & (UNINTERESTING | SEEN))
155 continue;
156 if (obj->type == tag_type) {
157 obj->flags |= SEEN;
158 p = add_object(obj, p, NULL, name);
159 continue;
161 if (obj->type == tree_type) {
162 p = process_tree((struct tree *)obj, p, NULL, name);
163 continue;
165 if (obj->type == blob_type) {
166 p = process_blob((struct blob *)obj, p, NULL, name);
167 continue;
169 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
171 while (objects) {
172 /* An object with name "foo\n0000000..." can be used to
173 * confuse downstream git-pack-objects very badly.
175 const char *ep = strchr(objects->name, '\n');
176 if (ep) {
177 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
178 (int) (ep - objects->name),
179 objects->name);
181 else
182 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
183 objects = objects->next;
188 * This is a truly stupid algorithm, but it's only
189 * used for bisection, and we just don't care enough.
191 * We care just barely enough to avoid recursing for
192 * non-merge entries.
194 static int count_distance(struct commit_list *entry)
196 int nr = 0;
198 while (entry) {
199 struct commit *commit = entry->item;
200 struct commit_list *p;
202 if (commit->object.flags & (UNINTERESTING | COUNTED))
203 break;
204 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
205 nr++;
206 commit->object.flags |= COUNTED;
207 p = commit->parents;
208 entry = p;
209 if (p) {
210 p = p->next;
211 while (p) {
212 nr += count_distance(p);
213 p = p->next;
218 return nr;
221 static void clear_distance(struct commit_list *list)
223 while (list) {
224 struct commit *commit = list->item;
225 commit->object.flags &= ~COUNTED;
226 list = list->next;
230 static struct commit_list *find_bisection(struct commit_list *list)
232 int nr, closest;
233 struct commit_list *p, *best;
235 nr = 0;
236 p = list;
237 while (p) {
238 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
239 nr++;
240 p = p->next;
242 closest = 0;
243 best = list;
245 for (p = list; p; p = p->next) {
246 int distance;
248 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
249 continue;
251 distance = count_distance(p);
252 clear_distance(list);
253 if (nr - distance < distance)
254 distance = nr - distance;
255 if (distance > closest) {
256 best = p;
257 closest = distance;
260 if (best)
261 best->next = NULL;
262 return best;
265 static void mark_edge_parents_uninteresting(struct commit *commit)
267 struct commit_list *parents;
269 for (parents = commit->parents; parents; parents = parents->next) {
270 struct commit *parent = parents->item;
271 if (!(parent->object.flags & UNINTERESTING))
272 continue;
273 mark_tree_uninteresting(parent->tree);
274 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
275 parent->object.flags |= SHOWN;
276 printf("-%s\n", sha1_to_hex(parent->object.sha1));
281 static void mark_edges_uninteresting(struct commit_list *list)
283 for ( ; list; list = list->next) {
284 struct commit *commit = list->item;
286 if (commit->object.flags & UNINTERESTING) {
287 mark_tree_uninteresting(commit->tree);
288 continue;
290 mark_edge_parents_uninteresting(commit);
294 int main(int argc, const char **argv)
296 struct commit_list *list;
297 int i;
299 argc = setup_revisions(argc, argv, &revs, NULL);
301 for (i = 1 ; i < argc; i++) {
302 const char *arg = argv[i];
304 /* accept -<digit>, like traditilnal "head" */
305 if ((*arg == '-') && isdigit(arg[1])) {
306 revs.max_count = atoi(arg + 1);
307 continue;
309 if (!strcmp(arg, "-n")) {
310 if (++i >= argc)
311 die("-n requires an argument");
312 revs.max_count = atoi(argv[i]);
313 continue;
315 if (!strncmp(arg,"-n",2)) {
316 revs.max_count = atoi(arg + 2);
317 continue;
319 if (!strcmp(arg, "--header")) {
320 verbose_header = 1;
321 continue;
323 if (!strcmp(arg, "--no-abbrev")) {
324 abbrev = 0;
325 continue;
327 if (!strcmp(arg, "--abbrev")) {
328 abbrev = DEFAULT_ABBREV;
329 continue;
331 if (!strcmp(arg, "--abbrev-commit")) {
332 abbrev_commit = 1;
333 continue;
335 if (!strncmp(arg, "--abbrev=", 9)) {
336 abbrev = strtoul(arg + 9, NULL, 10);
337 if (abbrev && abbrev < MINIMUM_ABBREV)
338 abbrev = MINIMUM_ABBREV;
339 else if (40 < abbrev)
340 abbrev = 40;
341 continue;
343 if (!strncmp(arg, "--pretty", 8)) {
344 commit_format = get_commit_format(arg+8);
345 verbose_header = 1;
346 hdr_termination = '\n';
347 if (commit_format == CMIT_FMT_ONELINE)
348 commit_prefix = "";
349 else
350 commit_prefix = "commit ";
351 continue;
353 if (!strcmp(arg, "--timestamp")) {
354 show_timestamp = 1;
355 continue;
357 if (!strcmp(arg, "--bisect")) {
358 bisect_list = 1;
359 continue;
361 usage(rev_list_usage);
365 list = revs.commits;
367 if (!list &&
368 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && !revs.pending_objects))
369 usage(rev_list_usage);
371 save_commit_buffer = verbose_header;
372 track_object_refs = 0;
374 prepare_revision_walk(&revs);
375 if (revs.tree_objects)
376 mark_edges_uninteresting(revs.commits);
378 if (bisect_list)
379 revs.commits = find_bisection(revs.commits);
381 show_commit_list(&revs);
383 return 0;