Skip to content

Commit

Permalink
fix: Use correct branch for {push_files} template (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox authored Feb 22, 2023
1 parent 802de54 commit 88fb8e8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
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

0 comments on commit 88fb8e8

Please sign in to comment.