Merge branch 'master' into js/fmt-patch
[git/git.git] / builtin-log.c
... / ...
CommitLineData
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
14static int cmd_log_wc(int argc, const char **argv, char **envp,
15 struct rev_info *rev)
16{
17 struct commit *commit;
18
19 rev->abbrev = DEFAULT_ABBREV;
20 rev->commit_format = CMIT_FMT_DEFAULT;
21 rev->verbose_header = 1;
22 argc = setup_revisions(argc, argv, rev, "HEAD");
23
24 if (argc > 1)
25 die("unrecognized argument: %s", argv[1]);
26
27 prepare_revision_walk(rev);
28 setup_pager();
29 while ((commit = get_revision(rev)) != NULL) {
30 log_tree_commit(rev, commit);
31 free(commit->buffer);
32 commit->buffer = NULL;
33 }
34 return 0;
35}
36
37int cmd_whatchanged(int argc, const char **argv, char **envp)
38{
39 struct rev_info rev;
40
41 init_revisions(&rev);
42 rev.diff = 1;
43 rev.diffopt.recursive = 1;
44 return cmd_log_wc(argc, argv, envp, &rev);
45}
46
47int cmd_show(int argc, const char **argv, char **envp)
48{
49 struct rev_info rev;
50
51 init_revisions(&rev);
52 rev.diff = 1;
53 rev.diffopt.recursive = 1;
54 rev.combine_merges = 1;
55 rev.dense_combined_merges = 1;
56 rev.always_show_header = 1;
57 rev.ignore_merges = 0;
58 rev.no_walk = 1;
59 return cmd_log_wc(argc, argv, envp, &rev);
60}
61
62int cmd_log(int argc, const char **argv, char **envp)
63{
64 struct rev_info rev;
65
66 init_revisions(&rev);
67 rev.always_show_header = 1;
68 rev.diffopt.recursive = 1;
69 return cmd_log_wc(argc, argv, envp, &rev);
70}
71
72static int istitlechar(char c)
73{
74 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
75 (c >= '0' && c <= '9') || c == '.' || c == '_';
76}
77
78static FILE *realstdout = NULL;
79static char *output_directory = NULL;
80
81static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
82{
83 char filename[1024];
84 char *sol;
85 int len = 0;
86
87 if (output_directory) {
88 strncpy(filename, output_directory, 1010);
89 len = strlen(filename);
90 if (filename[len - 1] != '/')
91 filename[len++] = '/';
92 }
93
94 sprintf(filename + len, "%04d", nr);
95 len = strlen(filename);
96
97 sol = strstr(commit->buffer, "\n\n");
98 if (sol) {
99 int j, space = 1;
100
101 sol += 2;
102 /* strip [PATCH] or [PATCH blabla] */
103 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
104 char *eos = strchr(sol + 6, ']');
105 if (eos) {
106 while (isspace(*eos))
107 eos++;
108 sol = eos;
109 }
110 }
111
112 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
113 if (istitlechar(sol[j])) {
114 if (space) {
115 filename[len++] = '-';
116 space = 0;
117 }
118 filename[len++] = sol[j];
119 if (sol[j] == '.')
120 while (sol[j + 1] == '.')
121 j++;
122 } else
123 space = 1;
124 }
125 while (filename[len - 1] == '.' || filename[len - 1] == '-')
126 len--;
127 }
128 strcpy(filename + len, ".txt");
129 fprintf(realstdout, "%s\n", filename);
130 freopen(filename, "w", stdout);
131}
132
133int cmd_format_patch(int argc, const char **argv, char **envp)
134{
135 struct commit *commit;
136 struct commit **list = NULL;
137 struct rev_info rev;
138 int nr = 0, total, i, j;
139 int use_stdout = 0;
140 int numbered = 0;
141 int keep_subject = 0;
142
143 init_revisions(&rev);
144 rev.commit_format = CMIT_FMT_EMAIL;
145 rev.verbose_header = 1;
146 rev.diff = 1;
147 rev.diffopt.with_raw = 0;
148 rev.diffopt.with_stat = 1;
149 rev.combine_merges = 0;
150 rev.ignore_merges = 1;
151 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
152
153 /*
154 * Parse the arguments before setup_revisions(), or something
155 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
156 * possibly a valid SHA1.
157 */
158 for (i = 1, j = 1; i < argc; i++) {
159 if (!strcmp(argv[i], "--stdout"))
160 use_stdout = 1;
161 else if (!strcmp(argv[i], "-n") ||
162 !strcmp(argv[i], "--numbered"))
163 numbered = 1;
164 else if (!strcmp(argv[i], "-k") ||
165 !strcmp(argv[i], "--keep-subject")) {
166 keep_subject = 1;
167 rev.total = -1;
168 } else if (!strcmp(argv[i], "-o")) {
169 if (argc < 3)
170 die ("Which directory?");
171 if (mkdir(argv[i + 1], 0777) < 0 && errno != EEXIST)
172 die("Could not create directory %s",
173 argv[i + 1]);
174 output_directory = strdup(argv[i + 1]);
175 i++;
176 } else
177 argv[j++] = argv[i];
178 }
179 argc = j;
180
181 if (numbered && keep_subject < 0)
182 die ("-n and -k are mutually exclusive.");
183
184 argc = setup_revisions(argc, argv, &rev, "HEAD");
185 if (argc > 1)
186 die ("unrecognized argument: %s", argv[1]);
187
188 if (!use_stdout)
189 realstdout = fdopen(dup(1), "w");
190
191 prepare_revision_walk(&rev);
192 while ((commit = get_revision(&rev)) != NULL) {
193 /* ignore merges */
194 if (commit->parents && commit->parents->next)
195 continue;
196 nr++;
197 list = realloc(list, nr * sizeof(list[0]));
198 list[nr - 1] = commit;
199 }
200 total = nr;
201 if (numbered)
202 rev.total = total;
203 while (0 <= --nr) {
204 int shown;
205 commit = list[nr];
206 rev.nr = total - nr;
207 if (!use_stdout)
208 reopen_stdout(commit, rev.nr, keep_subject);
209 shown = log_tree_commit(&rev, commit);
210 free(commit->buffer);
211 commit->buffer = NULL;
212 if (shown)
213 printf("-- \n%s\n\n", git_version_string);
214 if (!use_stdout)
215 fclose(stdout);
216 }
217 if (output_directory)
218 free(output_directory);
219 free(list);
220 return 0;
221}
222