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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
package main
import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
// SEP is a browser generated UUID v4 used to separate out commit line items.
const SEP = "6f6c1745-e902-474a-9e99-08d0084fb011"
// Helps keep track of file extensions git thinks of as binary.
var types = make(map[string]bool)
type project struct {
base string
Branches []branch
Name string
repo string
}
// Creates base directories for holding objects, branches, and commits.
func (pro *project) init(f bool) error {
dirs := []string{"branch", "commit", "object"}
for _, dir := range dirs {
d := filepath.Join(pro.base, dir)
// Clear existing dirs when -f true.
if f && dir != "branch" {
if err := os.RemoveAll(d); err != nil {
return fmt.Errorf("unable to remove directory: %v", err)
}
}
if err := os.MkdirAll(d, 0755); err != nil {
return fmt.Errorf("unable to create directory: %v", err)
}
}
return nil
}
// Saves a local clone of `target` repo.
func (pro *project) save(target string) error {
if _, err := os.Stat(pro.repo); err != nil {
return err
}
return exec.Command("git", "clone", target, pro.repo).Run()
}
// Goes through list of branches and returns those that match whitelist.
func (pro *project) branchfilter(whitelist manyflag) ([]branch, error) {
cmd := exec.Command("git", "branch", "-a")
cmd.Dir = pro.repo
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 := pro.commitparser(k)
if err != nil {
continue
}
b[k] = branch{commits, k, pro.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 (pro *project) commitparser(b 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 = pro.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 _, p := range parents {
diffstat, err := pro.diffstatparser(h, p)
if err != nil {
log.Printf("unable to diffstat against parent: %s", err)
continue
}
history = append(history, overview{diffstat, h, p})
}
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 := pro.bodyparser(h)
if err != nil {
log.Printf("unable to parse commit body: %s", err)
continue
}
tree, err := pro.treeparser(h)
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: pro.Name,
Subject: data[2],
Tree: tree,
}
results = append(results, c)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return results, nil
}
func (pro *project) treeparser(h string) ([]object, error) {
cmd := exec.Command("git", "ls-tree", "-r", "--format=%(objectname) %(path)", h)
cmd.Dir = pro.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 (pro *project) diffstatparser(h, p string) (string, error) {
cmd := exec.Command("git", "diff", "--stat", fmt.Sprintf("%s..%s", p, h))
cmd.Dir = pro.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 (pro *project) bodyparser(h 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 = pro.repo
out, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSuffix(fmt.Sprintf("%s", out), "\n"), nil
}
|