shared repository - add a few missing calls to adjust_shared_perm().
[git.git] / builtin-rev-list.c
blobe885624255ac8e1007df797d339e3e81020f95a2
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 "builtin.h"
12 /* bits #0-15 in revision.h */
14 #define COUNTED (1u<<16)
16 static const char rev_list_usage[] =
17 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
18 " limiting output:\n"
19 " --max-count=nr\n"
20 " --max-age=epoch\n"
21 " --min-age=epoch\n"
22 " --sparse\n"
23 " --no-merges\n"
24 " --remove-empty\n"
25 " --all\n"
26 " ordering output:\n"
27 " --topo-order\n"
28 " --date-order\n"
29 " formatting output:\n"
30 " --parents\n"
31 " --objects | --objects-edge\n"
32 " --unpacked\n"
33 " --header | --pretty\n"
34 " --abbrev=nr | --no-abbrev\n"
35 " --abbrev-commit\n"
36 " special purpose:\n"
37 " --bisect"
40 static struct rev_info revs;
42 static int bisect_list = 0;
43 static int show_timestamp = 0;
44 static int hdr_termination = 0;
45 static const char *header_prefix;
47 static void show_commit(struct commit *commit)
49 if (show_timestamp)
50 printf("%lu ", commit->date);
51 if (header_prefix)
52 fputs(header_prefix, stdout);
53 if (commit->object.flags & BOUNDARY)
54 putchar('-');
55 if (revs.abbrev_commit && revs.abbrev)
56 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
57 stdout);
58 else
59 fputs(sha1_to_hex(commit->object.sha1), stdout);
60 if (revs.parents) {
61 struct commit_list *parents = commit->parents;
62 while (parents) {
63 struct object *o = &(parents->item->object);
64 parents = parents->next;
65 if (o->flags & TMP_MARK)
66 continue;
67 printf(" %s", sha1_to_hex(o->sha1));
68 o->flags |= TMP_MARK;
70 /* TMP_MARK is a general purpose flag that can
71 * be used locally, but the user should clean
72 * things up after it is done with them.
74 for (parents = commit->parents;
75 parents;
76 parents = parents->next)
77 parents->item->object.flags &= ~TMP_MARK;
79 if (revs.commit_format == CMIT_FMT_ONELINE)
80 putchar(' ');
81 else
82 putchar('\n');
84 if (revs.verbose_header) {
85 static char pretty_header[16384];
86 pretty_print_commit(revs.commit_format, commit, ~0,
87 pretty_header, sizeof(pretty_header),
88 revs.abbrev, NULL, NULL);
89 printf("%s%c", pretty_header, hdr_termination);
91 fflush(stdout);
94 static struct object_list **process_blob(struct blob *blob,
95 struct object_list **p,
96 struct name_path *path,
97 const char *name)
99 struct object *obj = &blob->object;
101 if (!revs.blob_objects)
102 return p;
103 if (obj->flags & (UNINTERESTING | SEEN))
104 return p;
105 obj->flags |= SEEN;
106 name = strdup(name);
107 return add_object(obj, p, path, name);
110 static struct object_list **process_tree(struct tree *tree,
111 struct object_list **p,
112 struct name_path *path,
113 const char *name)
115 struct object *obj = &tree->object;
116 struct tree_desc desc;
117 struct name_entry entry;
118 struct name_path me;
120 if (!revs.tree_objects)
121 return p;
122 if (obj->flags & (UNINTERESTING | SEEN))
123 return p;
124 if (parse_tree(tree) < 0)
125 die("bad tree object %s", sha1_to_hex(obj->sha1));
126 obj->flags |= SEEN;
127 name = strdup(name);
128 p = add_object(obj, p, path, name);
129 me.up = path;
130 me.elem = name;
131 me.elem_len = strlen(name);
133 desc.buf = tree->buffer;
134 desc.size = tree->size;
136 while (tree_entry(&desc, &entry)) {
137 if (S_ISDIR(entry.mode))
138 p = process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
139 else
140 p = process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
142 free(tree->buffer);
143 tree->buffer = NULL;
144 return p;
147 static void show_commit_list(struct rev_info *revs)
149 struct commit *commit;
150 struct object_list *objects = NULL, **p = &objects, *pending;
152 while ((commit = get_revision(revs)) != NULL) {
153 p = process_tree(commit->tree, p, NULL, "");
154 show_commit(commit);
156 for (pending = revs->pending_objects; pending; pending = pending->next) {
157 struct object *obj = pending->item;
158 const char *name = pending->name;
159 if (obj->flags & (UNINTERESTING | SEEN))
160 continue;
161 if (obj->type == tag_type) {
162 obj->flags |= SEEN;
163 p = add_object(obj, p, NULL, name);
164 continue;
166 if (obj->type == tree_type) {
167 p = process_tree((struct tree *)obj, p, NULL, name);
168 continue;
170 if (obj->type == blob_type) {
171 p = process_blob((struct blob *)obj, p, NULL, name);
172 continue;
174 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
176 while (objects) {
177 /* An object with name "foo\n0000000..." can be used to
178 * confuse downstream git-pack-objects very badly.
180 const char *ep = strchr(objects->name, '\n');
181 if (ep) {
182 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
183 (int) (ep - objects->name),
184 objects->name);
186 else
187 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
188 objects = objects->next;
193 * This is a truly stupid algorithm, but it's only
194 * used for bisection, and we just don't care enough.
196 * We care just barely enough to avoid recursing for
197 * non-merge entries.
199 static int count_distance(struct commit_list *entry)
201 int nr = 0;
203 while (entry) {
204 struct commit *commit = entry->item;
205 struct commit_list *p;
207 if (commit->object.flags & (UNINTERESTING | COUNTED))
208 break;
209 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
210 nr++;
211 commit->object.flags |= COUNTED;
212 p = commit->parents;
213 entry = p;
214 if (p) {
215 p = p->next;
216 while (p) {
217 nr += count_distance(p);
218 p = p->next;
223 return nr;
226 static void clear_distance(struct commit_list *list)
228 while (list) {
229 struct commit *commit = list->item;
230 commit->object.flags &= ~COUNTED;
231 list = list->next;
235 static struct commit_list *find_bisection(struct commit_list *list)
237 int nr, closest;
238 struct commit_list *p, *best;
240 nr = 0;
241 p = list;
242 while (p) {
243 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
244 nr++;
245 p = p->next;
247 closest = 0;
248 best = list;
250 for (p = list; p; p = p->next) {
251 int distance;
253 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
254 continue;
256 distance = count_distance(p);
257 clear_distance(list);
258 if (nr - distance < distance)
259 distance = nr - distance;
260 if (distance > closest) {
261 best = p;
262 closest = distance;
265 if (best)
266 best->next = NULL;
267 return best;
270 static void mark_edge_parents_uninteresting(struct commit *commit)
272 struct commit_list *parents;
274 for (parents = commit->parents; parents; parents = parents->next) {
275 struct commit *parent = parents->item;
276 if (!(parent->object.flags & UNINTERESTING))
277 continue;
278 mark_tree_uninteresting(parent->tree);
279 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
280 parent->object.flags |= SHOWN;
281 printf("-%s\n", sha1_to_hex(parent->object.sha1));
286 static void mark_edges_uninteresting(struct commit_list *list)
288 for ( ; list; list = list->next) {
289 struct commit *commit = list->item;
291 if (commit->object.flags & UNINTERESTING) {
292 mark_tree_uninteresting(commit->tree);
293 continue;
295 mark_edge_parents_uninteresting(commit);
299 int cmd_rev_list(int argc, const char **argv, char **envp)
301 struct commit_list *list;
302 int i;
304 init_revisions(&revs);
305 revs.abbrev = 0;
306 revs.commit_format = CMIT_FMT_UNSPECIFIED;
307 argc = setup_revisions(argc, argv, &revs, NULL);
309 for (i = 1 ; i < argc; i++) {
310 const char *arg = argv[i];
312 if (!strcmp(arg, "--header")) {
313 revs.verbose_header = 1;
314 continue;
316 if (!strcmp(arg, "--timestamp")) {
317 show_timestamp = 1;
318 continue;
320 if (!strcmp(arg, "--bisect")) {
321 bisect_list = 1;
322 continue;
324 usage(rev_list_usage);
327 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
328 /* The command line has a --pretty */
329 hdr_termination = '\n';
330 if (revs.commit_format == CMIT_FMT_ONELINE)
331 header_prefix = "";
332 else
333 header_prefix = "commit ";
335 else if (revs.verbose_header)
336 /* Only --header was specified */
337 revs.commit_format = CMIT_FMT_RAW;
339 list = revs.commits;
341 if ((!list &&
342 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
343 !revs.pending_objects)) ||
344 revs.diff)
345 usage(rev_list_usage);
347 save_commit_buffer = revs.verbose_header;
348 track_object_refs = 0;
349 if (bisect_list)
350 revs.limited = 1;
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 show_commit_list(&revs);
361 return 0;