]> git.ipfire.org Git - thirdparty/git.git/blame - rev-list.c
Clean up trailing whitespace when pretty-printing commits
[thirdparty/git.git] / rev-list.c
CommitLineData
64745109 1#include "cache.h"
e091eb93 2#include "refs.h"
36f8d174 3#include "tag.h"
64745109 4#include "commit.h"
9de48752
LT
5#include "tree.h"
6#include "blob.h"
1b0c7174 7#include "tree-walk.h"
c4e05b1a 8#include "diff.h"
ae563542
LT
9#include "revision.h"
10
3381c790 11/* bits #0-6 in revision.h */
64745109 12
3381c790 13#define COUNTED (1u<<7)
8906300f 14
a6f68d47 15static const char rev_list_usage[] =
69e0c256
JH
16"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
17" limiting output:\n"
18" --max-count=nr\n"
19" --max-age=epoch\n"
20" --min-age=epoch\n"
21" --sparse\n"
22" --no-merges\n"
93b74bca 23" --remove-empty\n"
69e0c256
JH
24" --all\n"
25" ordering output:\n"
69e0c256 26" --topo-order\n"
4c8725f1 27" --date-order\n"
69e0c256
JH
28" formatting output:\n"
29" --parents\n"
c6496575 30" --objects | --objects-edge\n"
69e0c256
JH
31" --unpacked\n"
32" --header | --pretty\n"
9da5c2f0 33" --abbrev=nr | --no-abbrev\n"
5c51c985 34" --abbrev-commit\n"
69e0c256
JH
35" special purpose:\n"
36" --bisect"
37;
a6f68d47 38
ae563542
LT
39struct rev_info revs;
40
8b3a1e05 41static int bisect_list = 0;
81f2bb1f 42static int verbose_header = 0;
9da5c2f0 43static int abbrev = DEFAULT_ABBREV;
5c51c985 44static int abbrev_commit = 0;
dc68c4ff 45static int show_timestamp = 0;
81f2bb1f 46static int hdr_termination = 0;
d998a089 47static const char *commit_prefix = "";
000182ea 48static enum cmit_fmt commit_format = CMIT_FMT_RAW;
e646de0d 49
81f2bb1f
LT
50static void show_commit(struct commit *commit)
51{
dc68c4ff
JH
52 if (show_timestamp)
53 printf("%lu ", commit->date);
54 if (commit_prefix[0])
55 fputs(commit_prefix, stdout);
384e99a4
JH
56 if (commit->object.flags & BOUNDARY)
57 putchar('-');
5c51c985
JH
58 if (abbrev_commit && abbrev)
59 fputs(find_unique_abbrev(commit->object.sha1, abbrev), stdout);
60 else
61 fputs(sha1_to_hex(commit->object.sha1), stdout);
7b0c9966 62 if (revs.parents) {
81f2bb1f
LT
63 struct commit_list *parents = commit->parents;
64 while (parents) {
88494423 65 struct object *o = &(parents->item->object);
81f2bb1f 66 parents = parents->next;
88494423
JH
67 if (o->flags & TMP_MARK)
68 continue;
69 printf(" %s", sha1_to_hex(o->sha1));
70 o->flags |= TMP_MARK;
81f2bb1f 71 }
88494423
JH
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;
81f2bb1f 80 }
d87449c5
JH
81 if (commit_format == CMIT_FMT_ONELINE)
82 putchar(' ');
83 else
84 putchar('\n');
85
81f2bb1f 86 if (verbose_header) {
000182ea 87 static char pretty_header[16384];
9da5c2f0 88 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), abbrev);
000182ea 89 printf("%s%c", pretty_header, hdr_termination);
7620d39f
LT
90 }
91 fflush(stdout);
a3437b8c
JS
92}
93
e646de0d
JH
94static struct object_list **process_blob(struct blob *blob,
95 struct object_list **p,
96 struct name_path *path,
97 const char *name)
9de48752
LT
98{
99 struct object *obj = &blob->object;
100
ae563542 101 if (!revs.blob_objects)
9de48752
LT
102 return p;
103 if (obj->flags & (UNINTERESTING | SEEN))
104 return p;
105 obj->flags |= SEEN;
e646de0d 106 return add_object(obj, p, path, name);
9de48752
LT
107}
108
e646de0d
JH
109static struct object_list **process_tree(struct tree *tree,
110 struct object_list **p,
111 struct name_path *path,
112 const char *name)
9de48752
LT
113{
114 struct object *obj = &tree->object;
115 struct tree_entry_list *entry;
e646de0d 116 struct name_path me;
9de48752 117
ae563542 118 if (!revs.tree_objects)
9de48752
LT
119 return p;
120 if (obj->flags & (UNINTERESTING | SEEN))
121 return p;
122 if (parse_tree(tree) < 0)
123 die("bad tree object %s", sha1_to_hex(obj->sha1));
124 obj->flags |= SEEN;
e646de0d
JH
125 p = add_object(obj, p, path, name);
126 me.up = path;
127 me.elem = name;
128 me.elem_len = strlen(name);
b0d8923e
LT
129 entry = tree->entries;
130 tree->entries = NULL;
131 while (entry) {
132 struct tree_entry_list *next = entry->next;
9de48752 133 if (entry->directory)
e646de0d 134 p = process_tree(entry->item.tree, p, &me, entry->name);
9de48752 135 else
e646de0d 136 p = process_blob(entry->item.blob, p, &me, entry->name);
b0d8923e
LT
137 free(entry);
138 entry = next;
9de48752
LT
139 }
140 return p;
141}
142
a4a88b2b 143static void show_commit_list(struct rev_info *revs)
81f2bb1f 144{
a4a88b2b 145 struct commit *commit;
36f8d174 146 struct object_list *objects = NULL, **p = &objects, *pending;
81f2bb1f 147
a4a88b2b 148 while ((commit = get_revision(revs)) != NULL) {
e646de0d 149 p = process_tree(commit->tree, p, NULL, "");
765ac8ec 150 show_commit(commit);
81f2bb1f 151 }
a4a88b2b 152 for (pending = revs->pending_objects; pending; pending = pending->next) {
36f8d174
LT
153 struct object *obj = pending->item;
154 const char *name = pending->name;
155 if (obj->flags & (UNINTERESTING | SEEN))
156 continue;
157 if (obj->type == tag_type) {
158 obj->flags |= SEEN;
e646de0d 159 p = add_object(obj, p, NULL, name);
36f8d174
LT
160 continue;
161 }
162 if (obj->type == tree_type) {
e646de0d 163 p = process_tree((struct tree *)obj, p, NULL, name);
36f8d174
LT
164 continue;
165 }
166 if (obj->type == blob_type) {
e646de0d 167 p = process_blob((struct blob *)obj, p, NULL, name);
36f8d174
LT
168 continue;
169 }
170 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
171 }
9de48752 172 while (objects) {
50319850
JH
173 /* An object with name "foo\n0000000..." can be used to
174 * confuse downstream git-pack-objects very badly.
c807f771
JH
175 */
176 const char *ep = strchr(objects->name, '\n');
177 if (ep) {
178 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
179 (int) (ep - objects->name),
180 objects->name);
181 }
182 else
183 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
9de48752
LT
184 objects = objects->next;
185 }
186}
187
8b3a1e05
LT
188/*
189 * This is a truly stupid algorithm, but it's only
190 * used for bisection, and we just don't care enough.
191 *
192 * We care just barely enough to avoid recursing for
193 * non-merge entries.
194 */
195static int count_distance(struct commit_list *entry)
196{
197 int nr = 0;
198
199 while (entry) {
200 struct commit *commit = entry->item;
201 struct commit_list *p;
202
203 if (commit->object.flags & (UNINTERESTING | COUNTED))
204 break;
8efdc326 205 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
b3cfd939 206 nr++;
8b3a1e05
LT
207 commit->object.flags |= COUNTED;
208 p = commit->parents;
209 entry = p;
210 if (p) {
211 p = p->next;
212 while (p) {
213 nr += count_distance(p);
214 p = p->next;
215 }
216 }
217 }
b3cfd939 218
8b3a1e05
LT
219 return nr;
220}
221
3d958064 222static void clear_distance(struct commit_list *list)
8b3a1e05
LT
223{
224 while (list) {
225 struct commit *commit = list->item;
226 commit->object.flags &= ~COUNTED;
227 list = list->next;
228 }
229}
230
231static struct commit_list *find_bisection(struct commit_list *list)
232{
233 int nr, closest;
234 struct commit_list *p, *best;
235
236 nr = 0;
237 p = list;
238 while (p) {
8efdc326 239 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
b3cfd939 240 nr++;
8b3a1e05
LT
241 p = p->next;
242 }
243 closest = 0;
244 best = list;
245
b3cfd939
LT
246 for (p = list; p; p = p->next) {
247 int distance;
248
8efdc326 249 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
b3cfd939
LT
250 continue;
251
252 distance = count_distance(p);
8b3a1e05
LT
253 clear_distance(list);
254 if (nr - distance < distance)
255 distance = nr - distance;
256 if (distance > closest) {
257 best = p;
258 closest = distance;
259 }
8b3a1e05
LT
260 }
261 if (best)
262 best->next = NULL;
263 return best;
264}
265
c6496575
JH
266static void mark_edge_parents_uninteresting(struct commit *commit)
267{
268 struct commit_list *parents;
269
270 for (parents = commit->parents; parents; parents = parents->next) {
271 struct commit *parent = parents->item;
272 if (!(parent->object.flags & UNINTERESTING))
273 continue;
274 mark_tree_uninteresting(parent->tree);
ae563542 275 if (revs.edge_hint && !(parent->object.flags & SHOWN)) {
eb38cc68 276 parent->object.flags |= SHOWN;
c6496575 277 printf("-%s\n", sha1_to_hex(parent->object.sha1));
eb38cc68 278 }
c6496575
JH
279 }
280}
281
5bdbaaa4
LT
282static void mark_edges_uninteresting(struct commit_list *list)
283{
284 for ( ; list; list = list->next) {
c6496575 285 struct commit *commit = list->item;
5bdbaaa4 286
c6496575
JH
287 if (commit->object.flags & UNINTERESTING) {
288 mark_tree_uninteresting(commit->tree);
289 continue;
5bdbaaa4 290 }
c6496575 291 mark_edge_parents_uninteresting(commit);
5bdbaaa4
LT
292 }
293}
294
cf484544 295int main(int argc, const char **argv)
64745109 296{
ae563542 297 struct commit_list *list;
d9a83684 298 int i;
64745109 299
a4a88b2b 300 argc = setup_revisions(argc, argv, &revs, NULL);
ae563542 301
fcfda02b 302 for (i = 1 ; i < argc; i++) {
cf484544 303 const char *arg = argv[i];
fcfda02b 304
8233340c
EW
305 /* accept -<digit>, like traditilnal "head" */
306 if ((*arg == '-') && isdigit(arg[1])) {
ae563542 307 revs.max_count = atoi(arg + 1);
8233340c
EW
308 continue;
309 }
3af06987
EW
310 if (!strcmp(arg, "-n")) {
311 if (++i >= argc)
312 die("-n requires an argument");
ae563542 313 revs.max_count = atoi(argv[i]);
3af06987
EW
314 continue;
315 }
316 if (!strncmp(arg,"-n",2)) {
ae563542 317 revs.max_count = atoi(arg + 2);
a6f68d47 318 continue;
fcfda02b 319 }
a6f68d47
LT
320 if (!strcmp(arg, "--header")) {
321 verbose_header = 1;
322 continue;
323 }
9da5c2f0
JH
324 if (!strcmp(arg, "--no-abbrev")) {
325 abbrev = 0;
326 continue;
327 }
5c51c985
JH
328 if (!strcmp(arg, "--abbrev")) {
329 abbrev = DEFAULT_ABBREV;
330 continue;
331 }
332 if (!strcmp(arg, "--abbrev-commit")) {
333 abbrev_commit = 1;
334 continue;
335 }
9da5c2f0
JH
336 if (!strncmp(arg, "--abbrev=", 9)) {
337 abbrev = strtoul(arg + 9, NULL, 10);
338 if (abbrev && abbrev < MINIMUM_ABBREV)
339 abbrev = MINIMUM_ABBREV;
340 else if (40 < abbrev)
341 abbrev = 40;
342 continue;
343 }
000182ea
LT
344 if (!strncmp(arg, "--pretty", 8)) {
345 commit_format = get_commit_format(arg+8);
9d97aa64 346 verbose_header = 1;
9d97aa64 347 hdr_termination = '\n';
d87449c5 348 if (commit_format == CMIT_FMT_ONELINE)
d998a089 349 commit_prefix = "";
d87449c5 350 else
d998a089 351 commit_prefix = "commit ";
9d97aa64
LT
352 continue;
353 }
dc68c4ff
JH
354 if (!strcmp(arg, "--timestamp")) {
355 show_timestamp = 1;
356 continue;
357 }
8b3a1e05
LT
358 if (!strcmp(arg, "--bisect")) {
359 bisect_list = 1;
360 continue;
361 }
ae563542 362 usage(rev_list_usage);
a6f68d47 363
fcfda02b
KS
364 }
365
ae563542 366 list = revs.commits;
ae563542 367
ef1cc2cc 368 if (!list &&
ae563542 369 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) && !revs.pending_objects))
7b34c2fa
LT
370 usage(rev_list_usage);
371
9181ca2c
JH
372 save_commit_buffer = verbose_header;
373 track_object_refs = 0;
374
a4a88b2b
LT
375 prepare_revision_walk(&revs);
376 if (revs.tree_objects)
377 mark_edges_uninteresting(revs.commits);
378
379 if (bisect_list)
380 revs.commits = find_bisection(revs.commits);
7b34c2fa 381
765ac8ec 382 show_commit_list(&revs);
8906300f 383
64745109
LT
384 return 0;
385}