Skip to content

Commit 88fb8e8

Browse files
authored
fix: Use correct branch for {push_files} template (#429)
1 parent 802de54 commit 88fb8e8

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

internal/git/exec.go

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"os"
55
"os/exec"
66
"strings"
7+
8+
"github.com/evilmartians/lefthook/internal/log"
79
)
810

911
type Exec interface {
@@ -56,10 +58,14 @@ func (o *OsExec) RawCmd(cmd string) (string, error) {
5658
// rawExecArgs executes git command with LEFTHOOK=0 in order
5759
// to prevent calling subsequent lefthook hooks.
5860
func (o *OsExec) rawExecArgs(args ...string) (string, error) {
61+
log.Debug("[lefthook] cmd: ", args)
62+
5963
cmd := exec.Command(args[0], args[1:]...)
6064
cmd.Env = append(os.Environ(), "LEFTHOOK=0")
6165

6266
out, err := cmd.CombinedOutput()
67+
log.Debug("[lefthook] err: ", err)
68+
log.Debug("[lefthook] out: ", string(out))
6369
if err != nil {
6470
return "", err
6571
}

internal/git/repository.go

+36-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package git
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67
"regexp"
@@ -10,23 +11,26 @@ import (
1011
)
1112

1213
const (
13-
cmdRootPath = "git rev-parse --show-toplevel"
14-
cmdHooksPath = "git rev-parse --git-path hooks"
15-
cmdInfoPath = "git rev-parse --git-path info"
16-
cmdGitPath = "git rev-parse --git-dir"
17-
cmdStagedFiles = "git diff --name-only --cached --diff-filter=ACMR"
18-
cmdAllFiles = "git ls-files --cached"
19-
cmdPushFiles = "git diff --name-only HEAD @{push} || git diff --name-only HEAD master"
20-
cmdStatusShort = "git status --short"
21-
cmdCreateStash = "git stash create"
22-
cmdListStash = "git stash list"
14+
cmdRootPath = "git rev-parse --show-toplevel"
15+
cmdHooksPath = "git rev-parse --git-path hooks"
16+
cmdInfoPath = "git rev-parse --git-path info"
17+
cmdGitPath = "git rev-parse --git-dir"
18+
cmdStagedFiles = "git diff --name-only --cached --diff-filter=ACMR"
19+
cmdAllFiles = "git ls-files --cached"
20+
cmdPushFilesBase = "git diff --name-only HEAD @{push}"
21+
cmdPushFilesHead = "git diff --name-only HEAD %s"
22+
cmdStatusShort = "git status --short"
23+
cmdCreateStash = "git stash create"
24+
cmdListStash = "git stash list"
2325

2426
stashMessage = "lefthook auto backup"
2527
unstagedPatchName = "lefthook-unstaged.patch"
2628
infoDirMode = 0o775
2729
minStatusLen = 3
2830
)
2931

32+
var headBranchRegexp = regexp.MustCompile(`HEAD -> (?P<name>.*)$`)
33+
3034
// Repository represents a git repository.
3135
type Repository struct {
3236
Fs afero.Fs
@@ -36,6 +40,7 @@ type Repository struct {
3640
GitPath string
3741
InfoPath string
3842
unstagedPatchPath string
43+
headBranch string
3944
}
4045

4146
// NewRepository returns a Repository or an error, if git repository it not initialized.
@@ -99,7 +104,27 @@ func (r *Repository) AllFiles() ([]string, error) {
99104
// PushFiles returns a list of files that are ready to be pushed
100105
// or an error if git command fails.
101106
func (r *Repository) PushFiles() ([]string, error) {
102-
return r.FilesByCommand(cmdPushFiles)
107+
res, err := r.FilesByCommand(cmdPushFilesBase)
108+
if err == nil {
109+
return res, nil
110+
}
111+
112+
if len(r.headBranch) == 0 {
113+
branches, err := r.Git.CmdLines("git branch --remotes")
114+
if err != nil {
115+
return nil, err
116+
}
117+
for _, branch := range branches {
118+
if !headBranchRegexp.MatchString(branch) {
119+
continue
120+
}
121+
122+
matches := headBranchRegexp.FindStringSubmatch(branch)
123+
r.headBranch = matches[headBranchRegexp.SubexpIndex("name")]
124+
break
125+
}
126+
}
127+
return r.FilesByCommand(fmt.Sprintf(cmdPushFilesHead, r.headBranch))
103128
}
104129

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

0 commit comments

Comments
 (0)