Use "-M" instead of "-C" for "git diff" and "git status"
[git/git.git] / rev-list.c
CommitLineData
64745109
LT
1#include "cache.h"
2#include "commit.h"
a3437b8c 3#include "epoch.h"
64745109 4
8906300f
LT
5#define SEEN (1u << 0)
6#define INTERESTING (1u << 1)
8906300f 7
a6f68d47
LT
8static const char rev_list_usage[] =
9 "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
10 " --max-count=nr\n"
11 " --max-age=epoch\n"
12 " --min-age=epoch\n"
9d97aa64 13 " --header\n"
a3437b8c
JS
14 " --pretty\n"
15 " --merge-order [ --show-breaks ]";
a6f68d47 16
81f2bb1f
LT
17static int verbose_header = 0;
18static int show_parents = 0;
81f2bb1f
LT
19static int hdr_termination = 0;
20static const char *prefix = "";
21static unsigned long max_age = -1;
22static unsigned long min_age = -1;
23static int max_count = -1;
000182ea 24static enum cmit_fmt commit_format = CMIT_FMT_RAW;
a3437b8c
JS
25static int merge_order = 0;
26static int show_breaks = 0;
81f2bb1f
LT
27
28static void show_commit(struct commit *commit)
29{
a3437b8c
JS
30 if (show_breaks) {
31 prefix = "| ";
32 if (commit->object.flags & DISCONTINUITY) {
33 prefix = "^ ";
34 } else if (commit->object.flags & BOUNDARY) {
35 prefix = "= ";
36 }
37 }
81f2bb1f
LT
38 printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
39 if (show_parents) {
40 struct commit_list *parents = commit->parents;
41 while (parents) {
42 printf(" %s", sha1_to_hex(parents->item->object.sha1));
43 parents = parents->next;
44 }
45 }
46 putchar('\n');
47 if (verbose_header) {
000182ea
LT
48 static char pretty_header[16384];
49 pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
50 printf("%s%c", pretty_header, hdr_termination);
a3437b8c
JS
51 }
52}
53
54static int filter_commit(struct commit * commit)
55{
56 if (commit->object.flags & UNINTERESTING)
57 return CONTINUE;
58 if (min_age != -1 && (commit->date > min_age))
59 return CONTINUE;
60 if (max_age != -1 && (commit->date < max_age))
61 return STOP;
62 if (max_count != -1 && !max_count--)
63 return STOP;
64
65 return DO;
66}
67
68static int process_commit(struct commit * commit)
69{
70 int action=filter_commit(commit);
71
72 if (action == STOP) {
73 return STOP;
74 }
75
76 if (action == CONTINUE) {
77 return CONTINUE;
81f2bb1f 78 }
a3437b8c
JS
79
80 show_commit(commit);
81
82 return CONTINUE;
81f2bb1f
LT
83}
84
85static void show_commit_list(struct commit_list *list)
86{
87 while (list) {
88 struct commit *commit = pop_most_recent_commit(&list, SEEN);
89
a3437b8c 90 if (process_commit(commit) == STOP)
81f2bb1f 91 break;
81f2bb1f
LT
92 }
93}
94
8906300f
LT
95static void mark_parents_uninteresting(struct commit *commit)
96{
97 struct commit_list *parents = commit->parents;
98
99 while (parents) {
100 struct commit *commit = parents->item;
101 commit->object.flags |= UNINTERESTING;
102 parents = parents->next;
103 }
104}
105
106static int everybody_uninteresting(struct commit_list *list)
107{
108 while (list) {
109 struct commit *commit = list->item;
110 list = list->next;
111 if (commit->object.flags & UNINTERESTING)
112 continue;
113 return 0;
114 }
115 return 1;
116}
117
337cb3fb 118struct commit_list *limit_list(struct commit_list *list)
3b42a63c
LT
119{
120 struct commit_list *newlist = NULL;
121 struct commit_list **p = &newlist;
122 do {
123 struct commit *commit = pop_most_recent_commit(&list, SEEN);
124 struct object *obj = &commit->object;
125
337cb3fb 126 if (obj->flags & UNINTERESTING) {
3b42a63c
LT
127 mark_parents_uninteresting(commit);
128 if (everybody_uninteresting(list))
129 break;
130 continue;
131 }
132 p = &commit_list_insert(commit, p)->next;
133 } while (list);
134 return newlist;
135}
136
000182ea
LT
137static enum cmit_fmt get_commit_format(const char *arg)
138{
139 if (!*arg)
140 return CMIT_FMT_DEFAULT;
141 if (!strcmp(arg, "=raw"))
142 return CMIT_FMT_RAW;
143 if (!strcmp(arg, "=medium"))
144 return CMIT_FMT_MEDIUM;
145 if (!strcmp(arg, "=short"))
146 return CMIT_FMT_SHORT;
147 usage(rev_list_usage);
148}
149
150
64745109
LT
151int main(int argc, char **argv)
152{
64745109 153 struct commit_list *list = NULL;
337cb3fb 154 int i, limited = 0;
64745109 155
fcfda02b 156 for (i = 1 ; i < argc; i++) {
337cb3fb 157 int flags;
fcfda02b 158 char *arg = argv[i];
337cb3fb
LT
159 unsigned char sha1[20];
160 struct commit *commit;
fcfda02b
KS
161
162 if (!strncmp(arg, "--max-count=", 12)) {
163 max_count = atoi(arg + 12);
a6f68d47
LT
164 continue;
165 }
166 if (!strncmp(arg, "--max-age=", 10)) {
fcfda02b 167 max_age = atoi(arg + 10);
a6f68d47
LT
168 continue;
169 }
170 if (!strncmp(arg, "--min-age=", 10)) {
fcfda02b 171 min_age = atoi(arg + 10);
a6f68d47 172 continue;
fcfda02b 173 }
a6f68d47
LT
174 if (!strcmp(arg, "--header")) {
175 verbose_header = 1;
176 continue;
177 }
000182ea
LT
178 if (!strncmp(arg, "--pretty", 8)) {
179 commit_format = get_commit_format(arg+8);
9d97aa64 180 verbose_header = 1;
9d97aa64
LT
181 hdr_termination = '\n';
182 prefix = "commit ";
183 continue;
184 }
97658004
LT
185 if (!strcmp(arg, "--parents")) {
186 show_parents = 1;
187 continue;
188 }
a3437b8c
JS
189 if (!strncmp(arg, "--merge-order", 13)) {
190 merge_order = 1;
191 continue;
192 }
193 if (!strncmp(arg, "--show-breaks", 13)) {
194 show_breaks = 1;
195 continue;
196 }
a6f68d47 197
337cb3fb
LT
198 flags = 0;
199 if (*arg == '^') {
200 flags = UNINTERESTING;
201 arg++;
202 limited = 1;
203 }
a3437b8c 204 if (get_sha1(arg, sha1) || (show_breaks && !merge_order))
a6f68d47 205 usage(rev_list_usage);
337cb3fb
LT
206 commit = lookup_commit_reference(sha1);
207 if (!commit || parse_commit(commit) < 0)
208 die("bad commit object %s", arg);
209 commit->object.flags |= flags;
210 commit_list_insert(commit, &list);
fcfda02b
KS
211 }
212
337cb3fb 213 if (!list)
a6f68d47 214 usage(rev_list_usage);
64745109 215
a3437b8c 216 if (!merge_order) {
17ebe977 217 if (limited)
a3437b8c
JS
218 list = limit_list(list);
219 show_commit_list(list);
a3437b8c 220 } else {
a3437b8c
JS
221 if (sort_list_in_merge_order(list, &process_commit)) {
222 die("merge order sort failed\n");
223 }
a3437b8c 224 }
8906300f 225
64745109
LT
226 return 0;
227}