Clean up sha1 file writing
[git/git-svn.git] / builtin-log.c
blobc8feb0f7953a4d431c180e33f9f71d4434a4e51a
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;
44 return 0;
47 int cmd_whatchanged(int argc, const char **argv, char **envp)
49 struct rev_info rev;
51 init_revisions(&rev);
52 rev.diff = 1;
53 rev.diffopt.recursive = 1;
54 return cmd_log_wc(argc, argv, envp, &rev);
57 int cmd_show(int argc, const char **argv, char **envp)
59 struct rev_info rev;
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);
72 int cmd_log(int argc, const char **argv, char **envp)
74 struct rev_info rev;
76 init_revisions(&rev);
77 rev.always_show_header = 1;
78 rev.diffopt.recursive = 1;
79 return cmd_log_wc(argc, argv, envp, &rev);
82 static int istitlechar(char c)
84 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
85 (c >= '0' && c <= '9') || c == '.' || c == '_';
88 static FILE *realstdout = NULL;
89 static char *output_directory = NULL;
91 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
93 char filename[1024];
94 char *sol;
95 int len = 0;
97 if (output_directory) {
98 strncpy(filename, output_directory, 1010);
99 len = strlen(filename);
100 if (filename[len - 1] != '/')
101 filename[len++] = '/';
104 sprintf(filename + len, "%04d", nr);
105 len = strlen(filename);
107 sol = strstr(commit->buffer, "\n\n");
108 if (sol) {
109 int j, space = 1;
111 sol += 2;
112 /* strip [PATCH] or [PATCH blabla] */
113 if (!keep_subject && !strncmp(sol, "[PATCH", 6)) {
114 char *eos = strchr(sol + 6, ']');
115 if (eos) {
116 while (isspace(*eos))
117 eos++;
118 sol = eos;
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;
128 filename[len++] = sol[j];
129 if (sol[j] == '.')
130 while (sol[j + 1] == '.')
131 j++;
132 } else
133 space = 1;
135 while (filename[len - 1] == '.' || filename[len - 1] == '-')
136 len--;
138 strcpy(filename + len, ".txt");
139 fprintf(realstdout, "%s\n", filename);
140 freopen(filename, "w", stdout);
143 int cmd_format_patch(int argc, const char **argv, char **envp)
145 struct commit *commit;
146 struct commit **list = NULL;
147 struct rev_info rev;
148 int nr = 0, total, i, j;
149 int use_stdout = 0;
150 int numbered = 0;
151 int keep_subject = 0;
153 init_revisions(&rev);
154 rev.commit_format = CMIT_FMT_EMAIL;
155 rev.verbose_header = 1;
156 rev.diff = 1;
157 rev.diffopt.with_raw = 0;
158 rev.diffopt.with_stat = 1;
159 rev.combine_merges = 0;
160 rev.ignore_merges = 1;
161 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
164 * Parse the arguments before setup_revisions(), or something
165 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
166 * possibly a valid SHA1.
168 for (i = 1, j = 1; i < argc; i++) {
169 if (!strcmp(argv[i], "--stdout"))
170 use_stdout = 1;
171 else if (!strcmp(argv[i], "-n") ||
172 !strcmp(argv[i], "--numbered"))
173 numbered = 1;
174 else if (!strcmp(argv[i], "-k") ||
175 !strcmp(argv[i], "--keep-subject")) {
176 keep_subject = 1;
177 rev.total = -1;
178 } else if (!strcmp(argv[i], "-o")) {
179 if (argc < 3)
180 die ("Which directory?");
181 if (mkdir(argv[i + 1], 0777) < 0 && errno != EEXIST)
182 die("Could not create directory %s",
183 argv[i + 1]);
184 output_directory = strdup(argv[i + 1]);
185 i++;
187 else if (!strcmp(argv[i], "--attach"))
188 rev.mime_boundary = git_version_string;
189 else if (!strncmp(argv[i], "--attach=", 9))
190 rev.mime_boundary = argv[i] + 9;
191 else
192 argv[j++] = argv[i];
194 argc = j;
196 if (numbered && keep_subject < 0)
197 die ("-n and -k are mutually exclusive.");
199 argc = setup_revisions(argc, argv, &rev, "HEAD");
200 if (argc > 1)
201 die ("unrecognized argument: %s", argv[1]);
203 if (rev.pending_objects && rev.pending_objects->next == NULL) {
204 rev.pending_objects->item->flags |= UNINTERESTING;
205 add_head(&rev);
208 if (!use_stdout)
209 realstdout = fdopen(dup(1), "w");
211 prepare_revision_walk(&rev);
212 while ((commit = get_revision(&rev)) != NULL) {
213 /* ignore merges */
214 if (commit->parents && commit->parents->next)
215 continue;
216 nr++;
217 list = realloc(list, nr * sizeof(list[0]));
218 list[nr - 1] = commit;
220 total = nr;
221 if (numbered)
222 rev.total = total;
223 while (0 <= --nr) {
224 int shown;
225 commit = list[nr];
226 rev.nr = total - nr;
227 if (!use_stdout)
228 reopen_stdout(commit, rev.nr, keep_subject);
229 shown = log_tree_commit(&rev, commit);
230 free(commit->buffer);
231 commit->buffer = NULL;
232 if (shown) {
233 if (rev.mime_boundary)
234 printf("\n--%s%s--\n\n\n",
235 mime_boundary_leader,
236 rev.mime_boundary);
237 else
238 printf("-- \n%s\n\n", git_version_string);
240 if (!use_stdout)
241 fclose(stdout);
243 if (output_directory)
244 free(output_directory);
245 free(list);
246 return 0;