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