2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
14 /* this is in builtin-diff.c */
15 void add_head(struct rev_info *revs);
17 static int cmd_log_wc(int argc, const char **argv, char **envp,
20 struct commit *commit;
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;
35 die("unrecognized argument: %s", argv[1]);
37 prepare_revision_walk(rev);
39 while ((commit = get_revision(rev)) != NULL) {
40 log_tree_commit(rev, commit);
42 commit->buffer = NULL;
43 free_commit_list(commit->parents);
44 commit->parents = NULL;
49 int cmd_whatchanged(int argc, const char **argv, char **envp)
55 rev.diffopt.recursive = 1;
56 rev.simplify_history = 0;
57 return cmd_log_wc(argc, argv, envp, &rev);
60 int cmd_show(int argc, const char **argv, char **envp)
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;
72 return cmd_log_wc(argc, argv, envp, &rev);
75 int cmd_log(int argc, const char **argv, char **envp)
80 rev.always_show_header = 1;
81 rev.diffopt.recursive = 1;
82 return cmd_log_wc(argc, argv, envp, &rev);
85 static int istitlechar(char c)
87 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
88 (c >= '0' && c <= '9') || c == '.' || c == '_';
91 static char *extra_headers = NULL;
92 static int extra_headers_size = 0;
94 static int git_format_config(const char *var, const char *value)
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);
104 return git_default_config(var, value);
108 static FILE *realstdout = NULL;
109 static const char *output_directory = NULL;
111 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
117 if (output_directory) {
118 strlcpy(filename, output_directory, 1010);
119 len = strlen(filename);
120 if (filename[len - 1] != '/')
121 filename[len++] = '/';
124 sprintf(filename + len, "%04d", nr);
125 len = strlen(filename);
127 sol = strstr(commit->buffer, "\n\n");
132 /* strip [PATCH] or [PATCH blabla] */
133 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
134 char *eos = strchr(sol + 6, ']');
136 while (isspace(*eos))
142 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
143 if (istitlechar(sol[j])) {
145 filename[len++] = '-';
148 filename[len++] = sol[j];
150 while (sol[j + 1] == '.')
155 while (filename[len - 1] == '.' || filename[len - 1] == '-')
158 strcpy(filename + len, ".txt");
159 fprintf(realstdout, "%s\n", filename);
160 freopen(filename, "w", stdout);
163 static int get_patch_id(struct commit *commit, struct diff_options *options,
166 diff_tree_sha1(commit->parents->item->object.sha1, commit->object.sha1,
168 diffcore_std(options);
169 return diff_flush_patch_id(options, sha1);
172 static void get_patch_ids(struct rev_info *rev, struct diff_options *options)
174 struct rev_info check_rev;
175 struct commit *commit;
176 struct object *o1, *o2;
177 unsigned flags1, flags2;
178 unsigned char sha1[20];
180 if (rev->pending.nr != 2)
181 die("Need exactly one range.");
183 o1 = rev->pending.objects[0].item;
185 o2 = rev->pending.objects[1].item;
188 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
192 options->recursive = 1;
193 if (diff_setup_done(options) < 0)
194 die("diff_setup_done failed");
196 /* given a range a..b get all patch ids for b..a */
197 init_revisions(&check_rev);
198 o1->flags ^= UNINTERESTING;
199 o2->flags ^= UNINTERESTING;
200 add_pending_object(&check_rev, o1, "o1");
201 add_pending_object(&check_rev, o2, "o2");
202 prepare_revision_walk(&check_rev);
204 while ((commit = get_revision(&check_rev)) != NULL) {
206 if (commit->parents && commit->parents->next)
209 if (!get_patch_id(commit, options, sha1))
210 created_object(sha1, xcalloc(1, sizeof(struct object)));
213 /* reset for next revision walk */
214 clear_commit_marks((struct commit *)o1,
215 SEEN | UNINTERESTING | SHOWN | ADDED);
216 clear_commit_marks((struct commit *)o2,
217 SEEN | UNINTERESTING | SHOWN | ADDED);
222 int cmd_format_patch(int argc, const char **argv, char **envp)
224 struct commit *commit;
225 struct commit **list = NULL;
227 int nr = 0, total, i, j;
230 int start_number = -1;
231 int keep_subject = 0;
232 int ignore_if_in_upstream = 0;
233 struct diff_options patch_id_opts;
234 char *add_signoff = NULL;
236 init_revisions(&rev);
237 rev.commit_format = CMIT_FMT_EMAIL;
238 rev.verbose_header = 1;
240 rev.diffopt.with_raw = 0;
241 rev.diffopt.with_stat = 1;
242 rev.combine_merges = 0;
243 rev.ignore_merges = 1;
244 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
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"))
257 else if (!strcmp(argv[i], "-n") ||
258 !strcmp(argv[i], "--numbered"))
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")) {
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")) {
273 else if (!strcmp(argv[i], "--output-directory") ||
274 !strcmp(argv[i], "-o")) {
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;
287 committer = git_committer_info(1);
288 endpos = strchr(committer, '>');
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;
306 if (start_number < 0)
308 if (numbered && keep_subject)
309 die ("-n and -k are mutually exclusive.");
311 argc = setup_revisions(argc, argv, &rev, "HEAD");
313 die ("unrecognized argument: %s", argv[1]);
315 if (output_directory) {
317 die("standard output, or directory, which one?");
318 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
319 die("Could not create directory %s",
323 if (rev.pending.nr == 1) {
324 rev.pending.objects[0].item->flags |= UNINTERESTING;
328 if (ignore_if_in_upstream)
329 get_patch_ids(&rev, &patch_id_opts);
332 realstdout = fdopen(dup(1), "w");
334 prepare_revision_walk(&rev);
335 while ((commit = get_revision(&rev)) != NULL) {
336 unsigned char sha1[20];
339 if (commit->parents && commit->parents->next)
342 if (ignore_if_in_upstream &&
343 !get_patch_id(commit, &patch_id_opts, sha1) &&
348 list = realloc(list, nr * sizeof(list[0]));
349 list[nr - 1] = commit;
353 rev.total = total + start_number - 1;
354 rev.add_signoff = add_signoff;
358 rev.nr = total - nr + (start_number - 1);
360 reopen_stdout(commit, rev.nr, keep_subject);
361 shown = log_tree_commit(&rev, commit);
362 free(commit->buffer);
363 commit->buffer = NULL;
365 /* We put one extra blank line between formatted
366 * patches and this flag is used by log-tree code
367 * to see if it needs to emit a LF before showing
368 * the log; when using one file per patch, we do
369 * not want the extra blank line.
374 if (rev.mime_boundary)
375 printf("\n--%s%s--\n\n\n",
376 mime_boundary_leader,
379 printf("-- \n%s\n\n", git_version_string);