Merge branch 'master' of git://repo.or.cz/git-gui
[git] / builtin-rev-list.c
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"
12
13 /* bits #0-15 in revision.h */
14
15 #define COUNTED         (1u<<16)
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 "    --left-right\n"
39 "  special purpose:\n"
40 "    --bisect\n"
41 "    --bisect-vars"
42 ;
43
44 static struct rev_info revs;
45
46 static int bisect_list;
47 static int show_timestamp;
48 static int hdr_termination;
49 static const char *header_prefix;
50
51 static void show_commit(struct commit *commit)
52 {
53         if (show_timestamp)
54                 printf("%lu ", commit->date);
55         if (header_prefix)
56                 fputs(header_prefix, stdout);
57         if (commit->object.flags & BOUNDARY)
58                 putchar('-');
59         else if (revs.left_right) {
60                 if (commit->object.flags & SYMMETRIC_LEFT)
61                         putchar('<');
62                 else
63                         putchar('>');
64         }
65         if (revs.abbrev_commit && revs.abbrev)
66                 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
67                       stdout);
68         else
69                 fputs(sha1_to_hex(commit->object.sha1), stdout);
70         if (revs.parents) {
71                 struct commit_list *parents = commit->parents;
72                 while (parents) {
73                         struct object *o = &(parents->item->object);
74                         parents = parents->next;
75                         if (o->flags & TMP_MARK)
76                                 continue;
77                         printf(" %s", sha1_to_hex(o->sha1));
78                         o->flags |= TMP_MARK;
79                 }
80                 /* TMP_MARK is a general purpose flag that can
81                  * be used locally, but the user should clean
82                  * things up after it is done with them.
83                  */
84                 for (parents = commit->parents;
85                      parents;
86                      parents = parents->next)
87                         parents->item->object.flags &= ~TMP_MARK;
88         }
89         if (revs.commit_format == CMIT_FMT_ONELINE)
90                 putchar(' ');
91         else
92                 putchar('\n');
93
94         if (revs.verbose_header) {
95                 static char pretty_header[16384];
96                 pretty_print_commit(revs.commit_format, commit, ~0,
97                                     pretty_header, sizeof(pretty_header),
98                                     revs.abbrev, NULL, NULL, revs.relative_date);
99                 printf("%s%c", pretty_header, hdr_termination);
100         }
101         fflush(stdout);
102         if (commit->parents) {
103                 free_commit_list(commit->parents);
104                 commit->parents = NULL;
105         }
106         free(commit->buffer);
107         commit->buffer = NULL;
108 }
109
110 static void show_object(struct object_array_entry *p)
111 {
112         /* An object with name "foo\n0000000..." can be used to
113          * confuse downstream git-pack-objects very badly.
114          */
115         const char *ep = strchr(p->name, '\n');
116         if (ep) {
117                 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
118                        (int) (ep - p->name),
119                        p->name);
120         }
121         else
122                 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
123 }
124
125 static void show_edge(struct commit *commit)
126 {
127         printf("-%s\n", sha1_to_hex(commit->object.sha1));
128 }
129
130 /*
131  * This is a truly stupid algorithm, but it's only
132  * used for bisection, and we just don't care enough.
133  *
134  * We care just barely enough to avoid recursing for
135  * non-merge entries.
136  */
137 static int count_distance(struct commit_list *entry)
138 {
139         int nr = 0;
140
141         while (entry) {
142                 struct commit *commit = entry->item;
143                 struct commit_list *p;
144
145                 if (commit->object.flags & (UNINTERESTING | COUNTED))
146                         break;
147                 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
148                         nr++;
149                 commit->object.flags |= COUNTED;
150                 p = commit->parents;
151                 entry = p;
152                 if (p) {
153                         p = p->next;
154                         while (p) {
155                                 nr += count_distance(p);
156                                 p = p->next;
157                         }
158                 }
159         }
160
161         return nr;
162 }
163
164 static void clear_distance(struct commit_list *list)
165 {
166         while (list) {
167                 struct commit *commit = list->item;
168                 commit->object.flags &= ~COUNTED;
169                 list = list->next;
170         }
171 }
172
173 #define DEBUG_BISECT 0
174
175 static inline int weight(struct commit_list *elem)
176 {
177         return *((int*)(elem->item->util));
178 }
179
180 static inline void weight_set(struct commit_list *elem, int weight)
181 {
182         *((int*)(elem->item->util)) = weight;
183 }
184
185 static int count_interesting_parents(struct commit *commit)
186 {
187         struct commit_list *p;
188         int count;
189
190         for (count = 0, p = commit->parents; p; p = p->next) {
191                 if (p->item->object.flags & UNINTERESTING)
192                         continue;
193                 count++;
194         }
195         return count;
196 }
197
198 static inline int halfway(struct commit_list *p, int distance, int nr)
199 {
200         /*
201          * Don't short-cut something we are not going to return!
202          */
203         if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
204                 return 0;
205         if (DEBUG_BISECT)
206                 return 0;
207         /*
208          * 2 and 3 are halfway of 5.
209          * 3 is halfway of 6 but 2 and 4 are not.
210          */
211         distance *= 2;
212         switch (distance - nr) {
213         case -1: case 0: case 1:
214                 return 1;
215         default:
216                 return 0;
217         }
218 }
219
220 #if !DEBUG_BISECT
221 #define show_list(a,b,c,d) do { ; } while (0)
222 #else
223 static void show_list(const char *debug, int counted, int nr,
224                       struct commit_list *list)
225 {
226         struct commit_list *p;
227
228         fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
229
230         for (p = list; p; p = p->next) {
231                 struct commit_list *pp;
232                 struct commit *commit = p->item;
233                 unsigned flags = commit->object.flags;
234                 enum object_type type;
235                 unsigned long size;
236                 char *buf = read_sha1_file(commit->object.sha1, &type, &size);
237                 char *ep, *sp;
238
239                 fprintf(stderr, "%c%c%c ",
240                         (flags & TREECHANGE) ? 'T' : ' ',
241                         (flags & UNINTERESTING) ? 'U' : ' ',
242                         (flags & COUNTED) ? 'C' : ' ');
243                 if (commit->util)
244                         fprintf(stderr, "%3d", weight(p));
245                 else
246                         fprintf(stderr, "---");
247                 fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
248                 for (pp = commit->parents; pp; pp = pp->next)
249                         fprintf(stderr, " %.*s", 8,
250                                 sha1_to_hex(pp->item->object.sha1));
251
252                 sp = strstr(buf, "\n\n");
253                 if (sp) {
254                         sp += 2;
255                         for (ep = sp; *ep && *ep != '\n'; ep++)
256                                 ;
257                         fprintf(stderr, " %.*s", (int)(ep - sp), sp);
258                 }
259                 fprintf(stderr, "\n");
260         }
261 }
262 #endif /* DEBUG_BISECT */
263
264 /*
265  * zero or positive weight is the number of interesting commits it can
266  * reach, including itself.  Especially, weight = 0 means it does not
267  * reach any tree-changing commits (e.g. just above uninteresting one
268  * but traversal is with pathspec).
269  *
270  * weight = -1 means it has one parent and its distance is yet to
271  * be computed.
272  *
273  * weight = -2 means it has more than one parent and its distance is
274  * unknown.  After running count_distance() first, they will get zero
275  * or positive distance.
276  */
277
278 static struct commit_list *find_bisection(struct commit_list *list,
279                                           int *reaches, int *all)
280 {
281         int n, nr, on_list, counted, distance;
282         struct commit_list *p, *best, *next, *last;
283         int *weights;
284
285         show_list("bisection 2 entry", 0, 0, list);
286
287         /*
288          * Count the number of total and tree-changing items on the
289          * list, while reversing the list.
290          */
291         for (nr = on_list = 0, last = NULL, p = list;
292              p;
293              p = next) {
294                 unsigned flags = p->item->object.flags;
295
296                 next = p->next;
297                 if (flags & UNINTERESTING)
298                         continue;
299                 p->next = last;
300                 last = p;
301                 if (!revs.prune_fn || (flags & TREECHANGE))
302                         nr++;
303                 on_list++;
304         }
305         list = last;
306         show_list("bisection 2 sorted", 0, nr, list);
307
308         *all = nr;
309         weights = xcalloc(on_list, sizeof(int*));
310         counted = 0;
311
312         for (n = 0, p = list; p; p = p->next) {
313                 struct commit *commit = p->item;
314                 unsigned flags = commit->object.flags;
315
316                 p->item->util = &weights[n++];
317                 switch (count_interesting_parents(commit)) {
318                 case 0:
319                         if (!revs.prune_fn || (flags & TREECHANGE)) {
320                                 weight_set(p, 1);
321                                 counted++;
322                                 show_list("bisection 2 count one",
323                                           counted, nr, list);
324                         }
325                         /*
326                          * otherwise, it is known not to reach any
327                          * tree-changing commit and gets weight 0.
328                          */
329                         break;
330                 case 1:
331                         weight_set(p, -1);
332                         break;
333                 default:
334                         weight_set(p, -2);
335                         break;
336                 }
337         }
338
339         show_list("bisection 2 initialize", counted, nr, list);
340
341         /*
342          * If you have only one parent in the resulting set
343          * then you can reach one commit more than that parent
344          * can reach.  So we do not have to run the expensive
345          * count_distance() for single strand of pearls.
346          *
347          * However, if you have more than one parents, you cannot
348          * just add their distance and one for yourself, since
349          * they usually reach the same ancestor and you would
350          * end up counting them twice that way.
351          *
352          * So we will first count distance of merges the usual
353          * way, and then fill the blanks using cheaper algorithm.
354          */
355         for (p = list; p; p = p->next) {
356                 if (p->item->object.flags & UNINTERESTING)
357                         continue;
358                 n = weight(p);
359                 if (n != -2)
360                         continue;
361                 distance = count_distance(p);
362                 clear_distance(list);
363                 weight_set(p, distance);
364
365                 /* Does it happen to be at exactly half-way? */
366                 if (halfway(p, distance, nr)) {
367                         p->next = NULL;
368                         *reaches = distance;
369                         free(weights);
370                         return p;
371                 }
372                 counted++;
373         }
374
375         show_list("bisection 2 count_distance", counted, nr, list);
376
377         while (counted < nr) {
378                 for (p = list; p; p = p->next) {
379                         struct commit_list *q;
380                         unsigned flags = p->item->object.flags;
381
382                         if (0 <= weight(p))
383                                 continue;
384                         for (q = p->item->parents; q; q = q->next) {
385                                 if (q->item->object.flags & UNINTERESTING)
386                                         continue;
387                                 if (0 <= weight(q))
388                                         break;
389                         }
390                         if (!q)
391                                 continue;
392
393                         /*
394                          * weight for p is unknown but q is known.
395                          * add one for p itself if p is to be counted,
396                          * otherwise inherit it from q directly.
397                          */
398                         if (!revs.prune_fn || (flags & TREECHANGE)) {
399                                 weight_set(p, weight(q)+1);
400                                 counted++;
401                                 show_list("bisection 2 count one",
402                                           counted, nr, list);
403                         }
404                         else
405                                 weight_set(p, weight(q));
406
407                         /* Does it happen to be at exactly half-way? */
408                         distance = weight(p);
409                         if (halfway(p, distance, nr)) {
410                                 p->next = NULL;
411                                 *reaches = distance;
412                                 free(weights);
413                                 return p;
414                         }
415                 }
416         }
417
418         show_list("bisection 2 counted all", counted, nr, list);
419
420         /* Then find the best one */
421         counted = -1;
422         best = list;
423         for (p = list; p; p = p->next) {
424                 unsigned flags = p->item->object.flags;
425
426                 if (revs.prune_fn && !(flags & TREECHANGE))
427                         continue;
428                 distance = weight(p);
429                 if (nr - distance < distance)
430                         distance = nr - distance;
431                 if (distance > counted) {
432                         best = p;
433                         counted = distance;
434                         *reaches = weight(p);
435                 }
436         }
437         if (best)
438                 best->next = NULL;
439         free(weights);
440         return best;
441 }
442
443 static void read_revisions_from_stdin(struct rev_info *revs)
444 {
445         char line[1000];
446
447         while (fgets(line, sizeof(line), stdin) != NULL) {
448                 int len = strlen(line);
449                 if (line[len - 1] == '\n')
450                         line[--len] = 0;
451                 if (!len)
452                         break;
453                 if (line[0] == '-')
454                         die("options not supported in --stdin mode");
455                 if (handle_revision_arg(line, revs, 0, 1))
456                         die("bad revision '%s'", line);
457         }
458 }
459
460 int cmd_rev_list(int argc, const char **argv, const char *prefix)
461 {
462         struct commit_list *list;
463         int i;
464         int read_from_stdin = 0;
465         int bisect_show_vars = 0;
466
467         git_config(git_default_config);
468         init_revisions(&revs, prefix);
469         revs.abbrev = 0;
470         revs.commit_format = CMIT_FMT_UNSPECIFIED;
471         argc = setup_revisions(argc, argv, &revs, NULL);
472
473         for (i = 1 ; i < argc; i++) {
474                 const char *arg = argv[i];
475
476                 if (!strcmp(arg, "--header")) {
477                         revs.verbose_header = 1;
478                         continue;
479                 }
480                 if (!strcmp(arg, "--timestamp")) {
481                         show_timestamp = 1;
482                         continue;
483                 }
484                 if (!strcmp(arg, "--bisect")) {
485                         bisect_list = 1;
486                         continue;
487                 }
488                 if (!strcmp(arg, "--bisect-vars")) {
489                         bisect_list = 1;
490                         bisect_show_vars = 1;
491                         continue;
492                 }
493                 if (!strcmp(arg, "--stdin")) {
494                         if (read_from_stdin++)
495                                 die("--stdin given twice?");
496                         read_revisions_from_stdin(&revs);
497                         continue;
498                 }
499                 usage(rev_list_usage);
500
501         }
502         if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
503                 /* The command line has a --pretty  */
504                 hdr_termination = '\n';
505                 if (revs.commit_format == CMIT_FMT_ONELINE)
506                         header_prefix = "";
507                 else
508                         header_prefix = "commit ";
509         }
510         else if (revs.verbose_header)
511                 /* Only --header was specified */
512                 revs.commit_format = CMIT_FMT_RAW;
513
514         list = revs.commits;
515
516         if ((!list &&
517              (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
518               !revs.pending.nr)) ||
519             revs.diff)
520                 usage(rev_list_usage);
521
522         save_commit_buffer = revs.verbose_header || revs.grep_filter;
523         track_object_refs = 0;
524         if (bisect_list)
525                 revs.limited = 1;
526
527         prepare_revision_walk(&revs);
528         if (revs.tree_objects)
529                 mark_edges_uninteresting(revs.commits, &revs, show_edge);
530
531         if (bisect_list) {
532                 int reaches = reaches, all = all;
533
534                 revs.commits = find_bisection(revs.commits, &reaches, &all);
535                 if (bisect_show_vars) {
536                         int cnt;
537                         if (!revs.commits)
538                                 return 1;
539                         /*
540                          * revs.commits can reach "reaches" commits among
541                          * "all" commits.  If it is good, then there are
542                          * (all-reaches) commits left to be bisected.
543                          * On the other hand, if it is bad, then the set
544                          * to bisect is "reaches".
545                          * A bisect set of size N has (N-1) commits further
546                          * to test, as we already know one bad one.
547                          */
548                         cnt = all-reaches;
549                         if (cnt < reaches)
550                                 cnt = reaches;
551                         printf("bisect_rev=%s\n"
552                                "bisect_nr=%d\n"
553                                "bisect_good=%d\n"
554                                "bisect_bad=%d\n"
555                                "bisect_all=%d\n",
556                                sha1_to_hex(revs.commits->item->object.sha1),
557                                cnt - 1,
558                                all - reaches - 1,
559                                reaches - 1,
560                                all);
561                         return 0;
562                 }
563         }
564
565         traverse_commit_list(&revs, show_commit, show_object);
566
567         return 0;
568 }