@@ -69,6 +69,67 @@ func TestInit_CustomTrunk(t *testing.T) {
6969 assert .Equal (t , "develop" , sf .Stacks [0 ].Trunk .Branch )
7070}
7171
72+ func TestInit_RestoresMissingLocalTrunk (t * testing.T ) {
73+ gitDir := t .TempDir ()
74+ trunkExists := false
75+ var fetchedRemote string
76+ var fetchedBranches []string
77+ var created [][2 ]string
78+
79+ restore := git .SetOps (& git.MockOps {
80+ GitDirFn : func () (string , error ) { return gitDir , nil },
81+ DefaultBranchFn : func () (string , error ) { return "main" , nil },
82+ CurrentBranchFn : func () (string , error ) { return "renamed-branch" , nil },
83+ IsRerereEnabledFn : func () (bool , error ) { return true , nil },
84+ BranchExistsFn : func (name string ) bool {
85+ return name == "renamed-branch" || (name == "main" && trunkExists )
86+ },
87+ ResolveRemoteFn : func (branch string ) (string , error ) {
88+ assert .Equal (t , "renamed-branch" , branch )
89+ return "origin" , nil
90+ },
91+ FetchBranchesFn : func (remote string , branches []string ) error {
92+ fetchedRemote = remote
93+ fetchedBranches = branches
94+ return nil
95+ },
96+ RevParseFn : func (ref string ) (string , error ) {
97+ if ref == "main" && ! trunkExists {
98+ return "" , fmt .Errorf ("unknown revision %s" , ref )
99+ }
100+ return "sha-" + ref , nil
101+ },
102+ CreateBranchFn : func (name , base string ) error {
103+ created = append (created , [2 ]string {name , base })
104+ if name == "main" {
105+ trunkExists = true
106+ }
107+ return nil
108+ },
109+ CheckoutBranchFn : func (string ) error { return nil },
110+ })
111+ defer restore ()
112+
113+ cfg , outR , errR := config .NewTestConfig ()
114+ err := runInit (cfg , & initOptions {branches : []string {"first-layer" }})
115+ output := collectOutput (cfg , outR , errR )
116+
117+ require .NoError (t , err )
118+ assert .Equal (t , "origin" , fetchedRemote )
119+ assert .Equal (t , []string {"main" }, fetchedBranches )
120+ assert .Equal (t , [][2 ]string {
121+ {"main" , "origin/main" },
122+ {"first-layer" , "main" },
123+ }, created )
124+ assert .Contains (t , output , "Created local trunk branch main from origin/main" )
125+
126+ sf , loadErr := stack .Load (gitDir )
127+ require .NoError (t , loadErr )
128+ require .Len (t , sf .Stacks , 1 )
129+ assert .Equal (t , "main" , sf .Stacks [0 ].Trunk .Branch )
130+ assert .Equal (t , []string {"first-layer" }, sf .Stacks [0 ].BranchNames ())
131+ }
132+
72133func TestInit_AdoptExistingBranches (t * testing.T ) {
73134 gitDir := t .TempDir ()
74135 restore := git .SetOps (& git.MockOps {
0 commit comments