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