Rename safe_strncpy() to strlcpy().
[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
91efcf60
JH
163int 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;
2448482b 168 int nr = 0, total, i, j;
0377db77 169 int use_stdout = 0;
596524b3 170 int numbered = 0;
fa0f02df 171 int start_number = -1;
8ac80a57 172 int keep_subject = 0;
cf2251b6 173 char *add_signoff = NULL;
91efcf60
JH
174
175 init_revisions(&rev);
176 rev.commit_format = CMIT_FMT_EMAIL;
177 rev.verbose_header = 1;
178 rev.diff = 1;
179 rev.diffopt.with_raw = 0;
180 rev.diffopt.with_stat = 1;
181 rev.combine_merges = 0;
182 rev.ignore_merges = 1;
183 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
91efcf60 184
20ff0680
JS
185 git_config(git_format_config);
186 rev.extra_headers = extra_headers;
187
2448482b
JS
188 /*
189 * Parse the arguments before setup_revisions(), or something
190 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
191 * possibly a valid SHA1.
192 */
193 for (i = 1, j = 1; i < argc; i++) {
194 if (!strcmp(argv[i], "--stdout"))
0377db77 195 use_stdout = 1;
596524b3
JS
196 else if (!strcmp(argv[i], "-n") ||
197 !strcmp(argv[i], "--numbered"))
198 numbered = 1;
fa0f02df
JS
199 else if (!strncmp(argv[i], "--start-number=", 15))
200 start_number = strtol(argv[i] + 15, NULL, 10);
201 else if (!strcmp(argv[i], "--start-number")) {
202 i++;
203 if (i == argc)
204 die("Need a number for --start-number");
205 start_number = strtol(argv[i], NULL, 10);
cf2251b6
JH
206 }
207 else if (!strcmp(argv[i], "-k") ||
8ac80a57
JS
208 !strcmp(argv[i], "--keep-subject")) {
209 keep_subject = 1;
210 rev.total = -1;
cf2251b6 211 }
efd02016
JH
212 else if (!strcmp(argv[i], "--output-directory") ||
213 !strcmp(argv[i], "-o")) {
2448482b 214 i++;
efd02016
JH
215 if (argc <= i)
216 die("Which directory?");
217 if (output_directory)
218 die("Two output directories?");
219 output_directory = argv[i];
698ce6f8 220 }
cf2251b6
JH
221 else if (!strcmp(argv[i], "--signoff") ||
222 !strcmp(argv[i], "-s")) {
6c4cca1c
EB
223 const char *committer;
224 const char *endpos;
225 setup_ident();
226 committer = git_committer_info(1);
227 endpos = strchr(committer, '>');
cf2251b6
JH
228 if (!endpos)
229 die("bogos committer info %s\n", committer);
230 add_signoff = xmalloc(endpos - committer + 2);
231 memcpy(add_signoff, committer, endpos - committer + 1);
232 add_signoff[endpos - committer + 1] = 0;
233 }
698ce6f8
JS
234 else if (!strcmp(argv[i], "--attach"))
235 rev.mime_boundary = git_version_string;
236 else if (!strncmp(argv[i], "--attach=", 9))
237 rev.mime_boundary = argv[i] + 9;
238 else
2448482b 239 argv[j++] = argv[i];
0377db77 240 }
2448482b
JS
241 argc = j;
242
add5c8a5 243 if (start_number < 0)
fa0f02df 244 start_number = 1;
63b398a4 245 if (numbered && keep_subject)
8ac80a57
JS
246 die ("-n and -k are mutually exclusive.");
247
2448482b
JS
248 argc = setup_revisions(argc, argv, &rev, "HEAD");
249 if (argc > 1)
250 die ("unrecognized argument: %s", argv[1]);
0377db77 251
efd02016
JH
252 if (output_directory) {
253 if (use_stdout)
254 die("standard output, or directory, which one?");
255 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
256 die("Could not create directory %s",
257 output_directory);
258 }
259
1f1e895f
LT
260 if (rev.pending.nr == 1) {
261 rev.pending.objects[0].item->flags |= UNINTERESTING;
e686eb98
JS
262 add_head(&rev);
263 }
264
81f3a188
JS
265 if (!use_stdout)
266 realstdout = fdopen(dup(1), "w");
267
91efcf60
JH
268 prepare_revision_walk(&rev);
269 while ((commit = get_revision(&rev)) != NULL) {
0377db77
JS
270 /* ignore merges */
271 if (commit->parents && commit->parents->next)
272 continue;
91efcf60
JH
273 nr++;
274 list = realloc(list, nr * sizeof(list[0]));
275 list[nr - 1] = commit;
276 }
0377db77 277 total = nr;
596524b3 278 if (numbered)
fa0f02df 279 rev.total = total + start_number - 1;
cf2251b6 280 rev.add_signoff = add_signoff;
91efcf60
JH
281 while (0 <= --nr) {
282 int shown;
283 commit = list[nr];
add5c8a5 284 rev.nr = total - nr + (start_number - 1);
0377db77 285 if (!use_stdout)
8ac80a57 286 reopen_stdout(commit, rev.nr, keep_subject);
91efcf60
JH
287 shown = log_tree_commit(&rev, commit);
288 free(commit->buffer);
289 commit->buffer = NULL;
add5c8a5
JH
290
291 /* We put one extra blank line between formatted
292 * patches and this flag is used by log-tree code
293 * to see if it needs to emit a LF before showing
294 * the log; when using one file per patch, we do
295 * not want the extra blank line.
296 */
297 if (!use_stdout)
298 rev.shown_one = 0;
698ce6f8
JS
299 if (shown) {
300 if (rev.mime_boundary)
301 printf("\n--%s%s--\n\n\n",
302 mime_boundary_leader,
303 rev.mime_boundary);
304 else
305 printf("-- \n%s\n\n", git_version_string);
306 }
0377db77
JS
307 if (!use_stdout)
308 fclose(stdout);
91efcf60
JH
309 }
310 free(list);
311 return 0;
312}
313