GIT 1.3.0 rc1
[git/mingw/4msysgit/kblees.git] / rev-list.c
blob441c43785540bfc4bdfc66c4e70e0a4168bb30ad
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 show_timestamp = 0;
44 static int hdr_termination = 0;
45 static const char *commit_prefix = "";
46 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
48 static void show_commit(struct commit *commit)
50 if (show_timestamp)
51 printf("%lu ", commit->date);
52 if (commit_prefix[0])
53 fputs(commit_prefix, stdout);
54 fputs(sha1_to_hex(commit->object.sha1), stdout);
55 if (show_parents) {
56 struct commit_list *parents = commit->parents;
57 while (parents) {
58 struct object *o = &(parents->item->object);
59 parents = parents->next;
60 if (o->flags & TMP_MARK)
61 continue;
62 printf(" %s", sha1_to_hex(o->sha1));
63 o->flags |= TMP_MARK;
65 /* TMP_MARK is a general purpose flag that can
66 * be used locally, but the user should clean
67 * things up after it is done with them.
69 for (parents = commit->parents;
70 parents;
71 parents = parents->next)
72 parents->item->object.flags &= ~TMP_MARK;
74 if (commit_format == CMIT_FMT_ONELINE)
75 putchar(' ');
76 else
77 putchar('\n');
79 if (verbose_header) {
80 static char pretty_header[16384];
81 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), abbrev);
82 printf("%s%c", pretty_header, hdr_termination);
84 fflush(stdout);
87 static struct object_list **process_blob(struct blob *blob,
88 struct object_list **p,
89 struct name_path *path,
90 const char *name)
92 struct object *obj = &blob->object;
94 if (!revs.blob_objects)
95 return p;
96 if (obj->flags & (UNINTERESTING | SEEN))
97 return p;
98 obj->flags |= SEEN;
99 return add_object(obj, p, path, name);
102 static struct object_list **process_tree(struct tree *tree,
103 struct object_list **p,
104 struct name_path *path,
105 const char *name)
107 struct object *obj = &tree->object;
108 struct tree_entry_list *entry;
109 struct name_path me;
111 if (!revs.tree_objects)
112 return p;
113 if (obj->flags & (UNINTERESTING | SEEN))
114 return p;
115 if (parse_tree(tree) < 0)
116 die("bad tree object %s", sha1_to_hex(obj->sha1));
117 obj->flags |= SEEN;
118 p = add_object(obj, p, path, name);
119 me.up = path;
120 me.elem = name;
121 me.elem_len = strlen(name);
122 entry = tree->entries;
123 tree->entries = NULL;
124 while (entry) {
125 struct tree_entry_list *next = entry->next;
126 if (entry->directory)
127 p = process_tree(entry->item.tree, p, &me, entry->name);
128 else
129 p = process_blob(entry->item.blob, p, &me, entry->name);
130 free(entry);
131 entry = next;
133 return p;
136 static void show_commit_list(struct rev_info *revs)
138 struct commit *commit;
139 struct object_list *objects = NULL, **p = &objects, *pending;
141 while ((commit = get_revision(revs)) != NULL) {
142 p = process_tree(commit->tree, p, NULL, "");
143 show_commit(commit);
145 for (pending = revs->pending_objects; pending; pending = pending->next) {
146 struct object *obj = pending->item;
147 const char *name = pending->name;
148 if (obj->flags & (UNINTERESTING | SEEN))
149 continue;
150 if (obj->type == tag_type) {
151 obj->flags |= SEEN;
152 p = add_object(obj, p, NULL, name);
153 continue;
155 if (obj->type == tree_type) {
156 p = process_tree((struct tree *)obj, p, NULL, name);
157 continue;
159 if (obj->type == blob_type) {
160 p = process_blob((struct blob *)obj, p, NULL, name);
161 continue;
163 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
165 while (objects) {
166 /* An object with name "foo\n0000000..." can be used to
167 * confuse downstream git-pack-objects very badly.
169 const char *ep = strchr(objects->name, '\n');
170 if (ep) {
171 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
172 (int) (ep - objects->name),
173 objects->name);
175 else
176 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
177 objects = objects->next;
182 * This is a truly stupid algorithm, but it's only
183 * used for bisection, and we just don't care enough.
185 * We care just barely enough to avoid recursing for
186 * non-merge entries.
188 static int count_distance(struct commit_list *entry)
190 int nr = 0;
192 while (entry) {
193 struct commit *commit = entry->item;
194 struct commit_list *p;
196 if (commit->object.flags & (UNINTERESTING | COUNTED))
197 break;
198 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
199 nr++;
200 commit->object.flags |= COUNTED;
201 p = commit->parents;
202 entry = p;
203 if (p) {
204 p = p->next;
205 while (p) {
206 nr += count_distance(p);
207 p = p->next;
212 return nr;
215 static void clear_distance(struct commit_list *list)
217 while (list) {
218 struct commit *commit = list->item;
219 commit->object.flags &= ~COUNTED;
220 list = list->next;
224 static struct commit_list *find_bisection(struct commit_list *list)
226 int nr, closest;
227 struct commit_list *p, *best;
229 nr = 0;
230 p = list;
231 while (p) {
232 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
233 nr++;
234 p = p->next;
236 closest = 0;
237 best = list;
239 for (p = list; p; p = p->next) {
240 int distance;
242 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
243 continue;
245 distance = count_distance(p);
246 clear_distance(list);
247 if (nr - distance < distance)
248 distance = nr - distance;
249 if (distance > closest) {
250 best = p;
251 closest = distance;
254 if (best)
255 best->next = NULL;
256 return best;
259 static void mark_edge_parents_uninteresting(struct commit *commit)
261 struct commit_list *parents;
263 for (parents = commit->parents; parents; parents = parents->next) {
264 struct commit *parent = parents->item;
265 if (!(parent->object.flags & UNINTERESTING))
266 continue;
267 mark_tree_uninteresting(parent->tree);
268 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
269 parent->object.flags |= SHOWN;
270 printf("-%s\n", sha1_to_hex(parent->object.sha1));
275 static void mark_edges_uninteresting(struct commit_list *list)
277 for ( ; list; list = list->next) {
278 struct commit *commit = list->item;
280 if (commit->object.flags & UNINTERESTING) {
281 mark_tree_uninteresting(commit->tree);
282 continue;
284 mark_edge_parents_uninteresting(commit);
288 int main(int argc, const char **argv)
290 struct commit_list *list;
291 int i;
293 argc = setup_revisions(argc, argv, &revs, NULL);
295 for (i = 1 ; i < argc; i++) {
296 const char *arg = argv[i];
298 /* accept -<digit>, like traditilnal "head" */
299 if ((*arg == '-') && isdigit(arg[1])) {
300 revs.max_count = atoi(arg + 1);
301 continue;
303 if (!strcmp(arg, "-n")) {
304 if (++i >= argc)
305 die("-n requires an argument");
306 revs.max_count = atoi(argv[i]);
307 continue;
309 if (!strncmp(arg,"-n",2)) {
310 revs.max_count = atoi(arg + 2);
311 continue;
313 if (!strcmp(arg, "--header")) {
314 verbose_header = 1;
315 continue;
317 if (!strcmp(arg, "--no-abbrev")) {
318 abbrev = 0;
319 continue;
321 if (!strncmp(arg, "--abbrev=", 9)) {
322 abbrev = strtoul(arg + 9, NULL, 10);
323 if (abbrev && abbrev < MINIMUM_ABBREV)
324 abbrev = MINIMUM_ABBREV;
325 else if (40 < abbrev)
326 abbrev = 40;
327 continue;
329 if (!strncmp(arg, "--pretty", 8)) {
330 commit_format = get_commit_format(arg+8);
331 verbose_header = 1;
332 hdr_termination = '\n';
333 if (commit_format == CMIT_FMT_ONELINE)
334 commit_prefix = "";
335 else
336 commit_prefix = "commit ";
337 continue;
339 if (!strcmp(arg, "--parents")) {
340 show_parents = 1;
341 continue;
343 if (!strcmp(arg, "--timestamp")) {
344 show_timestamp = 1;
345 continue;
347 if (!strcmp(arg, "--bisect")) {
348 bisect_list = 1;
349 continue;
351 usage(rev_list_usage);
355 list = revs.commits;
357 if (!list &&
358 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && !revs.pending_objects))
359 usage(rev_list_usage);
361 prepare_revision_walk(&revs);
362 if (revs.tree_objects)
363 mark_edges_uninteresting(revs.commits);
365 if (bisect_list)
366 revs.commits = find_bisection(revs.commits);
368 save_commit_buffer = verbose_header;
369 track_object_refs = 0;
371 show_commit_list(&revs);
373 return 0;