Teach fmt-patch to write individual files.
[git/jrn.git] / builtin-log.c
blob576703c47ec84c4028b0ca40985888a61556f3d2
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 void reopen_stdout(struct commit *commit, int nr)
80 char filename[1024];
81 char *sol;
82 int len;
85 sprintf(filename, "%04d", nr);
86 len = strlen(filename);
88 sol = strstr(commit->buffer, "\n\n");
89 if (sol) {
90 int j, space = 1;
92 sol += 2;
93 /* strip [PATCH] or [PATCH blabla] */
94 if (!strncmp(sol, "[PATCH", 6)) {
95 char *eos = strchr(sol + 6, ']');
96 if (eos) {
97 while (isspace(*eos))
98 eos++;
99 sol = eos;
103 for (j = 0; len < 1024 - 6 && sol[j] && sol[j] != '\n'; j++) {
104 if (istitlechar(sol[j])) {
105 if (space) {
106 filename[len++] = '-';
107 space = 0;
109 filename[len++] = sol[j];
110 if (sol[j] == '.')
111 while (sol[j + 1] == '.')
112 j++;
113 } else
114 space = 1;
116 while (filename[len - 1] == '.' || filename[len - 1] == '-')
117 len--;
119 strcpy(filename + len, ".txt");
120 fprintf(stderr, "%s\n", filename);
121 freopen(filename, "w", stdout);
124 int cmd_format_patch(int argc, const char **argv, char **envp)
126 struct commit *commit;
127 struct commit **list = NULL;
128 struct rev_info rev;
129 int nr = 0, total;
130 int use_stdout = 0;
132 init_revisions(&rev);
133 rev.commit_format = CMIT_FMT_EMAIL;
134 rev.verbose_header = 1;
135 rev.diff = 1;
136 rev.diffopt.with_raw = 0;
137 rev.diffopt.with_stat = 1;
138 rev.combine_merges = 0;
139 rev.ignore_merges = 1;
140 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
141 argc = setup_revisions(argc, argv, &rev, "HEAD");
143 while (argc > 1) {
144 if (!strcmp(argv[1], "--stdout"))
145 use_stdout = 1;
146 else
147 die ("unrecognized argument: %s", argv[1]);
148 argc--;
149 argv++;
152 prepare_revision_walk(&rev);
153 while ((commit = get_revision(&rev)) != NULL) {
154 /* ignore merges */
155 if (commit->parents && commit->parents->next)
156 continue;
157 nr++;
158 list = realloc(list, nr * sizeof(list[0]));
159 list[nr - 1] = commit;
161 total = nr;
162 while (0 <= --nr) {
163 int shown;
164 commit = list[nr];
165 if (!use_stdout)
166 reopen_stdout(commit, total - nr);
167 shown = log_tree_commit(&rev, commit);
168 free(commit->buffer);
169 commit->buffer = NULL;
170 if (shown)
171 printf("-- \n%s\n\n", git_version_string);
172 if (!use_stdout)
173 fclose(stdout);
175 free(list);
176 return 0;