@matheusbsbs / matheus-bagliano / commits / 2448482b3d
Fork 0

fmt-patch: implement -o <dir>

I had to move the command line parsing around a little; setup_revisions() could mistaken <dir> for a valid ref. 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 03:33 UTC 2448482b3d5e265dd29fa38c3827565f6f2f31ec
1 file changed +34 -10
builtin-log.c
+34 -10
@@ -76,15 +76,22 @@ static int istitlechar(char 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;
84 - int len;
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
87 - sprintf(filename, "%04d", nr);
94 + sprintf(filename + len, "%04d", nr);
95 len = strlen(filename);
96
97 sol = strstr(commit->buffer, "\n\n");
@@ -128,7 +135,7 @@ int cmd_format_patch(int argc, const char **argv, char **envp)
135 struct commit *commit;
136 struct commit **list = NULL;
137 struct rev_info rev;
131 - int nr = 0, total;
138 + int nr = 0, total, i, j;
139 int use_stdout = 0;
140
141 init_revisions(&rev);
@@ -140,16 +147,31 @@ int cmd_format_patch(int argc, const char **argv, char **envp)
147 rev.combine_merges = 0;
148 rev.ignore_merges = 1;
149 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
143 - argc = setup_revisions(argc, argv, &rev, "HEAD");
150
145 - while (argc > 1) {
146 - if (!strcmp(argv[1], "--stdout"))
151 + /*
152 + * Parse the arguments before setup_revisions(), or something
153 + * like "git fmt-patch -o a123 HEAD^.." may fail; a123 is
154 + * possibly a valid SHA1.
155 + */
156 + for (i = 1, j = 1; i < argc; i++) {
157 + if (!strcmp(argv[i], "--stdout"))
158 use_stdout = 1;
148 - else
149 - die ("unrecognized argument: %s", argv[1]);
150 - argc--;
151 - argv++;
159 + else if (!strcmp(argv[i], "-o")) {
160 + if (argc < 3)
161 + die ("Which directory?");
162 + if (mkdir(argv[i + 1], 0777) < 0 && errno != EEXIST)
163 + die("Could not create directory %s",
164 + argv[i + 1]);
165 + output_directory = strdup(argv[i + 1]);
166 + i++;
167 + } else
168 + argv[j++] = argv[i];
169 }
170 + argc = j;
171 +
172 + argc = setup_revisions(argc, argv, &rev, "HEAD");
173 + if (argc > 1)
174 + die ("unrecognized argument: %s", argv[1]);
175
176 if (!use_stdout)
177 realstdout = fdopen(dup(1), "w");
@@ -177,6 +199,8 @@ int cmd_format_patch(int argc, const char **argv, char **envp)
199 if (!use_stdout)
200 fclose(stdout);
201 }
202 + if (output_directory)
203 + free(output_directory);
204 free(list);
205 return 0;
206 }