1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-05-17 22:32:58 +02:00

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