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

pull branch model out into models package

This commit is contained in:
Jesse Duffield
2020-09-29 18:34:01 +10:00
parent c87b2c02fa
commit 44248d9ab0
9 changed files with 35 additions and 29 deletions

View File

@ -4,6 +4,7 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/jesseduffield/lazygit/pkg/models"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -35,7 +36,7 @@ func NewBranchListBuilder(log *logrus.Entry, gitCommand *GitCommand, reflogCommi
}, nil }, nil
} }
func (b *BranchListBuilder) obtainBranches() []*Branch { func (b *BranchListBuilder) obtainBranches() []*models.Branch {
cmdStr := `git for-each-ref --sort=-committerdate --format="%(HEAD)|%(refname:short)|%(upstream:short)|%(upstream:track)" refs/heads` cmdStr := `git for-each-ref --sort=-committerdate --format="%(HEAD)|%(refname:short)|%(upstream:short)|%(upstream:track)" refs/heads`
output, err := b.GitCommand.OSCommand.RunCommandWithOutput(cmdStr) output, err := b.GitCommand.OSCommand.RunCommandWithOutput(cmdStr)
if err != nil { if err != nil {
@ -44,7 +45,7 @@ func (b *BranchListBuilder) obtainBranches() []*Branch {
trimmedOutput := strings.TrimSpace(output) trimmedOutput := strings.TrimSpace(output)
outputLines := strings.Split(trimmedOutput, "\n") outputLines := strings.Split(trimmedOutput, "\n")
branches := make([]*Branch, 0, len(outputLines)) branches := make([]*models.Branch, 0, len(outputLines))
for _, line := range outputLines { for _, line := range outputLines {
if line == "" { if line == "" {
continue continue
@ -53,7 +54,7 @@ func (b *BranchListBuilder) obtainBranches() []*Branch {
split := strings.Split(line, SEPARATION_CHAR) split := strings.Split(line, SEPARATION_CHAR)
name := strings.TrimPrefix(split[1], "heads/") name := strings.TrimPrefix(split[1], "heads/")
branch := &Branch{ branch := &models.Branch{
Name: name, Name: name,
Pullables: "?", Pullables: "?",
Pushables: "?", Pushables: "?",
@ -92,13 +93,13 @@ func (b *BranchListBuilder) obtainBranches() []*Branch {
} }
// Build the list of branches for the current repo // Build the list of branches for the current repo
func (b *BranchListBuilder) Build() []*Branch { func (b *BranchListBuilder) Build() []*models.Branch {
branches := b.obtainBranches() branches := b.obtainBranches()
reflogBranches := b.obtainReflogBranches() reflogBranches := b.obtainReflogBranches()
// loop through reflog branches. If there is a match, merge them, then remove it from the branches and keep it in the reflog branches // loop through reflog branches. If there is a match, merge them, then remove it from the branches and keep it in the reflog branches
branchesWithRecency := make([]*Branch, 0) branchesWithRecency := make([]*models.Branch, 0)
outer: outer:
for _, reflogBranch := range reflogBranches { for _, reflogBranch := range reflogBranches {
for j, branch := range branches { for j, branch := range branches {
@ -122,7 +123,7 @@ outer:
foundHead = true foundHead = true
branch.Recency = " *" branch.Recency = " *"
branches = append(branches[0:i], branches[i+1:]...) branches = append(branches[0:i], branches[i+1:]...)
branches = append([]*Branch{branch}, branches...) branches = append([]*models.Branch{branch}, branches...)
break break
} }
} }
@ -131,24 +132,24 @@ outer:
if err != nil { if err != nil {
panic(err) panic(err)
} }
branches = append([]*Branch{{Name: currentBranchName, DisplayName: currentBranchDisplayName, Head: true, Recency: " *"}}, branches...) branches = append([]*models.Branch{{Name: currentBranchName, DisplayName: currentBranchDisplayName, Head: true, Recency: " *"}}, branches...)
} }
return branches return branches
} }
// TODO: only look at the new reflog commits, and otherwise store the recencies in // TODO: only look at the new reflog commits, and otherwise store the recencies in
// int form against the branch to recalculate the time ago // int form against the branch to recalculate the time ago
func (b *BranchListBuilder) obtainReflogBranches() []*Branch { func (b *BranchListBuilder) obtainReflogBranches() []*models.Branch {
foundBranchesMap := map[string]bool{} foundBranchesMap := map[string]bool{}
re := regexp.MustCompile(`checkout: moving from ([\S]+) to ([\S]+)`) re := regexp.MustCompile(`checkout: moving from ([\S]+) to ([\S]+)`)
reflogBranches := make([]*Branch, 0, len(b.ReflogCommits)) reflogBranches := make([]*models.Branch, 0, len(b.ReflogCommits))
for _, commit := range b.ReflogCommits { for _, commit := range b.ReflogCommits {
if match := re.FindStringSubmatch(commit.Name); len(match) == 3 { if match := re.FindStringSubmatch(commit.Name); len(match) == 3 {
recency := utils.UnixToTimeAgo(commit.UnixTimestamp) recency := utils.UnixToTimeAgo(commit.UnixTimestamp)
for _, branchName := range match[1:] { for _, branchName := range match[1:] {
if !foundBranchesMap[branchName] { if !foundBranchesMap[branchName] {
foundBranchesMap[branchName] = true foundBranchesMap[branchName] = true
reflogBranches = append(reflogBranches, &Branch{ reflogBranches = append(reflogBranches, &models.Branch{
Recency: recency, Recency: recency,
Name: branchName, Name: branchName,
}) })

View File

@ -20,6 +20,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/env" "github.com/jesseduffield/lazygit/pkg/env"
"github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/models"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
gitconfig "github.com/tcnksm/go-gitconfig" gitconfig "github.com/tcnksm/go-gitconfig"
@ -813,7 +814,7 @@ func (c *GitCommand) GetRemoteURL() string {
} }
// CheckRemoteBranchExists Returns remote branch // CheckRemoteBranchExists Returns remote branch
func (c *GitCommand) CheckRemoteBranchExists(branch *Branch) bool { func (c *GitCommand) CheckRemoteBranchExists(branch *models.Branch) bool {
_, err := c.OSCommand.RunCommandWithOutput( _, err := c.OSCommand.RunCommandWithOutput(
"git show-ref --verify -- refs/remotes/origin/%s", "git show-ref --verify -- refs/remotes/origin/%s",
branch.Name, branch.Name,

View File

@ -6,6 +6,7 @@ import (
"github.com/go-errors/errors" "github.com/go-errors/errors"
"github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/models"
) )
// Service is a service that repository is on (Github, Bitbucket, ...) // Service is a service that repository is on (Github, Bitbucket, ...)
@ -89,7 +90,7 @@ func NewPullRequest(gitCommand *GitCommand) *PullRequest {
} }
// Create opens link to new pull request in browser // Create opens link to new pull request in browser
func (pr *PullRequest) Create(branch *Branch) error { func (pr *PullRequest) Create(branch *models.Branch) error {
branchExistsOnRemote := pr.GitCommand.CheckRemoteBranchExists(branch) branchExistsOnRemote := pr.GitCommand.CheckRemoteBranchExists(branch)
if !branchExistsOnRemote { if !branchExistsOnRemote {

View File

@ -46,7 +46,7 @@ func TestGetRepoInfoFromURL(t *testing.T) {
func TestCreatePullRequest(t *testing.T) { func TestCreatePullRequest(t *testing.T) {
type scenario struct { type scenario struct {
testName string testName string
branch *Branch branch *models.Branch
command func(string, ...string) *exec.Cmd command func(string, ...string) *exec.Cmd
test func(err error) test func(err error)
} }
@ -54,7 +54,7 @@ func TestCreatePullRequest(t *testing.T) {
scenarios := []scenario{ scenarios := []scenario{
{ {
"Opens a link to new pull request on bitbucket", "Opens a link to new pull request on bitbucket",
&Branch{ &models.Branch{
Name: "feature/profile-page", Name: "feature/profile-page",
}, },
func(cmd string, args ...string) *exec.Cmd { func(cmd string, args ...string) *exec.Cmd {
@ -73,7 +73,7 @@ func TestCreatePullRequest(t *testing.T) {
}, },
{ {
"Opens a link to new pull request on bitbucket with http remote url", "Opens a link to new pull request on bitbucket with http remote url",
&Branch{ &models.Branch{
Name: "feature/events", Name: "feature/events",
}, },
func(cmd string, args ...string) *exec.Cmd { func(cmd string, args ...string) *exec.Cmd {
@ -92,7 +92,7 @@ func TestCreatePullRequest(t *testing.T) {
}, },
{ {
"Opens a link to new pull request on github", "Opens a link to new pull request on github",
&Branch{ &models.Branch{
Name: "feature/sum-operation", Name: "feature/sum-operation",
}, },
func(cmd string, args ...string) *exec.Cmd { func(cmd string, args ...string) *exec.Cmd {
@ -111,7 +111,7 @@ func TestCreatePullRequest(t *testing.T) {
}, },
{ {
"Opens a link to new pull request on gitlab", "Opens a link to new pull request on gitlab",
&Branch{ &models.Branch{
Name: "feature/ui", Name: "feature/ui",
}, },
func(cmd string, args ...string) *exec.Cmd { func(cmd string, args ...string) *exec.Cmd {
@ -130,7 +130,7 @@ func TestCreatePullRequest(t *testing.T) {
}, },
{ {
"Throws an error if git service is unsupported", "Throws an error if git service is unsupported",
&Branch{ &models.Branch{
Name: "feature/divide-operation", Name: "feature/divide-operation",
}, },
func(cmd string, args ...string) *exec.Cmd { func(cmd string, args ...string) *exec.Cmd {

View File

@ -6,11 +6,12 @@ import (
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/models"
) )
// list panel functions // list panel functions
func (gui *Gui) getSelectedBranch() *commands.Branch { func (gui *Gui) getSelectedBranch() *models.Branch {
if len(gui.State.Branches) == 0 { if len(gui.State.Branches) == 0 {
return nil return nil
} }
@ -209,7 +210,7 @@ func (gui *Gui) handleCheckoutByName(g *gocui.Gui, v *gocui.View) error {
}) })
} }
func (gui *Gui) getCheckedOutBranch() *commands.Branch { func (gui *Gui) getCheckedOutBranch() *models.Branch {
if len(gui.State.Branches) == 0 { if len(gui.State.Branches) == 0 {
return nil return nil
} }
@ -247,7 +248,7 @@ func (gui *Gui) deleteBranch(force bool) error {
return gui.deleteNamedBranch(selectedBranch, force) return gui.deleteNamedBranch(selectedBranch, force)
} }
func (gui *Gui) deleteNamedBranch(selectedBranch *commands.Branch, force bool) error { func (gui *Gui) deleteNamedBranch(selectedBranch *models.Branch, force bool) error {
title := gui.Tr.SLocalize("DeleteBranch") title := gui.Tr.SLocalize("DeleteBranch")
var messageID string var messageID string
if force { if force {
@ -446,7 +447,7 @@ func (gui *Gui) handleRenameBranch(g *gocui.Gui, v *gocui.View) error {
}) })
} }
func (gui *Gui) currentBranch() *commands.Branch { func (gui *Gui) currentBranch() *models.Branch {
if len(gui.State.Branches) == 0 { if len(gui.State.Branches) == 0 {
return nil return nil
} }

View File

@ -9,6 +9,7 @@ import (
"github.com/fatih/color" "github.com/fatih/color"
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/models"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
@ -17,13 +18,13 @@ type CustomCommandObjects struct {
SelectedReflogCommit *commands.Commit SelectedReflogCommit *commands.Commit
SelectedSubCommit *commands.Commit SelectedSubCommit *commands.Commit
SelectedFile *commands.File SelectedFile *commands.File
SelectedLocalBranch *commands.Branch SelectedLocalBranch *models.Branch
SelectedRemoteBranch *commands.RemoteBranch SelectedRemoteBranch *commands.RemoteBranch
SelectedRemote *commands.Remote SelectedRemote *commands.Remote
SelectedTag *commands.Tag SelectedTag *commands.Tag
SelectedStashEntry *commands.StashEntry SelectedStashEntry *commands.StashEntry
SelectedCommitFile *commands.CommitFile SelectedCommitFile *commands.CommitFile
CheckedOutBranch *commands.Branch CheckedOutBranch *models.Branch
PromptResponses []string PromptResponses []string
} }

View File

@ -25,6 +25,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands/patch" "github.com/jesseduffield/lazygit/pkg/commands/patch"
"github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/models"
"github.com/jesseduffield/lazygit/pkg/tasks" "github.com/jesseduffield/lazygit/pkg/tasks"
"github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/theme"
"github.com/jesseduffield/lazygit/pkg/updates" "github.com/jesseduffield/lazygit/pkg/updates"
@ -273,7 +274,7 @@ type Modes struct {
type guiState struct { type guiState struct {
Files []*commands.File Files []*commands.File
SubmoduleConfigs []*commands.SubmoduleConfig SubmoduleConfigs []*commands.SubmoduleConfig
Branches []*commands.Branch Branches []*models.Branch
Commits []*commands.Commit Commits []*commands.Commit
StashEntries []*commands.StashEntry StashEntries []*commands.StashEntry
CommitFiles []*commands.CommitFile CommitFiles []*commands.CommitFile

View File

@ -5,12 +5,12 @@ import (
"strings" "strings"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/models"
"github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/theme"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
func GetBranchListDisplayStrings(branches []*commands.Branch, fullDescription bool, diffName string) [][]string { func GetBranchListDisplayStrings(branches []*models.Branch, fullDescription bool, diffName string) [][]string {
lines := make([][]string, len(branches)) lines := make([][]string, len(branches))
for i := range branches { for i := range branches {
@ -22,7 +22,7 @@ func GetBranchListDisplayStrings(branches []*commands.Branch, fullDescription bo
} }
// getBranchDisplayStrings returns the display string of branch // getBranchDisplayStrings returns the display string of branch
func getBranchDisplayStrings(b *commands.Branch, fullDescription bool, diffed bool) []string { func getBranchDisplayStrings(b *models.Branch, fullDescription bool, diffed bool) []string {
displayName := b.Name displayName := b.Name
if b.DisplayName != "" { if b.DisplayName != "" {
displayName = b.DisplayName displayName = b.DisplayName

View File

@ -1,4 +1,4 @@
package commands package models
// Branch : A git branch // Branch : A git branch
// duplicating this for now // duplicating this for now