gitweb: optimize per-file history generation
[git/jrn.git] / builtin-log.c
blobf9515a8a4ac8a53e87b6fb9f73e1964350481bfb
1 /*
2 * Builtin "git log" and related commands (show, whatchanged)
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"
14 /* this is in builtin-diff.c */
15 void add_head(struct rev_info *revs);
17 static int cmd_log_wc(int argc, const char **argv, char **envp,
18 struct rev_info *rev)
20 struct commit *commit;
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 if (rev->diffopt.output_format == DIFF_FORMAT_RAW)
30 rev->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
34 if (argc > 1)
35 die("unrecognized argument: %s", argv[1]);
37 prepare_revision_walk(rev);
38 setup_pager();
39 while ((commit = get_revision(rev)) != NULL) {
40 log_tree_commit(rev, commit);
41 free(commit->buffer);
42 commit->buffer = NULL;
43 free_commit_list(commit->parents);
44 commit->parents = NULL;
46 return 0;
49 int cmd_whatchanged(int argc, const char **argv, char **envp)
51 struct rev_info rev;
53 init_revisions(&rev);
54 rev.diff = 1;
55 rev.diffopt.recursive = 1;
56 rev.simplify_history = 0;
57 return cmd_log_wc(argc, argv, envp, &rev);
60 int cmd_show(int argc, const char **argv, char **envp)
62 struct rev_info rev;
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 return cmd_log_wc(argc, argv, envp, &rev);
75 int cmd_log(int argc, const char **argv, char **envp)
77 struct rev_info rev;
79 init_revisions(&rev);
80 rev.always_show_header = 1;
81 rev.diffopt.recursive = 1;
82 return cmd_log_wc(argc, argv, envp, &rev);
85 static int istitlechar(char c)
87 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
88 (c >= '0' && c <= '9') || c == '.' || c == '_';
91 static char *extra_headers = NULL;
92 static int extra_headers_size = 0;
94 static int git_format_config(const char *var, const char *value)
96 if (!strcmp(var, "format.headers")) {
97 int len = strlen(value);
98 extra_headers_size += len + 1;
99 extra_headers = realloc(extra_headers, extra_headers_size);
100 extra_headers[extra_headers_size - len - 1] = 0;
101 strcat(extra_headers, value);
102 return 0;
104 return git_default_config(var, value);
108 static FILE *realstdout = NULL;
109 static const char *output_directory = NULL;
111 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
113 char filename[1024];
114 char *sol;
115 int len = 0;
117 if (output_directory) {
118 strlcpy(filename, output_directory, 1010);
119 len = strlen(filename);
120 if (filename[len - 1] != '/')
121 filename[len++] = '/';
124 sprintf(filename + len, "%04d", nr);
125 len = strlen(filename);
127 sol = strstr(commit->buffer, "\n\n");
128 if (sol) {
129 int j, space = 1;
131 sol += 2;
132 /* strip [PATCH] or [PATCH blabla] */
133 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
134 char *eos = strchr(sol + 6, ']');
135 if (eos) {
136 while (isspace(*eos))
137 eos++;
138 sol = eos;
142 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
143 if (istitlechar(sol[j])) {
144 if (space) {
145 filename[len++] = '-';
146 space = 0;
148 filename[len++] = sol[j];
149 if (sol[j] == '.')
150 while (sol[j + 1] == '.')
151 j++;
152 } else
153 space = 1;
155 while (filename[len - 1] == '.' || filename[len - 1] == '-')
156 len--;
158 strcpy(filename + len, ".txt");
159 fprintf(realstdout, "%s\n", filename);
160 freopen(filename, "w", stdout);
163 static int get_patch_id(struct commit *commit, struct diff_options *options,
164 unsigned char *sha1)
166 diff_tree_sha1(commit->parents->item->object.sha1, commit->object.sha1,
167 "", options);
168 diffcore_std(options);
169 return diff_flush_patch_id(options, sha1);
172 static void get_patch_ids(struct rev_info *rev, struct diff_options *options)
174 struct rev_info check_rev;
175 struct commit *commit;
176 struct object *o1, *o2;
177 unsigned flags1, flags2;
178 unsigned char sha1[20];
180 if (rev->pending.nr != 2)
181 die("Need exactly one range.");
183 o1 = rev->pending.objects[0].item;
184 flags1 = o1->flags;
185 o2 = rev->pending.objects[1].item;
186 flags2 = o2->flags;
188 if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
189 die("Not a range.");
191 diff_setup(options);
192 options->recursive = 1;
193 if (diff_setup_done(options) < 0)
194 die("diff_setup_done failed");
196 /* given a range a..b get all patch ids for b..a */
197 init_revisions(&check_rev);
198 o1->flags ^= UNINTERESTING;
199 o2->flags ^= UNINTERESTING;
200 add_pending_object(&check_rev, o1, "o1");
201 add_pending_object(&check_rev, o2, "o2");
202 prepare_revision_walk(&check_rev);
204 while ((commit = get_revision(&check_rev)) != NULL) {
205 /* ignore merges */
206 if (commit->parents && commit->parents->next)
207 continue;
209 if (!get_patch_id(commit, options, sha1))
210 created_object(sha1, xcalloc(1, sizeof(struct object)));
213 /* reset for next revision walk */
214 clear_commit_marks((struct commit *)o1,
215 SEEN | UNINTERESTING | SHOWN | ADDED);
216 clear_commit_marks((struct commit *)o2,
217 SEEN | UNINTERESTING | SHOWN | ADDED);
218 o1->flags = flags1;
219 o2->flags = flags2;
222 int cmd_format_patch(int argc, const char **argv, char **envp)
224 struct commit *commit;
225 struct commit **list = NULL;
226 struct rev_info rev;
227 int nr = 0, total, i, j;
228 int use_stdout = 0;
229 int numbered = 0;
230 int start_number = -1;
231 int keep_subject = 0;
232 int ignore_if_in_upstream = 0;
233 struct diff_options patch_id_opts;
234 char *add_signoff = NULL;
236 init_revisions(&rev);
237 rev.commit_format = CMIT_FMT_EMAIL;
238 rev.verbose_header = 1;
239 rev.diff = 1;
240 rev.diffopt.with_raw = 0;
241 rev.diffopt.with_stat = 1;
242 rev.combine_merges = 0;
243 rev.ignore_merges = 1;
244 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
246 git_config(git_format_config);
247 rev.extra_headers = extra_headers;
250 * Parse the arguments before setup_revisions(), or something
251 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
252 * possibly a valid SHA1.
254 for (i = 1, j = 1; i < argc; i++) {
255 if (!strcmp(argv[i], "--stdout"))
256 use_stdout = 1;
257 else if (!strcmp(argv[i], "-n") ||
258 !strcmp(argv[i], "--numbered"))
259 numbered = 1;
260 else if (!strncmp(argv[i], "--start-number=", 15))
261 start_number = strtol(argv[i] + 15, NULL, 10);
262 else if (!strcmp(argv[i], "--start-number")) {
263 i++;
264 if (i == argc)
265 die("Need a number for --start-number");
266 start_number = strtol(argv[i], NULL, 10);
268 else if (!strcmp(argv[i], "-k") ||
269 !strcmp(argv[i], "--keep-subject")) {
270 keep_subject = 1;
271 rev.total = -1;
273 else if (!strcmp(argv[i], "--output-directory") ||
274 !strcmp(argv[i], "-o")) {
275 i++;
276 if (argc <= i)
277 die("Which directory?");
278 if (output_directory)
279 die("Two output directories?");
280 output_directory = argv[i];
282 else if (!strcmp(argv[i], "--signoff") ||
283 !strcmp(argv[i], "-s")) {
284 const char *committer;
285 const char *endpos;
286 setup_ident();
287 committer = git_committer_info(1);
288 endpos = strchr(committer, '>');
289 if (!endpos)
290 die("bogos committer info %s\n", committer);
291 add_signoff = xmalloc(endpos - committer + 2);
292 memcpy(add_signoff, committer, endpos - committer + 1);
293 add_signoff[endpos - committer + 1] = 0;
295 else if (!strcmp(argv[i], "--attach"))
296 rev.mime_boundary = git_version_string;
297 else if (!strncmp(argv[i], "--attach=", 9))
298 rev.mime_boundary = argv[i] + 9;
299 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
300 ignore_if_in_upstream = 1;
301 else
302 argv[j++] = argv[i];
304 argc = j;
306 if (start_number < 0)
307 start_number = 1;
308 if (numbered && keep_subject)
309 die ("-n and -k are mutually exclusive.");
311 argc = setup_revisions(argc, argv, &rev, "HEAD");
312 if (argc > 1)
313 die ("unrecognized argument: %s", argv[1]);
315 if (output_directory) {
316 if (use_stdout)
317 die("standard output, or directory, which one?");
318 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
319 die("Could not create directory %s",
320 output_directory);
323 if (rev.pending.nr == 1) {
324 rev.pending.objects[0].item->flags |= UNINTERESTING;
325 add_head(&rev);
328 if (ignore_if_in_upstream)
329 get_patch_ids(&rev, &patch_id_opts);
331 if (!use_stdout)
332 realstdout = fdopen(dup(1), "w");
334 prepare_revision_walk(&rev);
335 while ((commit = get_revision(&rev)) != NULL) {
336 unsigned char sha1[20];
338 /* ignore merges */
339 if (commit->parents && commit->parents->next)
340 continue;
342 if (ignore_if_in_upstream &&
343 !get_patch_id(commit, &patch_id_opts, sha1) &&
344 lookup_object(sha1))
345 continue;
347 nr++;
348 list = realloc(list, nr * sizeof(list[0]));
349 list[nr - 1] = commit;
351 total = nr;
352 if (numbered)
353 rev.total = total + start_number - 1;
354 rev.add_signoff = add_signoff;
355 while (0 <= --nr) {
356 int shown;
357 commit = list[nr];
358 rev.nr = total - nr + (start_number - 1);
359 if (!use_stdout)
360 reopen_stdout(commit, rev.nr, keep_subject);
361 shown = log_tree_commit(&rev, commit);
362 free(commit->buffer);
363 commit->buffer = NULL;
365 /* We put one extra blank line between formatted
366 * patches and this flag is used by log-tree code
367 * to see if it needs to emit a LF before showing
368 * the log; when using one file per patch, we do
369 * not want the extra blank line.
371 if (!use_stdout)
372 rev.shown_one = 0;
373 if (shown) {
374 if (rev.mime_boundary)
375 printf("\n--%s%s--\n\n\n",
376 mime_boundary_leader,
377 rev.mime_boundary);
378 else
379 printf("-- \n%s\n\n", git_version_string);
381 if (!use_stdout)
382 fclose(stdout);
384 free(list);
385 return 0;