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