pack-objects: further work on internal rev-list logic.
[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('-');
7594c4b2
JH
57 if (revs.abbrev_commit && revs.abbrev)
58 fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
59 stdout);
5c51c985
JH
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 }
7594c4b2 81 if (revs.commit_format == CMIT_FMT_ONELINE)
d87449c5
JH
82 putchar(' ');
83 else
84 putchar('\n');
85
7594c4b2 86 if (revs.verbose_header) {
000182ea 87 static char pretty_header[16384];
7594c4b2
JH
88 pretty_print_commit(revs.commit_format, commit, ~0,
89 pretty_header, sizeof(pretty_header),
3dfb9278 90 revs.abbrev, NULL, NULL, revs.relative_date);
000182ea 91 printf("%s%c", pretty_header, hdr_termination);
7620d39f
LT
92 }
93 fflush(stdout);
cb115748
LT
94 if (commit->parents) {
95 free_commit_list(commit->parents);
96 commit->parents = NULL;
97 }
4cac42b1
JH
98 free(commit->buffer);
99 commit->buffer = NULL;
a3437b8c
JS
100}
101
c64ed70d 102static void show_object(struct object_array_entry *p)
9de48752 103{
c64ed70d
JH
104 /* An object with name "foo\n0000000..." can be used to
105 * confuse downstream git-pack-objects very badly.
106 */
107 const char *ep = strchr(p->name, '\n');
108 if (ep) {
109 printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
110 (int) (ep - p->name),
111 p->name);
9de48752 112 }
c64ed70d
JH
113 else
114 printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
9de48752
LT
115}
116
8d1d8f83
JH
117static void show_edge(struct commit *commit)
118{
119 printf("-%s\n", sha1_to_hex(commit->object.sha1));
120}
121
8b3a1e05
LT
122/*
123 * This is a truly stupid algorithm, but it's only
124 * used for bisection, and we just don't care enough.
125 *
126 * We care just barely enough to avoid recursing for
127 * non-merge entries.
128 */
129static int count_distance(struct commit_list *entry)
130{
131 int nr = 0;
132
133 while (entry) {
134 struct commit *commit = entry->item;
135 struct commit_list *p;
136
137 if (commit->object.flags & (UNINTERESTING | COUNTED))
138 break;
8efdc326 139 if (!revs.prune_fn || (commit->object.flags & TREECHANGE))
b3cfd939 140 nr++;
8b3a1e05
LT
141 commit->object.flags |= COUNTED;
142 p = commit->parents;
143 entry = p;
144 if (p) {
145 p = p->next;
146 while (p) {
147 nr += count_distance(p);
148 p = p->next;
149 }
150 }
151 }
b3cfd939 152
8b3a1e05
LT
153 return nr;
154}
155
3d958064 156static void clear_distance(struct commit_list *list)
8b3a1e05
LT
157{
158 while (list) {
159 struct commit *commit = list->item;
160 commit->object.flags &= ~COUNTED;
161 list = list->next;
162 }
163}
164
165static struct commit_list *find_bisection(struct commit_list *list)
166{
167 int nr, closest;
168 struct commit_list *p, *best;
169
170 nr = 0;
171 p = list;
172 while (p) {
8efdc326 173 if (!revs.prune_fn || (p->item->object.flags & TREECHANGE))
b3cfd939 174 nr++;
8b3a1e05
LT
175 p = p->next;
176 }
177 closest = 0;
178 best = list;
179
b3cfd939
LT
180 for (p = list; p; p = p->next) {
181 int distance;
182
8efdc326 183 if (revs.prune_fn && !(p->item->object.flags & TREECHANGE))
b3cfd939
LT
184 continue;
185
186 distance = count_distance(p);
8b3a1e05
LT
187 clear_distance(list);
188 if (nr - distance < distance)
189 distance = nr - distance;
190 if (distance > closest) {
191 best = p;
192 closest = distance;
193 }
8b3a1e05
LT
194 }
195 if (best)
196 best->next = NULL;
197 return best;
198}
199
42cabc34
JH
200static void read_revisions_from_stdin(struct rev_info *revs)
201{
202 char line[1000];
203
204 while (fgets(line, sizeof(line), stdin) != NULL) {
205 int len = strlen(line);
206 if (line[len - 1] == '\n')
207 line[--len] = 0;
208 if (!len)
209 break;
210 if (line[0] == '-')
211 die("options not supported in --stdin mode");
212 if (handle_revision_arg(line, revs, 0, 1))
213 die("bad revision '%s'", line);
214 }
215}
216
a633fca0 217int cmd_rev_list(int argc, const char **argv, const char *prefix)
64745109 218{
ae563542 219 struct commit_list *list;
d9a83684 220 int i;
42cabc34 221 int read_from_stdin = 0;
64745109 222
a633fca0 223 init_revisions(&revs, prefix);
7594c4b2
JH
224 revs.abbrev = 0;
225 revs.commit_format = CMIT_FMT_UNSPECIFIED;
a4a88b2b 226 argc = setup_revisions(argc, argv, &revs, NULL);
ae563542 227
fcfda02b 228 for (i = 1 ; i < argc; i++) {
cf484544 229 const char *arg = argv[i];
fcfda02b 230
a6f68d47 231 if (!strcmp(arg, "--header")) {
7594c4b2 232 revs.verbose_header = 1;
9d97aa64
LT
233 continue;
234 }
dc68c4ff
JH
235 if (!strcmp(arg, "--timestamp")) {
236 show_timestamp = 1;
237 continue;
238 }
8b3a1e05
LT
239 if (!strcmp(arg, "--bisect")) {
240 bisect_list = 1;
241 continue;
242 }
42cabc34
JH
243 if (!strcmp(arg, "--stdin")) {
244 if (read_from_stdin++)
245 die("--stdin given twice?");
246 read_revisions_from_stdin(&revs);
247 continue;
248 }
ae563542 249 usage(rev_list_usage);
a6f68d47 250
fcfda02b 251 }
7594c4b2
JH
252 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
253 /* The command line has a --pretty */
254 hdr_termination = '\n';
255 if (revs.commit_format == CMIT_FMT_ONELINE)
91539833 256 header_prefix = "";
7594c4b2 257 else
91539833 258 header_prefix = "commit ";
7594c4b2 259 }
db89665f
JH
260 else if (revs.verbose_header)
261 /* Only --header was specified */
262 revs.commit_format = CMIT_FMT_RAW;
fcfda02b 263
ae563542 264 list = revs.commits;
ae563542 265
8c1f0b44
JH
266 if ((!list &&
267 (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
1f1e895f 268 !revs.pending.nr)) ||
8c1f0b44 269 revs.diff)
7b34c2fa
LT
270 usage(rev_list_usage);
271
7594c4b2 272 save_commit_buffer = revs.verbose_header;
9181ca2c 273 track_object_refs = 0;
4e1dc640
JH
274 if (bisect_list)
275 revs.limited = 1;
9181ca2c 276
a4a88b2b
LT
277 prepare_revision_walk(&revs);
278 if (revs.tree_objects)
8d1d8f83 279 mark_edges_uninteresting(revs.commits, &revs, show_edge);
a4a88b2b
LT
280
281 if (bisect_list)
282 revs.commits = find_bisection(revs.commits);
7b34c2fa 283
c64ed70d 284 traverse_commit_list(&revs, show_commit, show_object);
8906300f 285
64745109
LT
286 return 0;
287}