refactor getting branches

This commit is contained in:
Jesse Duffield 2018-08-07 19:13:41 +10:00
parent f8ca0ddcad
commit 6cf6fecb64

View file

@ -7,6 +7,7 @@ import (
"fmt"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"time"
@ -164,8 +165,7 @@ func withPadding(str string, padding int) string {
return str + strings.Repeat(" ", padding-len(str))
}
func branchFromLine(line string, index int) Branch {
recency, name := branchStringParts(line)
func branchFromLine(recency, name string, index int) Branch {
branchType, branchBase, colourAttr := branchPropertiesFromName(name)
if index == 0 {
recency = " *"
@ -181,28 +181,19 @@ func branchFromLine(line string, index int) Branch {
}
func getGitBranches() []Branch {
branches := make([]Branch, 0)
// check if there are any branches
branchCheck, _ := runCommand("git branch")
if branchCheck == "" {
return append(branches, branchFromLine("master", 0))
}
if rawString, err := runDirectCommand(getBranchesCommand); err == nil {
branchLines := splitLines(rawString)
for i, line := range branchLines {
branches = append(branches, branchFromLine(line, i))
}
} else {
// TODO: DRY this up
branches = append(branches, branchFromLine(gitCurrentBranchName(), 0))
return []Branch{branchFromLine("", gitCurrentBranchName(), 0)}
}
branches := getBranches()
branches = getAndMergeFetchedBranches(branches)
return branches
}
func branchAlreadyStored(branchLine string, branches []Branch) bool {
for _, branch := range branches {
if branch.Name == branchLine {
func branchAlreadyStored(branchName string, branches []Branch) bool {
for _, existingBranch := range branches {
if existingBranch.Name == branchName {
return true
}
}
@ -225,7 +216,7 @@ func getAndMergeFetchedBranches(branches []Branch) []Branch {
if branchAlreadyStored(line, branches) {
continue
}
branches = append(branches, branchFromLine(line, len(branches)))
branches = append(branches, branchFromLine("", line, len(branches)))
}
return branches
}
@ -545,33 +536,38 @@ func gitCurrentBranchName() string {
return strings.TrimSpace(branchName)
}
const getBranchesCommand = `set -e
git reflog -n100 --pretty='%cr|%gs' --grep-reflog='checkout: moving' HEAD | {
seen=":"
git_dir="$(git rev-parse --git-dir)"
while read line; do
date="${line%%|*}"
branch="${line##* }"
if ! [[ $seen == *:"${branch}":* ]]; then
seen="${seen}${branch}:"
if [ -f "${git_dir}/refs/heads/${branch}" ]; then
printf "%s\t%s\n" "$date" "$branch"
fi
fi
done \
| sed 's/ months /m /g' \
| sed 's/ month /m /g' \
| sed 's/ days /d /g' \
| sed 's/ day /d /g' \
| sed 's/ weeks /w /g' \
| sed 's/ week /w /g' \
| sed 's/ hours /h /g' \
| sed 's/ hour /h /g' \
| sed 's/ minutes /m /g' \
| sed 's/ minute /m /g' \
| sed 's/ seconds /s /g' \
| sed 's/ second /s /g' \
| sed 's/ago//g' \
| tr -d ' '
func getBranches() []Branch {
rawString, _ := runDirectCommand("git reflog -n100 --pretty='%cr|%gs' --grep-reflog='checkout: moving' HEAD")
branches := make([]Branch, 0)
branchLines := splitLines(rawString)
for i, line := range branchLines {
r := regexp.MustCompile("\\|.*\\s")
line = r.ReplaceAllString(line, " ")
words := strings.Split(line, " ")
timeNumber := words[0]
timeUnit := words[1]
branchName := words[3]
if branchAlreadyStored(branchName, branches) {
continue
}
r = regexp.MustCompile("s$")
timeUnit = r.ReplaceAllString(timeUnit, "")
timeUnitMap := map[string]string{
"hour": "h",
"minute": "m",
"second": "s",
"week": "w",
"year": "y",
"day": "d",
"month": "m",
}
timeUnit = timeUnitMap[timeUnit]
branch := branchFromLine(timeNumber+timeUnit, branchName, i)
branches = append(branches, branch)
}
return branches
}
`