Teach fmt-patch about --numbered
[git/git.git] / builtin-log.c
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 static 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
37 int 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
47 int 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
62 int 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
72 static int istitlechar(char c)
73 {
74 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
75 (c >= '0' && c <= '9') || c == '.' || c == '_';
76 }
77
78 static FILE *realstdout = NULL;
79 static char *output_directory = NULL;
80
81 static void reopen_stdout(struct commit *commit, int nr)
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 (!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
133 int 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
142 init_revisions(&rev);
143 rev.commit_format = CMIT_FMT_EMAIL;
144 rev.verbose_header = 1;
145 rev.diff = 1;
146 rev.diffopt.with_raw = 0;
147 rev.diffopt.with_stat = 1;
148 rev.combine_merges = 0;
149 rev.ignore_merges = 1;
150 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
151
152 /*
153 * Parse the arguments before setup_revisions(), or something
154 * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
155 * possibly a valid SHA1.
156 */
157 for (i = 1, j = 1; i < argc; i++) {
158 if (!strcmp(argv[i], "--stdout"))
159 use_stdout = 1;
160 else if (!strcmp(argv[i], "-n") ||
161 !strcmp(argv[i], "--numbered"))
162 numbered = 1;
163 else if (!strcmp(argv[i], "-o")) {
164 if (argc < 3)
165 die ("Which directory?");
166 if (mkdir(argv[i + 1], 0777) < 0 && errno != EEXIST)
167 die("Could not create directory %s",
168 argv[i + 1]);
169 output_directory = strdup(argv[i + 1]);
170 i++;
171 } else
172 argv[j++] = argv[i];
173 }
174 argc = j;
175
176 argc = setup_revisions(argc, argv, &rev, "HEAD");
177 if (argc > 1)
178 die ("unrecognized argument: %s", argv[1]);
179
180 if (!use_stdout)
181 realstdout = fdopen(dup(1), "w");
182
183 prepare_revision_walk(&rev);
184 while ((commit = get_revision(&rev)) != NULL) {
185 /* ignore merges */
186 if (commit->parents && commit->parents->next)
187 continue;
188 nr++;
189 list = realloc(list, nr * sizeof(list[0]));
190 list[nr - 1] = commit;
191 }
192 total = nr;
193 if (numbered)
194 rev.total = total;
195 while (0 <= --nr) {
196 int shown;
197 commit = list[nr];
198 rev.nr = total - nr;
199 if (!use_stdout)
200 reopen_stdout(commit, rev.nr);
201 shown = log_tree_commit(&rev, commit);
202 free(commit->buffer);
203 commit->buffer = NULL;
204 if (shown)
205 printf("-- \n%s\n\n", git_version_string);
206 if (!use_stdout)
207 fclose(stdout);
208 }
209 if (output_directory)
210 free(output_directory);
211 free(list);
212 return 0;
213 }
214