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

fix: Use correct branch for {push_files} template #429

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions internal/git/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"os"
"os/exec"
"strings"

"github.com/evilmartians/lefthook/internal/log"
)

type Exec interface {
Expand Down Expand Up @@ -56,10 +58,14 @@ func (o *OsExec) RawCmd(cmd string) (string, error) {
// rawExecArgs executes git command with LEFTHOOK=0 in order
// to prevent calling subsequent lefthook hooks.
func (o *OsExec) rawExecArgs(args ...string) (string, error) {
log.Debug("[lefthook] cmd: ", args)

cmd := exec.Command(args[0], args[1:]...)
cmd.Env = append(os.Environ(), "LEFTHOOK=0")

out, err := cmd.CombinedOutput()
log.Debug("[lefthook] err: ", err)
log.Debug("[lefthook] out: ", string(out))
if err != nil {
return "", err
}
Expand Down
47 changes: 36 additions & 11 deletions internal/git/repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package git

import (
"fmt"
"os"
"path/filepath"
"regexp"
Expand All @@ -10,23 +11,26 @@ import (
)

const (
cmdRootPath = "git rev-parse --show-toplevel"
cmdHooksPath = "git rev-parse --git-path hooks"
cmdInfoPath = "git rev-parse --git-path info"
cmdGitPath = "git rev-parse --git-dir"
cmdStagedFiles = "git diff --name-only --cached --diff-filter=ACMR"
cmdAllFiles = "git ls-files --cached"
cmdPushFiles = "git diff --name-only HEAD @{push} || git diff --name-only HEAD master"
cmdStatusShort = "git status --short"
cmdCreateStash = "git stash create"
cmdListStash = "git stash list"
cmdRootPath = "git rev-parse --show-toplevel"
cmdHooksPath = "git rev-parse --git-path hooks"
cmdInfoPath = "git rev-parse --git-path info"
cmdGitPath = "git rev-parse --git-dir"
cmdStagedFiles = "git diff --name-only --cached --diff-filter=ACMR"
cmdAllFiles = "git ls-files --cached"
cmdPushFilesBase = "git diff --name-only HEAD @{push}"
cmdPushFilesHead = "git diff --name-only HEAD %s"
cmdStatusShort = "git status --short"
cmdCreateStash = "git stash create"
cmdListStash = "git stash list"

stashMessage = "lefthook auto backup"
unstagedPatchName = "lefthook-unstaged.patch"
infoDirMode = 0o775
minStatusLen = 3
)

var headBranchRegexp = regexp.MustCompile(`HEAD -> (?P<name>.*)$`)

// Repository represents a git repository.
type Repository struct {
Fs afero.Fs
Expand All @@ -36,6 +40,7 @@ type Repository struct {
GitPath string
InfoPath string
unstagedPatchPath string
headBranch string
}

// NewRepository returns a Repository or an error, if git repository it not initialized.
Expand Down Expand Up @@ -99,7 +104,27 @@ func (r *Repository) AllFiles() ([]string, error) {
// PushFiles returns a list of files that are ready to be pushed
// or an error if git command fails.
func (r *Repository) PushFiles() ([]string, error) {
return r.FilesByCommand(cmdPushFiles)
res, err := r.FilesByCommand(cmdPushFilesBase)
if err == nil {
return res, nil
}

if len(r.headBranch) == 0 {
branches, err := r.Git.CmdLines("git branch --remotes")
if err != nil {
return nil, err
}
for _, branch := range branches {
if !headBranchRegexp.MatchString(branch) {
continue
}

matches := headBranchRegexp.FindStringSubmatch(branch)
r.headBranch = matches[headBranchRegexp.SubexpIndex("name")]
break
}
}
return r.FilesByCommand(fmt.Sprintf(cmdPushFilesHead, r.headBranch))
}

// PartiallyStagedFiles returns the list of files that have both staged and
Expand Down