fmt-patch: output file names to stdout
[git/mergetool.git] / builtin-log.c
blob1649f4943e03f379f69716ecea2074b0d92c41f3
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 static int cmd_log_wc(int argc, const char **argv, char **envp,
15 struct rev_info *rev)
17 struct commit *commit;
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");
24 if (argc > 1)
25 die("unrecognized argument: %s", argv[1]);
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;
34 return 0;
37 int cmd_whatchanged(int argc, const char **argv, char **envp)
39 struct rev_info rev;
41 init_revisions(&rev);
42 rev.diff = 1;
43 rev.diffopt.recursive = 1;
44 return cmd_log_wc(argc, argv, envp, &rev);
47 int cmd_show(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 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);
62 int cmd_log(int argc, const char **argv, char **envp)
64 struct rev_info rev;
66 init_revisions(&rev);
67 rev.always_show_header = 1;
68 rev.diffopt.recursive = 1;
69 return cmd_log_wc(argc, argv, envp, &rev);
72 static int istitlechar(char c)
74 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
75 (c >= '0' && c <= '9') || c == '.' || c == '_';
78 static FILE *realstdout = NULL;
80 static void reopen_stdout(struct commit *commit, int nr)
82 char filename[1024];
83 char *sol;
84 int len;
87 sprintf(filename, "%04d", nr);
88 len = strlen(filename);
90 sol = strstr(commit->buffer, "\n\n");
91 if (sol) {
92 int j, space = 1;
94 sol += 2;
95 /* strip [PATCH] or [PATCH blabla] */
96 if (!strncmp(sol, "[PATCH", 6)) {
97 char *eos = strchr(sol + 6, ']');
98 if (eos) {
99 while (isspace(*eos))
100 eos++;
101 sol = eos;
105 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
106 if (istitlechar(sol[j])) {
107 if (space) {
108 filename[len++] = '-';
109 space = 0;
111 filename[len++] = sol[j];
112 if (sol[j] == '.')
113 while (sol[j + 1] == '.')
114 j++;
115 } else
116 space = 1;
118 while (filename[len - 1] == '.' || filename[len - 1] == '-')
119 len--;
121 strcpy(filename + len, ".txt");
122 fprintf(realstdout, "%s\n", filename);
123 freopen(filename, "w", stdout);
126 int cmd_format_patch(int argc, const char **argv, char **envp)
128 struct commit *commit;
129 struct commit **list = NULL;
130 struct rev_info rev;
131 int nr = 0, total;
132 int use_stdout = 0;
134 init_revisions(&rev);
135 rev.commit_format = CMIT_FMT_EMAIL;
136 rev.verbose_header = 1;
137 rev.diff = 1;
138 rev.diffopt.with_raw = 0;
139 rev.diffopt.with_stat = 1;
140 rev.combine_merges = 0;
141 rev.ignore_merges = 1;
142 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
143 argc = setup_revisions(argc, argv, &rev, "HEAD");
145 while (argc > 1) {
146 if (!strcmp(argv[1], "--stdout"))
147 use_stdout = 1;
148 else
149 die ("unrecognized argument: %s", argv[1]);
150 argc--;
151 argv++;
154 if (!use_stdout)
155 realstdout = fdopen(dup(1), "w");
157 prepare_revision_walk(&rev);
158 while ((commit = get_revision(&rev)) != NULL) {
159 /* ignore merges */
160 if (commit->parents && commit->parents->next)
161 continue;
162 nr++;
163 list = realloc(list, nr * sizeof(list[0]));
164 list[nr - 1] = commit;
166 total = nr;
167 while (0 <= --nr) {
168 int shown;
169 commit = list[nr];
170 if (!use_stdout)
171 reopen_stdout(commit, total - nr);
172 shown = log_tree_commit(&rev, commit);
173 free(commit->buffer);
174 commit->buffer = NULL;
175 if (shown)
176 printf("-- \n%s\n\n", git_version_string);
177 if (!use_stdout)
178 fclose(stdout);
180 free(list);
181 return 0;