Skip to content

Commit

Permalink
[FIX] bootstrap on different branch (#241)
Browse files Browse the repository at this point in the history
* [FIX] bootstrap on different branch

* fix lint and tests

* fix lint and tests
  • Loading branch information
roi-codefresh authored Mar 10, 2022
1 parent ea2f23a commit 419fdda
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 106 deletions.
6 changes: 6 additions & 0 deletions cmd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ func RunAppCreate(ctx context.Context, opts *AppCreateOptions) error {
appsfs fs.FS
)

log.G(ctx).WithFields(log.Fields{
"app-url": opts.AppsCloneOpts.URL(),
"app-revision": opts.AppsCloneOpts.Revision(),
"app-path": opts.AppsCloneOpts.Path(),
}).Debug("starting with options: ")

r, repofs, err := prepareRepo(ctx, opts.CloneOpts, opts.ProjectName)
if err != nil {
return err
Expand Down
63 changes: 36 additions & 27 deletions pkg/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ const (

// go-git functions (we mock those in tests)
var (
checkoutRef = func(r *repo, ref string) error {
return r.checkoutRef(ref)
checkoutBranch = func(r *repo, branch string, createIfNotExists bool) error {
return r.checkoutBranch(branch, createIfNotExists)
}

ggClone = func(ctx context.Context, s storage.Storer, worktree billy.Filesystem, o *gg.CloneOptions) (gogit.Repository, error) {
Expand Down Expand Up @@ -355,7 +355,7 @@ var clone = func(ctx context.Context, opts *CloneOptions) (*repo, error) {
repo := &repo{Repository: r, auth: opts.Auth, progress: progress}

if opts.revision != "" {
if err := checkoutRef(repo, opts.revision); err != nil {
if err := checkoutBranch(repo, opts.revision, opts.CreateIfNotExist); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -438,41 +438,50 @@ var initRepo = func(ctx context.Context, opts *CloneOptions) (*repo, error) {
return r, r.initBranch(ctx, opts.revision)
}

func (r *repo) checkoutRef(ref string) error {
hash, err := r.ResolveRevision(plumbing.Revision(ref))
func (r *repo) checkoutBranch(branch string, createIfNotExists bool) error {
wt, err := worktree(r)
if err != nil {
if err != plumbing.ErrReferenceNotFound {
return err
}
return err
}

log.G().WithField("ref", ref).Debug("failed resolving ref, trying to resolve from remote branch")
remotes, err := r.Remotes()
if err != nil {
return err
}
err = wt.Checkout(&gg.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(branch),
})
if err != plumbing.ErrReferenceNotFound {
return err
}

if len(remotes) == 0 {
return ErrNoRemotes
}
remotes, err := r.Remotes()
if err != nil {
return err
}

remoteref := fmt.Sprintf("%s/%s", remotes[0].Config().Name, ref)
hash, err = r.ResolveRevision(plumbing.Revision(remoteref))
if err != nil {
return err
}
if len(remotes) == 0 {
return ErrNoRemotes
}

wt, err := worktree(r)
err = wt.Checkout(&gg.CheckoutOptions{
Branch: plumbing.NewRemoteReferenceName(remotes[0].Config().Name, branch),
})
if err != nil {
if err == plumbing.ErrReferenceNotFound && createIfNotExists {
// no remote branch but create is true
// so we will create a new local branch
return wt.Checkout(&gg.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(branch),
Create: true,
})

}

return err
}

log.G().WithFields(log.Fields{
"ref": ref,
"hash": hash.String(),
}).Debug("checking out commit")
// if succeeded to checkout to a remote branch with this name,
// checkout to a local branch from the remote branch
return wt.Checkout(&gg.CheckoutOptions{
Hash: *hash,
Branch: plumbing.NewBranchReferenceName(branch),
Create: true,
})
}

Expand Down
Loading

0 comments on commit 419fdda

Please sign in to comment.