DIFF_FORMAT_RAW is not default anymore
[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 int cmd_log_wc(int argc, const char **argv, char **envp,
18                       struct rev_info *rev)
19 {
20         struct commit *commit;
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->always_show_header) {
27                 if (rev->diffopt.pickaxe || rev->diffopt.filter)
28                         rev->always_show_header = 0;
29         }
30
31         if (argc > 1)
32                 die("unrecognized argument: %s", argv[1]);
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         init_revisions(&rev);
51         rev.diff = 1;
52         rev.diffopt.recursive = 1;
53         rev.simplify_history = 0;
54         return cmd_log_wc(argc, argv, envp, &rev);
55 }
56
57 int cmd_show(int argc, const char **argv, char **envp)
58 {
59         struct rev_info rev;
60
61         init_revisions(&rev);
62         rev.diff = 1;
63         rev.diffopt.recursive = 1;
64         rev.combine_merges = 1;
65         rev.dense_combined_merges = 1;
66         rev.always_show_header = 1;
67         rev.ignore_merges = 0;
68         rev.no_walk = 1;
69         return cmd_log_wc(argc, argv, envp, &rev);
70 }
71
72 int cmd_log(int argc, const char **argv, char **envp)
73 {
74         struct rev_info rev;
75
76         init_revisions(&rev);
77         rev.always_show_header = 1;
78         rev.diffopt.recursive = 1;
79         return cmd_log_wc(argc, argv, envp, &rev);
80 }
81
82 static int istitlechar(char c)
83 {
84         return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
85                 (c >= '0' && c <= '9') || c == '.' || c == '_';
86 }
87
88 static char *extra_headers = NULL;
89 static int extra_headers_size = 0;
90
91 static int git_format_config(const char *var, const char *value)
92 {
93         if (!strcmp(var, "format.headers")) {
94                 int len = strlen(value);
95                 extra_headers_size += len + 1;
96                 extra_headers = realloc(extra_headers, extra_headers_size);
97                 extra_headers[extra_headers_size - len - 1] = 0;
98                 strcat(extra_headers, value);
99                 return 0;
100         }
101         return git_default_config(var, value);
102 }
103
104
105 static FILE *realstdout = NULL;
106 static const char *output_directory = NULL;
107
108 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
109 {
110         char filename[1024];
111         char *sol;
112         int len = 0;
113
114         if (output_directory) {
115                 strlcpy(filename, output_directory, 1010);
116                 len = strlen(filename);
117                 if (filename[len - 1] != '/')
118                         filename[len++] = '/';
119         }
120
121         sprintf(filename + len, "%04d", nr);
122         len = strlen(filename);
123
124         sol = strstr(commit->buffer, "\n\n");
125         if (sol) {
126                 int j, space = 1;
127
128                 sol += 2;
129                 /* strip [PATCH] or [PATCH blabla] */
130                 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
131                         char *eos = strchr(sol + 6, ']');
132                         if (eos) {
133                                 while (isspace(*eos))
134                                         eos++;
135                                 sol = eos;
136                         }
137                 }
138
139                 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
140                         if (istitlechar(sol[j])) {
141                                 if (space) {
142                                         filename[len++] = '-';
143                                         space = 0;
144                                 }
145                                 filename[len++] = sol[j];
146                                 if (sol[j] == '.')
147                                         while (sol[j + 1] == '.')
148                                                 j++;
149                         } else
150                                 space = 1;
151                 }
152                 while (filename[len - 1] == '.' || filename[len - 1] == '-')
153                         len--;
154         }
155         strcpy(filename + len, ".txt");
156         fprintf(realstdout, "%s\n", filename);
157         freopen(filename, "w", stdout);
158 }
159
160 int cmd_format_patch(int argc, const char **argv, char **envp)
161 {
162         struct commit *commit;
163         struct commit **list = NULL;
164         struct rev_info rev;
165         int nr = 0, total, i, j;
166         int use_stdout = 0;
167         int numbered = 0;
168         int start_number = -1;
169         int keep_subject = 0;
170         char *add_signoff = NULL;
171
172         init_revisions(&rev);
173         rev.commit_format = CMIT_FMT_EMAIL;
174         rev.verbose_header = 1;
175         rev.diff = 1;
176         rev.combine_merges = 0;
177         rev.ignore_merges = 1;
178
179         git_config(git_format_config);
180         rev.extra_headers = extra_headers;
181
182         /*
183          * Parse the arguments before setup_revisions(), or something
184          * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
185          * possibly a valid SHA1.
186          */
187         for (i = 1, j = 1; i < argc; i++) {
188                 if (!strcmp(argv[i], "--stdout"))
189                         use_stdout = 1;
190                 else if (!strcmp(argv[i], "-n") ||
191                                 !strcmp(argv[i], "--numbered"))
192                         numbered = 1;
193                 else if (!strncmp(argv[i], "--start-number=", 15))
194                         start_number = strtol(argv[i] + 15, NULL, 10);
195                 else if (!strcmp(argv[i], "--start-number")) {
196                         i++;
197                         if (i == argc)
198                                 die("Need a number for --start-number");
199                         start_number = strtol(argv[i], NULL, 10);
200                 }
201                 else if (!strcmp(argv[i], "-k") ||
202                                 !strcmp(argv[i], "--keep-subject")) {
203                         keep_subject = 1;
204                         rev.total = -1;
205                 }
206                 else if (!strcmp(argv[i], "--output-directory") ||
207                          !strcmp(argv[i], "-o")) {
208                         i++;
209                         if (argc <= i)
210                                 die("Which directory?");
211                         if (output_directory)
212                                 die("Two output directories?");
213                         output_directory = argv[i];
214                 }
215                 else if (!strcmp(argv[i], "--signoff") ||
216                          !strcmp(argv[i], "-s")) {
217                         const char *committer;
218                         const char *endpos;
219                         setup_ident();
220                         committer = git_committer_info(1);
221                         endpos = strchr(committer, '>');
222                         if (!endpos)
223                                 die("bogos committer info %s\n", committer);
224                         add_signoff = xmalloc(endpos - committer + 2);
225                         memcpy(add_signoff, committer, endpos - committer + 1);
226                         add_signoff[endpos - committer + 1] = 0;
227                 }
228                 else if (!strcmp(argv[i], "--attach"))
229                         rev.mime_boundary = git_version_string;
230                 else if (!strncmp(argv[i], "--attach=", 9))
231                         rev.mime_boundary = argv[i] + 9;
232                 else
233                         argv[j++] = argv[i];
234         }
235         argc = j;
236
237         if (start_number < 0)
238                 start_number = 1;
239         if (numbered && keep_subject)
240                 die ("-n and -k are mutually exclusive.");
241
242         argc = setup_revisions(argc, argv, &rev, "HEAD");
243         if (argc > 1)
244                 die ("unrecognized argument: %s", argv[1]);
245
246         if (!rev.diffopt.output_format)
247                 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
248
249         if (output_directory) {
250                 if (use_stdout)
251                         die("standard output, or directory, which one?");
252                 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
253                         die("Could not create directory %s",
254                             output_directory);
255         }
256
257         if (rev.pending.nr == 1) {
258                 rev.pending.objects[0].item->flags |= UNINTERESTING;
259                 add_head(&rev);
260         }
261
262         if (!use_stdout)
263                 realstdout = fdopen(dup(1), "w");
264
265         prepare_revision_walk(&rev);
266         while ((commit = get_revision(&rev)) != NULL) {
267                 /* ignore merges */
268                 if (commit->parents && commit->parents->next)
269                         continue;
270                 nr++;
271                 list = realloc(list, nr * sizeof(list[0]));
272                 list[nr - 1] = commit;
273         }
274         total = nr;
275         if (numbered)
276                 rev.total = total + start_number - 1;
277         rev.add_signoff = add_signoff;
278         while (0 <= --nr) {
279                 int shown;
280                 commit = list[nr];
281                 rev.nr = total - nr + (start_number - 1);
282                 if (!use_stdout)
283                         reopen_stdout(commit, rev.nr, keep_subject);
284                 shown = log_tree_commit(&rev, commit);
285                 free(commit->buffer);
286                 commit->buffer = NULL;
287
288                 /* We put one extra blank line between formatted
289                  * patches and this flag is used by log-tree code
290                  * to see if it needs to emit a LF before showing
291                  * the log; when using one file per patch, we do
292                  * not want the extra blank line.
293                  */
294                 if (!use_stdout)
295                         rev.shown_one = 0;
296                 if (shown) {
297                         if (rev.mime_boundary)
298                                 printf("\n--%s%s--\n\n\n",
299                                        mime_boundary_leader,
300                                        rev.mime_boundary);
301                         else
302                                 printf("-- \n%s\n\n", git_version_string);
303                 }
304                 if (!use_stdout)
305                         fclose(stdout);
306         }
307         free(list);
308         return 0;
309 }
310