fmt-patch: understand old <his> notation
[git/dscho.git] / builtin-log.c
blobd5bbc1cc06931974de46078b5cac17e315177e40
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");
27 if (argc > 1)
28 die("unrecognized argument: %s", argv[1]);
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;
37 return 0;
40 int cmd_whatchanged(int argc, const char **argv, char **envp)
42 struct rev_info rev;
44 init_revisions(&rev);
45 rev.diff = 1;
46 rev.diffopt.recursive = 1;
47 return cmd_log_wc(argc, argv, envp, &rev);
50 int cmd_show(int argc, const char **argv, char **envp)
52 struct rev_info rev;
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);
65 int cmd_log(int argc, const char **argv, char **envp)
67 struct rev_info rev;
69 init_revisions(&rev);
70 rev.always_show_header = 1;
71 rev.diffopt.recursive = 1;
72 return cmd_log_wc(argc, argv, envp, &rev);
75 static int istitlechar(char c)
77 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
78 (c >= '0' && c <= '9') || c == '.' || c == '_';
81 static FILE *realstdout = NULL;
82 static char *output_directory = NULL;
84 static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
86 char filename[1024];
87 char *sol;
88 int len = 0;
90 if (output_directory) {
91 strncpy(filename, output_directory, 1010);
92 len = strlen(filename);
93 if (filename[len - 1] != '/')
94 filename[len++] = '/';
97 sprintf(filename + len, "%04d", nr);
98 len = strlen(filename);
100 sol = strstr(commit->buffer, "\n\n");
101 if (sol) {
102 int j, space = 1;
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;
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;
121 filename[len++] = sol[j];
122 if (sol[j] == '.')
123 while (sol[j + 1] == '.')
124 j++;
125 } else
126 space = 1;
128 while (filename[len - 1] == '.' || filename[len - 1] == '-')
129 len--;
131 strcpy(filename + len, ".txt");
132 fprintf(realstdout, "%s\n", filename);
133 freopen(filename, "w", stdout);
136 int cmd_format_patch(int argc, const char **argv, char **envp)
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;
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;
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.
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];
182 argc = j;
184 if (numbered && keep_subject < 0)
185 die ("-n and -k are mutually exclusive.");
187 argc = setup_revisions(argc, argv, &rev, "HEAD");
188 if (argc > 1)
189 die ("unrecognized argument: %s", argv[1]);
191 if (rev.pending_objects && rev.pending_objects->next == NULL) {
192 rev.pending_objects->item->flags |= UNINTERESTING;
193 add_head(&rev);
196 if (!use_stdout)
197 realstdout = fdopen(dup(1), "w");
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;
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);
225 if (output_directory)
226 free(output_directory);
227 free(list);
228 return 0;