@matheusbsbs / matheus-bagliano / commits / 0377db77da
Fork 0

Teach fmt-patch to write individual files.

When called with "--stdout", it still writes to standard output. Notable differences to git-format-patch: - since fmt-patch uses the standardized logging machinery, it is no longer "From nobody", but "From <commit_sha1>", - the empty lines before and after the "---" just before the diffstat are no longer there, - git-format-patch outputs the commit_sha1 just before the first diff, which fmt-patch does not, - the file names are no longer output to stdout, but to stderr (since stdout is freopen()ed all the time), and - "git fmt-patch HEAD^" does not work as expected: it outputs *all* commits reachable from HEAD^! The last one is possibly a showstopper. At least I used to call that command quite often... Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed May 5, 2006 at 01:16 UTC 0377db77da3f66b8a2bd2f8a9391d22f7a576e34
1 file changed +71 -1
builtin-log.c
+71 -1
@@ -69,12 +69,65 @@ int cmd_log(int argc, const char **argv, char **envp)
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;
@@ -87,20 +140,37 @@ int cmd_format_patch(int argc, const char **argv, char **envp)
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;