Merge with_raw, with_stat and summary variables to output_format
[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                 free_commit_list(commit->parents);
44                 commit->parents = NULL;
45         }
46         return 0;
47 }
48
49 int 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;
56         rev.simplify_history = 0;
57         return cmd_log_wc(argc, argv, envp, &rev);
58 }
59
60 int 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
75 int 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 }
84
85 static int istitlechar(char c)
86 {
87         return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
88                 (c >= '0' && c <= '9') || c == '.' || c == '_';
89 }
90
91 static char *extra_headers = NULL;
92 static int extra_headers_size = 0;
93
94 static 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
108 static FILE *realstdout = NULL;
109 static const char *output_directory = NULL;
110
111 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
112 {
113         char filename[1024];
114         char *sol;
115         int len = 0;
116
117         if (output_directory) {
118                 strlcpy(filename, output_directory, 1010);
119                 len = strlen(filename);
120                 if (filename[len - 1] != '/')
121                         filename[len++] = '/';
122         }
123
124         sprintf(filename + len, "%04d", nr);
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] */
133                 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
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");
159         fprintf(realstdout, "%s\n", filename);
160         freopen(filename, "w", stdout);
161 }
162
163 int cmd_format_patch(int argc, const char **argv, char **envp)
164 {
165         struct commit *commit;
166         struct commit **list = NULL;
167         struct rev_info rev;
168         int nr = 0, total, i, j;
169         int use_stdout = 0;
170         int numbered = 0;
171         int start_number = -1;
172         int keep_subject = 0;
173         char *add_signoff = NULL;
174
175         init_revisions(&rev);
176         rev.commit_format = CMIT_FMT_EMAIL;
177         rev.verbose_header = 1;
178         rev.diff = 1;
179         rev.combine_merges = 0;
180         rev.ignore_merges = 1;
181         rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | 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;
222                         const char *endpos;
223                         setup_ident();
224                         committer = git_committer_info(1);
225                         endpos = strchr(committer, '>');
226                         if (!endpos)
227                                 die("bogos committer info %s\n", committer);
228                         add_signoff = xmalloc(endpos - committer + 2);
229                         memcpy(add_signoff, committer, endpos - committer + 1);
230                         add_signoff[endpos - committer + 1] = 0;
231                 }
232                 else if (!strcmp(argv[i], "--attach"))
233                         rev.mime_boundary = git_version_string;
234                 else if (!strncmp(argv[i], "--attach=", 9))
235                         rev.mime_boundary = argv[i] + 9;
236                 else
237                         argv[j++] = argv[i];
238         }
239         argc = j;
240
241         if (start_number < 0)
242                 start_number = 1;
243         if (numbered && keep_subject)
244                 die ("-n and -k are mutually exclusive.");
245
246         argc = setup_revisions(argc, argv, &rev, "HEAD");
247         if (argc > 1)
248                 die ("unrecognized argument: %s", argv[1]);
249
250         if (output_directory) {
251                 if (use_stdout)
252                         die("standard output, or directory, which one?");
253                 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
254                         die("Could not create directory %s",
255                             output_directory);
256         }
257
258         if (rev.pending.nr == 1) {
259                 rev.pending.objects[0].item->flags |= UNINTERESTING;
260                 add_head(&rev);
261         }
262
263         if (!use_stdout)
264                 realstdout = fdopen(dup(1), "w");
265
266         prepare_revision_walk(&rev);
267         while ((commit = get_revision(&rev)) != NULL) {
268                 /* ignore merges */
269                 if (commit->parents && commit->parents->next)
270                         continue;
271                 nr++;
272                 list = realloc(list, nr * sizeof(list[0]));
273                 list[nr - 1] = commit;
274         }
275         total = nr;
276         if (numbered)
277                 rev.total = total + start_number - 1;
278         rev.add_signoff = add_signoff;
279         while (0 <= --nr) {
280                 int shown;
281                 commit = list[nr];
282                 rev.nr = total - nr + (start_number - 1);
283                 if (!use_stdout)
284                         reopen_stdout(commit, rev.nr, keep_subject);
285                 shown = log_tree_commit(&rev, commit);
286                 free(commit->buffer);
287                 commit->buffer = NULL;
288
289                 /* We put one extra blank line between formatted
290                  * patches and this flag is used by log-tree code
291                  * to see if it needs to emit a LF before showing
292                  * the log; when using one file per patch, we do
293                  * not want the extra blank line.
294                  */
295                 if (!use_stdout)
296                         rev.shown_one = 0;
297                 if (shown) {
298                         if (rev.mime_boundary)
299                                 printf("\n--%s%s--\n\n\n",
300                                        mime_boundary_leader,
301                                        rev.mime_boundary);
302                         else
303                                 printf("-- \n%s\n\n", git_version_string);
304                 }
305                 if (!use_stdout)
306                         fclose(stdout);
307         }
308         free(list);
309         return 0;
310 }
311