Merge branch 'jc/merge'
[git/trast.git] / rev-list.c
blob812d237f472d943767764def599b70e4039d7d8a
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 "diff.h"
8 #include "revision.h"
10 /* bits #0-4 in revision.h */
12 #define COUNTED (1u<<5)
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 " special purpose:\n"
34 " --bisect"
37 struct rev_info revs;
39 static int bisect_list = 0;
40 static int verbose_header = 0;
41 static int abbrev = DEFAULT_ABBREV;
42 static int show_parents = 0;
43 static int hdr_termination = 0;
44 static const char *commit_prefix = "";
45 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
47 static void show_commit(struct commit *commit)
49 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
50 if (show_parents) {
51 struct commit_list *parents = commit->parents;
52 while (parents) {
53 struct object *o = &(parents->item->object);
54 parents = parents->next;
55 if (o->flags & TMP_MARK)
56 continue;
57 printf(" %s", sha1_to_hex(o->sha1));
58 o->flags |= TMP_MARK;
60 /* TMP_MARK is a general purpose flag that can
61 * be used locally, but the user should clean
62 * things up after it is done with them.
64 for (parents = commit->parents;
65 parents;
66 parents = parents->next)
67 parents->item->object.flags &= ~TMP_MARK;
69 if (commit_format == CMIT_FMT_ONELINE)
70 putchar(' ');
71 else
72 putchar('\n');
74 if (verbose_header) {
75 static char pretty_header[16384];
76 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), abbrev);
77 printf("%s%c", pretty_header, hdr_termination);
79 fflush(stdout);
82 static struct object_list **process_blob(struct blob *blob,
83 struct object_list **p,
84 struct name_path *path,
85 const char *name)
87 struct object *obj = &blob->object;
89 if (!revs.blob_objects)
90 return p;
91 if (obj->flags & (UNINTERESTING | SEEN))
92 return p;
93 obj->flags |= SEEN;
94 return add_object(obj, p, path, name);
97 static struct object_list **process_tree(struct tree *tree,
98 struct object_list **p,
99 struct name_path *path,
100 const char *name)
102 struct object *obj = &tree->object;
103 struct tree_entry_list *entry;
104 struct name_path me;
106 if (!revs.tree_objects)
107 return p;
108 if (obj->flags & (UNINTERESTING | SEEN))
109 return p;
110 if (parse_tree(tree) < 0)
111 die("bad tree object %s", sha1_to_hex(obj->sha1));
112 obj->flags |= SEEN;
113 p = add_object(obj, p, path, name);
114 me.up = path;
115 me.elem = name;
116 me.elem_len = strlen(name);
117 entry = tree->entries;
118 tree->entries = NULL;
119 while (entry) {
120 struct tree_entry_list *next = entry->next;
121 if (entry->directory)
122 p = process_tree(entry->item.tree, p, &me, entry->name);
123 else
124 p = process_blob(entry->item.blob, p, &me, entry->name);
125 free(entry);
126 entry = next;
128 return p;
131 static void show_commit_list(struct rev_info *revs)
133 struct commit *commit;
134 struct object_list *objects = NULL, **p = &objects, *pending;
136 while ((commit = get_revision(revs)) != NULL) {
137 p = process_tree(commit->tree, p, NULL, "");
138 show_commit(commit);
140 for (pending = revs->pending_objects; pending; pending = pending->next) {
141 struct object *obj = pending->item;
142 const char *name = pending->name;
143 if (obj->flags & (UNINTERESTING | SEEN))
144 continue;
145 if (obj->type == tag_type) {
146 obj->flags |= SEEN;
147 p = add_object(obj, p, NULL, name);
148 continue;
150 if (obj->type == tree_type) {
151 p = process_tree((struct tree *)obj, p, NULL, name);
152 continue;
154 if (obj->type == blob_type) {
155 p = process_blob((struct blob *)obj, p, NULL, name);
156 continue;
158 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
160 while (objects) {
161 /* An object with name "foo\n0000000..." can be used to
162 * confuse downstream git-pack-objects very badly.
164 const char *ep = strchr(objects->name, '\n');
165 if (ep) {
166 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
167 (int) (ep - objects->name),
168 objects->name);
170 else
171 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
172 objects = objects->next;
177 * This is a truly stupid algorithm, but it's only
178 * used for bisection, and we just don't care enough.
180 * We care just barely enough to avoid recursing for
181 * non-merge entries.
183 static int count_distance(struct commit_list *entry)
185 int nr = 0;
187 while (entry) {
188 struct commit *commit = entry->item;
189 struct commit_list *p;
191 if (commit->object.flags & (UNINTERESTING | COUNTED))
192 break;
193 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
194 nr++;
195 commit->object.flags |= COUNTED;
196 p = commit->parents;
197 entry = p;
198 if (p) {
199 p = p->next;
200 while (p) {
201 nr += count_distance(p);
202 p = p->next;
207 return nr;
210 static void clear_distance(struct commit_list *list)
212 while (list) {
213 struct commit *commit = list->item;
214 commit->object.flags &= ~COUNTED;
215 list = list->next;
219 static struct commit_list *find_bisection(struct commit_list *list)
221 int nr, closest;
222 struct commit_list *p, *best;
224 nr = 0;
225 p = list;
226 while (p) {
227 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
228 nr++;
229 p = p->next;
231 closest = 0;
232 best = list;
234 for (p = list; p; p = p->next) {
235 int distance;
237 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
238 continue;
240 distance = count_distance(p);
241 clear_distance(list);
242 if (nr - distance < distance)
243 distance = nr - distance;
244 if (distance > closest) {
245 best = p;
246 closest = distance;
249 if (best)
250 best->next = NULL;
251 return best;
254 static void mark_edge_parents_uninteresting(struct commit *commit)
256 struct commit_list *parents;
258 for (parents = commit->parents; parents; parents = parents->next) {
259 struct commit *parent = parents->item;
260 if (!(parent->object.flags & UNINTERESTING))
261 continue;
262 mark_tree_uninteresting(parent->tree);
263 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
264 parent->object.flags |= SHOWN;
265 printf("-%s\n", sha1_to_hex(parent->object.sha1));
270 static void mark_edges_uninteresting(struct commit_list *list)
272 for ( ; list; list = list->next) {
273 struct commit *commit = list->item;
275 if (commit->object.flags & UNINTERESTING) {
276 mark_tree_uninteresting(commit->tree);
277 continue;
279 mark_edge_parents_uninteresting(commit);
283 int main(int argc, const char **argv)
285 struct commit_list *list;
286 int i;
288 argc = setup_revisions(argc, argv, &revs, NULL);
290 for (i = 1 ; i < argc; i++) {
291 const char *arg = argv[i];
293 /* accept -<digit>, like traditilnal "head" */
294 if ((*arg == '-') && isdigit(arg[1])) {
295 revs.max_count = atoi(arg + 1);
296 continue;
298 if (!strcmp(arg, "-n")) {
299 if (++i >= argc)
300 die("-n requires an argument");
301 revs.max_count = atoi(argv[i]);
302 continue;
304 if (!strncmp(arg,"-n",2)) {
305 revs.max_count = atoi(arg + 2);
306 continue;
308 if (!strcmp(arg, "--header")) {
309 verbose_header = 1;
310 continue;
312 if (!strcmp(arg, "--no-abbrev")) {
313 abbrev = 0;
314 continue;
316 if (!strncmp(arg, "--abbrev=", 9)) {
317 abbrev = strtoul(arg + 9, NULL, 10);
318 if (abbrev && abbrev < MINIMUM_ABBREV)
319 abbrev = MINIMUM_ABBREV;
320 else if (40 < abbrev)
321 abbrev = 40;
322 continue;
324 if (!strncmp(arg, "--pretty", 8)) {
325 commit_format = get_commit_format(arg+8);
326 verbose_header = 1;
327 hdr_termination = '\n';
328 if (commit_format == CMIT_FMT_ONELINE)
329 commit_prefix = "";
330 else
331 commit_prefix = "commit ";
332 continue;
334 if (!strcmp(arg, "--parents")) {
335 show_parents = 1;
336 continue;
338 if (!strcmp(arg, "--bisect")) {
339 bisect_list = 1;
340 continue;
342 usage(rev_list_usage);
346 list = revs.commits;
348 if (!list &&
349 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && !revs.pending_objects))
350 usage(rev_list_usage);
352 prepare_revision_walk(&revs);
353 if (revs.tree_objects)
354 mark_edges_uninteresting(revs.commits);
356 if (bisect_list)
357 revs.commits = find_bisection(revs.commits);
359 save_commit_buffer = verbose_header;
360 track_object_refs = 0;
362 show_commit_list(&revs);
364 return 0;