@@ -6,6 +6,7 @@ use gix::{
66 prelude:: { ObjectIdExt , ReferenceExt } ,
77 refs:: Category ,
88} ;
9+ use std:: collections:: BTreeMap ;
910use tracing:: instrument;
1011
1112use crate :: { CommitFlags , CommitIndex , Edge , Graph , Segment , SegmentIndex , SegmentMetadata } ;
@@ -154,10 +155,17 @@ impl Graph {
154155 // It's OK to default-initialise this here as overlays are only used when redoing
155156 // the traversal.
156157 let ( _repo, meta, _entrypoint) = Overlay :: default ( ) . into_parts ( repo, meta) ;
158+ let wt_by_branch = {
159+ // Assume linked worktrees are never unborn!
160+ let mut m = BTreeMap :: new ( ) ;
161+ m. insert ( ref_name. clone ( ) , vec ! [ crate :: Worktree :: Main ] ) ;
162+ m
163+ } ;
157164 graph. insert_segment_set_entrypoint ( branch_segment_from_name_and_meta (
158165 Some ( ( ref_name, None ) ) ,
159166 & meta,
160167 None ,
168+ & wt_by_branch,
161169 ) ?) ;
162170 return Ok ( graph) ;
163171 }
@@ -182,7 +190,7 @@ impl Graph {
182190 if let Some ( ( rn, first_commit) ) = s
183191 . commits
184192 . first_mut ( )
185- . and_then ( |first_commit| s. ref_name . take ( ) . map ( |rn| ( rn, first_commit) ) )
193+ . and_then ( |first_commit| s. ref_info . take ( ) . map ( |rn| ( rn, first_commit) ) )
186194 {
187195 first_commit. refs . push ( rn) ;
188196 }
@@ -350,6 +358,8 @@ impl Graph {
350358 let tip_is_not_workspace_commit = !workspaces
351359 . iter ( )
352360 . any ( |( _, wsrn, _) | Some ( wsrn) == ref_name. as_ref ( ) ) ;
361+ let worktree_by_branch = worktree_branches ( repo. for_worktree_only ( ) ) ?;
362+
353363 let mut ctx = post:: Context {
354364 repo,
355365 symbolic_remote_names : & symbolic_remote_names,
@@ -358,12 +368,14 @@ impl Graph {
358368 refs_by_id,
359369 hard_limit : false ,
360370 dangerously_skip_postprocessing_for_debugging,
371+ worktree_by_branch,
361372 } ;
362373 if tip_is_not_workspace_commit {
363374 let current = graph. insert_segment_set_entrypoint ( branch_segment_from_name_and_meta (
364375 None ,
365376 meta,
366377 Some ( ( & ctx. refs_by_id , tip) ) ,
378+ & ctx. worktree_by_branch ,
367379 ) ?) ;
368380 _ = next. push_back_exhausted ( (
369381 tip,
@@ -406,8 +418,12 @@ impl Graph {
406418 max_limit. with_indirect_goal ( tip, & mut goals) ,
407419 )
408420 } ;
409- let mut ws_segment =
410- branch_segment_from_name_and_meta ( Some ( ( ws_ref, None ) ) , meta, None ) ?;
421+ let mut ws_segment = branch_segment_from_name_and_meta (
422+ Some ( ( ws_ref, None ) ) ,
423+ meta,
424+ None ,
425+ & ctx. worktree_by_branch ,
426+ ) ?;
411427 // The limits for the target ref and the worktree ref are synced so they can always find each other,
412428 // while being able to stop when the entrypoint is included.
413429 ws_segment. metadata = Some ( SegmentMetadata :: Workspace ( ws_meta) ) ;
@@ -439,6 +455,7 @@ impl Graph {
439455 Some ( ( target_ref, None ) ) ,
440456 meta,
441457 None ,
458+ & ctx. worktree_by_branch ,
442459 ) ?) ;
443460 let ( local_sidx, local_goal) =
444461 if let Some ( ( local_ref_name, target_local_tip) ) = local_tip_info {
@@ -448,12 +465,13 @@ impl Graph {
448465 Some ( target_segment) ,
449466 meta,
450467 Some ( ( & ctx. refs_by_id , target_local_tip) ) ,
468+ & ctx. worktree_by_branch ,
451469 ) ?) ;
452470 // We use auto-naming based on ambiguity - if the name ends up something else,
453471 // remove the nodes sibling link.
454472 let has_sibling_link = {
455473 let s = & mut graph[ local_sidx] ;
456- if s. ref_name . as_ref ( ) . is_none_or ( |rn| rn != & local_ref_name) {
474+ if s. ref_name ( ) . is_none_or ( |rn| rn != local_ref_name. as_ref ( ) ) {
457475 s. sibling_segment_id = None ;
458476 false
459477 } else {
@@ -504,6 +522,7 @@ impl Graph {
504522 None ,
505523 meta,
506524 Some ( ( & ctx. refs_by_id , extra_target) ) ,
525+ & ctx. worktree_by_branch ,
507526 ) ?) ;
508527 _ = next. push_front_exhausted ( (
509528 extra_target,
@@ -550,15 +569,19 @@ impl Graph {
550569 None ,
551570 meta,
552571 Some ( ( & ctx. refs_by_id , segment_tip. detach ( ) ) ) ,
572+ & ctx. worktree_by_branch ,
553573 ) ?;
554574
555575 // However, if this is a remote segment that is explicitly mentioned, and we couldn't name
556576 // it, then just fix it up here as we really want that name.
557577 let is_remote = segment_name
558578 . category ( )
559579 . is_some_and ( |c| c == Category :: RemoteBranch ) ;
560- if segment. ref_name . is_none ( ) && is_remote {
561- segment. ref_name = Some ( segment_name. clone ( ) ) ;
580+ if segment. ref_info . is_none ( ) && is_remote {
581+ segment. ref_info = Some ( crate :: RefInfo :: from_ref (
582+ segment_name. clone ( ) ,
583+ & ctx. worktree_by_branch ,
584+ ) ) ;
562585 segment. metadata = meta
563586 . branch_opt ( segment_name. as_ref ( ) ) ?
564587 . map ( SegmentMetadata :: Branch ) ;
@@ -577,6 +600,7 @@ impl Graph {
577600 & mut graph,
578601 & mut next,
579602 ( ws_tips, repo, meta) ,
603+ & ctx. worktree_by_branch ,
580604 ) ?;
581605 max_commits_recharge_location. sort ( ) ;
582606 while let Some ( ( id, mut propagated_flags, instruction, mut limit) ) = next. pop_front ( ) {
@@ -614,6 +638,7 @@ impl Graph {
614638 & info,
615639 & ctx. refs_by_id ,
616640 meta,
641+ & ctx. worktree_by_branch ,
617642 ) ?
618643 . unwrap_or ( src_sidx) ;
619644 e. insert ( src_sidx) ;
@@ -641,6 +666,7 @@ impl Graph {
641666 None ,
642667 meta,
643668 Some ( ( & ctx. refs_by_id , id) ) ,
669+ & ctx. worktree_by_branch ,
644670 ) ?;
645671 let segment_below = graph. connect_new_segment (
646672 parent_above,
@@ -672,6 +698,7 @@ impl Graph {
672698 limit,
673699 & mut goals,
674700 & next,
701+ & ctx. worktree_by_branch ,
675702 ) ?;
676703
677704 let segment = & mut graph[ segment_idx_for_id] ;
@@ -700,8 +727,9 @@ impl Graph {
700727 refs_at_commit_before_removal
701728 . clone ( )
702729 . into_iter ( )
703- . filter ( |rn| segment. ref_name . as_ref ( ) != Some ( rn) )
730+ . filter ( |rn| segment. ref_name ( ) != Some ( rn. as_ref ( ) ) )
704731 . collect ( ) ,
732+ & ctx. worktree_by_branch ,
705733 ) ?,
706734 ) ;
707735
@@ -738,7 +766,7 @@ impl Graph {
738766 . tip_skip_empty ( tip_sidx)
739767 . context ( "BUG: entrypoint must eventually point to a commit" ) ?
740768 . id ;
741- let ref_name = self [ tip_sidx] . ref_name . clone ( ) ;
769+ let ref_name = self [ tip_sidx] . ref_info . clone ( ) . map ( |ri| ri . ref_name ) ;
742770 ( tip, ref_name)
743771 }
744772 } ;
0 commit comments