Add instructions to commit template.
[git/git.git] / builtin-log.c
CommitLineData
70827b15
LT
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"
91efcf60 12#include "builtin.h"
70827b15 13
e686eb98
JS
14/* this is in builtin-diff.c */
15void add_head(struct rev_info *revs);
16
70827b15
LT
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");
582af688
SE
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;
31 }
32 }
70827b15
LT
33
34 if (argc > 1)
35 die("unrecognized argument: %s", argv[1]);
36
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 }
44 return 0;
45}
46
47int cmd_whatchanged(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 return cmd_log_wc(argc, argv, envp, &rev);
55}
56
57int cmd_show(int argc, const char **argv, char **envp)
58{
59 struct rev_info rev;
60
61 init_revisions(&rev);
62 rev.diff = 1;
63 rev.diffopt.recursive = 1;
64 rev.combine_merges = 1;
65 rev.dense_combined_merges = 1;
66 rev.always_show_header = 1;
67 rev.ignore_merges = 0;
68 rev.no_walk = 1;
69 return cmd_log_wc(argc, argv, envp, &rev);
70}
71
72int cmd_log(int argc, const char **argv, char **envp)
73{
74 struct rev_info rev;
75
76 init_revisions(&rev);
77 rev.always_show_header = 1;
78 rev.diffopt.recursive = 1;
79 return cmd_log_wc(argc, argv, envp, &rev);
80}
91efcf60 81
0377db77
JS
82static int istitlechar(char c)
83{
84 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
85 (c >= '0' && c <= '9') || c == '.' || c == '_';
86}
87
81f3a188 88static FILE *realstdout = NULL;
2448482b 89static char *output_directory = NULL;
81f3a188 90
8ac80a57 91static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
0377db77
JS
92{
93 char filename[1024];
94 char *sol;
2448482b 95 int len = 0;
0377db77 96
2448482b
JS
97 if (output_directory) {
98 strncpy(filename, output_directory, 1010);
99 len = strlen(filename);
100 if (filename[len - 1] != '/')
101 filename[len++] = '/';
102 }
0377db77 103
2448482b 104 sprintf(filename + len, "%04d", nr);
0377db77
JS
105 len = strlen(filename);
106
107 sol = strstr(commit->buffer, "\n\n");
108 if (sol) {
109 int j, space = 1;
110
111 sol += 2;
112 /* strip [PATCH] or [PATCH blabla] */
8ac80a57 113 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
0377db77
JS
114 char *eos = strchr(sol + 6, ']');
115 if (eos) {
116 while (isspace(*eos))
117 eos++;
118 sol = eos;
119 }
120 }
121
122 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
123 if (istitlechar(sol[j])) {
124 if (space) {
125 filename[len++] = '-';
126 space = 0;
127 }
128 filename[len++] = sol[j];
129 if (sol[j] == '.')
130 while (sol[j + 1] == '.')
131 j++;
132 } else
133 space = 1;
134 }
135 while (filename[len - 1] == '.' || filename[len - 1] == '-')
136 len--;
137 }
138 strcpy(filename + len, ".txt");
81f3a188 139 fprintf(realstdout, "%s\n", filename);
0377db77
JS
140 freopen(filename, "w", stdout);
141}
142
91efcf60
JH
143int cmd_format_patch(int argc, const char **argv, char **envp)
144{
145 struct commit *commit;
146 struct commit **list = NULL;
147 struct rev_info rev;
2448482b 148 int nr = 0, total, i, j;
0377db77 149 int use_stdout = 0;
596524b3 150 int numbered = 0;
fa0f02df 151 int start_number = -1;
8ac80a57 152 int keep_subject = 0;
91efcf60
JH
153
154 init_revisions(&rev);
155 rev.commit_format = CMIT_FMT_EMAIL;
156 rev.verbose_header = 1;
157 rev.diff = 1;
158 rev.diffopt.with_raw = 0;
159 rev.diffopt.with_stat = 1;
160 rev.combine_merges = 0;
161 rev.ignore_merges = 1;
162 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
91efcf60 163
2448482b
JS
164 /*
165 * Parse the arguments before setup_revisions(), or something
166 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
167 * possibly a valid SHA1.
168 */
169 for (i = 1, j = 1; i < argc; i++) {
170 if (!strcmp(argv[i], "--stdout"))
0377db77 171 use_stdout = 1;
596524b3
JS
172 else if (!strcmp(argv[i], "-n") ||
173 !strcmp(argv[i], "--numbered"))
174 numbered = 1;
fa0f02df
JS
175 else if (!strncmp(argv[i], "--start-number=", 15))
176 start_number = strtol(argv[i] + 15, NULL, 10);
177 else if (!strcmp(argv[i], "--start-number")) {
178 i++;
179 if (i == argc)
180 die("Need a number for --start-number");
181 start_number = strtol(argv[i], NULL, 10);
182 } else if (!strcmp(argv[i], "-k") ||
8ac80a57
JS
183 !strcmp(argv[i], "--keep-subject")) {
184 keep_subject = 1;
185 rev.total = -1;
186 } else if (!strcmp(argv[i], "-o")) {
2448482b
JS
187 if (argc < 3)
188 die ("Which directory?");
189 if (mkdir(argv[i + 1], 0777) < 0 && errno != EEXIST)
190 die("Could not create directory %s",
191 argv[i + 1]);
192 output_directory = strdup(argv[i + 1]);
193 i++;
698ce6f8
JS
194 }
195 else if (!strcmp(argv[i], "--attach"))
196 rev.mime_boundary = git_version_string;
197 else if (!strncmp(argv[i], "--attach=", 9))
198 rev.mime_boundary = argv[i] + 9;
199 else
2448482b 200 argv[j++] = argv[i];
0377db77 201 }
2448482b
JS
202 argc = j;
203
fa0f02df
JS
204 if (numbered && start_number < 0)
205 start_number = 1;
8ac80a57
JS
206 if (numbered && keep_subject < 0)
207 die ("-n and -k are mutually exclusive.");
208
2448482b
JS
209 argc = setup_revisions(argc, argv, &rev, "HEAD");
210 if (argc > 1)
211 die ("unrecognized argument: %s", argv[1]);
0377db77 212
e686eb98
JS
213 if (rev.pending_objects && rev.pending_objects->next == NULL) {
214 rev.pending_objects->item->flags |= UNINTERESTING;
215 add_head(&rev);
216 }
217
81f3a188
JS
218 if (!use_stdout)
219 realstdout = fdopen(dup(1), "w");
220
91efcf60
JH
221 prepare_revision_walk(&rev);
222 while ((commit = get_revision(&rev)) != NULL) {
0377db77
JS
223 /* ignore merges */
224 if (commit->parents && commit->parents->next)
225 continue;
91efcf60
JH
226 nr++;
227 list = realloc(list, nr * sizeof(list[0]));
228 list[nr - 1] = commit;
229 }
0377db77 230 total = nr;
596524b3 231 if (numbered)
fa0f02df 232 rev.total = total + start_number - 1;
91efcf60
JH
233 while (0 <= --nr) {
234 int shown;
235 commit = list[nr];
fa0f02df 236 rev.nr = rev.total - nr;
0377db77 237 if (!use_stdout)
8ac80a57 238 reopen_stdout(commit, rev.nr, keep_subject);
91efcf60
JH
239 shown = log_tree_commit(&rev, commit);
240 free(commit->buffer);
241 commit->buffer = NULL;
698ce6f8
JS
242 if (shown) {
243 if (rev.mime_boundary)
244 printf("\n--%s%s--\n\n\n",
245 mime_boundary_leader,
246 rev.mime_boundary);
247 else
248 printf("-- \n%s\n\n", git_version_string);
249 }
0377db77
JS
250 if (!use_stdout)
251 fclose(stdout);
91efcf60 252 }
2448482b
JS
253 if (output_directory)
254 free(output_directory);
91efcf60
JH
255 free(list);
256 return 0;
257}
258