-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
A.A.Abroskin
committed
Jul 12, 2019
1 parent
ee165d7
commit afd67f9
Showing
2 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |