]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin-rev-list.c
10 #include "list-objects.h"
13 /* bits #0-15 in revision.h */
15 #define COUNTED (1u<<16)
17 static const char rev_list_usage
[] =
18 "git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
31 " formatting output:\n"
33 " --objects | --objects-edge\n"
35 " --header | --pretty\n"
36 " --abbrev=nr | --no-abbrev\n"
42 static struct rev_info revs
;
44 static int bisect_list
;
45 static int show_timestamp
;
46 static int hdr_termination
;
47 static const char *header_prefix
;
49 static void show_commit(struct commit
*commit
)
52 printf("%lu ", commit
->date
);
54 fputs(header_prefix
, stdout
);
55 if (commit
->object
.flags
& BOUNDARY
)
57 if (revs
.abbrev_commit
&& revs
.abbrev
)
58 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
.abbrev
),
61 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
63 struct commit_list
*parents
= commit
->parents
;
65 struct object
*o
= &(parents
->item
->object
);
66 parents
= parents
->next
;
67 if (o
->flags
& TMP_MARK
)
69 printf(" %s", sha1_to_hex(o
->sha1
));
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.
76 for (parents
= commit
->parents
;
78 parents
= parents
->next
)
79 parents
->item
->object
.flags
&= ~TMP_MARK
;
81 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
86 if (revs
.verbose_header
) {
87 static char pretty_header
[16384];
88 pretty_print_commit(revs
.commit_format
, commit
, ~0,
89 pretty_header
, sizeof(pretty_header
),
90 revs
.abbrev
, NULL
, NULL
, revs
.relative_date
);
91 printf("%s%c", pretty_header
, hdr_termination
);
94 if (commit
->parents
) {
95 free_commit_list(commit
->parents
);
96 commit
->parents
= NULL
;
99 commit
->buffer
= NULL
;
102 static void show_object(struct object_array_entry
*p
)
104 /* An object with name "foo\n0000000..." can be used to
105 * confuse downstream git-pack-objects very badly.
107 const char *ep
= strchr(p
->name
, '\n');
109 printf("%s %.*s\n", sha1_to_hex(p
->item
->sha1
),
110 (int) (ep
- p
->name
),
114 printf("%s %s\n", sha1_to_hex(p
->item
->sha1
), p
->name
);
118 * This is a truly stupid algorithm, but it's only
119 * used for bisection, and we just don't care enough.
121 * We care just barely enough to avoid recursing for
124 static int count_distance(struct commit_list
*entry
)
129 struct commit
*commit
= entry
->item
;
130 struct commit_list
*p
;
132 if (commit
->object
.flags
& (UNINTERESTING
| COUNTED
))
134 if (!revs
.prune_fn
|| (commit
->object
.flags
& TREECHANGE
))
136 commit
->object
.flags
|= COUNTED
;
142 nr
+= count_distance(p
);
151 static void clear_distance(struct commit_list
*list
)
154 struct commit
*commit
= list
->item
;
155 commit
->object
.flags
&= ~COUNTED
;
160 static struct commit_list
*find_bisection(struct commit_list
*list
)
163 struct commit_list
*p
, *best
;
168 if (!revs
.prune_fn
|| (p
->item
->object
.flags
& TREECHANGE
))
175 for (p
= list
; p
; p
= p
->next
) {
178 if (revs
.prune_fn
&& !(p
->item
->object
.flags
& TREECHANGE
))
181 distance
= count_distance(p
);
182 clear_distance(list
);
183 if (nr
- distance
< distance
)
184 distance
= nr
- distance
;
185 if (distance
> closest
) {
195 static void mark_edge_parents_uninteresting(struct commit
*commit
)
197 struct commit_list
*parents
;
199 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
200 struct commit
*parent
= parents
->item
;
201 if (!(parent
->object
.flags
& UNINTERESTING
))
203 mark_tree_uninteresting(parent
->tree
);
204 if (revs
.edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
205 parent
->object
.flags
|= SHOWN
;
206 printf("-%s\n", sha1_to_hex(parent
->object
.sha1
));
211 static void mark_edges_uninteresting(struct commit_list
*list
)
213 for ( ; list
; list
= list
->next
) {
214 struct commit
*commit
= list
->item
;
216 if (commit
->object
.flags
& UNINTERESTING
) {
217 mark_tree_uninteresting(commit
->tree
);
220 mark_edge_parents_uninteresting(commit
);
224 static void read_revisions_from_stdin(struct rev_info
*revs
)
228 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
229 int len
= strlen(line
);
230 if (line
[len
- 1] == '\n')
235 die("options not supported in --stdin mode");
236 if (handle_revision_arg(line
, revs
, 0, 1))
237 die("bad revision '%s'", line
);
241 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
243 struct commit_list
*list
;
245 int read_from_stdin
= 0;
247 init_revisions(&revs
, prefix
);
249 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
250 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
252 for (i
= 1 ; i
< argc
; i
++) {
253 const char *arg
= argv
[i
];
255 if (!strcmp(arg
, "--header")) {
256 revs
.verbose_header
= 1;
259 if (!strcmp(arg
, "--timestamp")) {
263 if (!strcmp(arg
, "--bisect")) {
267 if (!strcmp(arg
, "--stdin")) {
268 if (read_from_stdin
++)
269 die("--stdin given twice?");
270 read_revisions_from_stdin(&revs
);
273 usage(rev_list_usage
);
276 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
277 /* The command line has a --pretty */
278 hdr_termination
= '\n';
279 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
282 header_prefix
= "commit ";
284 else if (revs
.verbose_header
)
285 /* Only --header was specified */
286 revs
.commit_format
= CMIT_FMT_RAW
;
291 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
292 !revs
.pending
.nr
)) ||
294 usage(rev_list_usage
);
296 save_commit_buffer
= revs
.verbose_header
;
297 track_object_refs
= 0;
301 prepare_revision_walk(&revs
);
302 if (revs
.tree_objects
)
303 mark_edges_uninteresting(revs
.commits
);
306 revs
.commits
= find_bisection(revs
.commits
);
308 traverse_commit_list(&revs
, show_commit
, show_object
);