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 void reopen_stdout(struct commit *commit, int nr)
79
+{
80
+ char filename[1024];
81
+ char *sol;
82
+ int len;
83
+
84
+
85
+ sprintf(filename, "%04d", nr);
86
+ len = strlen(filename);
87
+
88
+ sol = strstr(commit->buffer, "\n\n");
89
+ if (sol) {
90
+ int j, space = 1;
91
+
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;
100
+ }
101
+ }
102
+
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;
108
+ }
109
+ filename[len++] = sol[j];
110
+ if (sol[j] == '.')
111
+ while (sol[j + 1] == '.')
112
+ j++;
113
+ } else
114
+ space = 1;
115
+ }
116
+ while (filename[len - 1] == '.' || filename[len - 1] == '-')
117
+ len--;
118
+ }
119
+ strcpy(filename + len, ".txt");
120
+ fprintf(stderr, "%s\n", filename);
121
+ freopen(filename, "w", stdout);
122
+}
123
+
124
int cmd_format_patch(int argc, const char **argv, char **envp)
125
{
126
struct commit *commit;
127
struct commit **list = NULL;
128
struct rev_info rev;
77
- int nr = 0;
129
+ int nr = 0, total;
130
+ int use_stdout = 0;
131
132
init_revisions(&rev);
133
rev.commit_format = CMIT_FMT_EMAIL;
140
rev.diffopt.output_format = DIFF_FORMAT_PATCH;
141
argc = setup_revisions(argc, argv, &rev, "HEAD");
142
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++;
150
+ }
151
+
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;
160
}
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);
174
}
175
free(list);
176
return 0;