]> git.ipfire.org Git - thirdparty/git.git/blob - builtin-rev-list.c
Separate object listing routines out of rev-list
[thirdparty/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 " special purpose:\n"
39 " --bisect"
40 ;
41
42 static struct rev_info revs;
43
44 static int bisect_list;
45 static int show_timestamp;
46 static int hdr_termination;
47 static const char *header_prefix;
48
49 static void show_commit(struct commit *commit)
50 {
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;
71 }
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.
75 */
76 for (parents = commit->parents;
77 parents;
78 parents = parents->next)
79 parents->item->object.flags &= ~TMP_MARK;
80 }
81 if (revs.commit_format == CMIT_FMT_ONELINE)
82 putchar(' ');
83 else
84 putchar('\n');
85
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);
92 }
93 fflush(stdout);
94 if (commit->parents) {
95 free_commit_list(commit->parents);
96 commit->parents = NULL;
97 }
98 free(commit->buffer);
99 commit->buffer = NULL;
100 }
101
102 static void show_object(struct object_array_entry *p)
103 {
104 /* An object with name "foo\n0000000..." can be used to
105 * confuse downstream git-pack-objects very badly.
106 */
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);
112 }
113 else
114 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
115 }
116
117 /*
118 * This is a truly stupid algorithm, but it's only
119 * used for bisection, and we just don't care enough.
120 *
121 * We care just barely enough to avoid recursing for
122 * non-merge entries.
123 */
124 static int count_distance(struct commit_list *entry)
125 {
126 int nr = 0;
127
128 while (entry) {
129 struct commit *commit = entry->item;
130 struct commit_list *p;
131
132 if (commit->object.flags & (UNINTERESTING | COUNTED))
133 break;
134 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
135 nr++;
136 commit->object.flags |= COUNTED;
137 p = commit->parents;
138 entry = p;
139 if (p) {
140 p = p->next;
141 while (p) {
142 nr += count_distance(p);
143 p = p->next;
144 }
145 }
146 }
147
148 return nr;
149 }
150
151 static void clear_distance(struct commit_list *list)
152 {
153 while (list) {
154 struct commit *commit = list->item;
155 commit->object.flags &= ~COUNTED;
156 list = list->next;
157 }
158 }
159
160 static struct commit_list *find_bisection(struct commit_list *list)
161 {
162 int nr, closest;
163 struct commit_list *p, *best;
164
165 nr = 0;
166 p = list;
167 while (p) {
168 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
169 nr++;
170 p = p->next;
171 }
172 closest = 0;
173 best = list;
174
175 for (p = list; p; p = p->next) {
176 int distance;
177
178 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
179 continue;
180
181 distance = count_distance(p);
182 clear_distance(list);
183 if (nr - distance < distance)
184 distance = nr - distance;
185 if (distance > closest) {
186 best = p;
187 closest = distance;
188 }
189 }
190 if (best)
191 best->next = NULL;
192 return best;
193 }
194
195 static void mark_edge_parents_uninteresting(struct commit *commit)
196 {
197 struct commit_list *parents;
198
199 for (parents = commit->parents; parents; parents = parents->next) {
200 struct commit *parent = parents->item;
201 if (!(parent->object.flags & UNINTERESTING))
202 continue;
203 mark_tree_uninteresting(parent->tree);
204 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
205 parent->object.flags |= SHOWN;
206 printf("-%s\n", sha1_to_hex(parent->object.sha1));
207 }
208 }
209 }
210
211 static void mark_edges_uninteresting(struct commit_list *list)
212 {
213 for ( ; list; list = list->next) {
214 struct commit *commit = list->item;
215
216 if (commit->object.flags & UNINTERESTING) {
217 mark_tree_uninteresting(commit->tree);
218 continue;
219 }
220 mark_edge_parents_uninteresting(commit);
221 }
222 }
223
224 static void read_revisions_from_stdin(struct rev_info *revs)
225 {
226 char line[1000];
227
228 while (fgets(line, sizeof(line), stdin) != NULL) {
229 int len = strlen(line);
230 if (line[len - 1] == '\n')
231 line[--len] = 0;
232 if (!len)
233 break;
234 if (line[0] == '-')
235 die("options not supported in --stdin mode");
236 if (handle_revision_arg(line, revs, 0, 1))
237 die("bad revision '%s'", line);
238 }
239 }
240
241 int cmd_rev_list(int argc, const char **argv, const char *prefix)
242 {
243 struct commit_list *list;
244 int i;
245 int read_from_stdin = 0;
246
247 init_revisions(&revs, prefix);
248 revs.abbrev = 0;
249 revs.commit_format = CMIT_FMT_UNSPECIFIED;
250 argc = setup_revisions(argc, argv, &revs, NULL);
251
252 for (i = 1 ; i < argc; i++) {
253 const char *arg = argv[i];
254
255 if (!strcmp(arg, "--header")) {
256 revs.verbose_header = 1;
257 continue;
258 }
259 if (!strcmp(arg, "--timestamp")) {
260 show_timestamp = 1;
261 continue;
262 }
263 if (!strcmp(arg, "--bisect")) {
264 bisect_list = 1;
265 continue;
266 }
267 if (!strcmp(arg, "--stdin")) {
268 if (read_from_stdin++)
269 die("--stdin given twice?");
270 read_revisions_from_stdin(&revs);
271 continue;
272 }
273 usage(rev_list_usage);
274
275 }
276 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
277 /* The command line has a --pretty */
278 hdr_termination = '\n';
279 if (revs.commit_format == CMIT_FMT_ONELINE)
280 header_prefix = "";
281 else
282 header_prefix = "commit ";
283 }
284 else if (revs.verbose_header)
285 /* Only --header was specified */
286 revs.commit_format = CMIT_FMT_RAW;
287
288 list = revs.commits;
289
290 if ((!list &&
291 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
292 !revs.pending.nr)) ||
293 revs.diff)
294 usage(rev_list_usage);
295
296 save_commit_buffer = revs.verbose_header;
297 track_object_refs = 0;
298 if (bisect_list)
299 revs.limited = 1;
300
301 prepare_revision_walk(&revs);
302 if (revs.tree_objects)
303 mark_edges_uninteresting(revs.commits);
304
305 if (bisect_list)
306 revs.commits = find_bisection(revs.commits);
307
308 traverse_commit_list(&revs, show_commit, show_object);
309
310 return 0;
311 }