@matheusbsbs / matheus-bagliano / commits / 694a577519
Fork 0

git-branch --contains=commit

This teaches git-branch to limit its listing to branches that are descendants to the named commit. When you are using many topic branches, you often would want to see which branch already includes a commit, so that you know which can and cannot be rewound without disrupting other people. One thing that sometimes happens to me is: * Somebody sends a patch that is a good maint material. I apply it to 'maint': $ git checkout maint $ git am -3 -s obvious-fix.patch * Then somebody else sends another patch that is possibly a good maint material, but I'd want to cook it in 'next' to be extra sure. I fork a topic from 'maint' and apply the patch: $ git checkout -b xx/maint-fix-foo $ git am -3 -s ,xx-maint-fix-foo.patch * A minor typo is found in the "obvious-fix.patch". The above happens without pushing the results out, so I can freely recover from it by amending 'maint', as long as I do not forget to rebase the topics that were forked previously. With this patch, I can do this to find out which topic branches already contain the faulty commit: $ git branch --contains=maint^ xx/maint-fix-foo so I can rebase the xx/maint-fix-foo branch before merging it to 'next'. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Nov 7, 2007 at 14:58 UTC 694a577519a762d12b8a53e76b6f1dd3ccf25e7d
1 file changed +54 -3
builtin-branch.c
+54 -3
@@ -184,9 +184,30 @@ struct ref_item {
184 struct ref_list {
185 int index, alloc, maxwidth;
186 struct ref_item *list;
187 + struct commit_list *with_commit;
188 int kinds;
189 };
190
191 +static int has_commit(const unsigned char *sha1, struct commit_list *with_commit)
192 +{
193 + struct commit *commit;
194 +
195 + if (!with_commit)
196 + return 1;
197 + commit = lookup_commit_reference_gently(sha1, 1);
198 + if (!commit)
199 + return 0;
200 + while (with_commit) {
201 + struct commit *other;
202 +
203 + other = with_commit->item;
204 + with_commit = with_commit->next;
205 + if (in_merge_bases(other, &commit, 1))
206 + return 1;
207 + }
208 + return 0;
209 +}
210 +
211 static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
212 {
213 struct ref_list *ref_list = (struct ref_list*)(cb_data);
@@ -206,6 +227,10 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
227 refname += 10;
228 }
229
230 + /* Filter with with_commit if specified */
231 + if (!has_commit(sha1, ref_list->with_commit))
232 + return 0;
233 +
234 /* Don't add types the caller doesn't want */
235 if ((kind & ref_list->kinds) == 0)
236 return 0;
@@ -296,19 +321,20 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
321 }
322 }
323
299 -static void print_ref_list(int kinds, int detached, int verbose, int abbrev)
324 +static void print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit)
325 {
326 int i;
327 struct ref_list ref_list;
328
329 memset(&ref_list, 0, sizeof(ref_list));
330 ref_list.kinds = kinds;
331 + ref_list.with_commit = with_commit;
332 for_each_ref(append_ref, &ref_list);
333
334 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
335
336 detached = (detached && (kinds & REF_LOCAL_BRANCH));
311 - if (detached) {
337 + if (detached && has_commit(head_sha1, with_commit)) {
338 struct ref_item item;
339 item.name = xstrdup("(no branch)");
340 item.kind = REF_LOCAL_BRANCH;
@@ -505,12 +531,29 @@ static void rename_branch(const char *oldname, const char *newname, int force)
531 die("Branch is renamed, but update of config-file failed");
532 }
533
534 +static int opt_parse_with_commit(const struct option *opt, const char *arg, int unset)
535 +{
536 + unsigned char sha1[20];
537 + struct commit *commit;
538 +
539 + if (!arg)
540 + return -1;
541 + if (get_sha1(arg, sha1))
542 + die("malformed object name %s", arg);
543 + commit = lookup_commit_reference(sha1);
544 + if (!commit)
545 + die("no such commit %s", arg);
546 + commit_list_insert(commit, opt->value);
547 + return 0;
548 +}
549 +
550 int cmd_branch(int argc, const char **argv, const char *prefix)
551 {
552 int delete = 0, rename = 0, force_create = 0;
553 int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0;
554 int reflog = 0, track;
555 int kinds = REF_LOCAL_BRANCH;
556 + struct commit_list *with_commit = NULL;
557
558 struct option options[] = {
559 OPT_GROUP("Generic options"),
@@ -519,6 +562,14 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
562 OPT_BOOLEAN( 0 , "color", &branch_use_color, "use colored output"),
563 OPT_SET_INT('r', NULL, &kinds, "act on remote-tracking branches",
564 REF_REMOTE_BRANCH),
565 + OPT_CALLBACK(0, "contains", &with_commit, "commit",
566 + "print only branches that contain the commit",
567 + opt_parse_with_commit),
568 + {
569 + OPTION_CALLBACK, 0, "with", &with_commit, "commit",
570 + "print only branches that contain the commit",
571 + PARSE_OPT_HIDDEN, opt_parse_with_commit,
572 + },
573 OPT__ABBREV(&abbrev),
574
575 OPT_GROUP("Specific git-branch actions:"),
@@ -554,7 +605,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
605 if (delete)
606 return delete_branches(argc, argv, delete > 1, kinds);
607 else if (argc == 0)
557 - print_ref_list(kinds, detached, verbose, abbrev);
608 + print_ref_list(kinds, detached, verbose, abbrev, with_commit);
609 else if (rename && (argc == 1))
610 rename_branch(head, argv[0], rename > 1);
611 else if (rename && (argc == 2))