Skip to content

Commit ee165d7

Browse files
author
Abroskin Alexander
authored
Merge pull request #26 from Arkweid/add-branch-files-shortcut
add branch files shortcut
2 parents af087b0 + 2fe7030 commit ee165d7

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

cmd/run.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const (
5454
subFiles string = "{files}"
5555
subAllFiles string = "{all_files}"
5656
subStagedFiles string = "{staged_files}"
57+
pushFiles string = "{push_files}"
5758
runnerWrapPattern string = "{cmd}"
5859
tagsConfigKey string = "tags"
5960
pipedConfigKey string = "piped"
@@ -168,19 +169,28 @@ func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup) {
168169
return
169170
}
170171

171-
files, _ := context.AllFiles()
172+
files := []string{}
172173
runner := getRunner(hooksGroup, commandsConfigKey, commandName)
173174

174175
if strings.Contains(runner, subStagedFiles) {
175176
files, _ = context.StagedFiles()
176177
} else if strings.Contains(runner, subFiles) || getCommandFiles(hooksGroup, commandName) != "" {
177178
files, _ = context.ExecGitCommand(getCommandFiles(hooksGroup, commandName))
179+
} else if strings.Contains(runner, pushFiles) {
180+
files, _ = context.PushFiles()
181+
} else {
182+
files, _ = context.AllFiles()
178183
}
179184

185+
VerbosePrint("\nFiles before filters: \n", files)
186+
180187
files = FilterGlob(files, getCommandGlobRegexp(hooksGroup, commandName))
181188
files = FilterInclude(files, getCommandIncludeRegexp(hooksGroup, commandName)) // NOTE: confusing option, suppose delete it
182189
files = FilterExclude(files, getCommandExcludeRegexp(hooksGroup, commandName))
183190

191+
VerbosePrint("Files after filters: \n", files)
192+
193+
runner = strings.Replace(runner, pushFiles, strings.Join(files, " "), -1)
184194
runner = strings.Replace(runner, subStagedFiles, strings.Join(files, " "), -1)
185195
runner = strings.Replace(runner, subAllFiles, strings.Join(files, " "), -1)
186196
runner = strings.Replace(runner, subFiles, strings.Join(files, " "), -1)

cmd/run_windows.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package cmd
55
import (
66
"errors"
77
"fmt"
8+
89
// "io" // win specific
910
"log"
1011
"os"
@@ -20,6 +21,7 @@ import (
2021

2122
arrop "github.com/adam-hanna/arrayOperations"
2223
"github.com/gobwas/glob"
24+
2325
// "github.com/kr/pty" //win specific
2426
"github.com/spf13/afero"
2527
"github.com/spf13/cobra"
@@ -53,6 +55,7 @@ const (
5355
subFiles string = "{files}"
5456
subAllFiles string = "{all_files}"
5557
subStagedFiles string = "{staged_files}"
58+
pushFiles string = "{push_files}"
5659
runnerWrapPattern string = "{cmd}"
5760
tagsConfigKey string = "tags"
5861
pipedConfigKey string = "piped"
@@ -162,19 +165,28 @@ func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup) {
162165
return
163166
}
164167

165-
files, _ := context.AllFiles()
168+
files := []string{}
166169
runner := getRunner(hooksGroup, commandsConfigKey, commandName)
167170

168171
if strings.Contains(runner, subStagedFiles) {
169172
files, _ = context.StagedFiles()
170173
} else if strings.Contains(runner, subFiles) || getCommandFiles(hooksGroup, commandName) != "" {
171174
files, _ = context.ExecGitCommand(getCommandFiles(hooksGroup, commandName))
175+
} else if strings.Contains(runner, pushFiles) {
176+
files, _ = context.PushFiles()
177+
} else {
178+
files, _ = context.AllFiles()
172179
}
173180

181+
VerbosePrint("Files before filters: \n", files)
182+
174183
files = FilterGlob(files, getCommandGlobRegexp(hooksGroup, commandName))
175184
files = FilterInclude(files, getCommandIncludeRegexp(hooksGroup, commandName)) // NOTE: confusing option, suppose delete it
176185
files = FilterExclude(files, getCommandExcludeRegexp(hooksGroup, commandName))
177186

187+
VerbosePrint("Files after filters: \n", files)
188+
189+
runner = strings.Replace(runner, pushFiles, strings.Join(files, " "), -1)
178190
runner = strings.Replace(runner, subStagedFiles, strings.Join(files, " "), -1)
179191
runner = strings.Replace(runner, subAllFiles, strings.Join(files, " "), -1)
180192
runner = strings.Replace(runner, subFiles, strings.Join(files, " "), -1)

context/context.go

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ func AllFiles() ([]string, error) {
1515
return ExecGitCommand("git ls-files --cached")
1616
}
1717

18+
func PushFiles() ([]string, error) {
19+
return ExecGitCommand("git diff-tree --no-commit-id --name-only -r HEAD --")
20+
}
21+
1822
func ExecGitCommand(command string) ([]string, error) {
1923
commandArg := strings.Split(command, " ")
2024
cmd := exec.Command(commandArg[0], commandArg[1:]...)

0 commit comments

Comments
 (0)