t4014: fix for whitespace from "wc -l"
[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
70827b15
LT
17static int cmd_log_wc(int argc, const char **argv, char **envp,
18 struct rev_info *rev)
19{
20 struct commit *commit;
21
22 rev->abbrev = DEFAULT_ABBREV;
23 rev->commit_format = CMIT_FMT_DEFAULT;
24 rev->verbose_header = 1;
25 argc = setup_revisions(argc, argv, rev, "HEAD");
582af688
SE
26 if (rev->always_show_header) {
27 if (rev->diffopt.pickaxe || rev->diffopt.filter) {
28 rev->always_show_header = 0;
29 if (rev->diffopt.output_format == DIFF_FORMAT_RAW)
30 rev->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
31 }
32 }
70827b15
LT
33
34 if (argc > 1)
35 die("unrecognized argument: %s", argv[1]);
36
37 prepare_revision_walk(rev);
38 setup_pager();
39 while ((commit = get_revision(rev)) != NULL) {
40 log_tree_commit(rev, commit);
41 free(commit->buffer);
42 commit->buffer = NULL;
cb115748
LT
43 free_commit_list(commit->parents);
44 commit->parents = NULL;
70827b15
LT
45 }
46 return 0;
47}
48
49int cmd_whatchanged(int argc, const char **argv, char **envp)
50{
51 struct rev_info rev;
52
53 init_revisions(&rev);
54 rev.diff = 1;
55 rev.diffopt.recursive = 1;
9202434c 56 rev.simplify_history = 0;
70827b15
LT
57 return cmd_log_wc(argc, argv, envp, &rev);
58}
59
60int cmd_show(int argc, const char **argv, char **envp)
61{
62 struct rev_info rev;
63
64 init_revisions(&rev);
65 rev.diff = 1;
66 rev.diffopt.recursive = 1;
67 rev.combine_merges = 1;
68 rev.dense_combined_merges = 1;
69 rev.always_show_header = 1;
70 rev.ignore_merges = 0;
71 rev.no_walk = 1;
72 return cmd_log_wc(argc, argv, envp, &rev);
73}
74
75int cmd_log(int argc, const char **argv, char **envp)
76{
77 struct rev_info rev;
78
79 init_revisions(&rev);
80 rev.always_show_header = 1;
81 rev.diffopt.recursive = 1;
82 return cmd_log_wc(argc, argv, envp, &rev);
83}
91efcf60 84
0377db77
JS
85static int istitlechar(char c)
86{
87 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
88 (c >= '0' && c <= '9') || c == '.' || c == '_';
89}
90
20ff0680
JS
91static char *extra_headers = NULL;
92static int extra_headers_size = 0;
93
94static int git_format_config(const char *var, const char *value)
95{
96 if (!strcmp(var, "format.headers")) {
97 int len = strlen(value);
98 extra_headers_size += len + 1;
99 extra_headers = realloc(extra_headers, extra_headers_size);
100 extra_headers[extra_headers_size - len - 1] = 0;
101 strcat(extra_headers, value);
102 return 0;
103 }
104 return git_default_config(var, value);
105}
106
107
81f3a188 108static FILE *realstdout = NULL;
efd02016 109static const char *output_directory = NULL;
81f3a188 110
8ac80a57 111static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
0377db77
JS
112{
113 char filename[1024];
114 char *sol;
2448482b 115 int len = 0;
0377db77 116
2448482b 117 if (output_directory) {
817151e6 118 strlcpy(filename, output_directory, 1010);
2448482b
JS
119 len = strlen(filename);
120 if (filename[len - 1] != '/')
121 filename[len++] = '/';
122 }
0377db77 123
2448482b 124 sprintf(filename + len, "%04d", nr);
0377db77
JS
125 len = strlen(filename);
126
127 sol = strstr(commit->buffer, "\n\n");
128 if (sol) {
129 int j, space = 1;
130
131 sol += 2;
132 /* strip [PATCH] or [PATCH blabla] */
8ac80a57 133 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
0377db77
JS
134 char *eos = strchr(sol + 6, ']');
135 if (eos) {
136 while (isspace(*eos))
137 eos++;
138 sol = eos;
139 }
140 }
141
142 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
143 if (istitlechar(sol[j])) {
144 if (space) {
145 filename[len++] = '-';
146 space = 0;
147 }
148 filename[len++] = sol[j];
149 if (sol[j] == '.')
150 while (sol[j + 1] == '.')
151 j++;
152 } else
153 space = 1;
154 }
155 while (filename[len - 1] == '.' || filename[len - 1] == '-')
156 len--;
157 }
158 strcpy(filename + len, ".txt");
81f3a188 159 fprintf(realstdout, "%s\n", filename);
0377db77
JS
160 freopen(filename, "w", stdout);
161}
162
9c6efa36
JS
163static void reset_all_objects_flags()
164{
165 int i;
166
167 for (i = 0; i < obj_allocs; i++)
168 if (objs[i])
169 objs[i]->flags = 0;
170}
171
172static int get_patch_id(struct commit *commit, struct diff_options *options,
173 unsigned char *sha1)
174{
175 diff_tree_sha1(commit->parents->item->object.sha1, commit->object.sha1,
176 "", options);
177 diffcore_std(options);
178 return diff_flush_patch_id(options, sha1);
179}
180
181static void get_patch_ids(struct rev_info *rev, struct diff_options *options)
182{
183 struct rev_info check_rev;
184 struct commit *commit;
185 struct object *o1, *o2;
186 unsigned flags1, flags2;
187 unsigned char sha1[20];
188
189 if (rev->pending.nr != 2)
190 die("Need exactly one range.");
191
192 o1 = rev->pending.objects[0].item;
193 flags1 = o1->flags;
194 o2 = rev->pending.objects[1].item;
195 flags2 = o2->flags;
196
197 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
198 die("Not a range.");
199
200 diff_setup(options);
201 options->recursive = 1;
202 if (diff_setup_done(options) < 0)
203 die("diff_setup_done failed");
204
205 /* given a range a..b get all patch ids for b..a */
206 init_revisions(&check_rev);
207 o1->flags ^= UNINTERESTING;
208 o2->flags ^= UNINTERESTING;
209 add_pending_object(&check_rev, o1, "o1");
210 add_pending_object(&check_rev, o2, "o2");
211 prepare_revision_walk(&check_rev);
212
213 while ((commit = get_revision(&check_rev)) != NULL) {
214 /* ignore merges */
215 if (commit->parents && commit->parents->next)
216 continue;
217
218 if (!get_patch_id(commit, options, sha1))
219 created_object(sha1, xcalloc(1, sizeof(struct object)));
220 }
221
222 /* reset for next revision walk */
223 reset_all_objects_flags();
224 o1->flags = flags1;
225 o2->flags = flags2;
226}
227
91efcf60
JH
228int cmd_format_patch(int argc, const char **argv, char **envp)
229{
230 struct commit *commit;
231 struct commit **list = NULL;
232 struct rev_info rev;
2448482b 233 int nr = 0, total, i, j;
0377db77 234 int use_stdout = 0;
596524b3 235 int numbered = 0;
fa0f02df 236 int start_number = -1;
8ac80a57 237 int keep_subject = 0;
9c6efa36
JS
238 int ignore_if_in_upstream = 0;
239 struct diff_options patch_id_opts;
cf2251b6 240 char *add_signoff = NULL;
91efcf60
JH
241
242 init_revisions(&rev);
243 rev.commit_format = CMIT_FMT_EMAIL;
244 rev.verbose_header = 1;
245 rev.diff = 1;
246 rev.diffopt.with_raw = 0;
247 rev.diffopt.with_stat = 1;
248 rev.combine_merges = 0;
249 rev.ignore_merges = 1;
250 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
91efcf60 251
20ff0680
JS
252 git_config(git_format_config);
253 rev.extra_headers = extra_headers;
254
2448482b
JS
255 /*
256 * Parse the arguments before setup_revisions(), or something
257 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
258 * possibly a valid SHA1.
259 */
260 for (i = 1, j = 1; i < argc; i++) {
261 if (!strcmp(argv[i], "--stdout"))
0377db77 262 use_stdout = 1;
596524b3
JS
263 else if (!strcmp(argv[i], "-n") ||
264 !strcmp(argv[i], "--numbered"))
265 numbered = 1;
fa0f02df
JS
266 else if (!strncmp(argv[i], "--start-number=", 15))
267 start_number = strtol(argv[i] + 15, NULL, 10);
268 else if (!strcmp(argv[i], "--start-number")) {
269 i++;
270 if (i == argc)
271 die("Need a number for --start-number");
272 start_number = strtol(argv[i], NULL, 10);
cf2251b6
JH
273 }
274 else if (!strcmp(argv[i], "-k") ||
8ac80a57
JS
275 !strcmp(argv[i], "--keep-subject")) {
276 keep_subject = 1;
277 rev.total = -1;
cf2251b6 278 }
efd02016
JH
279 else if (!strcmp(argv[i], "--output-directory") ||
280 !strcmp(argv[i], "-o")) {
2448482b 281 i++;
efd02016
JH
282 if (argc <= i)
283 die("Which directory?");
284 if (output_directory)
285 die("Two output directories?");
286 output_directory = argv[i];
698ce6f8 287 }
cf2251b6
JH
288 else if (!strcmp(argv[i], "--signoff") ||
289 !strcmp(argv[i], "-s")) {
6c4cca1c
EB
290 const char *committer;
291 const char *endpos;
292 setup_ident();
293 committer = git_committer_info(1);
294 endpos = strchr(committer, '>');
cf2251b6
JH
295 if (!endpos)
296 die("bogos committer info %s\n", committer);
297 add_signoff = xmalloc(endpos - committer + 2);
298 memcpy(add_signoff, committer, endpos - committer + 1);
299 add_signoff[endpos - committer + 1] = 0;
300 }
698ce6f8
JS
301 else if (!strcmp(argv[i], "--attach"))
302 rev.mime_boundary = git_version_string;
303 else if (!strncmp(argv[i], "--attach=", 9))
304 rev.mime_boundary = argv[i] + 9;
9c6efa36
JS
305 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
306 ignore_if_in_upstream = 1;
698ce6f8 307 else
2448482b 308 argv[j++] = argv[i];
0377db77 309 }
2448482b
JS
310 argc = j;
311
add5c8a5 312 if (start_number < 0)
fa0f02df 313 start_number = 1;
63b398a4 314 if (numbered && keep_subject)
8ac80a57
JS
315 die ("-n and -k are mutually exclusive.");
316
2448482b
JS
317 argc = setup_revisions(argc, argv, &rev, "HEAD");
318 if (argc > 1)
319 die ("unrecognized argument: %s", argv[1]);
0377db77 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