1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-13 00:07:59 +02:00

refactor into smaller functions

This commit is contained in:
Jesse Duffield 2018-08-07 19:31:19 +10:00
parent 2b37c9b19c
commit 232e1c74e4

View File

@ -480,27 +480,17 @@ func gitCurrentBranchName() string {
return strings.TrimSpace(branchName)
}
func getBranches() []Branch {
branches := make([]Branch, 0)
rawString, err := runDirectCommand("git reflog -n100 --pretty='%cr|%gs' --grep-reflog='checkout: moving' HEAD")
if err != nil {
return branches
}
branchLines := splitLines(rawString)
for i, line := range branchLines {
// A line will have the form '10 days ago master' so we need to strip out the
// useful information from that into timeNumber, timeUnit, and branchName
func branchInfoFromLine(line string) (string, string, string) {
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
return words[0], words[1], words[3]
}
r = regexp.MustCompile("s$")
func abbreviatedTimeUnit(timeUnit string) string {
r := regexp.MustCompile("s$")
timeUnit = r.ReplaceAllString(timeUnit, "")
timeUnitMap := map[string]string{
"hour": "h",
@ -511,7 +501,24 @@ func getBranches() []Branch {
"day": "d",
"month": "m",
}
timeUnit = timeUnitMap[timeUnit]
return timeUnitMap[timeUnit]
}
func getBranches() []Branch {
branches := make([]Branch, 0)
rawString, err := runDirectCommand("git reflog -n100 --pretty='%cr|%gs' --grep-reflog='checkout: moving' HEAD")
if err != nil {
return branches
}
branchLines := splitLines(rawString)
for i, line := range branchLines {
timeNumber, timeUnit, branchName := branchInfoFromLine(line)
timeUnit = abbreviatedTimeUnit(timeUnit)
if branchAlreadyStored(branchName, branches) {
continue
}
branch := constructBranch(timeNumber+timeUnit, branchName, i)
branches = append(branches, branch)