Makefile: export NO_SVN_TESTS
[git/fastimport.git] / builtin-log.c
blob864c6cd9ea1777bc15b12333b88b2e3a3f013321
1 /*
2 * Builtin "git log" and related commands (show, whatchanged)
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"
14 /* this is in builtin-diff.c */
15 void add_head(struct rev_info *revs);
17 static void cmd_log_init(int argc, const char **argv, char **envp,
18 struct rev_info *rev)
20 rev->abbrev = DEFAULT_ABBREV;
21 rev->commit_format = CMIT_FMT_DEFAULT;
22 rev->verbose_header = 1;
23 argc = setup_revisions(argc, argv, rev, "HEAD");
24 if (rev->diffopt.pickaxe || rev->diffopt.filter)
25 rev->always_show_header = 0;
26 if (argc > 1)
27 die("unrecognized argument: %s", argv[1]);
30 static int cmd_log_walk(struct rev_info *rev)
32 struct commit *commit;
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;
40 free_commit_list(commit->parents);
41 commit->parents = NULL;
43 return 0;
46 int cmd_whatchanged(int argc, const char **argv, char **envp)
48 struct rev_info rev;
50 init_revisions(&rev);
51 rev.diff = 1;
52 rev.diffopt.recursive = 1;
53 rev.simplify_history = 0;
54 cmd_log_init(argc, argv, envp, &rev);
55 if (!rev.diffopt.output_format)
56 rev.diffopt.output_format = DIFF_FORMAT_RAW;
57 return cmd_log_walk(&rev);
60 int cmd_show(int argc, const char **argv, char **envp)
62 struct rev_info rev;
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 cmd_log_init(argc, argv, envp, &rev);
73 return cmd_log_walk(&rev);
76 int cmd_log(int argc, const char **argv, char **envp)
78 struct rev_info rev;
80 init_revisions(&rev);
81 rev.always_show_header = 1;
82 cmd_log_init(argc, argv, envp, &rev);
83 return cmd_log_walk(&rev);
86 static int istitlechar(char c)
88 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
89 (c >= '0' && c <= '9') || c == '.' || c == '_';
92 static char *extra_headers = NULL;
93 static int extra_headers_size = 0;
95 static int git_format_config(const char *var, const char *value)
97 if (!strcmp(var, "format.headers")) {
98 int len = strlen(value);
99 extra_headers_size += len + 1;
100 extra_headers = realloc(extra_headers, extra_headers_size);
101 extra_headers[extra_headers_size - len - 1] = 0;
102 strcat(extra_headers, value);
103 return 0;
105 return git_default_config(var, value);
109 static FILE *realstdout = NULL;
110 static const char *output_directory = NULL;
112 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
114 char filename[1024];
115 char *sol;
116 int len = 0;
118 if (output_directory) {
119 strlcpy(filename, output_directory, 1010);
120 len = strlen(filename);
121 if (filename[len - 1] != '/')
122 filename[len++] = '/';
125 sprintf(filename + len, "%04d", nr);
126 len = strlen(filename);
128 sol = strstr(commit->buffer, "\n\n");
129 if (sol) {
130 int j, space = 1;
132 sol += 2;
133 /* strip [PATCH] or [PATCH blabla] */
134 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
135 char *eos = strchr(sol + 6, ']');
136 if (eos) {
137 while (isspace(*eos))
138 eos++;
139 sol = eos;
143 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
144 if (istitlechar(sol[j])) {
145 if (space) {
146 filename[len++] = '-';
147 space = 0;
149 filename[len++] = sol[j];
150 if (sol[j] == '.')
151 while (sol[j + 1] == '.')
152 j++;
153 } else
154 space = 1;
156 while (filename[len - 1] == '.' || filename[len - 1] == '-')
157 len--;
159 strcpy(filename + len, ".txt");
160 fprintf(realstdout, "%s\n", filename);
161 freopen(filename, "w", stdout);
164 static int get_patch_id(struct commit *commit, struct diff_options *options,
165 unsigned char *sha1)
167 diff_tree_sha1(commit->parents->item->object.sha1, commit->object.sha1,
168 "", options);
169 diffcore_std(options);
170 return diff_flush_patch_id(options, sha1);
173 static void get_patch_ids(struct rev_info *rev, struct diff_options *options)
175 struct rev_info check_rev;
176 struct commit *commit;
177 struct object *o1, *o2;
178 unsigned flags1, flags2;
179 unsigned char sha1[20];
181 if (rev->pending.nr != 2)
182 die("Need exactly one range.");
184 o1 = rev->pending.objects[0].item;
185 flags1 = o1->flags;
186 o2 = rev->pending.objects[1].item;
187 flags2 = o2->flags;
189 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
190 die("Not a range.");
192 diff_setup(options);
193 options->recursive = 1;
194 if (diff_setup_done(options) < 0)
195 die("diff_setup_done failed");
197 /* given a range a..b get all patch ids for b..a */
198 init_revisions(&check_rev);
199 o1->flags ^= UNINTERESTING;
200 o2->flags ^= UNINTERESTING;
201 add_pending_object(&check_rev, o1, "o1");
202 add_pending_object(&check_rev, o2, "o2");
203 prepare_revision_walk(&check_rev);
205 while ((commit = get_revision(&check_rev)) != NULL) {
206 /* ignore merges */
207 if (commit->parents && commit->parents->next)
208 continue;
210 if (!get_patch_id(commit, options, sha1))
211 created_object(sha1, xcalloc(1, sizeof(struct object)));
214 /* reset for next revision walk */
215 clear_commit_marks((struct commit *)o1,
216 SEEN | UNINTERESTING | SHOWN | ADDED);
217 clear_commit_marks((struct commit *)o2,
218 SEEN | UNINTERESTING | SHOWN | ADDED);
219 o1->flags = flags1;
220 o2->flags = flags2;
223 int cmd_format_patch(int argc, const char **argv, char **envp)
225 struct commit *commit;
226 struct commit **list = NULL;
227 struct rev_info rev;
228 int nr = 0, total, i, j;
229 int use_stdout = 0;
230 int numbered = 0;
231 int start_number = -1;
232 int keep_subject = 0;
233 int ignore_if_in_upstream = 0;
234 struct diff_options patch_id_opts;
235 char *add_signoff = NULL;
237 init_revisions(&rev);
238 rev.commit_format = CMIT_FMT_EMAIL;
239 rev.verbose_header = 1;
240 rev.diff = 1;
241 rev.combine_merges = 0;
242 rev.ignore_merges = 1;
243 rev.diffopt.msg_sep = "";
244 rev.diffopt.recursive = 1;
246 git_config(git_format_config);
247 rev.extra_headers = extra_headers;
250 * Parse the arguments before setup_revisions(), or something
251 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
252 * possibly a valid SHA1.
254 for (i = 1, j = 1; i < argc; i++) {
255 if (!strcmp(argv[i], "--stdout"))
256 use_stdout = 1;
257 else if (!strcmp(argv[i], "-n") ||
258 !strcmp(argv[i], "--numbered"))
259 numbered = 1;
260 else if (!strncmp(argv[i], "--start-number=", 15))
261 start_number = strtol(argv[i] + 15, NULL, 10);
262 else if (!strcmp(argv[i], "--start-number")) {
263 i++;
264 if (i == argc)
265 die("Need a number for --start-number");
266 start_number = strtol(argv[i], NULL, 10);
268 else if (!strcmp(argv[i], "-k") ||
269 !strcmp(argv[i], "--keep-subject")) {
270 keep_subject = 1;
271 rev.total = -1;
273 else if (!strcmp(argv[i], "--output-directory") ||
274 !strcmp(argv[i], "-o")) {
275 i++;
276 if (argc <= i)
277 die("Which directory?");
278 if (output_directory)
279 die("Two output directories?");
280 output_directory = argv[i];
282 else if (!strcmp(argv[i], "--signoff") ||
283 !strcmp(argv[i], "-s")) {
284 const char *committer;
285 const char *endpos;
286 setup_ident();
287 committer = git_committer_info(1);
288 endpos = strchr(committer, '>');
289 if (!endpos)
290 die("bogos committer info %s\n", committer);
291 add_signoff = xmalloc(endpos - committer + 2);
292 memcpy(add_signoff, committer, endpos - committer + 1);
293 add_signoff[endpos - committer + 1] = 0;
295 else if (!strcmp(argv[i], "--attach"))
296 rev.mime_boundary = git_version_string;
297 else if (!strncmp(argv[i], "--attach=", 9))
298 rev.mime_boundary = argv[i] + 9;
299 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
300 ignore_if_in_upstream = 1;
301 else
302 argv[j++] = argv[i];
304 argc = j;
306 if (start_number < 0)
307 start_number = 1;
308 if (numbered && keep_subject)
309 die ("-n and -k are mutually exclusive.");
311 argc = setup_revisions(argc, argv, &rev, "HEAD");
312 if (argc > 1)
313 die ("unrecognized argument: %s", argv[1]);
315 if (!rev.diffopt.output_format)
316 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
318 if (output_directory) {
319 if (use_stdout)
320 die("standard output, or directory, which one?");
321 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
322 die("Could not create directory %s",
323 output_directory);
326 if (rev.pending.nr == 1) {
327 rev.pending.objects[0].item->flags |= UNINTERESTING;
328 add_head(&rev);
331 if (ignore_if_in_upstream)
332 get_patch_ids(&rev, &patch_id_opts);
334 if (!use_stdout)
335 realstdout = fdopen(dup(1), "w");
337 prepare_revision_walk(&rev);
338 while ((commit = get_revision(&rev)) != NULL) {
339 unsigned char sha1[20];
341 /* ignore merges */
342 if (commit->parents && commit->parents->next)
343 continue;
345 if (ignore_if_in_upstream &&
346 !get_patch_id(commit, &patch_id_opts, sha1) &&
347 lookup_object(sha1))
348 continue;
350 nr++;
351 list = realloc(list, nr * sizeof(list[0]));
352 list[nr - 1] = commit;
354 total = nr;
355 if (numbered)
356 rev.total = total + start_number - 1;
357 rev.add_signoff = add_signoff;
358 while (0 <= --nr) {
359 int shown;
360 commit = list[nr];
361 rev.nr = total - nr + (start_number - 1);
362 if (!use_stdout)
363 reopen_stdout(commit, rev.nr, keep_subject);
364 shown = log_tree_commit(&rev, commit);
365 free(commit->buffer);
366 commit->buffer = NULL;
368 /* We put one extra blank line between formatted
369 * patches and this flag is used by log-tree code
370 * to see if it needs to emit a LF before showing
371 * the log; when using one file per patch, we do
372 * not want the extra blank line.
374 if (!use_stdout)
375 rev.shown_one = 0;
376 if (shown) {
377 if (rev.mime_boundary)
378 printf("\n--%s%s--\n\n\n",
379 mime_boundary_leader,
380 rev.mime_boundary);
381 else
382 printf("-- \n%s\n\n", git_version_string);
384 if (!use_stdout)
385 fclose(stdout);
387 free(list);
388 return 0;