2 * Builtin "git log" and related commands (show, whatchanged)
4 * (C) Copyright 2006 Linus Torvalds
16 /* this is in builtin-diff.c */
17 void add_head(struct rev_info *revs);
19 static void cmd_log_init(int argc, const char **argv, char **envp,
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->diffopt.pickaxe || rev->diffopt.filter)
27 rev->always_show_header = 0;
29 die("unrecognized argument: %s", argv[1]);
32 static int cmd_log_walk(struct rev_info *rev)
34 struct commit *commit;
36 prepare_revision_walk(rev);
38 while ((commit = get_revision(rev)) != NULL) {
39 log_tree_commit(rev, commit);
41 commit->buffer = NULL;
42 free_commit_list(commit->parents);
43 commit->parents = NULL;
48 int cmd_whatchanged(int argc, const char **argv, char **envp)
53 git_config(git_diff_ui_config);
54 diff_setup(&rev.diffopt);
56 rev.diffopt.recursive = 1;
57 rev.simplify_history = 0;
58 cmd_log_init(argc, argv, envp, &rev);
59 if (!rev.diffopt.output_format)
60 rev.diffopt.output_format = DIFF_FORMAT_RAW;
61 return cmd_log_walk(&rev);
64 int cmd_show(int argc, const char **argv, char **envp)
69 git_config(git_diff_ui_config);
70 diff_setup(&rev.diffopt);
72 rev.diffopt.recursive = 1;
73 rev.combine_merges = 1;
74 rev.dense_combined_merges = 1;
75 rev.always_show_header = 1;
76 rev.ignore_merges = 0;
78 cmd_log_init(argc, argv, envp, &rev);
79 return cmd_log_walk(&rev);
82 int cmd_log(int argc, const char **argv, char **envp)
87 git_config(git_diff_ui_config);
88 diff_setup(&rev.diffopt);
89 rev.always_show_header = 1;
90 cmd_log_init(argc, argv, envp, &rev);
91 return cmd_log_walk(&rev);
94 static int istitlechar(char c)
96 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
97 (c >= '0' && c <= '9') || c == '.' || c == '_';
100 static char *extra_headers = NULL;
101 static int extra_headers_size = 0;
103 static int git_format_config(const char *var, const char *value)
105 if (!strcmp(var, "format.headers")) {
106 int len = strlen(value);
107 extra_headers_size += len + 1;
108 extra_headers = realloc(extra_headers, extra_headers_size);
109 extra_headers[extra_headers_size - len - 1] = 0;
110 strcat(extra_headers, value);
113 if (!strcmp(var, "diff.color")) {
116 return git_diff_ui_config(var, value);
120 static FILE *realstdout = NULL;
121 static const char *output_directory = NULL;
123 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
129 if (output_directory) {
130 strlcpy(filename, output_directory, 1010);
131 len = strlen(filename);
132 if (filename[len - 1] != '/')
133 filename[len++] = '/';
136 sprintf(filename + len, "%04d", nr);
137 len = strlen(filename);
139 sol = strstr(commit->buffer, "\n\n");
144 /* strip [PATCH] or [PATCH blabla] */
145 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
146 char *eos = strchr(sol + 6, ']');
148 while (isspace(*eos))
154 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
155 if (istitlechar(sol[j])) {
157 filename[len++] = '-';
160 filename[len++] = sol[j];
162 while (sol[j + 1] == '.')
167 while (filename[len - 1] == '.' || filename[len - 1] == '-')
170 strcpy(filename + len, ".txt");
171 fprintf(realstdout, "%s\n", filename);
172 freopen(filename, "w", stdout);
175 static int get_patch_id(struct commit *commit, struct diff_options *options,
178 diff_tree_sha1(commit->parents->item->object.sha1, commit->object.sha1,
180 diffcore_std(options);
181 return diff_flush_patch_id(options, sha1);
184 static void get_patch_ids(struct rev_info *rev, struct diff_options *options)
186 struct rev_info check_rev;
187 struct commit *commit;
188 struct object *o1, *o2;
189 unsigned flags1, flags2;
190 unsigned char sha1[20];
192 if (rev->pending.nr != 2)
193 die("Need exactly one range.");
195 o1 = rev->pending.objects[0].item;
197 o2 = rev->pending.objects[1].item;
200 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
204 options->recursive = 1;
205 if (diff_setup_done(options) < 0)
206 die("diff_setup_done failed");
208 /* given a range a..b get all patch ids for b..a */
209 init_revisions(&check_rev);
210 o1->flags ^= UNINTERESTING;
211 o2->flags ^= UNINTERESTING;
212 add_pending_object(&check_rev, o1, "o1");
213 add_pending_object(&check_rev, o2, "o2");
214 prepare_revision_walk(&check_rev);
216 while ((commit = get_revision(&check_rev)) != NULL) {
218 if (commit->parents && commit->parents->next)
221 if (!get_patch_id(commit, options, sha1))
222 created_object(sha1, xcalloc(1, sizeof(struct object)));
225 /* reset for next revision walk */
226 clear_commit_marks((struct commit *)o1,
227 SEEN | UNINTERESTING | SHOWN | ADDED);
228 clear_commit_marks((struct commit *)o2,
229 SEEN | UNINTERESTING | SHOWN | ADDED);
234 static void gen_message_id(char *dest, unsigned int length, char *base)
236 const char *committer = git_committer_info(1);
237 const char *email_start = strrchr(committer, '<');
238 const char *email_end = strrchr(committer, '>');
239 if(!email_start || !email_end || email_start > email_end - 1)
240 die("Could not extract email from committer identity.");
241 snprintf(dest, length, "%s.%lu.git.%.*s", base,
242 (unsigned long) time(NULL),
243 (int)(email_end - email_start - 1), email_start + 1);
246 int cmd_format_patch(int argc, const char **argv, char **envp)
248 struct commit *commit;
249 struct commit **list = NULL;
251 int nr = 0, total, i, j;
254 int start_number = -1;
255 int keep_subject = 0;
256 int ignore_if_in_upstream = 0;
258 const char *in_reply_to = NULL;
259 struct diff_options patch_id_opts;
260 char *add_signoff = NULL;
261 char message_id[1024];
262 char ref_message_id[1024];
264 git_config(git_format_config);
265 init_revisions(&rev);
266 rev.commit_format = CMIT_FMT_EMAIL;
267 rev.verbose_header = 1;
269 rev.combine_merges = 0;
270 rev.ignore_merges = 1;
271 rev.diffopt.msg_sep = "";
272 rev.diffopt.recursive = 1;
274 rev.extra_headers = extra_headers;
277 * Parse the arguments before setup_revisions(), or something
278 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
279 * possibly a valid SHA1.
281 for (i = 1, j = 1; i < argc; i++) {
282 if (!strcmp(argv[i], "--stdout"))
284 else if (!strcmp(argv[i], "-n") ||
285 !strcmp(argv[i], "--numbered"))
287 else if (!strncmp(argv[i], "--start-number=", 15))
288 start_number = strtol(argv[i] + 15, NULL, 10);
289 else if (!strcmp(argv[i], "--start-number")) {
292 die("Need a number for --start-number");
293 start_number = strtol(argv[i], NULL, 10);
295 else if (!strcmp(argv[i], "-k") ||
296 !strcmp(argv[i], "--keep-subject")) {
300 else if (!strcmp(argv[i], "--output-directory") ||
301 !strcmp(argv[i], "-o")) {
304 die("Which directory?");
305 if (output_directory)
306 die("Two output directories?");
307 output_directory = argv[i];
309 else if (!strcmp(argv[i], "--signoff") ||
310 !strcmp(argv[i], "-s")) {
311 const char *committer;
314 committer = git_committer_info(1);
315 endpos = strchr(committer, '>');
317 die("bogos committer info %s\n", committer);
318 add_signoff = xmalloc(endpos - committer + 2);
319 memcpy(add_signoff, committer, endpos - committer + 1);
320 add_signoff[endpos - committer + 1] = 0;
322 else if (!strcmp(argv[i], "--attach"))
323 rev.mime_boundary = git_version_string;
324 else if (!strncmp(argv[i], "--attach=", 9))
325 rev.mime_boundary = argv[i] + 9;
326 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
327 ignore_if_in_upstream = 1;
328 else if (!strcmp(argv[i], "--thread"))
330 else if (!strncmp(argv[i], "--in-reply-to=", 14))
331 in_reply_to = argv[i] + 14;
332 else if (!strcmp(argv[i], "--in-reply-to")) {
335 die("Need a Message-Id for --in-reply-to");
336 in_reply_to = argv[i];
343 if (start_number < 0)
345 if (numbered && keep_subject)
346 die ("-n and -k are mutually exclusive.");
348 argc = setup_revisions(argc, argv, &rev, "HEAD");
350 die ("unrecognized argument: %s", argv[1]);
352 if (!rev.diffopt.output_format)
353 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
355 if (output_directory) {
357 die("standard output, or directory, which one?");
358 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
359 die("Could not create directory %s",
363 if (rev.pending.nr == 1) {
364 rev.pending.objects[0].item->flags |= UNINTERESTING;
368 if (ignore_if_in_upstream)
369 get_patch_ids(&rev, &patch_id_opts);
372 realstdout = fdopen(dup(1), "w");
374 prepare_revision_walk(&rev);
375 while ((commit = get_revision(&rev)) != NULL) {
376 unsigned char sha1[20];
379 if (commit->parents && commit->parents->next)
382 if (ignore_if_in_upstream &&
383 !get_patch_id(commit, &patch_id_opts, sha1) &&
388 list = realloc(list, nr * sizeof(list[0]));
389 list[nr - 1] = commit;
393 rev.total = total + start_number - 1;
394 rev.add_signoff = add_signoff;
395 rev.ref_message_id = in_reply_to;
399 rev.nr = total - nr + (start_number - 1);
400 /* Make the second and subsequent mails replies to the first */
402 if (nr == (total - 2)) {
403 strncpy(ref_message_id, message_id,
404 sizeof(ref_message_id));
405 ref_message_id[sizeof(ref_message_id)-1]='\0';
406 rev.ref_message_id = ref_message_id;
408 gen_message_id(message_id, sizeof(message_id),
409 sha1_to_hex(commit->object.sha1));
410 rev.message_id = message_id;
413 reopen_stdout(commit, rev.nr, keep_subject);
414 shown = log_tree_commit(&rev, commit);
415 free(commit->buffer);
416 commit->buffer = NULL;
418 /* We put one extra blank line between formatted
419 * patches and this flag is used by log-tree code
420 * to see if it needs to emit a LF before showing
421 * the log; when using one file per patch, we do
422 * not want the extra blank line.
427 if (rev.mime_boundary)
428 printf("\n--%s%s--\n\n\n",
429 mime_boundary_leader,
432 printf("-- \n%s\n\n", git_version_string);