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