Skip to content

Commit

Permalink
add fallback for pushFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
A.A.Abroskin committed Jul 12, 2019
1 parent ee165d7 commit afd67f9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
7 changes: 4 additions & 3 deletions context/context.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !windows

package context

import (
Expand All @@ -16,12 +18,11 @@ func AllFiles() ([]string, error) {
}

func PushFiles() ([]string, error) {
return ExecGitCommand("git diff-tree --no-commit-id --name-only -r HEAD --")
return ExecGitCommand("git diff --name-only HEAD @{push} || git diff --name-only HEAD master")
}

func ExecGitCommand(command string) ([]string, error) {
commandArg := strings.Split(command, " ")
cmd := exec.Command(commandArg[0], commandArg[1:]...)
cmd := exec.Command("sh", "-c", command)

outputBytes, err := cmd.CombinedOutput()
if err != nil {
Expand Down
93 changes: 93 additions & 0 deletions context/context_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package context

import (
"os"
"os/exec"
"path/filepath"
"strings"
)

func StagedFiles() ([]string, error) {
return ExecGitCommand("git diff --name-only --cached")
}

func AllFiles() ([]string, error) {
return ExecGitCommand("git ls-files --cached")
}

func PushFiles() ([]string, error) {
return ExecGitCommand("git diff --name-only HEAD master")
}

func ExecGitCommand(command string) ([]string, error) {
commandArg := strings.Split(command, " ")
cmd := exec.Command(commandArg[0], commandArg[1:]...)

outputBytes, err := cmd.CombinedOutput()
if err != nil {
return []string{}, err
}

lines := strings.Split(string(outputBytes), "\n")

return ExtractFiles(lines)
}

func FilterByExt(files []string, ext string) []string {
filtred := make([]string, 0)

for _, f := range files {
if filepath.Ext(f) == ext {
filtred = append(filtred, f)
}
}
return filtred
}

func IsFile(path string) (bool, error) {
stat, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
} else {
return false, err
}
}

return !stat.IsDir(), nil
}

func IsDir(path string) (bool, error) {
stat, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
} else {
return false, err
}
}

return stat.IsDir(), nil
}

func ExtractFiles(lines []string) ([]string, error) {
var files []string

for _, line := range lines {
file := strings.TrimSpace(line)
if len(file) == 0 {
continue
}

isFile, err := IsFile(file)
if err != nil {
return nil, err
}

if isFile {
files = append(files, file)
}
}

return files, nil
}

0 comments on commit afd67f9

Please sign in to comment.