Don't xcalloc() struct diffstat_t
[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 26 if (rev->always_show_header) {
0e677e1a 27 if (rev->diffopt.pickaxe || rev->diffopt.filter)
582af688 28 rev->always_show_header = 0;
582af688 29 }
70827b15
LT
30
31 if (argc > 1)
32 die("unrecognized argument: %s", argv[1]);
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
50 init_revisions(&rev);
51 rev.diff = 1;
52 rev.diffopt.recursive = 1;
9202434c 53 rev.simplify_history = 0;
70827b15
LT
54 return cmd_log_wc(argc, argv, envp, &rev);
55}
56
57int cmd_show(int argc, const char **argv, char **envp)
58{
59 struct rev_info rev;
60
61 init_revisions(&rev);
62 rev.diff = 1;
63 rev.diffopt.recursive = 1;
64 rev.combine_merges = 1;
65 rev.dense_combined_merges = 1;
66 rev.always_show_header = 1;
67 rev.ignore_merges = 0;
68 rev.no_walk = 1;
69 return cmd_log_wc(argc, argv, envp, &rev);
70}
71
72int cmd_log(int argc, const char **argv, char **envp)
73{
74 struct rev_info rev;
75
76 init_revisions(&rev);
77 rev.always_show_header = 1;
78 rev.diffopt.recursive = 1;
79 return cmd_log_wc(argc, argv, envp, &rev);
80}
91efcf60 81
0377db77
JS
82static int istitlechar(char c)
83{
84 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
85 (c >= '0' && c <= '9') || c == '.' || c == '_';
86}
87
20ff0680
JS
88static char *extra_headers = NULL;
89static int extra_headers_size = 0;
90
91static int git_format_config(const char *var, const char *value)
92{
93 if (!strcmp(var, "format.headers")) {
94 int len = strlen(value);
95 extra_headers_size += len + 1;
96 extra_headers = realloc(extra_headers, extra_headers_size);
97 extra_headers[extra_headers_size - len - 1] = 0;
98 strcat(extra_headers, value);
99 return 0;
100 }
101 return git_default_config(var, value);
102}
103
104
81f3a188 105static FILE *realstdout = NULL;
efd02016 106static const char *output_directory = NULL;
81f3a188 107
8ac80a57 108static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
0377db77
JS
109{
110 char filename[1024];
111 char *sol;
2448482b 112 int len = 0;
0377db77 113
2448482b 114 if (output_directory) {
817151e6 115 strlcpy(filename, output_directory, 1010);
2448482b
JS
116 len = strlen(filename);
117 if (filename[len - 1] != '/')
118 filename[len++] = '/';
119 }
0377db77 120
2448482b 121 sprintf(filename + len, "%04d", nr);
0377db77
JS
122 len = strlen(filename);
123
124 sol = strstr(commit->buffer, "\n\n");
125 if (sol) {
126 int j, space = 1;
127
128 sol += 2;
129 /* strip [PATCH] or [PATCH blabla] */
8ac80a57 130 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
0377db77
JS
131 char *eos = strchr(sol + 6, ']');
132 if (eos) {
133 while (isspace(*eos))
134 eos++;
135 sol = eos;
136 }
137 }
138
139 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
140 if (istitlechar(sol[j])) {
141 if (space) {
142 filename[len++] = '-';
143 space = 0;
144 }
145 filename[len++] = sol[j];
146 if (sol[j] == '.')
147 while (sol[j + 1] == '.')
148 j++;
149 } else
150 space = 1;
151 }
152 while (filename[len - 1] == '.' || filename[len - 1] == '-')
153 len--;
154 }
155 strcpy(filename + len, ".txt");
81f3a188 156 fprintf(realstdout, "%s\n", filename);
0377db77
JS
157 freopen(filename, "w", stdout);
158}
159
91efcf60
JH
160int cmd_format_patch(int argc, const char **argv, char **envp)
161{
162 struct commit *commit;
163 struct commit **list = NULL;
164 struct rev_info rev;
2448482b 165 int nr = 0, total, i, j;
0377db77 166 int use_stdout = 0;
596524b3 167 int numbered = 0;
fa0f02df 168 int start_number = -1;
8ac80a57 169 int keep_subject = 0;
cf2251b6 170 char *add_signoff = NULL;
91efcf60
JH
171
172 init_revisions(&rev);
173 rev.commit_format = CMIT_FMT_EMAIL;
174 rev.verbose_header = 1;
175 rev.diff = 1;
91efcf60
JH
176 rev.combine_merges = 0;
177 rev.ignore_merges = 1;
39bc9a6c 178 rev.diffopt.msg_sep = "---\n";
91efcf60 179
20ff0680
JS
180 git_config(git_format_config);
181 rev.extra_headers = extra_headers;
182
2448482b
JS
183 /*
184 * Parse the arguments before setup_revisions(), or something
185 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
186 * possibly a valid SHA1.
187 */
188 for (i = 1, j = 1; i < argc; i++) {
189 if (!strcmp(argv[i], "--stdout"))
0377db77 190 use_stdout = 1;
596524b3
JS
191 else if (!strcmp(argv[i], "-n") ||
192 !strcmp(argv[i], "--numbered"))
193 numbered = 1;
fa0f02df
JS
194 else if (!strncmp(argv[i], "--start-number=", 15))
195 start_number = strtol(argv[i] + 15, NULL, 10);
196 else if (!strcmp(argv[i], "--start-number")) {
197 i++;
198 if (i == argc)
199 die("Need a number for --start-number");
200 start_number = strtol(argv[i], NULL, 10);
cf2251b6
JH
201 }
202 else if (!strcmp(argv[i], "-k") ||
8ac80a57
JS
203 !strcmp(argv[i], "--keep-subject")) {
204 keep_subject = 1;
205 rev.total = -1;
cf2251b6 206 }
efd02016
JH
207 else if (!strcmp(argv[i], "--output-directory") ||
208 !strcmp(argv[i], "-o")) {
2448482b 209 i++;
efd02016
JH
210 if (argc <= i)
211 die("Which directory?");
212 if (output_directory)
213 die("Two output directories?");
214 output_directory = argv[i];
698ce6f8 215 }
cf2251b6
JH
216 else if (!strcmp(argv[i], "--signoff") ||
217 !strcmp(argv[i], "-s")) {
6c4cca1c
EB
218 const char *committer;
219 const char *endpos;
220 setup_ident();
221 committer = git_committer_info(1);
222 endpos = strchr(committer, '>');
cf2251b6
JH
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 }
698ce6f8
JS
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
2448482b 234 argv[j++] = argv[i];
0377db77 235 }
2448482b
JS
236 argc = j;
237
add5c8a5 238 if (start_number < 0)
fa0f02df 239 start_number = 1;
63b398a4 240 if (numbered && keep_subject)
8ac80a57
JS
241 die ("-n and -k are mutually exclusive.");
242
2448482b
JS
243 argc = setup_revisions(argc, argv, &rev, "HEAD");
244 if (argc > 1)
245 die ("unrecognized argument: %s", argv[1]);
0377db77 246
c9b5ef99
TH
247 if (!rev.diffopt.output_format)
248 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
249
efd02016
JH
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
1f1e895f
LT
258 if (rev.pending.nr == 1) {
259 rev.pending.objects[0].item->flags |= UNINTERESTING;
e686eb98
JS
260 add_head(&rev);
261 }
262
81f3a188
JS
263 if (!use_stdout)
264 realstdout = fdopen(dup(1), "w");
265
91efcf60
JH
266 prepare_revision_walk(&rev);
267 while ((commit = get_revision(&rev)) != NULL) {
0377db77
JS
268 /* ignore merges */
269 if (commit->parents && commit->parents->next)
270 continue;
91efcf60
JH
271 nr++;
272 list = realloc(list, nr * sizeof(list[0]));
273 list[nr - 1] = commit;
274 }
0377db77 275 total = nr;
596524b3 276 if (numbered)
fa0f02df 277 rev.total = total + start_number - 1;
cf2251b6 278 rev.add_signoff = add_signoff;
91efcf60
JH
279 while (0 <= --nr) {
280 int shown;
281 commit = list[nr];
add5c8a5 282 rev.nr = total - nr + (start_number - 1);
0377db77 283 if (!use_stdout)
8ac80a57 284 reopen_stdout(commit, rev.nr, keep_subject);
91efcf60
JH
285 shown = log_tree_commit(&rev, commit);
286 free(commit->buffer);
287 commit->buffer = NULL;
add5c8a5
JH
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;
698ce6f8
JS
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 }
0377db77
JS
305 if (!use_stdout)
306 fclose(stdout);
91efcf60
JH
307 }
308 free(list);
309 return 0;
310}
311