|
| 1 | +package leeway |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + |
| 9 | + log "github.com/sirupsen/logrus" |
| 10 | + "golang.org/x/xerrors" |
| 11 | +) |
| 12 | + |
| 13 | +type GitInfo struct { |
| 14 | + WorkingCopyLoc string |
| 15 | + Commit string |
| 16 | + Origin string |
| 17 | + |
| 18 | + dirty bool |
| 19 | + dirtyFiles map[string]struct{} |
| 20 | +} |
| 21 | + |
| 22 | +// GetGitInfo returns the git status required during a leeway build |
| 23 | +func GetGitInfo(loc string) (*GitInfo, error) { |
| 24 | + gitfc := filepath.Join(loc, ".git") |
| 25 | + stat, err := os.Stat(gitfc) |
| 26 | + if err != nil || !stat.IsDir() { |
| 27 | + return nil, nil |
| 28 | + } |
| 29 | + |
| 30 | + cmd := exec.Command("git", "rev-parse", "HEAD") |
| 31 | + cmd.Dir = loc |
| 32 | + out, err := cmd.CombinedOutput() |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + res := GitInfo{ |
| 37 | + WorkingCopyLoc: loc, |
| 38 | + Commit: strings.TrimSpace(string(out)), |
| 39 | + } |
| 40 | + |
| 41 | + cmd = exec.Command("git", "config", "--get", "remote.origin.url") |
| 42 | + cmd.Dir = loc |
| 43 | + out, err = cmd.CombinedOutput() |
| 44 | + if err != nil && len(out) > 0 { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + res.Origin = strings.TrimSpace(string(out)) |
| 48 | + |
| 49 | + cmd = exec.Command("git", "status", "--porcelain") |
| 50 | + cmd.Dir = loc |
| 51 | + out, err = cmd.CombinedOutput() |
| 52 | + if serr, ok := err.(*exec.ExitError); ok && serr.ExitCode() != 128 { |
| 53 | + // git status --short seems to exit with 128 all the time - that's ok, but we need to account for that. |
| 54 | + log.WithField("exitCode", serr.ExitCode()).Debug("git status --porcelain exited with failed exit code. Working copy is dirty.") |
| 55 | + res.dirty = true |
| 56 | + } else if _, ok := err.(*exec.ExitError); !ok && err != nil { |
| 57 | + return nil, err |
| 58 | + } else if len(strings.TrimSpace(string(out))) != 0 { |
| 59 | + log.WithField("out", string(out)).Debug("`git status --porcelain` produced output. Working copy is dirty.") |
| 60 | + |
| 61 | + res.dirty = true |
| 62 | + res.dirtyFiles, err = parseGitStatus(out) |
| 63 | + if err != nil { |
| 64 | + log.WithError(err).Warn("cannot parse git status: assuming all files are dirty") |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return &res, nil |
| 69 | +} |
| 70 | + |
| 71 | +// parseGitStatus parses the output of "git status --porcelain" |
| 72 | +func parseGitStatus(out []byte) (files map[string]struct{}, err error) { |
| 73 | + in := strings.TrimSpace(string(out)) |
| 74 | + if len(in) == 0 { |
| 75 | + // no files - nothing's dirty |
| 76 | + return nil, nil |
| 77 | + } |
| 78 | + |
| 79 | + lines := strings.Split(in, "\n") |
| 80 | + files = make(map[string]struct{}, len(lines)) |
| 81 | + for _, l := range lines { |
| 82 | + segs := strings.Fields(l) |
| 83 | + if len(segs) == 0 { |
| 84 | + continue |
| 85 | + } |
| 86 | + if len(segs) != 2 { |
| 87 | + return nil, xerrors.Errorf("cannot parse git status \"%s\": expected two segments, got %d", l, len(segs)) |
| 88 | + } |
| 89 | + files[segs[1]] = struct{}{} |
| 90 | + } |
| 91 | + return |
| 92 | +} |
| 93 | + |
| 94 | +// DirtyFiles returns true if a single file of the file list |
| 95 | +// is dirty. |
| 96 | +func (gi *GitInfo) DirtyFiles(files []string) bool { |
| 97 | + if !gi.dirty { |
| 98 | + // nothing's dirty |
| 99 | + log.WithField("workingCopy", gi.WorkingCopyLoc).Debug("building from a clean working copy") |
| 100 | + return false |
| 101 | + } |
| 102 | + if len(gi.dirtyFiles) == 0 { |
| 103 | + // we don't have any record of dirty files, just that the |
| 104 | + // working copy is dirty. Hence, we assume all files are dirty. |
| 105 | + log.WithField("workingCopy", gi.WorkingCopyLoc).Debug("no records of dirty files - assuming dirty Git working copy") |
| 106 | + return true |
| 107 | + } |
| 108 | + for _, f := range files { |
| 109 | + if !strings.HasPrefix(f, gi.WorkingCopyLoc) { |
| 110 | + // We don't know anything about this file, but the caller |
| 111 | + // might make important decisions on the dirty-state of |
| 112 | + // the files. For good measure we assume the file is dirty. |
| 113 | + log.WithField("workingCopy", gi.WorkingCopyLoc).WithField("fn", f).Debug("no records of this file - assuming it's dirty") |
| 114 | + return true |
| 115 | + } |
| 116 | + |
| 117 | + fn := strings.TrimPrefix(f, gi.WorkingCopyLoc) |
| 118 | + fn = strings.TrimPrefix(fn, "/") |
| 119 | + _, dirty := gi.dirtyFiles[fn] |
| 120 | + if dirty { |
| 121 | + log.WithField("workingCopy", gi.WorkingCopyLoc).WithField("fn", f).Debug("found dirty source file") |
| 122 | + return true |
| 123 | + } |
| 124 | + } |
| 125 | + return false |
| 126 | +} |
0 commit comments