1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
package main
import (
"bufio"
"bytes"
"fmt"
"log"
"os/exec"
"path/filepath"
"strings"
"time"
)
// Goes through list of branches and returns those that match whitelist.
func branchFilter(repo string, options *options) ([]branch, error) {
cmd := exec.Command("git", "branch", "-a")
cmd.Dir = repo
whitelist := options.Branches
out, err := cmd.Output()
if err != nil {
return nil, err
}
var b = make(map[string]branch)
var m = make(map[string]bool)
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
t := strings.TrimSpace(strings.TrimPrefix(scanner.Text(), "*"))
_, f := filepath.Split(t)
m[f] = true
}
if err := scanner.Err(); err != nil {
return nil, err
}
// Filter to match options, but return all if no branch flags given.
if len(whitelist) > 0 {
for k := range m {
m[k] = contains(whitelist, k)
}
} else {
// In git given order at this point.
for k := range m {
whitelist = append(whitelist, k)
}
}
for k, v := range m {
if v {
// TODO: Try a goroutine?
commits, err := commitParser(k, repo, options.Name)
if err != nil {
continue
}
b[k] = branch{commits, k, options.Name}
}
}
// Fill in resulting slice with desired branches in order.
var results []branch
for _, v := range whitelist {
results = append(results, b[v])
}
return results, nil
}
func commitParser(b string, repo string, name string) ([]commit, error) {
fst := strings.Join([]string{"%H", "%P", "%s", "%aN", "%aE", "%aD", "%h"}, SEP)
ref := fmt.Sprintf("origin/%s", b)
cmd := exec.Command("git", "log", fmt.Sprintf("--format=%s", fst), ref)
cmd.Dir = repo
out, err := cmd.Output()
if err != nil {
return nil, err
}
results := []commit{}
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
text := strings.TrimSpace(scanner.Text())
data := strings.Split(text, SEP)
h := data[0]
var history []overview
var parents []string
if data[1] != "" {
parents = strings.Split(data[1], " ")
}
for _, parent := range parents {
diffstat, err := diffStatParser(h, parent, repo)
if err != nil {
log.Printf("unable to diffstat against parent: %s", err)
continue
}
history = append(history, overview{diffstat, h, parent})
}
a := author{data[4], data[3]}
date, err := time.Parse("Mon, 2 Jan 2006 15:04:05 -0700", data[5])
if err != nil {
log.Printf("unable to parse commit date: %s", err)
continue
}
body, err := bodyParser(h, repo)
if err != nil {
log.Printf("unable to parse commit body: %s", err)
continue
}
tree, err := treeParser(h, repo)
if err != nil {
log.Printf("unable to parse commit tree: %s", err)
continue
}
c := commit{
Abbr: data[6],
Author: a,
Body: body,
Branch: b,
Date: date,
Hash: h,
History: history,
Parents: parents,
Project: name,
Subject: data[2],
Tree: tree,
}
results = append(results, c)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return results, nil
}
func treeParser(h string, repo string) ([]object, error) {
cmd := exec.Command("git", "ls-tree", "-r", "--format=%(objectname) %(path)", h)
cmd.Dir = repo
out, err := cmd.Output()
if err != nil {
return nil, err
}
var results []object
feed := strings.Split(strings.TrimSuffix(fmt.Sprintf("%s", out), "\n"), "\n")
for _, line := range feed {
w := strings.Split(line, " ")
results = append(results, object{
Hash: w[0],
Path: w[1],
})
}
return results, nil
}
func diffStatParser(h, parent string, repo string) (string, error) {
cmd := exec.Command("git", "diff", "--stat", fmt.Sprintf("%s..%s", parent, h))
cmd.Dir = repo
out, err := cmd.Output()
if err != nil {
return "", err
}
var results []string
feed := strings.Split(strings.TrimSuffix(fmt.Sprintf("%s", out), "\n"), "\n")
for _, line := range feed {
// NOTE: This is hackish I know, attach to project?
i := strings.Index(line, "|")
if i != -1 {
ext := filepath.Ext(strings.TrimSpace(line[:i]))
types[ext] = strings.Contains(line, "Bin")
}
results = append(results, strings.TrimSpace(line))
}
return strings.Join(results, "\n"), nil
}
func bodyParser(h string, repo string) (string, error) {
// Because the commit message body is multiline and is tripping the scanner.
cmd := exec.Command("git", "show", "--no-patch", "--format=%B", h)
cmd.Dir = repo
out, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSuffix(fmt.Sprintf("%s", out), "\n"), nil
}
|