|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +// Commits returns a set of commits. |
| 14 | +// If commitrange is a git still range 12345...54321, then it will be isolated set of commits. |
| 15 | +// If commitrange is a single commit, all ancestor commits up through the hash provided. |
| 16 | +func Commits(commitrange string) ([]CommitEntry, error) { |
| 17 | + output, err := exec.Command("git", "log", prettyFormat+formatCommit, commitrange).Output() |
| 18 | + if err != nil { |
| 19 | + return nil, err |
| 20 | + } |
| 21 | + commitHashes := strings.Split(strings.TrimSpace(string(output)), "\n") |
| 22 | + commits := make([]CommitEntry, len(commitHashes)) |
| 23 | + for i, commitHash := range commitHashes { |
| 24 | + c, err := LogCommit(commitHash) |
| 25 | + if err != nil { |
| 26 | + return commits, err |
| 27 | + } |
| 28 | + commits[i] = *c |
| 29 | + } |
| 30 | + return commits, nil |
| 31 | +} |
| 32 | + |
| 33 | +// CommitEntry represents a single commit's information from `git` |
| 34 | +type CommitEntry map[string]string |
| 35 | + |
| 36 | +var ( |
| 37 | + prettyFormat = `--pretty=format:` |
| 38 | + formatSubject = `%s` |
| 39 | + formatBody = `%b` |
| 40 | + formatCommit = `%H` |
| 41 | + formatAuthorName = `%aN` |
| 42 | + formatAuthorEmail = `%aE` |
| 43 | + formatCommitterName = `%cN` |
| 44 | + formatCommitterEmail = `%cE` |
| 45 | + formatSigner = `%GS` |
| 46 | + formatCommitNotes = `%N` |
| 47 | + formatMap = `{"commit": "%H", "abbreviated_commit": "%h", "tree": "%T", "abbreviated_tree": "%t", "parent": "%P", "abbreviated_parent": "%p", "refs": "%D", "encoding": "%e", "sanitized_subject_line": "%f", "verification_flag": "%G?", "signer_key": "%GK", "author_date": "%aD" , "committer_date": "%cD" }` |
| 48 | +) |
| 49 | + |
| 50 | +// LogCommit assembles the full information on a commit from its commit hash |
| 51 | +func LogCommit(commit string) (*CommitEntry, error) { |
| 52 | + buf := bytes.NewBuffer([]byte{}) |
| 53 | + cmd := exec.Command("git", "log", "-1", prettyFormat+formatMap, commit) |
| 54 | + cmd.Stdout = buf |
| 55 | + cmd.Stderr = os.Stderr |
| 56 | + |
| 57 | + if err := cmd.Run(); err != nil { |
| 58 | + log.Println(strings.Join(cmd.Args, " ")) |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + c := CommitEntry{} |
| 62 | + output := buf.Bytes() |
| 63 | + if err := json.Unmarshal(output, &c); err != nil { |
| 64 | + fmt.Println(string(output)) |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + // any user provided fields can't be sanitized for the mock-json marshal above |
| 69 | + for k, v := range map[string]string{ |
| 70 | + "subject": formatSubject, |
| 71 | + "body": formatBody, |
| 72 | + "author_name": formatAuthorName, |
| 73 | + "author_email": formatAuthorEmail, |
| 74 | + "committer_name": formatCommitterName, |
| 75 | + "committer_email": formatCommitterEmail, |
| 76 | + "commit_notes": formatCommitNotes, |
| 77 | + "signer": formatSigner, |
| 78 | + } { |
| 79 | + output, err := exec.Command("git", "log", "-1", prettyFormat+v, commit).Output() |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + c[k] = strings.TrimSpace(string(output)) |
| 84 | + } |
| 85 | + |
| 86 | + return &c, nil |
| 87 | +} |
| 88 | + |
| 89 | +// FetchHeadCommit returns the hash of FETCH_HEAD |
| 90 | +func FetchHeadCommit() (string, error) { |
| 91 | + output, err := exec.Command("git", "rev-parse", "--verify", "FETCH_HEAD").Output() |
| 92 | + if err != nil { |
| 93 | + return "", err |
| 94 | + } |
| 95 | + return strings.TrimSpace(string(output)), nil |
| 96 | +} |
| 97 | + |
| 98 | +// HeadCommit returns the hash of HEAD |
| 99 | +func HeadCommit() (string, error) { |
| 100 | + output, err := exec.Command("git", "rev-parse", "--verify", "HEAD").Output() |
| 101 | + if err != nil { |
| 102 | + return "", err |
| 103 | + } |
| 104 | + return strings.TrimSpace(string(output)), nil |
| 105 | +} |
0 commit comments