t6002: minor spelling fix.
[git/git.git] / builtin-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 9#include "revision.h"
c64ed70d 10#include "list-objects.h"
5fb61b8d 11#include "builtin.h"
ae563542 12
1b65a5aa 13/* bits #0-15 in revision.h */
64745109 14
1b65a5aa 15#define COUNTED (1u<<16)
8906300f 16
a6f68d47 17static const char rev_list_usage[] =
69e0c256
JH
18"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
19" limiting output:\n"
20" --max-count=nr\n"
21" --max-age=epoch\n"
22" --min-age=epoch\n"
23" --sparse\n"
24" --no-merges\n"
93b74bca 25" --remove-empty\n"
69e0c256 26" --all\n"
42cabc34 27" --stdin\n"
69e0c256 28" ordering output:\n"
69e0c256 29" --topo-order\n"
4c8725f1 30" --date-order\n"
69e0c256
JH
31" formatting output:\n"
32" --parents\n"
c6496575 33" --objects | --objects-edge\n"
69e0c256
JH
34" --unpacked\n"
35" --header | --pretty\n"
9da5c2f0 36" --abbrev=nr | --no-abbrev\n"
5c51c985 37" --abbrev-commit\n"
69e0c256
JH
38" special purpose:\n"
39" --bisect"
40;
a6f68d47 41
5fb61b8d 42static struct rev_info revs;
ae563542 43
96f1e58f
DR
44static int bisect_list;
45static int show_timestamp;
46static int hdr_termination;
91539833 47static const char *header_prefix;
e646de0d 48
81f2bb1f
LT
49static void show_commit(struct commit *commit)
50{
dc68c4ff
JH
51 if (show_timestamp)
52 printf("%lu ", commit->date);
91539833
LT
53 if (header_prefix)
54 fputs(header_prefix, stdout);
384e99a4
JH
55 if (commit->object.flags & BOUNDARY)
56 putchar('-');
74bd9029 57 else if (revs.left_right) {
577ed5c2
JH
58 if (commit->object.flags & SYMMETRIC_LEFT)
59 putchar('<');
60 else
61 putchar('>');
62 }
7594c4b2
JH
63 if (revs.abbrev_commit && revs.abbrev)
64 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
65 stdout);
5c51c985
JH
66 else
67 fputs(sha1_to_hex(commit->object.sha1), stdout);
7b0c9966 68 if (revs.parents) {
81f2bb1f
LT
69 struct commit_list *parents = commit->parents;
70 while (parents) {
88494423 71 struct object *o = &(parents->item->object);
81f2bb1f 72 parents = parents->next;
88494423
JH
73 if (o->flags & TMP_MARK)
74 continue;
75 printf(" %s", sha1_to_hex(o->sha1));
76 o->flags |= TMP_MARK;
81f2bb1f 77 }
88494423
JH
78 /* TMP_MARK is a general purpose flag that can
79 * be used locally, but the user should clean
80 * things up after it is done with them.
81 */
82 for (parents = commit->parents;
83 parents;
84 parents = parents->next)
85 parents->item->object.flags &= ~TMP_MARK;
81f2bb1f 86 }
7594c4b2 87 if (revs.commit_format == CMIT_FMT_ONELINE)
d87449c5
JH
88 putchar(' ');
89 else
90 putchar('\n');
91
7594c4b2 92 if (revs.verbose_header) {
000182ea 93 static char pretty_header[16384];
7594c4b2
JH
94 pretty_print_commit(revs.commit_format, commit, ~0,
95 pretty_header, sizeof(pretty_header),
3dfb9278 96 revs.abbrev, NULL, NULL, revs.relative_date);
000182ea 97 printf("%s%c", pretty_header, hdr_termination);
7620d39f
LT
98 }
99 fflush(stdout);
cb115748
LT
100 if (commit->parents) {
101 free_commit_list(commit->parents);
102 commit->parents = NULL;
103 }
4cac42b1
JH
104 free(commit->buffer);
105 commit->buffer = NULL;
a3437b8c
JS
106}
107
c64ed70d 108static void show_object(struct object_array_entry *p)
9de48752 109{
c64ed70d
JH
110 /* An object with name "foo\n0000000..." can be used to
111 * confuse downstream git-pack-objects very badly.
112 */
113 const char *ep = strchr(p->name, '\n');
114 if (ep) {
115 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
116 (int) (ep - p->name),
117 p->name);
9de48752 118 }
c64ed70d
JH
119 else
120 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
9de48752
LT
121}
122
8d1d8f83
JH
123static void show_edge(struct commit *commit)
124{
125 printf("-%s\n", sha1_to_hex(commit->object.sha1));
126}
127
8b3a1e05
LT
128/*
129 * This is a truly stupid algorithm, but it's only
130 * used for bisection, and we just don't care enough.
131 *
132 * We care just barely enough to avoid recursing for
133 * non-merge entries.
134 */
135static int count_distance(struct commit_list *entry)
136{
137 int nr = 0;
138
139 while (entry) {
140 struct commit *commit = entry->item;
141 struct commit_list *p;
142
143 if (commit->object.flags & (UNINTERESTING | COUNTED))
144 break;
8efdc326 145 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
b3cfd939 146 nr++;
8b3a1e05
LT
147 commit->object.flags |= COUNTED;
148 p = commit->parents;
149 entry = p;
150 if (p) {
151 p = p->next;
152 while (p) {
153 nr += count_distance(p);
154 p = p->next;
155 }
156 }
157 }
b3cfd939 158
8b3a1e05
LT
159 return nr;
160}
161
3d958064 162static void clear_distance(struct commit_list *list)
8b3a1e05
LT
163{
164 while (list) {
165 struct commit *commit = list->item;
166 commit->object.flags &= ~COUNTED;
167 list = list->next;
168 }
169}
170
171static struct commit_list *find_bisection(struct commit_list *list)
172{
173 int nr, closest;
174 struct commit_list *p, *best;
175
176 nr = 0;
177 p = list;
178 while (p) {
8efdc326 179 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
b3cfd939 180 nr++;
8b3a1e05
LT
181 p = p->next;
182 }
183 closest = 0;
184 best = list;
185
b3cfd939
LT
186 for (p = list; p; p = p->next) {
187 int distance;
188
8efdc326 189 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
b3cfd939
LT
190 continue;
191
192 distance = count_distance(p);
8b3a1e05
LT
193 clear_distance(list);
194 if (nr - distance < distance)
195 distance = nr - distance;
196 if (distance > closest) {
197 best = p;
198 closest = distance;
199 }
8b3a1e05
LT
200 }
201 if (best)
202 best->next = NULL;
203 return best;
204}
205
42cabc34
JH
206static void read_revisions_from_stdin(struct rev_info *revs)
207{
208 char line[1000];
209
210 while (fgets(line, sizeof(line), stdin) != NULL) {
211 int len = strlen(line);
212 if (line[len - 1] == '\n')
213 line[--len] = 0;
214 if (!len)
215 break;
216 if (line[0] == '-')
217 die("options not supported in --stdin mode");
218 if (handle_revision_arg(line, revs, 0, 1))
219 die("bad revision '%s'", line);
220 }
221}
222
a633fca0 223int cmd_rev_list(int argc, const char **argv, const char *prefix)
64745109 224{
ae563542 225 struct commit_list *list;
d9a83684 226 int i;
42cabc34 227 int read_from_stdin = 0;
64745109 228
256c3fe6 229 git_config(git_default_config);
a633fca0 230 init_revisions(&revs, prefix);
7594c4b2
JH
231 revs.abbrev = 0;
232 revs.commit_format = CMIT_FMT_UNSPECIFIED;
a4a88b2b 233 argc = setup_revisions(argc, argv, &revs, NULL);
ae563542 234
fcfda02b 235 for (i = 1 ; i < argc; i++) {
cf484544 236 const char *arg = argv[i];
fcfda02b 237
a6f68d47 238 if (!strcmp(arg, "--header")) {
7594c4b2 239 revs.verbose_header = 1;
9d97aa64
LT
240 continue;
241 }
dc68c4ff
JH
242 if (!strcmp(arg, "--timestamp")) {
243 show_timestamp = 1;
244 continue;
245 }
8b3a1e05
LT
246 if (!strcmp(arg, "--bisect")) {
247 bisect_list = 1;
248 continue;
249 }
42cabc34
JH
250 if (!strcmp(arg, "--stdin")) {
251 if (read_from_stdin++)
252 die("--stdin given twice?");
253 read_revisions_from_stdin(&revs);
254 continue;
255 }
ae563542 256 usage(rev_list_usage);
a6f68d47 257
fcfda02b 258 }
7594c4b2
JH
259 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
260 /* The command line has a --pretty */
261 hdr_termination = '\n';
262 if (revs.commit_format == CMIT_FMT_ONELINE)
91539833 263 header_prefix = "";
7594c4b2 264 else
91539833 265 header_prefix = "commit ";
7594c4b2 266 }
db89665f
JH
267 else if (revs.verbose_header)
268 /* Only --header was specified */
269 revs.commit_format = CMIT_FMT_RAW;
fcfda02b 270
ae563542 271 list = revs.commits;
ae563542 272
8c1f0b44
JH
273 if ((!list &&
274 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
1f1e895f 275 !revs.pending.nr)) ||
8c1f0b44 276 revs.diff)
7b34c2fa
LT
277 usage(rev_list_usage);
278
2d10c555 279 save_commit_buffer = revs.verbose_header || revs.grep_filter;
9181ca2c 280 track_object_refs = 0;
4e1dc640
JH
281 if (bisect_list)
282 revs.limited = 1;
9181ca2c 283
a4a88b2b
LT
284 prepare_revision_walk(&revs);
285 if (revs.tree_objects)
8d1d8f83 286 mark_edges_uninteresting(revs.commits, &revs, show_edge);
a4a88b2b
LT
287
288 if (bisect_list)
289 revs.commits = find_bisection(revs.commits);
7b34c2fa 290
c64ed70d 291 traverse_commit_list(&revs, show_commit, show_object);
8906300f 292
64745109
LT
293 return 0;
294}