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

separate commits from cherry pick state

This commit is contained in:
Jesse Duffield
2020-03-26 21:11:21 +11:00
parent d027cf969c
commit 91a404d033
5 changed files with 74 additions and 51 deletions

View File

@ -5,9 +5,7 @@ type Commit struct {
Sha string Sha string
Name string Name string
Status string // one of "unpushed", "pushed", "merged", "rebasing" or "selected" Status string // one of "unpushed", "pushed", "merged", "rebasing" or "selected"
DisplayString string
Action string // one of "", "pick", "edit", "squash", "reword", "drop", "fixup" Action string // one of "", "pick", "edit", "squash", "reword", "drop", "fixup"
Copied bool // to know if this commit is ready to be cherry-picked somewhere
Tags []string Tags []string
ExtraInfo string // something like 'HEAD -> master, tag: v0.15.2' ExtraInfo string // something like 'HEAD -> master, tag: v0.15.2'
Author string Author string

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -72,7 +73,6 @@ func (c *CommitListBuilder) extractCommitFromLine(line string) *Commit {
return &Commit{ return &Commit{
Sha: sha, Sha: sha,
Name: message, Name: message,
DisplayString: line,
Tags: tags, Tags: tags,
ExtraInfo: extraInfo, ExtraInfo: extraInfo,
Date: date, Date: date,
@ -100,15 +100,20 @@ func (c *CommitListBuilder) GetCommits(limit bool) ([]*Commit, error) {
} }
unpushedCommits := c.getUnpushedCommits() unpushedCommits := c.getUnpushedCommits()
log := c.getLog(limit) cmd := c.getLogCmd(limit)
// now we can split it up and turn it into commits err = RunLineOutputCmd(cmd, func(line string) (bool, error) {
for _, line := range utils.SplitLines(log) {
commit := c.extractCommitFromLine(line) commit := c.extractCommitFromLine(line)
_, unpushed := unpushedCommits[commit.ShortSha()] _, unpushed := unpushedCommits[commit.ShortSha()]
commit.Status = map[bool]string{true: "unpushed", false: "pushed"}[unpushed] commit.Status = map[bool]string{true: "unpushed", false: "pushed"}[unpushed]
commits = append(commits, commit) commits = append(commits, commit)
return false, nil
})
if err != nil {
return nil, err
} }
if rebaseMode != "" { if rebaseMode != "" {
currentCommit := commits[len(rebasingCommits)] currentCommit := commits[len(rebasingCommits)]
blue := color.New(color.FgYellow) blue := color.New(color.FgYellow)
@ -121,11 +126,6 @@ func (c *CommitListBuilder) GetCommits(limit bool) ([]*Commit, error) {
return nil, err return nil, err
} }
commits, err = c.setCommitCherryPickStatuses(commits)
if err != nil {
return nil, err
}
for _, commit := range commits { for _, commit := range commits {
for _, entry := range c.DiffEntries { for _, entry := range c.DiffEntries {
if entry.Sha == commit.Sha { if entry.Sha == commit.Sha {
@ -267,17 +267,6 @@ func (c *CommitListBuilder) setCommitMergedStatuses(commits []*Commit) ([]*Commi
return commits, nil return commits, nil
} }
func (c *CommitListBuilder) setCommitCherryPickStatuses(commits []*Commit) ([]*Commit, error) {
for _, commit := range commits {
for _, cherryPickedCommit := range c.CherryPickedCommits {
if commit.Sha == cherryPickedCommit.Sha {
commit.Copied = true
}
}
}
return commits, nil
}
func (c *CommitListBuilder) getMergeBase() (string, error) { func (c *CommitListBuilder) getMergeBase() (string, error) {
currentBranch, _, err := c.GitCommand.CurrentBranchName() currentBranch, _, err := c.GitCommand.CurrentBranchName()
if err != nil { if err != nil {

View File

@ -1,6 +1,7 @@
package commands package commands
import ( import (
"bufio"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -425,3 +426,31 @@ func Kill(cmd *exec.Cmd) error {
} }
return cmd.Process.Kill() return cmd.Process.Kill()
} }
func RunLineOutputCmd(cmd *exec.Cmd, onLine func(line string) (bool, error)) error {
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
return err
}
scanner := bufio.NewScanner(stdoutPipe)
scanner.Split(bufio.ScanLines)
if err := cmd.Start(); err != nil {
return err
}
for scanner.Scan() {
line := scanner.Text()
stop, err := onLine(line)
if err != nil {
return err
}
if stop {
cmd.Process.Kill()
break
}
}
cmd.Wait()
return nil
}

View File

@ -351,14 +351,21 @@ func (gui *Gui) handleCopyCommit(g *gocui.Gui, v *gocui.View) error {
return gui.refreshCommits(gui.g) return gui.refreshCommits(gui.g)
} }
func (gui *Gui) cherryPickedCommitShaMap() map[string]bool {
commitShaMap := map[string]bool{}
for _, commit := range gui.State.CherryPickedCommits {
commitShaMap[commit.Sha] = true
}
return commitShaMap
}
func (gui *Gui) addCommitToCherryPickedCommits(index int) { func (gui *Gui) addCommitToCherryPickedCommits(index int) {
// not super happy with modifying the state of the Commits array here commitShaMap := gui.cherryPickedCommitShaMap()
// but the alternative would be very tricky commitShaMap[gui.State.Commits[index].Sha] = true
gui.State.Commits[index].Copied = true
newCommits := []*commands.Commit{} newCommits := []*commands.Commit{}
for _, commit := range gui.State.Commits { for _, commit := range gui.State.Commits {
if commit.Copied { if commitShaMap[commit.Sha] {
// duplicating just the things we need to put in the rebase TODO list // duplicating just the things we need to put in the rebase TODO list
newCommits = append(newCommits, &commands.Commit{Name: commit.Name, Sha: commit.Sha}) newCommits = append(newCommits, &commands.Commit{Name: commit.Name, Sha: commit.Sha})
} }
@ -368,13 +375,13 @@ func (gui *Gui) addCommitToCherryPickedCommits(index int) {
} }
func (gui *Gui) handleCopyCommitRange(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) handleCopyCommitRange(g *gocui.Gui, v *gocui.View) error {
// whenever I add a commit, I need to make sure I retain its order commitShaMap := gui.cherryPickedCommitShaMap()
// find the last commit that is copied that's above our position // find the last commit that is copied that's above our position
// if there are none, startIndex = 0 // if there are none, startIndex = 0
startIndex := 0 startIndex := 0
for index, commit := range gui.State.Commits[0:gui.State.Panels.Commits.SelectedLine] { for index, commit := range gui.State.Commits[0:gui.State.Panels.Commits.SelectedLine] {
if commit.Copied { if commitShaMap[commit.Sha] {
startIndex = index startIndex = index
} }
} }
@ -385,7 +392,7 @@ func (gui *Gui) handleCopyCommitRange(g *gocui.Gui, v *gocui.View) error {
gui.addCommitToCherryPickedCommits(index) gui.addCommitToCherryPickedCommits(index)
} }
return gui.refreshCommits(gui.g) return gui.renderBranchCommitsWithSelection()
} }
// HandlePasteCommits begins a cherry-pick rebase with the commits the user has copied // HandlePasteCommits begins a cherry-pick rebase with the commits the user has copied
@ -548,7 +555,7 @@ func (gui *Gui) renderBranchCommitsWithSelection() error {
commitsView := gui.getCommitsView() commitsView := gui.getCommitsView()
gui.refreshSelectedLine(&gui.State.Panels.Commits.SelectedLine, len(gui.State.Commits)) gui.refreshSelectedLine(&gui.State.Panels.Commits.SelectedLine, len(gui.State.Commits))
displayStrings := presentation.GetCommitListDisplayStrings(gui.State.Commits, gui.State.ScreenMode != SCREEN_NORMAL) displayStrings := presentation.GetCommitListDisplayStrings(gui.State.Commits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap())
gui.renderDisplayStrings(commitsView, displayStrings) gui.renderDisplayStrings(commitsView, displayStrings)
if gui.g.CurrentView() == commitsView && commitsView.Context == "branch-commits" { if gui.g.CurrentView() == commitsView && commitsView.Context == "branch-commits" {
if err := gui.handleCommitSelect(gui.g, commitsView); err != nil { if err := gui.handleCommitSelect(gui.g, commitsView); err != nil {

View File

@ -9,10 +9,10 @@ import (
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
func GetCommitListDisplayStrings(commits []*commands.Commit, fullDescription bool) [][]string { func GetCommitListDisplayStrings(commits []*commands.Commit, fullDescription bool, cherryPickedCommitShaMap map[string]bool) [][]string {
lines := make([][]string, len(commits)) lines := make([][]string, len(commits))
var displayFunc func(*commands.Commit) []string var displayFunc func(*commands.Commit, map[string]bool) []string
if fullDescription { if fullDescription {
displayFunc = getFullDescriptionDisplayStringsForCommit displayFunc = getFullDescriptionDisplayStringsForCommit
} else { } else {
@ -20,13 +20,13 @@ func GetCommitListDisplayStrings(commits []*commands.Commit, fullDescription boo
} }
for i := range commits { for i := range commits {
lines[i] = displayFunc(commits[i]) lines[i] = displayFunc(commits[i], cherryPickedCommitShaMap)
} }
return lines return lines
} }
func getFullDescriptionDisplayStringsForCommit(c *commands.Commit) []string { func getFullDescriptionDisplayStringsForCommit(c *commands.Commit, cherryPickedCommitShaMap map[string]bool) []string {
red := color.New(color.FgRed) red := color.New(color.FgRed)
yellow := color.New(color.FgYellow) yellow := color.New(color.FgYellow)
green := color.New(color.FgGreen) green := color.New(color.FgGreen)
@ -58,7 +58,7 @@ func getFullDescriptionDisplayStringsForCommit(c *commands.Commit) []string {
shaColor = defaultColor shaColor = defaultColor
} }
if c.Copied { if cherryPickedCommitShaMap[c.Sha] {
shaColor = copied shaColor = copied
} }
@ -77,7 +77,7 @@ func getFullDescriptionDisplayStringsForCommit(c *commands.Commit) []string {
return []string{shaColor.Sprint(c.ShortSha()), secondColumnString, yellow.Sprint(truncatedAuthor), tagString + defaultColor.Sprint(c.Name)} return []string{shaColor.Sprint(c.ShortSha()), secondColumnString, yellow.Sprint(truncatedAuthor), tagString + defaultColor.Sprint(c.Name)}
} }
func getDisplayStringsForCommit(c *commands.Commit) []string { func getDisplayStringsForCommit(c *commands.Commit, cherryPickedCommitShaMap map[string]bool) []string {
red := color.New(color.FgRed) red := color.New(color.FgRed)
yellow := color.New(color.FgYellow) yellow := color.New(color.FgYellow)
green := color.New(color.FgGreen) green := color.New(color.FgGreen)
@ -109,7 +109,7 @@ func getDisplayStringsForCommit(c *commands.Commit) []string {
shaColor = defaultColor shaColor = defaultColor
} }
if c.Copied { if cherryPickedCommitShaMap[c.Sha] {
shaColor = copied shaColor = copied
} }