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