Fix up some fallout from "setup_git_directory()" cleanups
[git] / builtin-log.c
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"
12 #include "builtin.h"
13 #include <time.h>
14 #include <sys/time.h>
15
16 /* this is in builtin-diff.c */
17 void add_head(struct rev_info *revs);
18
19 static void cmd_log_init(int argc, const char **argv, const char *prefix,
20                       struct rev_info *rev)
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");
26         if (rev->diffopt.pickaxe || rev->diffopt.filter)
27                 rev->always_show_header = 0;
28         if (argc > 1)
29                 die("unrecognized argument: %s", argv[1]);
30 }
31
32 static int cmd_log_walk(struct rev_info *rev)
33 {
34         struct commit *commit;
35
36         prepare_revision_walk(rev);
37         setup_pager();
38         while ((commit = get_revision(rev)) != NULL) {
39                 log_tree_commit(rev, commit);
40                 free(commit->buffer);
41                 commit->buffer = NULL;
42                 free_commit_list(commit->parents);
43                 commit->parents = NULL;
44         }
45         return 0;
46 }
47
48 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
49 {
50         struct rev_info rev;
51
52         git_config(git_diff_ui_config);
53         init_revisions(&rev, prefix);
54         rev.diff = 1;
55         rev.diffopt.recursive = 1;
56         rev.simplify_history = 0;
57         cmd_log_init(argc, argv, prefix, &rev);
58         if (!rev.diffopt.output_format)
59                 rev.diffopt.output_format = DIFF_FORMAT_RAW;
60         return cmd_log_walk(&rev);
61 }
62
63 int cmd_show(int argc, const char **argv, const char *prefix)
64 {
65         struct rev_info rev;
66
67         git_config(git_diff_ui_config);
68         init_revisions(&rev, prefix);
69         rev.diff = 1;
70         rev.diffopt.recursive = 1;
71         rev.combine_merges = 1;
72         rev.dense_combined_merges = 1;
73         rev.always_show_header = 1;
74         rev.ignore_merges = 0;
75         rev.no_walk = 1;
76         cmd_log_init(argc, argv, prefix, &rev);
77         return cmd_log_walk(&rev);
78 }
79
80 int cmd_log(int argc, const char **argv, const char *prefix)
81 {
82         struct rev_info rev;
83
84         git_config(git_diff_ui_config);
85         init_revisions(&rev, prefix);
86         rev.always_show_header = 1;
87         cmd_log_init(argc, argv, prefix, &rev);
88         return cmd_log_walk(&rev);
89 }
90
91 static int istitlechar(char c)
92 {
93         return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
94                 (c >= '0' && c <= '9') || c == '.' || c == '_';
95 }
96
97 static char *extra_headers = NULL;
98 static int extra_headers_size = 0;
99
100 static int git_format_config(const char *var, const char *value)
101 {
102         if (!strcmp(var, "format.headers")) {
103                 int len = strlen(value);
104                 extra_headers_size += len + 1;
105                 extra_headers = realloc(extra_headers, extra_headers_size);
106                 extra_headers[extra_headers_size - len - 1] = 0;
107                 strcat(extra_headers, value);
108                 return 0;
109         }
110         if (!strcmp(var, "diff.color")) {
111                 return 0;
112         }
113         return git_diff_ui_config(var, value);
114 }
115
116
117 static FILE *realstdout = NULL;
118 static const char *output_directory = NULL;
119
120 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
121 {
122         char filename[1024];
123         char *sol;
124         int len = 0;
125
126         if (output_directory) {
127                 strlcpy(filename, output_directory, 1010);
128                 len = strlen(filename);
129                 if (filename[len - 1] != '/')
130                         filename[len++] = '/';
131         }
132
133         sprintf(filename + len, "%04d", nr);
134         len = strlen(filename);
135
136         sol = strstr(commit->buffer, "\n\n");
137         if (sol) {
138                 int j, space = 1;
139
140                 sol += 2;
141                 /* strip [PATCH] or [PATCH blabla] */
142                 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
143                         char *eos = strchr(sol + 6, ']');
144                         if (eos) {
145                                 while (isspace(*eos))
146                                         eos++;
147                                 sol = eos;
148                         }
149                 }
150
151                 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
152                         if (istitlechar(sol[j])) {
153                                 if (space) {
154                                         filename[len++] = '-';
155                                         space = 0;
156                                 }
157                                 filename[len++] = sol[j];
158                                 if (sol[j] == '.')
159                                         while (sol[j + 1] == '.')
160                                                 j++;
161                         } else
162                                 space = 1;
163                 }
164                 while (filename[len - 1] == '.' || filename[len - 1] == '-')
165                         len--;
166         }
167         strcpy(filename + len, ".txt");
168         fprintf(realstdout, "%s\n", filename);
169         freopen(filename, "w", stdout);
170 }
171
172 static int get_patch_id(struct commit *commit, struct diff_options *options,
173                 unsigned char *sha1)
174 {
175         diff_tree_sha1(commit->parents->item->object.sha1, commit->object.sha1,
176                         "", options);
177         diffcore_std(options);
178         return diff_flush_patch_id(options, sha1);
179 }
180
181 static void get_patch_ids(struct rev_info *rev, struct diff_options *options, const char *prefix)
182 {
183         struct rev_info check_rev;
184         struct commit *commit;
185         struct object *o1, *o2;
186         unsigned flags1, flags2;
187         unsigned char sha1[20];
188
189         if (rev->pending.nr != 2)
190                 die("Need exactly one range.");
191
192         o1 = rev->pending.objects[0].item;
193         flags1 = o1->flags;
194         o2 = rev->pending.objects[1].item;
195         flags2 = o2->flags;
196
197         if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
198                 die("Not a range.");
199
200         diff_setup(options);
201         options->recursive = 1;
202         if (diff_setup_done(options) < 0)
203                 die("diff_setup_done failed");
204
205         /* given a range a..b get all patch ids for b..a */
206         init_revisions(&check_rev, prefix);
207         o1->flags ^= UNINTERESTING;
208         o2->flags ^= UNINTERESTING;
209         add_pending_object(&check_rev, o1, "o1");
210         add_pending_object(&check_rev, o2, "o2");
211         prepare_revision_walk(&check_rev);
212
213         while ((commit = get_revision(&check_rev)) != NULL) {
214                 /* ignore merges */
215                 if (commit->parents && commit->parents->next)
216                         continue;
217
218                 if (!get_patch_id(commit, options, sha1))
219                         created_object(sha1, xcalloc(1, sizeof(struct object)));
220         }
221
222         /* reset for next revision walk */
223         clear_commit_marks((struct commit *)o1,
224                         SEEN | UNINTERESTING | SHOWN | ADDED);
225         clear_commit_marks((struct commit *)o2,
226                         SEEN | UNINTERESTING | SHOWN | ADDED);
227         o1->flags = flags1;
228         o2->flags = flags2;
229 }
230
231 static void gen_message_id(char *dest, unsigned int length, char *base)
232 {
233         const char *committer = git_committer_info(1);
234         const char *email_start = strrchr(committer, '<');
235         const char *email_end = strrchr(committer, '>');
236         if(!email_start || !email_end || email_start > email_end - 1)
237                 die("Could not extract email from committer identity.");
238         snprintf(dest, length, "%s.%lu.git.%.*s", base,
239                  (unsigned long) time(NULL),
240                  (int)(email_end - email_start - 1), email_start + 1);
241 }
242
243 int cmd_format_patch(int argc, const char **argv, const char *prefix)
244 {
245         struct commit *commit;
246         struct commit **list = NULL;
247         struct rev_info rev;
248         int nr = 0, total, i, j;
249         int use_stdout = 0;
250         int numbered = 0;
251         int start_number = -1;
252         int keep_subject = 0;
253         int ignore_if_in_upstream = 0;
254         int thread = 0;
255         const char *in_reply_to = NULL;
256         struct diff_options patch_id_opts;
257         char *add_signoff = NULL;
258         char message_id[1024];
259         char ref_message_id[1024];
260
261         git_config(git_format_config);
262         init_revisions(&rev, prefix);
263         rev.commit_format = CMIT_FMT_EMAIL;
264         rev.verbose_header = 1;
265         rev.diff = 1;
266         rev.combine_merges = 0;
267         rev.ignore_merges = 1;
268         rev.diffopt.msg_sep = "";
269         rev.diffopt.recursive = 1;
270
271         rev.extra_headers = extra_headers;
272
273         /*
274          * Parse the arguments before setup_revisions(), or something
275          * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
276          * possibly a valid SHA1.
277          */
278         for (i = 1, j = 1; i < argc; i++) {
279                 if (!strcmp(argv[i], "--stdout"))
280                         use_stdout = 1;
281                 else if (!strcmp(argv[i], "-n") ||
282                                 !strcmp(argv[i], "--numbered"))
283                         numbered = 1;
284                 else if (!strncmp(argv[i], "--start-number=", 15))
285                         start_number = strtol(argv[i] + 15, NULL, 10);
286                 else if (!strcmp(argv[i], "--start-number")) {
287                         i++;
288                         if (i == argc)
289                                 die("Need a number for --start-number");
290                         start_number = strtol(argv[i], NULL, 10);
291                 }
292                 else if (!strcmp(argv[i], "-k") ||
293                                 !strcmp(argv[i], "--keep-subject")) {
294                         keep_subject = 1;
295                         rev.total = -1;
296                 }
297                 else if (!strcmp(argv[i], "--output-directory") ||
298                          !strcmp(argv[i], "-o")) {
299                         i++;
300                         if (argc <= i)
301                                 die("Which directory?");
302                         if (output_directory)
303                                 die("Two output directories?");
304                         output_directory = argv[i];
305                 }
306                 else if (!strcmp(argv[i], "--signoff") ||
307                          !strcmp(argv[i], "-s")) {
308                         const char *committer;
309                         const char *endpos;
310                         setup_ident();
311                         committer = git_committer_info(1);
312                         endpos = strchr(committer, '>');
313                         if (!endpos)
314                                 die("bogos committer info %s\n", committer);
315                         add_signoff = xmalloc(endpos - committer + 2);
316                         memcpy(add_signoff, committer, endpos - committer + 1);
317                         add_signoff[endpos - committer + 1] = 0;
318                 }
319                 else if (!strcmp(argv[i], "--attach"))
320                         rev.mime_boundary = git_version_string;
321                 else if (!strncmp(argv[i], "--attach=", 9))
322                         rev.mime_boundary = argv[i] + 9;
323                 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
324                         ignore_if_in_upstream = 1;
325                 else if (!strcmp(argv[i], "--thread"))
326                         thread = 1;
327                 else if (!strncmp(argv[i], "--in-reply-to=", 14))
328                         in_reply_to = argv[i] + 14;
329                 else if (!strcmp(argv[i], "--in-reply-to")) {
330                         i++;
331                         if (i == argc)
332                                 die("Need a Message-Id for --in-reply-to");
333                         in_reply_to = argv[i];
334                 }
335                 else
336                         argv[j++] = argv[i];
337         }
338         argc = j;
339
340         if (start_number < 0)
341                 start_number = 1;
342         if (numbered && keep_subject)
343                 die ("-n and -k are mutually exclusive.");
344
345         argc = setup_revisions(argc, argv, &rev, "HEAD");
346         if (argc > 1)
347                 die ("unrecognized argument: %s", argv[1]);
348
349         if (!rev.diffopt.output_format)
350                 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
351
352         if (output_directory) {
353                 if (use_stdout)
354                         die("standard output, or directory, which one?");
355                 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
356                         die("Could not create directory %s",
357                             output_directory);
358         }
359
360         if (rev.pending.nr == 1) {
361                 rev.pending.objects[0].item->flags |= UNINTERESTING;
362                 add_head(&rev);
363         }
364
365         if (ignore_if_in_upstream)
366                 get_patch_ids(&rev, &patch_id_opts, prefix);
367
368         if (!use_stdout)
369                 realstdout = fdopen(dup(1), "w");
370
371         prepare_revision_walk(&rev);
372         while ((commit = get_revision(&rev)) != NULL) {
373                 unsigned char sha1[20];
374
375                 /* ignore merges */
376                 if (commit->parents && commit->parents->next)
377                         continue;
378
379                 if (ignore_if_in_upstream &&
380                                 !get_patch_id(commit, &patch_id_opts, sha1) &&
381                                 lookup_object(sha1))
382                         continue;
383
384                 nr++;
385                 list = realloc(list, nr * sizeof(list[0]));
386                 list[nr - 1] = commit;
387         }
388         total = nr;
389         if (numbered)
390                 rev.total = total + start_number - 1;
391         rev.add_signoff = add_signoff;
392         rev.ref_message_id = in_reply_to;
393         while (0 <= --nr) {
394                 int shown;
395                 commit = list[nr];
396                 rev.nr = total - nr + (start_number - 1);
397                 /* Make the second and subsequent mails replies to the first */
398                 if (thread) {
399                         if (nr == (total - 2)) {
400                                 strncpy(ref_message_id, message_id,
401                                         sizeof(ref_message_id));
402                                 ref_message_id[sizeof(ref_message_id)-1]='\0';
403                                 rev.ref_message_id = ref_message_id;
404                         }
405                         gen_message_id(message_id, sizeof(message_id),
406                                        sha1_to_hex(commit->object.sha1));
407                         rev.message_id = message_id;
408                 }
409                 if (!use_stdout)
410                         reopen_stdout(commit, rev.nr, keep_subject);
411                 shown = log_tree_commit(&rev, commit);
412                 free(commit->buffer);
413                 commit->buffer = NULL;
414
415                 /* We put one extra blank line between formatted
416                  * patches and this flag is used by log-tree code
417                  * to see if it needs to emit a LF before showing
418                  * the log; when using one file per patch, we do
419                  * not want the extra blank line.
420                  */
421                 if (!use_stdout)
422                         rev.shown_one = 0;
423                 if (shown) {
424                         if (rev.mime_boundary)
425                                 printf("\n--%s%s--\n\n\n",
426                                        mime_boundary_leader,
427                                        rev.mime_boundary);
428                         else
429                                 printf("-- \n%s\n\n", git_version_string);
430                 }
431                 if (!use_stdout)
432                         fclose(stdout);
433         }
434         free(list);
435         return 0;
436 }
437