@matheusbsbs / matheus-bagliano / commits / 9c6efa366e
Fork 0

format-patch: introduce "--ignore-if-in-upstream"

With this flag, format-patch will try very hard not to output patches which are already in the upstream branch. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Jun 25, 2006 at 03:52 UTC 9c6efa366eed0526e82a052b77227451273af0c3
1 file changed +80
builtin-log.c
+80
@@ -160,6 +160,71 @@ static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
160 freopen(filename, "w", stdout);
161 }
162
163 +static void reset_all_objects_flags()
164 +{
165 + int i;
166 +
167 + for (i = 0; i < obj_allocs; i++)
168 + if (objs[i])
169 + objs[i]->flags = 0;
170 +}
171 +
172 +static int get_patch_id(struct commit *commit, struct diff_options *options,
173 + unsigned char *sha1)
174 +{
175 + diff_tree_sha1(commit->parents->item->object.sha1, commit->object.sha1,
176 + "", options);
177 + diffcore_std(options);
178 + return diff_flush_patch_id(options, sha1);
179 +}
180 +
181 +static void get_patch_ids(struct rev_info *rev, struct diff_options *options)
182 +{
183 + struct rev_info check_rev;
184 + struct commit *commit;
185 + struct object *o1, *o2;
186 + unsigned flags1, flags2;
187 + unsigned char sha1[20];
188 +
189 + if (rev->pending.nr != 2)
190 + die("Need exactly one range.");
191 +
192 + o1 = rev->pending.objects[0].item;
193 + flags1 = o1->flags;
194 + o2 = rev->pending.objects[1].item;
195 + flags2 = o2->flags;
196 +
197 + if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
198 + die("Not a range.");
199 +
200 + diff_setup(options);
201 + options->recursive = 1;
202 + if (diff_setup_done(options) < 0)
203 + die("diff_setup_done failed");
204 +
205 + /* given a range a..b get all patch ids for b..a */
206 + init_revisions(&check_rev);
207 + o1->flags ^= UNINTERESTING;
208 + o2->flags ^= UNINTERESTING;
209 + add_pending_object(&check_rev, o1, "o1");
210 + add_pending_object(&check_rev, o2, "o2");
211 + prepare_revision_walk(&check_rev);
212 +
213 + while ((commit = get_revision(&check_rev)) != NULL) {
214 + /* ignore merges */
215 + if (commit->parents && commit->parents->next)
216 + continue;
217 +
218 + if (!get_patch_id(commit, options, sha1))
219 + created_object(sha1, xcalloc(1, sizeof(struct object)));
220 + }
221 +
222 + /* reset for next revision walk */
223 + reset_all_objects_flags();
224 + o1->flags = flags1;
225 + o2->flags = flags2;
226 +}
227 +
228 int cmd_format_patch(int argc, const char **argv, char **envp)
229 {
230 struct commit *commit;
@@ -170,6 +235,8 @@ int cmd_format_patch(int argc, const char **argv, char **envp)
235 int numbered = 0;
236 int start_number = -1;
237 int keep_subject = 0;
238 + int ignore_if_in_upstream = 0;
239 + struct diff_options patch_id_opts;
240 char *add_signoff = NULL;
241
242 init_revisions(&rev);
@@ -235,6 +302,8 @@ int cmd_format_patch(int argc, const char **argv, char **envp)
302 rev.mime_boundary = git_version_string;
303 else if (!strncmp(argv[i], "--attach=", 9))
304 rev.mime_boundary = argv[i] + 9;
305 + else if (!strcmp(argv[i], "--ignore-if-in-upstream"))
306 + ignore_if_in_upstream = 1;
307 else
308 argv[j++] = argv[i];
309 }
@@ -262,14 +331,25 @@ int cmd_format_patch(int argc, const char **argv, char **envp)
331 add_head(&rev);
332 }
333
334 + if (ignore_if_in_upstream)
335 + get_patch_ids(&rev, &patch_id_opts);
336 +
337 if (!use_stdout)
338 realstdout = fdopen(dup(1), "w");
339
340 prepare_revision_walk(&rev);
341 while ((commit = get_revision(&rev)) != NULL) {
342 + unsigned char sha1[20];
343 +
344 /* ignore merges */
345 if (commit->parents && commit->parents->next)
346 continue;
347 +
348 + if (ignore_if_in_upstream &&
349 + !get_patch_id(commit, &patch_id_opts, sha1) &&
350 + lookup_object(sha1))
351 + continue;
352 +
353 nr++;
354 list = realloc(list, nr * sizeof(list[0]));
355 list[nr - 1] = commit;