@@ -385,6 +385,39 @@ int validate_branchname(const char *name, struct strbuf *ref)
385385static int initialized_checked_out_branches ;
386386static struct strmap current_checked_out_branches = STRMAP_INIT ;
387387
388+ enum branch_checkout_kind {
389+ BRANCH_CHECKOUT_KIND_CHECKOUT ,
390+ BRANCH_CHECKOUT_KIND_REBASE ,
391+ BRANCH_CHECKOUT_KIND_BISECT ,
392+ BRANCH_CHECKOUT_KIND_UPDATE_REF ,
393+ };
394+
395+ struct checked_out_branch {
396+ char * refname ;
397+ char * path ;
398+ enum branch_checkout_kind kind ;
399+ };
400+
401+ static struct checked_out_branch * checked_out_branches ;
402+ static size_t checked_out_branches_alloc , checked_out_branches_nr ;
403+
404+ static void register_checked_out_branch (const char * prefix , const char * name ,
405+ const char * path ,
406+ enum branch_checkout_kind kind )
407+ {
408+ char * refname = xstrfmt ("%s%s" , prefix , name );
409+ char * path_copy = xstrdup (path );
410+
411+ ALLOC_GROW (checked_out_branches , checked_out_branches_nr + 1 ,
412+ checked_out_branches_alloc );
413+ checked_out_branches [checked_out_branches_nr ].refname = refname ;
414+ checked_out_branches [checked_out_branches_nr ].path = path_copy ;
415+ checked_out_branches [checked_out_branches_nr ].kind = kind ;
416+ checked_out_branches_nr ++ ;
417+
418+ strmap_put (& current_checked_out_branches , refname , path_copy );
419+ }
420+
388421static void prepare_checked_out_branches (void )
389422{
390423 int i = 0 ;
@@ -397,7 +430,7 @@ static void prepare_checked_out_branches(void)
397430 worktrees = get_worktrees (the_repository );
398431
399432 while (worktrees [i ]) {
400- char * old , * wt_gitdir ;
433+ char * wt_gitdir ;
401434 struct wt_status_state state = { 0 };
402435 struct worktree * wt = worktrees [i ++ ];
403436 struct string_list update_refs = STRING_LIST_INIT_DUP ;
@@ -406,34 +439,25 @@ static void prepare_checked_out_branches(void)
406439 continue ;
407440
408441 if (wt -> head_ref ) {
409- old = strmap_put (& current_checked_out_branches ,
410- wt -> head_ref ,
411- xstrdup (wt -> path ));
412- free (old );
442+ register_checked_out_branch ("" , wt -> head_ref , wt -> path ,
443+ BRANCH_CHECKOUT_KIND_CHECKOUT );
413444 }
414445
415446 if (wt_status_check_rebase (wt , & state ) &&
416447 (state .rebase_in_progress || state .rebase_interactive_in_progress ) &&
417448 state .branch ) {
418- struct strbuf ref = STRBUF_INIT ;
419- strbuf_addf (& ref , "refs/heads/%s" , state .branch );
420- old = strmap_put (& current_checked_out_branches ,
421- ref .buf ,
422- xstrdup (wt -> path ));
423- free (old );
424- strbuf_release (& ref );
449+ register_checked_out_branch ("refs/heads/" , state .branch ,
450+ wt -> path ,
451+ BRANCH_CHECKOUT_KIND_REBASE );
425452 }
426453 wt_status_state_free_buffers (& state );
427454
428455 if (wt_status_check_bisect (wt , & state ) &&
429456 state .bisecting_from ) {
430- struct strbuf ref = STRBUF_INIT ;
431- strbuf_addf (& ref , "refs/heads/%s" , state .bisecting_from );
432- old = strmap_put (& current_checked_out_branches ,
433- ref .buf ,
434- xstrdup (wt -> path ));
435- free (old );
436- strbuf_release (& ref );
457+ register_checked_out_branch ("refs/heads/" ,
458+ state .bisecting_from ,
459+ wt -> path ,
460+ BRANCH_CHECKOUT_KIND_BISECT );
437461 }
438462 wt_status_state_free_buffers (& state );
439463
@@ -442,10 +466,9 @@ static void prepare_checked_out_branches(void)
442466 & update_refs )) {
443467 struct string_list_item * item ;
444468 for_each_string_list_item (item , & update_refs ) {
445- old = strmap_put (& current_checked_out_branches ,
446- item -> string ,
447- xstrdup (wt -> path ));
448- free (old );
469+ register_checked_out_branch ("" , item -> string ,
470+ wt -> path ,
471+ BRANCH_CHECKOUT_KIND_UPDATE_REF );
449472 }
450473 string_list_clear (& update_refs , 1 );
451474 }
@@ -462,6 +485,17 @@ const char *branch_checked_out(const char *refname)
462485 return strmap_get (& current_checked_out_branches , refname );
463486}
464487
488+ const char * branch_bisecting (const char * refname )
489+ {
490+ prepare_checked_out_branches ();
491+ for (size_t i = 0 ; i < checked_out_branches_nr ; i ++ ) {
492+ if (!strcmp (refname , checked_out_branches [i ].refname ) &&
493+ checked_out_branches [i ].kind == BRANCH_CHECKOUT_KIND_BISECT )
494+ return checked_out_branches [i ].path ;
495+ }
496+ return NULL ;
497+ }
498+
465499/*
466500 * Check if a branch 'name' can be created as a new branch; die otherwise.
467501 * 'force' can be used when it is OK for the named branch already exists.
0 commit comments