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);
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;
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;
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"),
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:"),
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))