Skip to content

Commit

Permalink
fix import func when using tags
Browse files Browse the repository at this point in the history
  • Loading branch information
euforic committed Sep 13, 2023
1 parent 6a97571 commit 31160cc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
31 changes: 27 additions & 4 deletions dep.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func (d *DefaultGitClient) Clone(host, owner, repo, dest string) error {
return nil
}

// Checkout checks out a branch in a Git repository.
func (d *DefaultGitClient) Checkout(path, branch string) error {
// Checkout checks out a branch, tag or commit hash in a Git repository.
func (d *DefaultGitClient) Checkout(path, ref string) error {
r, err := git.PlainOpen(path)
if err != nil {
return err
Expand All @@ -143,7 +143,30 @@ func (d *DefaultGitClient) Checkout(path, branch string) error {
return err
}

return w.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(branch),
// Try to checkout branch
err = w.Checkout(&git.CheckoutOptions{
Branch: plumbing.ReferenceName("refs/remotes/origin/" + ref),
})
if err == nil {
return nil // Branch checked out successfully
}

// If branch checkout fails, try tag
err = w.Checkout(&git.CheckoutOptions{
Branch: plumbing.ReferenceName("refs/tags/" + ref),
})
if err == nil {
return nil // Tag checked out successfully
}

// If tag checkout also fails, try using it as a commit hash
commitHash := plumbing.NewHash(ref)
err = w.Checkout(&git.CheckoutOptions{
Hash: commitHash,
})
if err != nil {
return fmt.Errorf("failed to checkout reference %s: %w", ref, err)
}

return nil
}
2 changes: 1 addition & 1 deletion import_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (e *Executor) ImportFunc(client GitClient, outputDir string) func(repoAndTa
return fmt.Errorf("failed to clone repo: %w", err)
}

if depInfo.Tag != "" {
if depInfo.Tag != "" && depInfo.Tag != "main" {
err = client.Checkout(tempDir, depInfo.Tag)
if err != nil {
return fmt.Errorf("failed to checkout branch: %w", err)
Expand Down

0 comments on commit 31160cc

Please sign in to comment.