@@ -174,34 +174,28 @@ func (ws *GitInitializer) realizeCloneTarget(ctx context.Context) (err error) {
174174 // checkout branch
175175 switch ws .TargetMode {
176176 case RemoteBranch :
177+ // confirm the value of the default branch name using rev-parse
178+ gitout , _ := ws .GitWithOutput (ctx , nil , "rev-parse" , "--abbrev-ref" , "origin/HEAD" )
179+ defaultBranch := strings .TrimSpace (strings .Replace (string (gitout ), "origin/" , "" , - 1 ))
180+
177181 // we already cloned the git repository but we need to check CloneTarget exists,
178182 // except when the name is main or master because either value could be wrong
179183 // and we are going to use the incorrect value (default is main).
180- if ws .CloneTarget == "main" || ws .CloneTarget == "master" {
181- // confirm the value of the default branch name using rev-parse
182- gitout , _ := ws .GitWithOutput (ctx , nil , "rev-parse" , "--abbrev-ref" , "origin/HEAD" )
183- defaultBranch := strings .TrimSpace (strings .Replace (string (gitout ), "origin/" , "" , - 1 ))
184- if defaultBranch != ws .CloneTarget {
185- // the default branch name we cloned is not the one specified by the user
186- // check if the branch exits in the repository
187- gitout , err := ws .GitWithOutput (ctx , nil , "ls-remote" , "--exit-code" , "origin" , ws .CloneTarget )
188- if err != nil || len (gitout ) == 0 {
189- log .WithField ("remoteURI" , ws .RemoteURI ).WithField ("branch" , ws .CloneTarget ).Warnf ("Invalid default branch name. Changing to %v" , defaultBranch )
190- ws .CloneTarget = defaultBranch
191- }
192- }
184+
185+ var cloneTargetExists bool
186+ gitout , err := ws .GitWithOutput (ctx , nil , "ls-remote" , "--exit-code" , "origin" , ws .CloneTarget )
187+ if err != nil || len (gitout ) == 0 {
188+ log .WithField ("remoteURI" , ws .RemoteURI ).WithField ("branch" , ws .CloneTarget ).Warnf ("Invalid default branch name. Changing to %v" , defaultBranch )
189+ ws .CloneTarget = defaultBranch
193190 } else {
194- // check remote branch exists before git checkout when the branch is not the default
195- gitout , err := ws .GitWithOutput (ctx , nil , "ls-remote" , "--exit-code" , "origin" , ws .CloneTarget )
196- if err != nil || len (gitout ) == 0 {
197- log .WithError (err ).WithField ("remoteURI" , ws .RemoteURI ).WithField ("branch" , ws .CloneTarget ).Error ("Remote branch doesn't exist." )
198- return xerrors .Errorf ("Remote branch %v does not exist in %v" , ws .CloneTarget , ws .RemoteURI )
199- }
191+ cloneTargetExists = true
200192 }
201193
202- if err := ws .Git (ctx , "fetch" , "--depth=1" , "origin" , ws .CloneTarget ); err != nil {
203- log .WithError (err ).WithField ("remoteURI" , ws .RemoteURI ).WithField ("branch" , ws .CloneTarget ).Error ("Cannot fetch remote branch" )
204- return err
194+ if cloneTargetExists {
195+ if err := ws .Git (ctx , "fetch" , "--depth=1" , "origin" , ws .CloneTarget ); err != nil {
196+ log .WithError (err ).WithField ("remoteURI" , ws .RemoteURI ).WithField ("branch" , ws .CloneTarget ).Error ("Cannot fetch remote branch" )
197+ return err
198+ }
205199 }
206200
207201 if err := ws .Git (ctx , "checkout" , ws .CloneTarget ); err != nil {
0 commit comments