gitweb: git_print_log: signoff line is non-empty line
[alt-git.git] / builtin-rev-list.c
blob402af8e1b5516143c5f4e2303abf05d2812c5a1c
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;
43 static int show_timestamp;
44 static int hdr_termination;
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, revs.relative_date);
89 printf("%s%c", pretty_header, hdr_termination);
91 fflush(stdout);
92 if (commit->parents) {
93 free_commit_list(commit->parents);
94 commit->parents = NULL;
96 free(commit->buffer);
97 commit->buffer = NULL;
100 static void process_blob(struct blob *blob,
101 struct object_array *p,
102 struct name_path *path,
103 const char *name)
105 struct object *obj = &blob->object;
107 if (!revs.blob_objects)
108 return;
109 if (obj->flags & (UNINTERESTING | SEEN))
110 return;
111 obj->flags |= SEEN;
112 name = strdup(name);
113 add_object(obj, p, path, name);
116 static void process_tree(struct tree *tree,
117 struct object_array *p,
118 struct name_path *path,
119 const char *name)
121 struct object *obj = &tree->object;
122 struct tree_desc desc;
123 struct name_entry entry;
124 struct name_path me;
126 if (!revs.tree_objects)
127 return;
128 if (obj->flags & (UNINTERESTING | SEEN))
129 return;
130 if (parse_tree(tree) < 0)
131 die("bad tree object %s", sha1_to_hex(obj->sha1));
132 obj->flags |= SEEN;
133 name = strdup(name);
134 add_object(obj, p, path, name);
135 me.up = path;
136 me.elem = name;
137 me.elem_len = strlen(name);
139 desc.buf = tree->buffer;
140 desc.size = tree->size;
142 while (tree_entry(&desc, &entry)) {
143 if (S_ISDIR(entry.mode))
144 process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
145 else
146 process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
148 free(tree->buffer);
149 tree->buffer = NULL;
152 static void show_commit_list(struct rev_info *revs)
154 int i;
155 struct commit *commit;
156 struct object_array objects = { 0, 0, NULL };
158 while ((commit = get_revision(revs)) != NULL) {
159 process_tree(commit->tree, &objects, NULL, "");
160 show_commit(commit);
162 for (i = 0; i < revs->pending.nr; i++) {
163 struct object_array_entry *pending = revs->pending.objects + i;
164 struct object *obj = pending->item;
165 const char *name = pending->name;
166 if (obj->flags & (UNINTERESTING | SEEN))
167 continue;
168 if (obj->type == OBJ_TAG) {
169 obj->flags |= SEEN;
170 add_object_array(obj, name, &objects);
171 continue;
173 if (obj->type == OBJ_TREE) {
174 process_tree((struct tree *)obj, &objects, NULL, name);
175 continue;
177 if (obj->type == OBJ_BLOB) {
178 process_blob((struct blob *)obj, &objects, NULL, name);
179 continue;
181 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
183 for (i = 0; i < objects.nr; i++) {
184 struct object_array_entry *p = objects.objects + i;
186 /* An object with name "foo\n0000000..." can be used to
187 * confuse downstream git-pack-objects very badly.
189 const char *ep = strchr(p->name, '\n');
190 if (ep) {
191 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
192 (int) (ep - p->name),
193 p->name);
195 else
196 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
201 * This is a truly stupid algorithm, but it's only
202 * used for bisection, and we just don't care enough.
204 * We care just barely enough to avoid recursing for
205 * non-merge entries.
207 static int count_distance(struct commit_list *entry)
209 int nr = 0;
211 while (entry) {
212 struct commit *commit = entry->item;
213 struct commit_list *p;
215 if (commit->object.flags & (UNINTERESTING | COUNTED))
216 break;
217 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
218 nr++;
219 commit->object.flags |= COUNTED;
220 p = commit->parents;
221 entry = p;
222 if (p) {
223 p = p->next;
224 while (p) {
225 nr += count_distance(p);
226 p = p->next;
231 return nr;
234 static void clear_distance(struct commit_list *list)
236 while (list) {
237 struct commit *commit = list->item;
238 commit->object.flags &= ~COUNTED;
239 list = list->next;
243 static struct commit_list *find_bisection(struct commit_list *list)
245 int nr, closest;
246 struct commit_list *p, *best;
248 nr = 0;
249 p = list;
250 while (p) {
251 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
252 nr++;
253 p = p->next;
255 closest = 0;
256 best = list;
258 for (p = list; p; p = p->next) {
259 int distance;
261 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
262 continue;
264 distance = count_distance(p);
265 clear_distance(list);
266 if (nr - distance < distance)
267 distance = nr - distance;
268 if (distance > closest) {
269 best = p;
270 closest = distance;
273 if (best)
274 best->next = NULL;
275 return best;
278 static void mark_edge_parents_uninteresting(struct commit *commit)
280 struct commit_list *parents;
282 for (parents = commit->parents; parents; parents = parents->next) {
283 struct commit *parent = parents->item;
284 if (!(parent->object.flags & UNINTERESTING))
285 continue;
286 mark_tree_uninteresting(parent->tree);
287 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
288 parent->object.flags |= SHOWN;
289 printf("-%s\n", sha1_to_hex(parent->object.sha1));
294 static void mark_edges_uninteresting(struct commit_list *list)
296 for ( ; list; list = list->next) {
297 struct commit *commit = list->item;
299 if (commit->object.flags & UNINTERESTING) {
300 mark_tree_uninteresting(commit->tree);
301 continue;
303 mark_edge_parents_uninteresting(commit);
307 int cmd_rev_list(int argc, const char **argv, const char *prefix)
309 struct commit_list *list;
310 int i;
312 init_revisions(&revs, prefix);
313 revs.abbrev = 0;
314 revs.commit_format = CMIT_FMT_UNSPECIFIED;
315 argc = setup_revisions(argc, argv, &revs, NULL);
317 for (i = 1 ; i < argc; i++) {
318 const char *arg = argv[i];
320 if (!strcmp(arg, "--header")) {
321 revs.verbose_header = 1;
322 continue;
324 if (!strcmp(arg, "--timestamp")) {
325 show_timestamp = 1;
326 continue;
328 if (!strcmp(arg, "--bisect")) {
329 bisect_list = 1;
330 continue;
332 usage(rev_list_usage);
335 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
336 /* The command line has a --pretty */
337 hdr_termination = '\n';
338 if (revs.commit_format == CMIT_FMT_ONELINE)
339 header_prefix = "";
340 else
341 header_prefix = "commit ";
343 else if (revs.verbose_header)
344 /* Only --header was specified */
345 revs.commit_format = CMIT_FMT_RAW;
347 list = revs.commits;
349 if ((!list &&
350 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
351 !revs.pending.nr)) ||
352 revs.diff)
353 usage(rev_list_usage);
355 save_commit_buffer = revs.verbose_header;
356 track_object_refs = 0;
357 if (bisect_list)
358 revs.limited = 1;
360 prepare_revision_walk(&revs);
361 if (revs.tree_objects)
362 mark_edges_uninteresting(revs.commits);
364 if (bisect_list)
365 revs.commits = find_bisection(revs.commits);
367 show_commit_list(&revs);
369 return 0;