2019-11-04 10:47:25 +02:00
|
|
|
package commands
|
2018-08-10 13:33:49 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2020-09-29 12:28:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2021-12-29 02:37:15 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/common"
|
2018-08-13 12:26:02 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2018-08-10 13:33:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// context:
|
|
|
|
// we want to only show 'safe' branches (ones that haven't e.g. been deleted)
|
|
|
|
// which `git branch -a` gives us, but we also want the recency data that
|
|
|
|
// git reflog gives us.
|
|
|
|
// So we get the HEAD, then append get the reflog branches that intersect with
|
|
|
|
// our safe branches, then add the remaining safe branches, ensuring uniqueness
|
|
|
|
// along the way
|
|
|
|
|
2019-02-19 00:18:30 +02:00
|
|
|
// if we find out we need to use one of these functions in the git.go file, we
|
|
|
|
// can just pull them out of here and put them there and then call them from in here
|
|
|
|
|
2018-08-13 12:26:02 +02:00
|
|
|
// BranchListBuilder returns a list of Branch objects for the current repo
|
2018-08-12 13:04:47 +02:00
|
|
|
type BranchListBuilder struct {
|
2021-12-29 02:37:15 +02:00
|
|
|
*common.Common
|
|
|
|
getRawBranches func() (string, error)
|
|
|
|
getCurrentBranchName func() (string, string, error)
|
|
|
|
reflogCommits []*models.Commit
|
2018-08-12 13:04:47 +02:00
|
|
|
}
|
2018-08-10 13:33:49 +02:00
|
|
|
|
2021-12-29 02:37:15 +02:00
|
|
|
func NewBranchListBuilder(
|
|
|
|
cmn *common.Common,
|
|
|
|
getRawBranches func() (string, error),
|
|
|
|
getCurrentBranchName func() (string, string, error),
|
|
|
|
reflogCommits []*models.Commit,
|
|
|
|
) *BranchListBuilder {
|
2018-08-13 12:26:02 +02:00
|
|
|
return &BranchListBuilder{
|
2021-12-29 02:37:15 +02:00
|
|
|
Common: cmn,
|
|
|
|
getRawBranches: getRawBranches,
|
|
|
|
getCurrentBranchName: getCurrentBranchName,
|
|
|
|
reflogCommits: reflogCommits,
|
|
|
|
}
|
2018-08-10 13:33:49 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 10:34:01 +02:00
|
|
|
func (b *BranchListBuilder) obtainBranches() []*models.Branch {
|
2021-12-29 02:37:15 +02:00
|
|
|
output, err := b.getRawBranches()
|
2018-08-10 13:33:49 +02:00
|
|
|
if err != nil {
|
2020-03-17 12:22:07 +02:00
|
|
|
panic(err)
|
2018-08-10 13:33:49 +02:00
|
|
|
}
|
|
|
|
|
2020-03-17 12:22:07 +02:00
|
|
|
trimmedOutput := strings.TrimSpace(output)
|
|
|
|
outputLines := strings.Split(trimmedOutput, "\n")
|
2020-09-29 10:34:01 +02:00
|
|
|
branches := make([]*models.Branch, 0, len(outputLines))
|
2020-03-23 12:26:24 +02:00
|
|
|
for _, line := range outputLines {
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-17 12:22:07 +02:00
|
|
|
split := strings.Split(line, SEPARATION_CHAR)
|
2021-07-26 11:07:42 +02:00
|
|
|
if len(split) != 4 {
|
|
|
|
// Ignore line if it isn't separated into 4 parts
|
|
|
|
// This is probably a warning message, for more info see:
|
|
|
|
// https://github.com/jesseduffield/lazygit/issues/1385#issuecomment-885580439
|
|
|
|
continue
|
|
|
|
}
|
2020-03-17 12:22:07 +02:00
|
|
|
|
2020-05-18 12:19:29 +02:00
|
|
|
name := strings.TrimPrefix(split[1], "heads/")
|
2020-09-29 10:34:01 +02:00
|
|
|
branch := &models.Branch{
|
2020-03-17 12:22:07 +02:00
|
|
|
Name: name,
|
|
|
|
Pullables: "?",
|
|
|
|
Pushables: "?",
|
2020-03-19 03:04:17 +02:00
|
|
|
Head: split[0] == "*",
|
2020-03-17 12:22:07 +02:00
|
|
|
}
|
2020-03-23 12:26:24 +02:00
|
|
|
|
2020-03-19 03:04:17 +02:00
|
|
|
upstreamName := split[2]
|
2020-03-17 12:22:07 +02:00
|
|
|
if upstreamName == "" {
|
2020-03-23 12:26:24 +02:00
|
|
|
branches = append(branches, branch)
|
2020-01-28 13:09:33 +02:00
|
|
|
continue
|
|
|
|
}
|
2018-08-10 13:33:49 +02:00
|
|
|
|
2020-03-23 12:26:24 +02:00
|
|
|
branch.UpstreamName = upstreamName
|
2018-08-10 13:33:49 +02:00
|
|
|
|
2020-03-19 03:04:17 +02:00
|
|
|
track := split[3]
|
2020-03-17 12:22:07 +02:00
|
|
|
re := regexp.MustCompile(`ahead (\d+)`)
|
|
|
|
match := re.FindStringSubmatch(track)
|
|
|
|
if len(match) > 1 {
|
2020-03-23 12:26:24 +02:00
|
|
|
branch.Pushables = match[1]
|
2020-03-17 12:22:07 +02:00
|
|
|
} else {
|
2020-03-23 12:26:24 +02:00
|
|
|
branch.Pushables = "0"
|
2018-08-10 13:33:49 +02:00
|
|
|
}
|
|
|
|
|
2020-03-17 12:22:07 +02:00
|
|
|
re = regexp.MustCompile(`behind (\d+)`)
|
|
|
|
match = re.FindStringSubmatch(track)
|
|
|
|
if len(match) > 1 {
|
2020-03-23 12:26:24 +02:00
|
|
|
branch.Pullables = match[1]
|
2020-03-17 12:22:07 +02:00
|
|
|
} else {
|
2020-03-23 12:26:24 +02:00
|
|
|
branch.Pullables = "0"
|
2018-08-11 08:11:17 +02:00
|
|
|
}
|
2020-03-23 12:26:24 +02:00
|
|
|
|
|
|
|
branches = append(branches, branch)
|
2018-08-11 08:11:17 +02:00
|
|
|
}
|
2020-03-17 12:22:07 +02:00
|
|
|
|
|
|
|
return branches
|
2018-08-11 08:11:17 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 12:26:02 +02:00
|
|
|
// Build the list of branches for the current repo
|
2020-09-29 10:34:01 +02:00
|
|
|
func (b *BranchListBuilder) Build() []*models.Branch {
|
2020-03-17 12:22:07 +02:00
|
|
|
branches := b.obtainBranches()
|
2020-03-19 03:04:17 +02:00
|
|
|
|
2018-08-10 13:38:51 +02:00
|
|
|
reflogBranches := b.obtainReflogBranches()
|
2018-08-10 13:33:49 +02:00
|
|
|
|
2020-03-17 12:22:07 +02:00
|
|
|
// loop through reflog branches. If there is a match, merge them, then remove it from the branches and keep it in the reflog branches
|
2020-09-29 10:34:01 +02:00
|
|
|
branchesWithRecency := make([]*models.Branch, 0)
|
2020-03-17 12:22:07 +02:00
|
|
|
outer:
|
|
|
|
for _, reflogBranch := range reflogBranches {
|
|
|
|
for j, branch := range branches {
|
2020-03-19 03:04:17 +02:00
|
|
|
if branch.Head {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-17 12:22:07 +02:00
|
|
|
if strings.EqualFold(reflogBranch.Name, branch.Name) {
|
|
|
|
branch.Recency = reflogBranch.Recency
|
|
|
|
branchesWithRecency = append(branchesWithRecency, branch)
|
|
|
|
branches = append(branches[0:j], branches[j+1:]...)
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 10:17:05 +02:00
|
|
|
}
|
|
|
|
|
2020-03-17 12:22:07 +02:00
|
|
|
branches = append(branchesWithRecency, branches...)
|
2018-11-14 10:17:05 +02:00
|
|
|
|
2020-03-23 12:26:24 +02:00
|
|
|
foundHead := false
|
2020-03-17 12:22:07 +02:00
|
|
|
for i, branch := range branches {
|
2020-03-19 03:04:17 +02:00
|
|
|
if branch.Head {
|
2020-03-23 12:26:24 +02:00
|
|
|
foundHead = true
|
2020-03-19 03:04:17 +02:00
|
|
|
branch.Recency = " *"
|
2020-03-17 12:22:07 +02:00
|
|
|
branches = append(branches[0:i], branches[i+1:]...)
|
2020-09-29 10:34:01 +02:00
|
|
|
branches = append([]*models.Branch{branch}, branches...)
|
2020-03-17 12:22:07 +02:00
|
|
|
break
|
2018-08-13 12:26:02 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-23 12:26:24 +02:00
|
|
|
if !foundHead {
|
2021-12-29 02:37:15 +02:00
|
|
|
currentBranchName, currentBranchDisplayName, err := b.getCurrentBranchName()
|
2020-03-26 11:29:35 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-09-29 10:34:01 +02:00
|
|
|
branches = append([]*models.Branch{{Name: currentBranchName, DisplayName: currentBranchDisplayName, Head: true, Recency: " *"}}, branches...)
|
2020-03-23 12:26:24 +02:00
|
|
|
}
|
2020-03-17 12:22:07 +02:00
|
|
|
return branches
|
2018-08-10 13:33:49 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 13:51:24 +02:00
|
|
|
// TODO: only look at the new reflog commits, and otherwise store the recencies in
|
|
|
|
// int form against the branch to recalculate the time ago
|
2020-09-29 10:34:01 +02:00
|
|
|
func (b *BranchListBuilder) obtainReflogBranches() []*models.Branch {
|
2020-03-26 13:51:24 +02:00
|
|
|
foundBranchesMap := map[string]bool{}
|
|
|
|
re := regexp.MustCompile(`checkout: moving from ([\S]+) to ([\S]+)`)
|
2021-12-29 02:37:15 +02:00
|
|
|
reflogBranches := make([]*models.Branch, 0, len(b.reflogCommits))
|
|
|
|
for _, commit := range b.reflogCommits {
|
2020-03-26 13:51:24 +02:00
|
|
|
if match := re.FindStringSubmatch(commit.Name); len(match) == 3 {
|
2020-03-27 10:12:15 +02:00
|
|
|
recency := utils.UnixToTimeAgo(commit.UnixTimestamp)
|
2020-03-26 13:51:24 +02:00
|
|
|
for _, branchName := range match[1:] {
|
|
|
|
if !foundBranchesMap[branchName] {
|
|
|
|
foundBranchesMap[branchName] = true
|
2020-09-29 10:34:01 +02:00
|
|
|
reflogBranches = append(reflogBranches, &models.Branch{
|
2020-03-26 13:51:24 +02:00
|
|
|
Recency: recency,
|
|
|
|
Name: branchName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-03-17 12:22:07 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-26 13:51:24 +02:00
|
|
|
return reflogBranches
|
2020-03-17 12:22:07 +02:00
|
|
|
}
|