Merge branch 'js/cvs'
[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, char **envp,
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, char **envp)
49 {
50         struct rev_info rev;
51
52         init_revisions(&rev);
53         git_config(git_diff_ui_config);
54         diff_setup(&rev.diffopt);
55         rev.diff = 1;
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);
62 }
63
64 int cmd_show(int argc, const char **argv, char **envp)
65 {
66         struct rev_info rev;
67
68         init_revisions(&rev);
69         git_config(git_diff_ui_config);
70         diff_setup(&rev.diffopt);
71         rev.diff = 1;
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;
77         rev.no_walk = 1;
78         cmd_log_init(argc, argv, envp, &rev);
79         return cmd_log_walk(&rev);
80 }
81
82 int cmd_log(int argc, const char **argv, char **envp)
83 {
84         struct rev_info rev;
85
86         init_revisions(&rev);
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);
92 }
93
94 static int istitlechar(char c)
95 {
96         return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
97                 (c >= '0' && c <= '9') || c == '.' || c == '_';
98 }
99
100 static char *extra_headers = NULL;
101 static int extra_headers_size = 0;
102
103 static int git_format_config(const char *var, const char *value)
104 {
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);
111                 return 0;
112         }
113         if (!strcmp(var, "diff.color")) {
114                 return 0;
115         }
116         return git_diff_ui_config(var, value);
117 }
118
119
120 static FILE *realstdout = NULL;
121 static const char *output_directory = NULL;
122
123 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
124 {
125         char filename[1024];
126         char *sol;
127         int len = 0;
128
129         if (output_directory) {
130                 strlcpy(filename, output_directory, 1010);
131                 len = strlen(filename);
132                 if (filename[len - 1] != '/')
133                         filename[len++] = '/';
134         }
135
136         sprintf(filename + len, "%04d", nr);
137         len = strlen(filename);
138
139         sol = strstr(commit->buffer, "\n\n");
140         if (sol) {
141                 int j, space = 1;
142
143                 sol += 2;
144                 /* strip [PATCH] or [PATCH blabla] */
145                 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
146                         char *eos = strchr(sol + 6, ']');
147                         if (eos) {
148                                 while (isspace(*eos))
149                                         eos++;
150                                 sol = eos;
151                         }
152                 }
153
154                 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
155                         if (istitlechar(sol[j])) {
156                                 if (space) {
157                                         filename[len++] = '-';
158                                         space = 0;
159                                 }
160                                 filename[len++] = sol[j];
161                                 if (sol[j] == '.')
162                                         while (sol[j + 1] == '.')
163                                                 j++;
164                         } else
165                                 space = 1;
166                 }
167                 while (filename[len - 1] == '.' || filename[len - 1] == '-')
168                         len--;
169         }
170         strcpy(filename + len, ".txt");
171         fprintf(realstdout, "%s\n", filename);
172         freopen(filename, "w", stdout);
173 }
174
175 static int get_patch_id(struct commit *commit, struct diff_options *options,
176                 unsigned char *sha1)
177 {
178         diff_tree_sha1(commit->parents->item->object.sha1, commit->object.sha1,
179                         "", options);
180         diffcore_std(options);
181         return diff_flush_patch_id(options, sha1);
182 }
183
184 static void get_patch_ids(struct rev_info *rev, struct diff_options *options)
185 {
186         struct rev_info check_rev;
187         struct commit *commit;
188         struct object *o1, *o2;
189         unsigned flags1, flags2;
190         unsigned char sha1[20];
191
192         if (rev->pending.nr != 2)
193                 die("Need exactly one range.");
194
195         o1 = rev->pending.objects[0].item;
196         flags1 = o1->flags;
197         o2 = rev->pending.objects[1].item;
198         flags2 = o2->flags;
199
200         if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
201                 die("Not a range.");
202
203         diff_setup(options);
204         options->recursive = 1;
205         if (diff_setup_done(options) < 0)
206                 die("diff_setup_done failed");
207
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);
215
216         while ((commit = get_revision(&check_rev)) != NULL) {
217                 /* ignore merges */
218                 if (commit->parents && commit->parents->next)
219                         continue;
220
221                 if (!get_patch_id(commit, options, sha1))
222                         created_object(sha1, xcalloc(1, sizeof(struct object)));
223         }
224
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);
230         o1->flags = flags1;
231         o2->flags = flags2;
232 }
233
234 static void gen_message_id(char *dest, unsigned int length, char *base)
235 {
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);
244 }
245
246 int cmd_format_patch(int argc, const char **argv, char **envp)
247 {
248         struct commit *commit;
249         struct commit **list = NULL;
250         struct rev_info rev;
251         int nr = 0, total, i, j;
252         int use_stdout = 0;
253         int numbered = 0;
254         int start_number = -1;
255         int keep_subject = 0;
256         int ignore_if_in_upstream = 0;
257         int thread = 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];
263
264         git_config(git_format_config);
265         init_revisions(&rev);
266         rev.commit_format = CMIT_FMT_EMAIL;
267         rev.verbose_header = 1;
268         rev.diff = 1;
269         rev.combine_merges = 0;
270         rev.ignore_merges = 1;
271         rev.diffopt.msg_sep = "";
272         rev.diffopt.recursive = 1;
273
274         rev.extra_headers = extra_headers;
275
276         /*
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.
280          */
281         for (i = 1, j = 1; i < argc; i++) {
282                 if (!strcmp(argv[i], "--stdout"))
283                         use_stdout = 1;
284                 else if (!strcmp(argv[i], "-n") ||
285                                 !strcmp(argv[i], "--numbered"))
286                         numbered = 1;
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")) {
290                         i++;
291                         if (i == argc)
292                                 die("Need a number for --start-number");
293                         start_number = strtol(argv[i], NULL, 10);
294                 }
295                 else if (!strcmp(argv[i], "-k") ||
296                                 !strcmp(argv[i], "--keep-subject")) {
297                         keep_subject = 1;
298                         rev.total = -1;
299                 }
300                 else if (!strcmp(argv[i], "--output-directory") ||
301                          !strcmp(argv[i], "-o")) {
302                         i++;
303                         if (argc <= i)
304                                 die("Which directory?");
305                         if (output_directory)
306                                 die("Two output directories?");
307                         output_directory = argv[i];
308                 }
309                 else if (!strcmp(argv[i], "--signoff") ||
310                          !strcmp(argv[i], "-s")) {
311                         const char *committer;
312                         const char *endpos;
313                         setup_ident();
314                         committer = git_committer_info(1);
315                         endpos = strchr(committer, '>');
316                         if (!endpos)
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;
321                 }
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"))
329                         thread = 1;
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")) {
333                         i++;
334                         if (i == argc)
335                                 die("Need a Message-Id for --in-reply-to");
336                         in_reply_to = argv[i];
337                 }
338                 else
339                         argv[j++] = argv[i];
340         }
341         argc = j;
342
343         if (start_number < 0)
344                 start_number = 1;
345         if (numbered && keep_subject)
346                 die ("-n and -k are mutually exclusive.");
347
348         argc = setup_revisions(argc, argv, &rev, "HEAD");
349         if (argc > 1)
350                 die ("unrecognized argument: %s", argv[1]);
351
352         if (!rev.diffopt.output_format)
353                 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
354
355         if (output_directory) {
356                 if (use_stdout)
357                         die("standard output, or directory, which one?");
358                 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
359                         die("Could not create directory %s",
360                             output_directory);
361         }
362
363         if (rev.pending.nr == 1) {
364                 rev.pending.objects[0].item->flags |= UNINTERESTING;
365                 add_head(&rev);
366         }
367
368         if (ignore_if_in_upstream)
369                 get_patch_ids(&rev, &patch_id_opts);
370
371         if (!use_stdout)
372                 realstdout = fdopen(dup(1), "w");
373
374         prepare_revision_walk(&rev);
375         while ((commit = get_revision(&rev)) != NULL) {
376                 unsigned char sha1[20];
377
378                 /* ignore merges */
379                 if (commit->parents && commit->parents->next)
380                         continue;
381
382                 if (ignore_if_in_upstream &&
383                                 !get_patch_id(commit, &patch_id_opts, sha1) &&
384                                 lookup_object(sha1))
385                         continue;
386
387                 nr++;
388                 list = realloc(list, nr * sizeof(list[0]));
389                 list[nr - 1] = commit;
390         }
391         total = nr;
392         if (numbered)
393                 rev.total = total + start_number - 1;
394         rev.add_signoff = add_signoff;
395         rev.ref_message_id = in_reply_to;
396         while (0 <= --nr) {
397                 int shown;
398                 commit = list[nr];
399                 rev.nr = total - nr + (start_number - 1);
400                 /* Make the second and subsequent mails replies to the first */
401                 if (thread) {
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;
407                         }
408                         gen_message_id(message_id, sizeof(message_id),
409                                        sha1_to_hex(commit->object.sha1));
410                         rev.message_id = message_id;
411                 }
412                 if (!use_stdout)
413                         reopen_stdout(commit, rev.nr, keep_subject);
414                 shown = log_tree_commit(&rev, commit);
415                 free(commit->buffer);
416                 commit->buffer = NULL;
417
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.
423                  */
424                 if (!use_stdout)
425                         rev.shown_one = 0;
426                 if (shown) {
427                         if (rev.mime_boundary)
428                                 printf("\n--%s%s--\n\n\n",
429                                        mime_boundary_leader,
430                                        rev.mime_boundary);
431                         else
432                                 printf("-- \n%s\n\n", git_version_string);
433                 }
434                 if (!use_stdout)
435                         fclose(stdout);
436         }
437         free(list);
438         return 0;
439 }
440