commit 0b02e2d1a0fc4c6b8911e48f82e730d6bd1bea55 Author: Martin Thielecke Date: Fri Jul 31 23:18:35 2015 +0000 [general] initial methods for easily getting changed files diff --git a/general/gochanges.go b/general/gochanges.go new file mode 100644 index 0000000..10ae505 --- /dev/null +++ b/general/gochanges.go @@ -0,0 +1,39 @@ +package general + +import ( + "bytes" +"os/exec" + "path/filepath" + "strings" +) + +// RunCommand is a simple wrapper to run commands +func RunCommand(command string, values ...string) string { + cmd := exec.Command(command, values...) + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + panic(err) + } + return out.String() +} + +// GetChangedGoFiles returns a list of changed .go files +// anything else is filtered out +func GetChangedGoFiles() (result []string) { + gitDir := RunCommand("git", "rev-parse", "--git-dir") + gitDiff := RunCommand("git", "diff", "--name-only", "--cached", "--diff-filter=ACM") + absolutePath := filepath.Dir(gitDir) + + resultLines := strings.Split(gitDiff, "\n") + if resultLines == nil { + return + } + for _, filename := range resultLines { + if filename != "" && strings.HasSuffix(filename, ".go") { + result = append(result, absolutePath+"/"+filename) + } + } + return result +}