Code

git-svn: documentation updates
[git.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>
16 static int default_show_root = 1;
18 /* this is in builtin-diff.c */
19 void add_head(struct rev_info *revs);
21 static void cmd_log_init(int argc, const char **argv, const char *prefix,
22                       struct rev_info *rev)
23 {
24         rev->abbrev = DEFAULT_ABBREV;
25         rev->commit_format = CMIT_FMT_DEFAULT;
26         rev->verbose_header = 1;
27         rev->show_root_diff = default_show_root;
28         argc = setup_revisions(argc, argv, rev, "HEAD");
29         if (rev->diffopt.pickaxe || rev->diffopt.filter)
30                 rev->always_show_header = 0;
31         if (argc > 1)
32                 die("unrecognized argument: %s", argv[1]);
33 }
35 static int cmd_log_walk(struct rev_info *rev)
36 {
37         struct commit *commit;
39         prepare_revision_walk(rev);
40         while ((commit = get_revision(rev)) != NULL) {
41                 log_tree_commit(rev, commit);
42                 free(commit->buffer);
43                 commit->buffer = NULL;
44                 free_commit_list(commit->parents);
45                 commit->parents = NULL;
46         }
47         return 0;
48 }
50 static int git_log_config(const char *var, const char *value)
51 {
52         if (!strcmp(var, "log.showroot")) {
53                 default_show_root = git_config_bool(var, value);
54                 return 0;
55         }
56         return git_diff_ui_config(var, value);
57 }
59 int cmd_whatchanged(int argc, const char **argv, const char *prefix)
60 {
61         struct rev_info rev;
63         git_config(git_log_config);
64         init_revisions(&rev, prefix);
65         rev.diff = 1;
66         rev.diffopt.recursive = 1;
67         rev.simplify_history = 0;
68         cmd_log_init(argc, argv, prefix, &rev);
69         if (!rev.diffopt.output_format)
70                 rev.diffopt.output_format = DIFF_FORMAT_RAW;
71         return cmd_log_walk(&rev);
72 }
74 int cmd_show(int argc, const char **argv, const char *prefix)
75 {
76         struct rev_info rev;
78         git_config(git_log_config);
79         init_revisions(&rev, prefix);
80         rev.diff = 1;
81         rev.diffopt.recursive = 1;
82         rev.combine_merges = 1;
83         rev.dense_combined_merges = 1;
84         rev.always_show_header = 1;
85         rev.ignore_merges = 0;
86         rev.no_walk = 1;
87         cmd_log_init(argc, argv, prefix, &rev);
88         return cmd_log_walk(&rev);
89 }
91 int cmd_log(int argc, const char **argv, const char *prefix)
92 {
93         struct rev_info rev;
95         git_config(git_log_config);
96         init_revisions(&rev, prefix);
97         rev.always_show_header = 1;
98         cmd_log_init(argc, argv, prefix, &rev);
99         return cmd_log_walk(&rev);
102 static int istitlechar(char c)
104         return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
105                 (c >= '0' && c <= '9') || c == '.' || c == '_';
108 static char *extra_headers = NULL;
109 static int extra_headers_size = 0;
111 static int git_format_config(const char *var, const char *value)
113         if (!strcmp(var, "format.headers")) {
114                 int len = strlen(value);
115                 extra_headers_size += len + 1;
116                 extra_headers = xrealloc(extra_headers, extra_headers_size);
117                 extra_headers[extra_headers_size - len - 1] = 0;
118                 strcat(extra_headers, value);
119                 return 0;
120         }
121         if (!strcmp(var, "diff.color")) {
122                 return 0;
123         }
124         return git_log_config(var, value);
128 static FILE *realstdout = NULL;
129 static const char *output_directory = NULL;
131 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
133         char filename[1024];
134         char *sol;
135         int len = 0;
137         if (output_directory) {
138                 strlcpy(filename, output_directory, 1010);
139                 len = strlen(filename);
140                 if (filename[len - 1] != '/')
141                         filename[len++] = '/';
142         }
144         sprintf(filename + len, "%04d", nr);
145         len = strlen(filename);
147         sol = strstr(commit->buffer, "\n\n");
148         if (sol) {
149                 int j, space = 1;
151                 sol += 2;
152                 /* strip [PATCH] or [PATCH blabla] */
153                 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
154                         char *eos = strchr(sol + 6, ']');
155                         if (eos) {
156                                 while (isspace(*eos))
157                                         eos++;
158                                 sol = eos;
159                         }
160                 }
162                 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
163                         if (istitlechar(sol[j])) {
164                                 if (space) {
165                                         filename[len++] = '-';
166                                         space = 0;
167                                 }
168                                 filename[len++] = sol[j];
169                                 if (sol[j] == '.')
170                                         while (sol[j + 1] == '.')
171                                                 j++;
172                         } else
173                                 space = 1;
174                 }
175                 while (filename[len - 1] == '.' || filename[len - 1] == '-')
176                         len--;
177         }
178         strcpy(filename + len, ".txt");
179         fprintf(realstdout, "%s\n", filename);
180         freopen(filename, "w", stdout);
183 static int get_patch_id(struct commit *commit, struct diff_options *options,
184                 unsigned char *sha1)
186         if (commit->parents)
187                 diff_tree_sha1(commit->parents->item->object.sha1,
188                                commit->object.sha1, "", options);
189         else
190                 diff_root_tree_sha1(commit->object.sha1, "", options);
191         diffcore_std(options);
192         return diff_flush_patch_id(options, sha1);
195 static void get_patch_ids(struct rev_info *rev, struct diff_options *options, const char *prefix)
197         struct rev_info check_rev;
198         struct commit *commit;
199         struct object *o1, *o2;
200         unsigned flags1, flags2;
201         unsigned char sha1[20];
203         if (rev->pending.nr != 2)
204                 die("Need exactly one range.");
206         o1 = rev->pending.objects[0].item;
207         flags1 = o1->flags;
208         o2 = rev->pending.objects[1].item;
209         flags2 = o2->flags;
211         if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
212                 die("Not a range.");
214         diff_setup(options);
215         options->recursive = 1;
216         if (diff_setup_done(options) < 0)
217                 die("diff_setup_done failed");
219         /* given a range a..b get all patch ids for b..a */
220         init_revisions(&check_rev, prefix);
221         o1->flags ^= UNINTERESTING;
222         o2->flags ^= UNINTERESTING;
223         add_pending_object(&check_rev, o1, "o1");
224         add_pending_object(&check_rev, o2, "o2");
225         prepare_revision_walk(&check_rev);
227         while ((commit = get_revision(&check_rev)) != NULL) {
228                 /* ignore merges */
229                 if (commit->parents && commit->parents->next)
230                         continue;
232                 if (!get_patch_id(commit, options, sha1))
233                         created_object(sha1, xcalloc(1, sizeof(struct object)));
234         }
236         /* reset for next revision walk */
237         clear_commit_marks((struct commit *)o1,
238                         SEEN | UNINTERESTING | SHOWN | ADDED);
239         clear_commit_marks((struct commit *)o2,
240                         SEEN | UNINTERESTING | SHOWN | ADDED);
241         o1->flags = flags1;
242         o2->flags = flags2;
245 static void gen_message_id(char *dest, unsigned int length, char *base)
247         const char *committer = git_committer_info(1);
248         const char *email_start = strrchr(committer, '<');
249         const char *email_end = strrchr(committer, '>');
250         if(!email_start || !email_end || email_start > email_end - 1)
251                 die("Could not extract email from committer identity.");
252         snprintf(dest, length, "%s.%lu.git.%.*s", base,
253                  (unsigned long) time(NULL),
254                  (int)(email_end - email_start - 1), email_start + 1);
257 int cmd_format_patch(int argc, const char **argv, const char *prefix)
259         struct commit *commit;
260         struct commit **list = NULL;
261         struct rev_info rev;
262         int nr = 0, total, i, j;
263         int use_stdout = 0;
264         int numbered = 0;
265         int start_number = -1;
266         int keep_subject = 0;
267         int ignore_if_in_upstream = 0;
268         int thread = 0;
269         const char *in_reply_to = NULL;
270         struct diff_options patch_id_opts;
271         char *add_signoff = NULL;
272         char message_id[1024];
273         char ref_message_id[1024];
275         setup_ident();
276         git_config(git_format_config);
277         init_revisions(&rev, prefix);
278         rev.commit_format = CMIT_FMT_EMAIL;
279         rev.verbose_header = 1;
280         rev.diff = 1;
281         rev.combine_merges = 0;
282         rev.ignore_merges = 1;
283         rev.diffopt.msg_sep = "";
284         rev.diffopt.recursive = 1;
286         rev.extra_headers = extra_headers;
288         /*
289          * Parse the arguments before setup_revisions(), or something
290          * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
291          * possibly a valid SHA1.
292          */
293         for (i = 1, j = 1; i < argc; i++) {
294                 if (!strcmp(argv[i], "--stdout"))
295                         use_stdout = 1;
296                 else if (!strcmp(argv[i], "-n") ||
297                                 !strcmp(argv[i], "--numbered"))
298                         numbered = 1;
299                 else if (!strncmp(argv[i], "--start-number=", 15))
300                         start_number = strtol(argv[i] + 15, NULL, 10);
301                 else if (!strcmp(argv[i], "--start-number")) {
302                         i++;
303                         if (i == argc)
304                                 die("Need a number for --start-number");
305                         start_number = strtol(argv[i], NULL, 10);
306                 }
307                 else if (!strcmp(argv[i], "-k") ||
308                                 !strcmp(argv[i], "--keep-subject")) {
309                         keep_subject = 1;
310                         rev.total = -1;
311                 }
312                 else if (!strcmp(argv[i], "--output-directory") ||
313                          !strcmp(argv[i], "-o")) {
314                         i++;
315                         if (argc <= i)
316                                 die("Which directory?");
317                         if (output_directory)
318                                 die("Two output directories?");
319                         output_directory = argv[i];
320                 }
321                 else if (!strcmp(argv[i], "--signoff") ||
322                          !strcmp(argv[i], "-s")) {
323                         const char *committer;
324                         const char *endpos;
325                         committer = git_committer_info(1);
326                         endpos = strchr(committer, '>');
327                         if (!endpos)
328                                 die("bogos committer info %s\n", committer);
329                         add_signoff = xmalloc(endpos - committer + 2);
330                         memcpy(add_signoff, committer, endpos - committer + 1);
331                         add_signoff[endpos - committer + 1] = 0;
332                 }
333                 else if (!strcmp(argv[i], "--attach"))
334                         rev.mime_boundary = git_version_string;
335                 else if (!strncmp(argv[i], "--attach=", 9))
336                         rev.mime_boundary = argv[i] + 9;
337                 else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
338                         ignore_if_in_upstream = 1;
339                 else if (!strcmp(argv[i], "--thread"))
340                         thread = 1;
341                 else if (!strncmp(argv[i], "--in-reply-to=", 14))
342                         in_reply_to = argv[i] + 14;
343                 else if (!strcmp(argv[i], "--in-reply-to")) {
344                         i++;
345                         if (i == argc)
346                                 die("Need a Message-Id for --in-reply-to");
347                         in_reply_to = argv[i];
348                 }
349                 else
350                         argv[j++] = argv[i];
351         }
352         argc = j;
354         if (start_number < 0)
355                 start_number = 1;
356         if (numbered && keep_subject)
357                 die ("-n and -k are mutually exclusive.");
359         argc = setup_revisions(argc, argv, &rev, "HEAD");
360         if (argc > 1)
361                 die ("unrecognized argument: %s", argv[1]);
363         if (!rev.diffopt.output_format)
364                 rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
366         if (!output_directory)
367                 output_directory = prefix;
369         if (output_directory) {
370                 if (use_stdout)
371                         die("standard output, or directory, which one?");
372                 if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
373                         die("Could not create directory %s",
374                             output_directory);
375         }
377         if (rev.pending.nr == 1) {
378                 rev.pending.objects[0].item->flags |= UNINTERESTING;
379                 add_head(&rev);
380         }
382         if (ignore_if_in_upstream)
383                 get_patch_ids(&rev, &patch_id_opts, prefix);
385         if (!use_stdout)
386                 realstdout = fdopen(dup(1), "w");
388         prepare_revision_walk(&rev);
389         while ((commit = get_revision(&rev)) != NULL) {
390                 unsigned char sha1[20];
392                 /* ignore merges */
393                 if (commit->parents && commit->parents->next)
394                         continue;
396                 if (ignore_if_in_upstream &&
397                                 !get_patch_id(commit, &patch_id_opts, sha1) &&
398                                 lookup_object(sha1))
399                         continue;
401                 nr++;
402                 list = xrealloc(list, nr * sizeof(list[0]));
403                 list[nr - 1] = commit;
404         }
405         total = nr;
406         if (numbered)
407                 rev.total = total + start_number - 1;
408         rev.add_signoff = add_signoff;
409         rev.ref_message_id = in_reply_to;
410         while (0 <= --nr) {
411                 int shown;
412                 commit = list[nr];
413                 rev.nr = total - nr + (start_number - 1);
414                 /* Make the second and subsequent mails replies to the first */
415                 if (thread) {
416                         if (nr == (total - 2)) {
417                                 strncpy(ref_message_id, message_id,
418                                         sizeof(ref_message_id));
419                                 ref_message_id[sizeof(ref_message_id)-1]='\0';
420                                 rev.ref_message_id = ref_message_id;
421                         }
422                         gen_message_id(message_id, sizeof(message_id),
423                                        sha1_to_hex(commit->object.sha1));
424                         rev.message_id = message_id;
425                 }
426                 if (!use_stdout)
427                         reopen_stdout(commit, rev.nr, keep_subject);
428                 shown = log_tree_commit(&rev, commit);
429                 free(commit->buffer);
430                 commit->buffer = NULL;
432                 /* We put one extra blank line between formatted
433                  * patches and this flag is used by log-tree code
434                  * to see if it needs to emit a LF before showing
435                  * the log; when using one file per patch, we do
436                  * not want the extra blank line.
437                  */
438                 if (!use_stdout)
439                         rev.shown_one = 0;
440                 if (shown) {
441                         if (rev.mime_boundary)
442                                 printf("\n--%s%s--\n\n\n",
443                                        mime_boundary_leader,
444                                        rev.mime_boundary);
445                         else
446                                 printf("-- \n%s\n\n", git_version_string);
447                 }
448                 if (!use_stdout)
449                         fclose(stdout);
450         }
451         free(list);
452         return 0;
455 static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
457         unsigned char sha1[20];
458         if (get_sha1(arg, sha1) == 0) {
459                 struct commit *commit = lookup_commit_reference(sha1);
460                 if (commit) {
461                         commit->object.flags |= flags;
462                         add_pending_object(revs, &commit->object, arg);
463                         return 0;
464                 }
465         }
466         return -1;
469 static const char cherry_usage[] =
470 "git-cherry [-v] <upstream> [<head>] [<limit>]";
471 int cmd_cherry(int argc, const char **argv, const char *prefix)
473         struct rev_info revs;
474         struct diff_options patch_id_opts;
475         struct commit *commit;
476         struct commit_list *list = NULL;
477         const char *upstream;
478         const char *head = "HEAD";
479         const char *limit = NULL;
480         int verbose = 0;
482         if (argc > 1 && !strcmp(argv[1], "-v")) {
483                 verbose = 1;
484                 argc--;
485                 argv++;
486         }
488         switch (argc) {
489         case 4:
490                 limit = argv[3];
491                 /* FALLTHROUGH */
492         case 3:
493                 head = argv[2];
494                 /* FALLTHROUGH */
495         case 2:
496                 upstream = argv[1];
497                 break;
498         default:
499                 usage(cherry_usage);
500         }
502         init_revisions(&revs, prefix);
503         revs.diff = 1;
504         revs.combine_merges = 0;
505         revs.ignore_merges = 1;
506         revs.diffopt.recursive = 1;
508         if (add_pending_commit(head, &revs, 0))
509                 die("Unknown commit %s", head);
510         if (add_pending_commit(upstream, &revs, UNINTERESTING))
511                 die("Unknown commit %s", upstream);
513         /* Don't say anything if head and upstream are the same. */
514         if (revs.pending.nr == 2) {
515                 struct object_array_entry *o = revs.pending.objects;
516                 if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
517                         return 0;
518         }
520         get_patch_ids(&revs, &patch_id_opts, prefix);
522         if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
523                 die("Unknown commit %s", limit);
525         /* reverse the list of commits */
526         prepare_revision_walk(&revs);
527         while ((commit = get_revision(&revs)) != NULL) {
528                 /* ignore merges */
529                 if (commit->parents && commit->parents->next)
530                         continue;
532                 commit_list_insert(commit, &list);
533         }
535         while (list) {
536                 unsigned char sha1[20];
537                 char sign = '+';
539                 commit = list->item;
540                 if (!get_patch_id(commit, &patch_id_opts, sha1) &&
541                     lookup_object(sha1))
542                         sign = '-';
544                 if (verbose) {
545                         static char buf[16384];
546                         pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
547                                             buf, sizeof(buf), 0, NULL, NULL, 0);
548                         printf("%c %s %s\n", sign,
549                                sha1_to_hex(commit->object.sha1), buf);
550                 }
551                 else {
552                         printf("%c %s\n", sign,
553                                sha1_to_hex(commit->object.sha1));
554                 }
556                 list = list->next;
557         }
559         return 0;