code.oscarkilo.com/klex-git/util/commit.go

..
commit.go
commit_test.go
committest/
files.go
files_test.go
paths.go
paths_test.go
root.go
root_test.go
run.go
run_test.go
package util

import (
  "encoding/json"
  "strings"
)

type CommitInfo struct {
  ChangedPaths []string `json:"changed_paths"`
}

// GetCommitInfo() works inside of a post-commit Git hook.
// It assumes that the current working directory is inside a Git repo.
func GetCommitInfo() (*CommitInfo, error) {
  stdout, _, err := Run(
    "git", "diff",
    "--cached",
    "--name-only",
    "--diff-filter=ACM",
  )
  if err != nil {
    return nil, err
  }
  lines := strings.Split(stdout, "\n")
  for len(lines) > 0 && lines[len(lines)-1] == "" {
    lines = lines[:len(lines)-1]
  }
  return &CommitInfo{ChangedPaths: lines}, nil
}

func (ci *CommitInfo) DebugString() string {
  b, _ := json.MarshalIndent(ci, "", "  ")
  return string(b)
}