git-cvsexportcommit can't handle merge commits correctly
[git/git.git] / builtin-log.c
CommitLineData
70827b15
LT
1/*
2 * Builtin "git log" and related commands (show, whatchanged)
3 *
4 * (C) Copyright 2006 Linus Torvalds
5 * 2006 Junio Hamano
6 */
7#include "cache.h"
8#include "commit.h"
9#include "diff.h"
10#include "revision.h"
11#include "log-tree.h"
91efcf60 12#include "builtin.h"
70827b15 13
e686eb98
JS
14/* this is in builtin-diff.c */
15void add_head(struct rev_info *revs);
16
9dafea26 17static void cmd_log_init(int argc, const char **argv, char **envp,
70827b15
LT
18 struct rev_info *rev)
19{
70827b15
LT
20 rev->abbrev = DEFAULT_ABBREV;
21 rev->commit_format = CMIT_FMT_DEFAULT;
22 rev->verbose_header = 1;
23 argc = setup_revisions(argc, argv, rev, "HEAD");
9dafea26
TH
24 if (rev->diffopt.pickaxe || rev->diffopt.filter)
25 rev->always_show_header = 0;
70827b15
LT
26 if (argc > 1)
27 die("unrecognized argument: %s", argv[1]);
9dafea26
TH
28}
29
30static int cmd_log_walk(struct rev_info *rev)
31{
32 struct commit *commit;
70827b15
LT
33
34 prepare_revision_walk(rev);
35 setup_pager();
36 while ((commit = get_revision(rev)) != NULL) {
37 log_tree_commit(rev, commit);
38 free(commit->buffer);
39 commit->buffer = NULL;
cb115748
LT
40 free_commit_list(commit->parents);
41 commit->parents = NULL;
70827b15
LT
42 }
43 return 0;
44}
45
46int cmd_whatchanged(int argc, const char **argv, char **envp)
47{
48 struct rev_info rev;
49
97beb812 50 git_config(git_diff_config);
70827b15
LT
51 init_revisions(&rev);
52 rev.diff = 1;
53 rev.diffopt.recursive = 1;
9202434c 54 rev.simplify_history = 0;
9dafea26
TH
55 cmd_log_init(argc, argv, envp, &rev);
56 if (!rev.diffopt.output_format)
57 rev.diffopt.output_format = DIFF_FORMAT_RAW;
58 return cmd_log_walk(&rev);
70827b15
LT
59}
60
61int cmd_show(int argc, const char **argv, char **envp)
62{
63 struct rev_info rev;
64
97beb812 65 git_config(git_diff_config);
70827b15
LT
66 init_revisions(&rev);
67 rev.diff = 1;
68 rev.diffopt.recursive = 1;
69 rev.combine_merges = 1;
70 rev.dense_combined_merges = 1;
71 rev.always_show_header = 1;
72 rev.ignore_merges = 0;
73 rev.no_walk = 1;
9dafea26
TH
74 cmd_log_init(argc, argv, envp, &rev);
75 return cmd_log_walk(&rev);
70827b15
LT
76}
77
78int cmd_log(int argc, const char **argv, char **envp)
79{
80 struct rev_info rev;
81
97beb812 82 git_config(git_diff_config);
70827b15
LT
83 init_revisions(&rev);
84 rev.always_show_header = 1;
9dafea26
TH
85 cmd_log_init(argc, argv, envp, &rev);
86 return cmd_log_walk(&rev);
70827b15 87}
91efcf60 88
0377db77
JS
89static int istitlechar(char c)
90{
91 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
92 (c >= '0' && c <= '9') || c == '.' || c == '_';
93}
94
20ff0680
JS
95static char *extra_headers = NULL;
96static int extra_headers_size = 0;
97
98static int git_format_config(const char *var, const char *value)
99{
100 if (!strcmp(var, "format.headers")) {
101 int len = strlen(value);
102 extra_headers_size += len + 1;
103 extra_headers = realloc(extra_headers, extra_headers_size);
104 extra_headers[extra_headers_size - len - 1] = 0;
105 strcat(extra_headers, value);
106 return 0;
107 }
97beb812 108 return git_diff_config(var, value);
20ff0680
JS
109}
110
111
81f3a188 112static FILE *realstdout = NULL;
efd02016 113static const char *output_directory = NULL;
81f3a188 114
8ac80a57 115static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
0377db77
JS
116{
117 char filename[1024];
118 char *sol;
2448482b 119 int len = 0;
0377db77 120
2448482b 121 if (output_directory) {
817151e6 122 strlcpy(filename, output_directory, 1010);
2448482b
JS
123 len = strlen(filename);
124 if (filename[len - 1] != '/')
125 filename[len++] = '/';
126 }
0377db77 127
2448482b 128 sprintf(filename + len, "%04d", nr);
0377db77
JS
129 len = strlen(filename);
130
131 sol = strstr(commit->buffer, "\n\n");
132 if (sol) {
133 int j, space = 1;
134
135 sol += 2;
136 /* strip [PATCH] or [PATCH blabla] */
8ac80a57 137 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
0377db77
JS
138 char *eos = strchr(sol + 6, ']');
139 if (eos) {
140 while (isspace(*eos))
141 eos++;
142 sol = eos;
143 }
144 }
145
146 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
147 if (istitlechar(sol[j])) {
148 if (space) {
149 filename[len++] = '-';
150 space = 0;
151 }
152 filename[len++] = sol[j];
153 if (sol[j] == '.')
154 while (sol[j + 1] == '.')
155 j++;
156 } else
157 space = 1;
158 }
159 while (filename[len - 1] == '.' || filename[len - 1] == '-')
160 len--;
161 }
162 strcpy(filename + len, ".txt");
81f3a188 163 fprintf(realstdout, "%s\n", filename);
0377db77
JS
164 freopen(filename, "w", stdout);
165}
166
9c6efa36
JS
167static int get_patch_id(struct commit *commit, struct diff_options *options,
168 unsigned char *sha1)
169{
170 diff_tree_sha1(commit->parents->item->object.sha1, commit->object.sha1,
171 "", options);
172 diffcore_std(options);
173 return diff_flush_patch_id(options, sha1);
174}
175
176static void get_patch_ids(struct rev_info *rev, struct diff_options *options)
177{
178 struct rev_info check_rev;
179 struct commit *commit;
180 struct object *o1, *o2;
181 unsigned flags1, flags2;
182 unsigned char sha1[20];
183
184 if (rev->pending.nr != 2)
185 die("Need exactly one range.");
186
187 o1 = rev->pending.objects[0].item;
188 flags1 = o1->flags;
189 o2 = rev->pending.objects[1].item;
190 flags2 = o2->flags;
191
192 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
193 die("Not a range.");
194
195 diff_setup(options);
196 options->recursive = 1;
197 if (diff_setup_done(options) < 0)
198 die("diff_setup_done failed");
199
200 /* given a range a..b get all patch ids for b..a */
201 init_revisions(&check_rev);
202 o1->flags ^= UNINTERESTING;
203 o2->flags ^= UNINTERESTING;
204 add_pending_object(&check_rev, o1, "o1");
205 add_pending_object(&check_rev, o2, "o2");
206 prepare_revision_walk(&check_rev);
207
208 while ((commit = get_revision(&check_rev)) != NULL) {
209 /* ignore merges */
210 if (commit->parents && commit->parents->next)
211 continue;
212
213 if (!get_patch_id(commit, options, sha1))
214 created_object(sha1, xcalloc(1, sizeof(struct object)));
215 }
216
217 /* reset for next revision walk */
81db0941
JS
218 clear_commit_marks((struct commit *)o1,
219 SEEN | UNINTERESTING | SHOWN | ADDED);
220 clear_commit_marks((struct commit *)o2,
221 SEEN | UNINTERESTING | SHOWN | ADDED);
9c6efa36
JS
222 o1->flags = flags1;
223 o2->flags = flags2;
224}
225
91efcf60
JH
226int cmd_format_patch(int argc, const char **argv, char **envp)
227{
228 struct commit *commit;
229 struct commit **list = NULL;
230 struct rev_info rev;
2448482b 231 int nr = 0, total, i, j;
0377db77 232 int use_stdout = 0;
596524b3 233 int numbered = 0;
fa0f02df 234 int start_number = -1;
8ac80a57 235 int keep_subject = 0;
9c6efa36
JS
236 int ignore_if_in_upstream = 0;
237 struct diff_options patch_id_opts;
cf2251b6 238 char *add_signoff = NULL;
91efcf60 239
97beb812 240 git_config(git_format_config);
91efcf60
JH
241 init_revisions(&rev);
242 rev.commit_format = CMIT_FMT_EMAIL;
243 rev.verbose_header = 1;
244 rev.diff = 1;
91efcf60
JH
245 rev.combine_merges = 0;
246 rev.ignore_merges = 1;
27e1b127
JH
247 rev.diffopt.msg_sep = "";
248 rev.diffopt.recursive = 1;
91efcf60 249
20ff0680
JS
250 rev.extra_headers = extra_headers;
251
2448482b
JS
252 /*
253 * Parse the arguments before setup_revisions(), or something
254 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
255 * possibly a valid SHA1.
256 */
257 for (i = 1, j = 1; i < argc; i++) {
258 if (!strcmp(argv[i], "--stdout"))
0377db77 259 use_stdout = 1;
596524b3
JS
260 else if (!strcmp(argv[i], "-n") ||
261 !strcmp(argv[i], "--numbered"))
262 numbered = 1;
fa0f02df
JS
263 else if (!strncmp(argv[i], "--start-number=", 15))
264 start_number = strtol(argv[i] + 15, NULL, 10);
265 else if (!strcmp(argv[i], "--start-number")) {
266 i++;
267 if (i == argc)
268 die("Need a number for --start-number");
269 start_number = strtol(argv[i], NULL, 10);
cf2251b6
JH
270 }
271 else if (!strcmp(argv[i], "-k") ||
8ac80a57
JS
272 !strcmp(argv[i], "--keep-subject")) {
273 keep_subject = 1;
274 rev.total = -1;
cf2251b6 275 }
efd02016
JH
276 else if (!strcmp(argv[i], "--output-directory") ||
277 !strcmp(argv[i], "-o")) {
2448482b 278 i++;
efd02016
JH
279 if (argc <= i)
280 die("Which directory?");
281 if (output_directory)
282 die("Two output directories?");
283 output_directory = argv[i];
698ce6f8 284 }
cf2251b6
JH
285 else if (!strcmp(argv[i], "--signoff") ||
286 !strcmp(argv[i], "-s")) {
6c4cca1c
EB
287 const char *committer;
288 const char *endpos;
289 setup_ident();
290 committer = git_committer_info(1);
291 endpos = strchr(committer, '>');
cf2251b6
JH
292 if (!endpos)
293 die("bogos committer info %s\n", committer);
294 add_signoff = xmalloc(endpos - committer + 2);
295 memcpy(add_signoff, committer, endpos - committer + 1);
296 add_signoff[endpos - committer + 1] = 0;
297 }
698ce6f8
JS
298 else if (!strcmp(argv[i], "--attach"))
299 rev.mime_boundary = git_version_string;
300 else if (!strncmp(argv[i], "--attach=", 9))
301 rev.mime_boundary = argv[i] + 9;
9c6efa36
JS
302 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
303 ignore_if_in_upstream = 1;
698ce6f8 304 else
2448482b 305 argv[j++] = argv[i];
0377db77 306 }
2448482b
JS
307 argc = j;
308
add5c8a5 309 if (start_number < 0)
fa0f02df 310 start_number = 1;
63b398a4 311 if (numbered && keep_subject)
8ac80a57
JS
312 die ("-n and -k are mutually exclusive.");
313
2448482b
JS
314 argc = setup_revisions(argc, argv, &rev, "HEAD");
315 if (argc > 1)
316 die ("unrecognized argument: %s", argv[1]);
0377db77 317
c9b5ef99
TH
318 if (!rev.diffopt.output_format)
319 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
320
efd02016
JH
321 if (output_directory) {
322 if (use_stdout)
323 die("standard output, or directory, which one?");
324 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
325 die("Could not create directory %s",
326 output_directory);
327 }
328
1f1e895f
LT
329 if (rev.pending.nr == 1) {
330 rev.pending.objects[0].item->flags |= UNINTERESTING;
e686eb98
JS
331 add_head(&rev);
332 }
333
9c6efa36
JS
334 if (ignore_if_in_upstream)
335 get_patch_ids(&rev, &patch_id_opts);
336
81f3a188
JS
337 if (!use_stdout)
338 realstdout = fdopen(dup(1), "w");
339
91efcf60
JH
340 prepare_revision_walk(&rev);
341 while ((commit = get_revision(&rev)) != NULL) {
9c6efa36
JS
342 unsigned char sha1[20];
343
0377db77
JS
344 /* ignore merges */
345 if (commit->parents && commit->parents->next)
346 continue;
9c6efa36
JS
347
348 if (ignore_if_in_upstream &&
349 !get_patch_id(commit, &patch_id_opts, sha1) &&
350 lookup_object(sha1))
351 continue;
352
91efcf60
JH
353 nr++;
354 list = realloc(list, nr * sizeof(list[0]));
355 list[nr - 1] = commit;
356 }
0377db77 357 total = nr;
596524b3 358 if (numbered)
fa0f02df 359 rev.total = total + start_number - 1;
cf2251b6 360 rev.add_signoff = add_signoff;
91efcf60
JH
361 while (0 <= --nr) {
362 int shown;
363 commit = list[nr];
add5c8a5 364 rev.nr = total - nr + (start_number - 1);
0377db77 365 if (!use_stdout)
8ac80a57 366 reopen_stdout(commit, rev.nr, keep_subject);
91efcf60
JH
367 shown = log_tree_commit(&rev, commit);
368 free(commit->buffer);
369 commit->buffer = NULL;
add5c8a5
JH
370
371 /* We put one extra blank line between formatted
372 * patches and this flag is used by log-tree code
373 * to see if it needs to emit a LF before showing
374 * the log; when using one file per patch, we do
375 * not want the extra blank line.
376 */
377 if (!use_stdout)
378 rev.shown_one = 0;
698ce6f8
JS
379 if (shown) {
380 if (rev.mime_boundary)
381 printf("\n--%s%s--\n\n\n",
382 mime_boundary_leader,
383 rev.mime_boundary);
384 else
385 printf("-- \n%s\n\n", git_version_string);
386 }
0377db77
JS
387 if (!use_stdout)
388 fclose(stdout);
91efcf60
JH
389 }
390 free(list);
391 return 0;
392}
393