Skip to content

Commit

Permalink
feat: switch to go-git
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
  • Loading branch information
moul committed Apr 23, 2021
1 parent f5bd0bf commit 01cfd98
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ builds:
- "-a"
ldflags:
- '-extldflags "-static"'
main: ./main.go
main: ./
checksum:
name_template: '{{.ProjectName}}_checksums.txt'
changelog:
Expand Down
45 changes: 0 additions & 45 deletions git.go

This file was deleted.

4 changes: 1 addition & 3 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 74 additions & 10 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestRun(t *testing.T) {
err := run(nil)
err := run([]string{"doctor", "-p", "."})
if err != nil {
t.Fatalf("err should be nil: %v", err)
}
Expand Down
71 changes: 61 additions & 10 deletions project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package main
import (
"fmt"
"path/filepath"
"strings"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"go.uber.org/zap"
"moul.io/u"
)
Expand All @@ -14,14 +17,15 @@ type project struct {
Root string
MainBranch string
CurrentBranch string
OriginRemote string
}
}
OriginRemotes []string

func (p *project) checkoutMainBranch() error {
return nil
head *plumbing.Reference
repo *git.Repository
origin *git.Remote
}
}

// nolint:nestif
func projectFromPath(path string) (*project, error) {
abs, err := filepath.Abs(path)
if err != nil {
Expand All @@ -30,19 +34,66 @@ func projectFromPath(path string) (*project, error) {
path = abs

if !u.DirExists(path) {
return nil, fmt.Errorf("path is not a directory: %q", path)
return nil, fmt.Errorf("path is not a directory: %q", path) // nolint:goerr113
}

project := &project{Path: path}
project.Git.Root = gitFindRootDir(path)
if project.Git.Root != "" {
project.Git.MainBranch, err = gitGetMainBranch(project.Git.Root)
if err != nil {
return nil, fmt.Errorf("cannot guess main branch: %w", err)
// open local repo
{
repo, err := git.PlainOpen(project.Git.Root)
if err != nil {
return nil, fmt.Errorf("failed to open git repo: %q: %w", project.Git.Root, err)
}
project.Git.repo = repo
}

// current branch
{
head, err := project.Git.repo.Head()
if err != nil {
return nil, fmt.Errorf("failed to get HEAD: %w", err)
}
project.Git.head = head
project.Git.CurrentBranch = project.Git.head.Name().Short()
}

// 'origin' remote
{
origin, err := project.Git.repo.Remote("origin")
if err != nil {
return nil, fmt.Errorf("failed to get 'origin' remote: %w", err)
}
project.Git.origin = origin
project.Git.OriginRemotes = origin.Config().URLs
}

// main branch
{
ref, err := project.Git.repo.Reference("refs/remotes/origin/HEAD", true)
if err != nil {
return nil, fmt.Errorf("failed to get main branch: %w", err)
}
project.Git.MainBranch = strings.TrimPrefix(ref.Name().Short(), "origin/")
}
} else {
logger.Warn("project not withing a git directory", zap.String("path", path))
logger.Warn("project not within a git directory", zap.String("path", path))
}

return project, nil
}

func gitFindRootDir(path string) string {
for {
if u.DirExists(filepath.Join(path, ".git")) {
return path
}
parent := filepath.Dir(path)
if parent == path {
break
}
path = parent
}
return ""
}

0 comments on commit 01cfd98

Please sign in to comment.