Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Persistent Task Invalidation #6

Merged
merged 7 commits into from
Nov 6, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions cache/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cache

import (
"bytes"
"context"
"fmt"
"strings"

"github.com/samsarahq/taskrunner/shell"
)

type gitClient struct {
shellRun shell.ShellRun
}

func stripStdout(buf bytes.Buffer) string {
return strings.Trim(buf.String(), "\n")
}

func splitStdout(buf bytes.Buffer) []string {
return strings.Split(stripStdout(buf), "\n")
}

func (g gitClient) currentCommit(ctx context.Context) (commitHash string, err error) {
var buffer bytes.Buffer
if err := g.shellRun(ctx, "git rev-parse HEAD", shell.Stdout(&buffer)); err != nil {
return "", err
}

return stripStdout(buffer), nil
}

func (g gitClient) diff(ctx context.Context, commitHash string) (modifiedFiles []string, error error) {
var buffer bytes.Buffer
if err := g.shellRun(ctx, fmt.Sprintf("git diff --name-only %s", commitHash), shell.Stdout(&buffer)); err != nil {
return nil, err
}

return splitStdout(buffer), nil
}

func (g gitClient) uncomittedFiles(ctx context.Context) (newFiles []string, modifiedFiles []string, err error) {
var buffer bytes.Buffer
if err := g.shellRun(ctx, "git status --porcelain", shell.Stdout(&buffer)); err != nil {
return nil, nil, err
}

for _, statusLine := range splitStdout(buffer) {
if strings.HasPrefix(statusLine, "??") {
newFiles = append(newFiles, statusLine[3:])
} else {
modifiedFiles = append(modifiedFiles, statusLine[3:])
}
}

return newFiles, modifiedFiles, nil
}