Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to specify the git work directory #173

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ All flags have a matching option function to configure the previous commands bey
- `svu.Next(svu.WithTagMode(svu.AllBranches))` or `svu.Next(svu.ForAllBranches())`
- `svu.Next(svu.WithTagMode(svu.CurrentBranch))` or `svu.Next(svu.ForCurrentBranch())`
- `svu.Next(svu.ForcePatchIncrement())`
- `svu.Current(svu.WithGitDirectory("/some/other/repo/.git"), svu.WithWorkTree("/some/other/repo"))`

Or multiple options:

Expand Down
80 changes: 64 additions & 16 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package git
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/gobwas/glob"
Expand All @@ -16,26 +18,68 @@ const (

// copied from goreleaser

// IsRepo returns true if current folder is a git repository
func IsRepo() bool {
out, err := run("rev-parse", "--is-inside-work-tree")
return err == nil && strings.TrimSpace(out) == "true"
type Repository struct {
WorkTree string
GitDirectory string
}

func getAllTags(args ...string) ([]string, error) {
tags, err := run(append([]string{"-c", "versionsort.suffix=-", "tag", "--sort=-version:refname"}, args...)...)
// NewRepository creates a Repository. worktree and gitdirectory will default to 'pwd' and 'pwd/.git'
func NewRepository(worktree, gitdirectory string) (*Repository, error) {
var wt string
var err error
if worktree == "" {
wt, err = os.Getwd()
if err != nil {
return nil, err
}
} else {
wt = filepath.Clean(worktree)
}
var gd string
if gitdirectory == "" {
gd, err = os.Getwd()
if err != nil {
return nil, err
}
gd = gd + string(os.PathSeparator) + ".git"
} else {
gd = filepath.Clean(gitdirectory)
}

r := &Repository{
WorkTree: wt,
GitDirectory: gd,
}
return r, nil
}
mcinj marked this conversation as resolved.
Show resolved Hide resolved

// IsRepo returns true if the current work-tree and git-dir are considered a git repository by the git binary.
// unless specified by --work-tree and --git-dir, the current working directory will be considered
func (r *Repository) IsRepo() bool {
_, err := r.run("status")
return err == nil
}

// Root returns the root of the git Repository
func (r *Repository) Root() (string, error) {
out, err := r.run("rev-parse", "--show-toplevel")
return strings.TrimSpace(out), err
}

func (r *Repository) getAllTags(args ...string) ([]string, error) {
tags, err := r.run(append([]string{"-c", "versionsort.suffix=-", "tag", "--sort=-version:refname"}, args...)...)
if err != nil {
return nil, err
}
return strings.Split(tags, "\n"), nil
}

func DescribeTag(tagMode string, pattern string) (string, error) {
func (r *Repository) DescribeTag(tagMode string, pattern string) (string, error) {
args := []string{}
if tagMode == CurrentBranchTagMode {
args = []string{"--merged"}
}
tags, err := getAllTags(args...)
tags, err := r.getAllTags(args...)
if err != nil {
return "", err
}
Expand All @@ -59,17 +103,21 @@ func DescribeTag(tagMode string, pattern string) (string, error) {
return "", fmt.Errorf("no tags match '%s'", pattern)
}

func Changelog(tag string, dir string) (string, error) {
func (r *Repository) Changelog(tag string, dir string) (string, error) {
if tag == "" {
return gitLog(dir, "HEAD")
return r.gitLog(dir, "HEAD")
} else {
return gitLog(dir, fmt.Sprintf("tags/%s..HEAD", tag))
return r.gitLog(dir, fmt.Sprintf("tags/%s..HEAD", tag))
}
}

func run(args ...string) (string, error) {
extraArgs := []string{
"-c", "log.showSignature=false",
func (r *Repository) run(args ...string) (string, error) {
extraArgs := []string{"-c", "log.showSignature=false"}
if r.WorkTree != "" {
extraArgs = append(extraArgs, fmt.Sprintf("--work-tree=%s", r.WorkTree))
}
if r.GitDirectory != "" {
extraArgs = append(extraArgs, fmt.Sprintf("--git-dir=%s", r.GitDirectory))
}
args = append(extraArgs, args...)
/* #nosec */
Expand All @@ -81,11 +129,11 @@ func run(args ...string) (string, error) {
return string(bts), nil
}

func gitLog(dir string, refs ...string) (string, error) {
func (r *Repository) gitLog(dir string, refs ...string) (string, error) {
args := []string{"log", "--no-decorate", "--no-color"}
args = append(args, refs...)
if dir != "" {
args = append(args, "--", dir)
}
return run(args...)
return r.run(args...)
}
Loading