Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
patinthehat committed Nov 14, 2024
1 parent eb2d9fb commit cbc86da
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

var Version = "0.3.1-dev.5"
var Version = "0.3.1-dev.7"
73 changes: 39 additions & 34 deletions cmd/listFrequentBranches.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,53 +74,58 @@ type FrequentBranchThresholds struct {
Older time.Time
}

func processRefLogLines(lines []string, existingBranches map[string]bool) map[string]git.BranchInfo {
branches := make(map[string]git.BranchInfo)

for _, line := range lines {
info := git.GetBranchInfoFromReflogLine(lineRegex, line, 4)
if info == nil || !utils.MapEntryExists(info.BranchName, existingBranches) {
continue
}

data := git.BranchInfo{Name: info.BranchName, CheckoutCount: 0, CheckedOutLast: time.Time{}}

if _, ok := branches[info.BranchName]; ok {
data = branches[info.BranchName]
}

data.CheckoutCount++

if data.CheckedOutLast.IsZero() || info.Timestamp.After(data.CheckedOutLast) {
data.CheckedOutLast = info.Timestamp
}

branches[info.BranchName] = data
}

for name, branch := range branches {
branch.Update()
branches[name] = branch
}

return branches
}

func init() {
rootCmd.AddCommand(&cobra.Command{
Use: "branch:freq",
Short: "Show recently checked out branch names",
Run: func(c *cobra.Command, args []string) {
lines, _ := git.GetGitReflogLines("%at ~ %gs ~ %gd")
existingBranches, _ := helpers.GetAvailableBranchesMap()
branchData := make(map[string]git.BranchInfo)
availableBranches, _ := helpers.GetAvailableBranchesMap()
branches := make(map[string]git.BranchInfo)

thresholds := FrequentBranchThresholds{
Recent: time.Now().AddDate(0, 0, -7),
Older: time.Now().AddDate(0, 0, -15),
}

for _, line := range lines {
data := git.BranchInfo{Name: "", CheckoutCount: 0, CheckedOutLast: time.Time{}}
info := git.GetBranchInfoFromReflogLine(lineRegex, line, 4)
if info == nil || !utils.MapEntryExists(info.BranchName, existingBranches) {
continue
}

if _, ok := branchData[info.BranchName]; ok {
data = branchData[info.BranchName]
} else {
data.Name = info.BranchName
}

data.CheckoutCount++
data.CheckoutHistory = append(data.CheckoutHistory, info)

if data.CheckedOutLast.IsZero() || info.Timestamp.After(data.CheckedOutLast) {
data.CheckedOutLast = info.Timestamp
}

branchData[info.BranchName] = data
}

for name, branch := range branchData {
branch.Update()
branchData[name] = branch
}

displayedBranches := getGroupedAndSortedDisplayBranches(branchData, &thresholds, 15)
branches = processRefLogLines(lines, availableBranches)
frequent := getGroupedAndSortedDisplayBranches(branches, &thresholds, 15)

for _, branch := range displayedBranches {
infoStr := fmt.Sprintf("%2d checkouts, %2d commits, %-15s", branch.CheckoutCount, branch.CommitCount, utils.GetRelativeTime(branch.CheckedOutLast))
fmt.Printf(" \033[33m%28s \033[37;1m %s\033[0m\n", infoStr, branch.Name)
for _, br := range frequent {
description := fmt.Sprintf("%2d checkouts, %2d commits, %-15s", br.CheckoutCount, br.CommitCount, utils.GetRelativeTime(br.CheckedOutLast))
fmt.Printf(" \033[33m%28s \033[37;1m %s\033[0m\n", description, br.Name)
}
},
})
Expand Down

0 comments on commit cbc86da

Please sign in to comment.