mailinfo: ignore blanks after in-body headers.
[git.git] / builtin-log.c
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"
12 #include "builtin.h"
13
14 /* this is in builtin-diff.c */
15 void add_head(struct rev_info *revs);
16
17 static 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");
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         }
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;
43         }
44         return 0;
45 }
46
47 int cmd_whatchanged(int argc, const char **argv, char **envp)
48 {
49         struct rev_info rev;
50
51         init_revisions(&rev);
52         rev.diff = 1;
53         rev.diffopt.recursive = 1;
54         rev.simplify_history = 0;
55         return cmd_log_wc(argc, argv, envp, &rev);
56 }
57
58 int cmd_show(int argc, const char **argv, char **envp)
59 {
60         struct rev_info rev;
61
62         init_revisions(&rev);
63         rev.diff = 1;
64         rev.diffopt.recursive = 1;
65         rev.combine_merges = 1;
66         rev.dense_combined_merges = 1;
67         rev.always_show_header = 1;
68         rev.ignore_merges = 0;
69         rev.no_walk = 1;
70         return cmd_log_wc(argc, argv, envp, &rev);
71 }
72
73 int cmd_log(int argc, const char **argv, char **envp)
74 {
75         struct rev_info rev;
76
77         init_revisions(&rev);
78         rev.always_show_header = 1;
79         rev.diffopt.recursive = 1;
80         return cmd_log_wc(argc, argv, envp, &rev);
81 }
82
83 static int istitlechar(char c)
84 {
85         return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
86                 (c >= '0' && c <= '9') || c == '.' || c == '_';
87 }
88
89 static char *extra_headers = NULL;
90 static int extra_headers_size = 0;
91
92 static int git_format_config(const char *var, const char *value)
93 {
94         if (!strcmp(var, "format.headers")) {
95                 int len = strlen(value);
96                 extra_headers_size += len + 1;
97                 extra_headers = realloc(extra_headers, extra_headers_size);
98                 extra_headers[extra_headers_size - len - 1] = 0;
99                 strcat(extra_headers, value);
100                 return 0;
101         }
102         return git_default_config(var, value);
103 }
104
105
106 static FILE *realstdout = NULL;
107 static const char *output_directory = NULL;
108
109 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
110 {
111         char filename[1024];
112         char *sol;
113         int len = 0;
114
115         if (output_directory) {
116                 safe_strncpy(filename, output_directory, 1010);
117                 len = strlen(filename);
118                 if (filename[len - 1] != '/')
119                         filename[len++] = '/';
120         }
121
122         sprintf(filename + len, "%04d", nr);
123         len = strlen(filename);
124
125         sol = strstr(commit->buffer, "\n\n");
126         if (sol) {
127                 int j, space = 1;
128
129                 sol += 2;
130                 /* strip [PATCH] or [PATCH blabla] */
131                 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
132                         char *eos = strchr(sol + 6, ']');
133                         if (eos) {
134                                 while (isspace(*eos))
135                                         eos++;
136                                 sol = eos;
137                         }
138                 }
139
140                 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
141                         if (istitlechar(sol[j])) {
142                                 if (space) {
143                                         filename[len++] = '-';
144                                         space = 0;
145                                 }
146                                 filename[len++] = sol[j];
147                                 if (sol[j] == '.')
148                                         while (sol[j + 1] == '.')
149                                                 j++;
150                         } else
151                                 space = 1;
152                 }
153                 while (filename[len - 1] == '.' || filename[len - 1] == '-')
154                         len--;
155         }
156         strcpy(filename + len, ".txt");
157         fprintf(realstdout, "%s\n", filename);
158         freopen(filename, "w", stdout);
159 }
160
161 int cmd_format_patch(int argc, const char **argv, char **envp)
162 {
163         struct commit *commit;
164         struct commit **list = NULL;
165         struct rev_info rev;
166         int nr = 0, total, i, j;
167         int use_stdout = 0;
168         int numbered = 0;
169         int start_number = -1;
170         int keep_subject = 0;
171         char *add_signoff = NULL;
172
173         init_revisions(&rev);
174         rev.commit_format = CMIT_FMT_EMAIL;
175         rev.verbose_header = 1;
176         rev.diff = 1;
177         rev.diffopt.with_raw = 0;
178         rev.diffopt.with_stat = 1;
179         rev.combine_merges = 0;
180         rev.ignore_merges = 1;
181         rev.diffopt.output_format = DIFF_FORMAT_PATCH;
182
183         git_config(git_format_config);
184         rev.extra_headers = extra_headers;
185
186         /*
187          * Parse the arguments before setup_revisions(), or something
188          * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
189          * possibly a valid SHA1.
190          */
191         for (i = 1, j = 1; i < argc; i++) {
192                 if (!strcmp(argv[i], "--stdout"))
193                         use_stdout = 1;
194                 else if (!strcmp(argv[i], "-n") ||
195                                 !strcmp(argv[i], "--numbered"))
196                         numbered = 1;
197                 else if (!strncmp(argv[i], "--start-number=", 15))
198                         start_number = strtol(argv[i] + 15, NULL, 10);
199                 else if (!strcmp(argv[i], "--start-number")) {
200                         i++;
201                         if (i == argc)
202                                 die("Need a number for --start-number");
203                         start_number = strtol(argv[i], NULL, 10);
204                 }
205                 else if (!strcmp(argv[i], "-k") ||
206                                 !strcmp(argv[i], "--keep-subject")) {
207                         keep_subject = 1;
208                         rev.total = -1;
209                 }
210                 else if (!strcmp(argv[i], "--output-directory") ||
211                          !strcmp(argv[i], "-o")) {
212                         i++;
213                         if (argc <= i)
214                                 die("Which directory?");
215                         if (output_directory)
216                                 die("Two output directories?");
217                         output_directory = argv[i];
218                 }
219                 else if (!strcmp(argv[i], "--signoff") ||
220                          !strcmp(argv[i], "-s")) {
221                         const char *committer = git_committer_info(1);
222                         const char *endpos = strchr(committer, '>');
223                         if (!endpos)
224                                 die("bogos committer info %s\n", committer);
225                         add_signoff = xmalloc(endpos - committer + 2);
226                         memcpy(add_signoff, committer, endpos - committer + 1);
227                         add_signoff[endpos - committer + 1] = 0;
228                 }
229                 else if (!strcmp(argv[i], "--attach"))
230                         rev.mime_boundary = git_version_string;
231                 else if (!strncmp(argv[i], "--attach=", 9))
232                         rev.mime_boundary = argv[i] + 9;
233                 else
234                         argv[j++] = argv[i];
235         }
236         argc = j;
237
238         if (start_number < 0)
239                 start_number = 1;
240         if (numbered && keep_subject)
241                 die ("-n and -k are mutually exclusive.");
242
243         argc = setup_revisions(argc, argv, &rev, "HEAD");
244         if (argc > 1)
245                 die ("unrecognized argument: %s", argv[1]);
246
247         if (output_directory) {
248                 if (use_stdout)
249                         die("standard output, or directory, which one?");
250                 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
251                         die("Could not create directory %s",
252                             output_directory);
253         }
254
255         if (rev.pending_objects && rev.pending_objects->next == NULL) {
256                 rev.pending_objects->item->flags |= UNINTERESTING;
257                 add_head(&rev);
258         }
259
260         if (!use_stdout)
261                 realstdout = fdopen(dup(1), "w");
262
263         prepare_revision_walk(&rev);
264         while ((commit = get_revision(&rev)) != NULL) {
265                 /* ignore merges */
266                 if (commit->parents && commit->parents->next)
267                         continue;
268                 nr++;
269                 list = realloc(list, nr * sizeof(list[0]));
270                 list[nr - 1] = commit;
271         }
272         total = nr;
273         if (numbered)
274                 rev.total = total + start_number - 1;
275         rev.add_signoff = add_signoff;
276         while (0 <= --nr) {
277                 int shown;
278                 commit = list[nr];
279                 rev.nr = total - nr + (start_number - 1);
280                 if (!use_stdout)
281                         reopen_stdout(commit, rev.nr, keep_subject);
282                 shown = log_tree_commit(&rev, commit);
283                 free(commit->buffer);
284                 commit->buffer = NULL;
285
286                 /* We put one extra blank line between formatted
287                  * patches and this flag is used by log-tree code
288                  * to see if it needs to emit a LF before showing
289                  * the log; when using one file per patch, we do
290                  * not want the extra blank line.
291                  */
292                 if (!use_stdout)
293                         rev.shown_one = 0;
294                 if (shown) {
295                         if (rev.mime_boundary)
296                                 printf("\n--%s%s--\n\n\n",
297                                        mime_boundary_leader,
298                                        rev.mime_boundary);
299                         else
300                                 printf("-- \n%s\n\n", git_version_string);
301                 }
302                 if (!use_stdout)
303                         fclose(stdout);
304         }
305         free(list);
306         return 0;
307 }
308