git-gc: run pack-refs by default unless the repo is bare
[debian-git.git] / builtin-rev-list.c
blob1bb3a06680194ce5855185ad1a1c7e39eeebba94
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 else if (revs.left_right) {
58 if (commit->object.flags & SYMMETRIC_LEFT)
59 putchar('<');
60 else
61 putchar('>');
63 if (revs.abbrev_commit && revs.abbrev)
64 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
65 stdout);
66 else
67 fputs(sha1_to_hex(commit->object.sha1), stdout);
68 if (revs.parents) {
69 struct commit_list *parents = commit->parents;
70 while (parents) {
71 struct object *o = &(parents->item->object);
72 parents = parents->next;
73 if (o->flags & TMP_MARK)
74 continue;
75 printf(" %s", sha1_to_hex(o->sha1));
76 o->flags |= TMP_MARK;
78 /* TMP_MARK is a general purpose flag that can
79 * be used locally, but the user should clean
80 * things up after it is done with them.
82 for (parents = commit->parents;
83 parents;
84 parents = parents->next)
85 parents->item->object.flags &= ~TMP_MARK;
87 if (revs.commit_format == CMIT_FMT_ONELINE)
88 putchar(' ');
89 else
90 putchar('\n');
92 if (revs.verbose_header) {
93 static char pretty_header[16384];
94 pretty_print_commit(revs.commit_format, commit, ~0,
95 pretty_header, sizeof(pretty_header),
96 revs.abbrev, NULL, NULL, revs.relative_date);
97 printf("%s%c", pretty_header, hdr_termination);
99 fflush(stdout);
100 if (commit->parents) {
101 free_commit_list(commit->parents);
102 commit->parents = NULL;
104 free(commit->buffer);
105 commit->buffer = NULL;
108 static void show_object(struct object_array_entry *p)
110 /* An object with name "foo\n0000000..." can be used to
111 * confuse downstream git-pack-objects very badly.
113 const char *ep = strchr(p->name, '\n');
114 if (ep) {
115 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
116 (int) (ep - p->name),
117 p->name);
119 else
120 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
123 static void show_edge(struct commit *commit)
125 printf("-%s\n", sha1_to_hex(commit->object.sha1));
129 * This is a truly stupid algorithm, but it's only
130 * used for bisection, and we just don't care enough.
132 * We care just barely enough to avoid recursing for
133 * non-merge entries.
135 static int count_distance(struct commit_list *entry)
137 int nr = 0;
139 while (entry) {
140 struct commit *commit = entry->item;
141 struct commit_list *p;
143 if (commit->object.flags & (UNINTERESTING | COUNTED))
144 break;
145 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
146 nr++;
147 commit->object.flags |= COUNTED;
148 p = commit->parents;
149 entry = p;
150 if (p) {
151 p = p->next;
152 while (p) {
153 nr += count_distance(p);
154 p = p->next;
159 return nr;
162 static void clear_distance(struct commit_list *list)
164 while (list) {
165 struct commit *commit = list->item;
166 commit->object.flags &= ~COUNTED;
167 list = list->next;
171 static struct commit_list *find_bisection(struct commit_list *list)
173 int nr, closest;
174 struct commit_list *p, *best;
176 nr = 0;
177 p = list;
178 while (p) {
179 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
180 nr++;
181 p = p->next;
183 closest = 0;
184 best = list;
186 for (p = list; p; p = p->next) {
187 int distance;
189 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
190 continue;
192 distance = count_distance(p);
193 clear_distance(list);
194 if (nr - distance < distance)
195 distance = nr - distance;
196 if (distance > closest) {
197 best = p;
198 closest = distance;
201 if (best)
202 best->next = NULL;
203 return best;
206 static void read_revisions_from_stdin(struct rev_info *revs)
208 char line[1000];
210 while (fgets(line, sizeof(line), stdin) != NULL) {
211 int len = strlen(line);
212 if (line[len - 1] == '\n')
213 line[--len] = 0;
214 if (!len)
215 break;
216 if (line[0] == '-')
217 die("options not supported in --stdin mode");
218 if (handle_revision_arg(line, revs, 0, 1))
219 die("bad revision '%s'", line);
223 int cmd_rev_list(int argc, const char **argv, const char *prefix)
225 struct commit_list *list;
226 int i;
227 int read_from_stdin = 0;
229 init_revisions(&revs, prefix);
230 revs.abbrev = 0;
231 revs.commit_format = CMIT_FMT_UNSPECIFIED;
232 argc = setup_revisions(argc, argv, &revs, NULL);
234 for (i = 1 ; i < argc; i++) {
235 const char *arg = argv[i];
237 if (!strcmp(arg, "--header")) {
238 revs.verbose_header = 1;
239 continue;
241 if (!strcmp(arg, "--timestamp")) {
242 show_timestamp = 1;
243 continue;
245 if (!strcmp(arg, "--bisect")) {
246 bisect_list = 1;
247 continue;
249 if (!strcmp(arg, "--stdin")) {
250 if (read_from_stdin++)
251 die("--stdin given twice?");
252 read_revisions_from_stdin(&revs);
253 continue;
255 usage(rev_list_usage);
258 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
259 /* The command line has a --pretty */
260 hdr_termination = '\n';
261 if (revs.commit_format == CMIT_FMT_ONELINE)
262 header_prefix = "";
263 else
264 header_prefix = "commit ";
266 else if (revs.verbose_header)
267 /* Only --header was specified */
268 revs.commit_format = CMIT_FMT_RAW;
270 list = revs.commits;
272 if ((!list &&
273 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
274 !revs.pending.nr)) ||
275 revs.diff)
276 usage(rev_list_usage);
278 save_commit_buffer = revs.verbose_header || revs.grep_filter;
279 track_object_refs = 0;
280 if (bisect_list)
281 revs.limited = 1;
283 prepare_revision_walk(&revs);
284 if (revs.tree_objects)
285 mark_edges_uninteresting(revs.commits, &revs, show_edge);
287 if (bisect_list)
288 revs.commits = find_bisection(revs.commits);
290 traverse_commit_list(&revs, show_commit, show_object);
292 return 0;