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

clean up fixup code

reduce log clutter
add log dumping with spew
This commit is contained in:
Jesse Duffield 2018-08-11 15:04:02 +10:00
parent f1eaeec9ee
commit 73e740d1ba
7 changed files with 91 additions and 48 deletions

View File

@ -42,7 +42,6 @@ func refreshCommits(g *gocui.Gui) error {
func handleResetToCommit(g *gocui.Gui, commitView *gocui.View) error { func handleResetToCommit(g *gocui.Gui, commitView *gocui.View) error {
return createConfirmationPanel(g, commitView, "Reset To Commit", "Are you sure you want to reset to this commit?", func(g *gocui.Gui, v *gocui.View) error { return createConfirmationPanel(g, commitView, "Reset To Commit", "Are you sure you want to reset to this commit?", func(g *gocui.Gui, v *gocui.View) error {
commit, err := getSelectedCommit(g) commit, err := getSelectedCommit(g)
devLog(commit)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -106,15 +105,30 @@ func handleCommitSquashDown(g *gocui.Gui, v *gocui.View) error {
return handleCommitSelect(g, v) return handleCommitSelect(g, v)
} }
// TODO: move to files panel
func anyUnStagedChanges(files []GitFile) bool {
for _, file := range files {
if file.Tracked && file.HasUnstagedChanges {
return true
}
}
return false
}
func handleCommitFixup(g *gocui.Gui, v *gocui.View) error { func handleCommitFixup(g *gocui.Gui, v *gocui.View) error {
if len(state.Commits) == 1 { if len(state.Commits) == 1 {
return createErrorPanel(g, "You have no commits to squash with") return createErrorPanel(g, "You have no commits to squash with")
} }
objectLog(state.GitFiles)
if anyUnStagedChanges(state.GitFiles) {
return createErrorPanel(g, "Can't fixup while there are unstaged changes")
}
branch := state.Branches[0] branch := state.Branches[0]
commit, err := getSelectedCommit(g) commit, err := getSelectedCommit(g)
if err != nil { if err != nil {
return err return err
} }
createConfirmationPanel(g, v, "Fixup", "Are you sure you want to fixup this commit? The commit beneath will be squashed up into this one", func(g *gocui.Gui, v *gocui.View) error {
if output, err := gitSquashFixupCommit(branch.Name, commit.Sha); err != nil { if output, err := gitSquashFixupCommit(branch.Name, commit.Sha); err != nil {
return createErrorPanel(g, output) return createErrorPanel(g, output)
} }
@ -123,6 +137,8 @@ func handleCommitFixup(g *gocui.Gui, v *gocui.View) error {
} }
refreshStatus(g) refreshStatus(g)
return handleCommitSelect(g, v) return handleCommitSelect(g, v)
}, nil)
return nil
} }
func handleRenameCommit(g *gocui.Gui, v *gocui.View) error { func handleRenameCommit(g *gocui.Gui, v *gocui.View) error {

View File

@ -295,7 +295,6 @@ func refreshFiles(g *gocui.Gui) error {
} }
func pullFiles(g *gocui.Gui, v *gocui.View) error { func pullFiles(g *gocui.Gui, v *gocui.View) error {
devLog("pulling...")
createMessagePanel(g, v, "", "Pulling...") createMessagePanel(g, v, "", "Pulling...")
go func() { go func() {
if output, err := gitPull(); err != nil { if output, err := gitPull(); err != nil {
@ -304,7 +303,6 @@ func pullFiles(g *gocui.Gui, v *gocui.View) error {
closeConfirmationPrompt(g) closeConfirmationPrompt(g)
refreshCommits(g) refreshCommits(g)
refreshStatus(g) refreshStatus(g)
devLog("pulled.")
} }
refreshFiles(g) refreshFiles(g)
}() }()
@ -312,7 +310,6 @@ func pullFiles(g *gocui.Gui, v *gocui.View) error {
} }
func pushFiles(g *gocui.Gui, v *gocui.View) error { func pushFiles(g *gocui.Gui, v *gocui.View) error {
devLog("pushing...")
createMessagePanel(g, v, "", "Pushing...") createMessagePanel(g, v, "", "Pushing...")
go func() { go func() {
if output, err := gitPush(); err != nil { if output, err := gitPush(); err != nil {
@ -321,7 +318,6 @@ func pushFiles(g *gocui.Gui, v *gocui.View) error {
closeConfirmationPrompt(g) closeConfirmationPrompt(g)
refreshCommits(g) refreshCommits(g)
refreshStatus(g) refreshStatus(g)
devLog("pushed.")
} }
}() }()
return nil return nil

View File

@ -119,6 +119,12 @@ func mergeGitStatusFiles(oldGitFiles, newGitFiles []GitFile) []GitFile {
return result return result
} }
// only to be used when you're already in an error state
func runDirectCommandIgnoringError(command string) string {
output, _ := runDirectCommand(command)
return output
}
func runDirectCommand(command string) (string, error) { func runDirectCommand(command string) (string, error) {
commandLog(command) commandLog(command)
@ -212,13 +218,9 @@ func getGitStatusFiles() []GitFile {
Deleted: unstagedChange == "D" || stagedChange == "D", Deleted: unstagedChange == "D" || stagedChange == "D",
HasMergeConflicts: change == "UU", HasMergeConflicts: change == "UU",
} }
devLog("tracked", gitFile.Tracked)
devLog("hasUnstagedChanges", gitFile.HasUnstagedChanges)
devLog("HasStagedChanges", gitFile.HasStagedChanges)
devLog("DisplayString", gitFile.DisplayString)
gitFiles = append(gitFiles, gitFile) gitFiles = append(gitFiles, gitFile)
} }
devLog(gitFiles) objectLog(gitFiles)
return gitFiles return gitFiles
} }
@ -258,7 +260,6 @@ func sanitisedCommandOutput(output []byte, err error) (string, error) {
func runCommand(command string) (string, error) { func runCommand(command string) (string, error) {
commandLog(command) commandLog(command)
splitCmd := strings.Split(command, " ") splitCmd := strings.Split(command, " ")
devLog(splitCmd)
cmdOut, err := exec.Command(splitCmd[0], splitCmd[1:]...).CombinedOutput() cmdOut, err := exec.Command(splitCmd[0], splitCmd[1:]...).CombinedOutput()
return sanitisedCommandOutput(cmdOut, err) return sanitisedCommandOutput(cmdOut, err)
} }
@ -419,7 +420,6 @@ func unStageFile(file string, tracked bool) error {
} else { } else {
command = "git rm --cached " command = "git rm --cached "
} }
devLog(command)
_, err := runCommand(command + file) _, err := runCommand(command + file)
return err return err
} }
@ -488,35 +488,28 @@ func gitSquashPreviousTwoCommits(message string) (string, error) {
} }
func gitSquashFixupCommit(branchName string, shaValue string) (string, error) { func gitSquashFixupCommit(branchName string, shaValue string) (string, error) {
var err error
commands := []string{
"git checkout -q " + shaValue,
"git reset --soft " + shaValue + "^",
"git commit --amend -C " + shaValue + "^",
"git rebase --onto HEAD " + shaValue + " " + branchName,
}
ret := "" ret := ""
output, err := runDirectCommand("git checkout -q " + shaValue) for _, command := range commands {
devLog(command)
output, err := runDirectCommand(command)
ret += output ret += output
if err != nil { if err != nil {
goto FIXUP_ERROR devLog(ret)
break
}
} }
output, err = runDirectCommand("git reset --soft " + shaValue + "^")
ret += output
if err != nil { if err != nil {
goto FIXUP_ERROR // We are already in an error state here so we're just going to append
} // the output of these commands
output, err = runDirectCommand("git commit --amend -C " + shaValue + "^") ret += runDirectCommandIgnoringError("git branch -d " + shaValue)
ret += output ret += runDirectCommandIgnoringError("git checkout " + branchName)
if err != nil {
goto FIXUP_ERROR
}
output, err = runDirectCommand("git rebase --onto HEAD " + shaValue + " " + branchName)
ret += output
if err != nil {
goto FIXUP_ERROR
}
return ret, err
FIXUP_ERROR:
//Failed to perform rebase, back to the original branch
output2, err2 := runDirectCommand("git checkout " + branchName)
ret += output2
if err2 != nil {
return ret, err2
} }
return ret, err return ret, err
} }

10
main.go
View File

@ -11,6 +11,8 @@ import (
"os/user" "os/user"
"path/filepath" "path/filepath"
"github.com/davecgh/go-spew/spew"
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
git "gopkg.in/src-d/go-git.v4" git "gopkg.in/src-d/go-git.v4"
) )
@ -47,6 +49,14 @@ func devLog(objects ...interface{}) {
localLog("development.log", objects...) localLog("development.log", objects...)
} }
func objectLog(object interface{}) {
if !*debuggingFlag {
return
}
str := spew.Sdump(object)
localLog("development.log", str)
}
func commandLog(objects ...interface{}) { func commandLog(objects ...interface{}) {
localLog("commands.log", objects...) localLog("commands.log", objects...)
} }

28
test/lots_of_commits.sh Executable file
View File

@ -0,0 +1,28 @@
#!/bin/bash
# this script makes a repo with lots of commits
# call this command from the test directory:
# ./lots_of_commits.sh; cd testrepo; gg; cd ..
# -e means exit if something fails
# -x means print out simple commands before running them
set -ex
reponame="testrepo"
rm -rf ${reponame}
mkdir ${reponame}
cd ${reponame}
git init
i=2
end=100
while [ $i -le $end ]; do
echo "file${i}" > file${i}
git add file${i}
git commit -m file${i}
i=$(($i+1))
done

View File

@ -4,7 +4,7 @@
# on the master branch and if we try and merge master we get a merge conflict # on the master branch and if we try and merge master we get a merge conflict
# call this command from the test directory: # call this command from the test directory:
# ./generate_basic_repo.sh; cd testrepo; gg; cd .. # ./merge_conflict.sh; cd testrepo; gg; cd ..
# -e means exit if something fails # -e means exit if something fails
# -x means print out simple commands before running them # -x means print out simple commands before running them

View File

@ -107,7 +107,7 @@ func switchFocus(g *gocui.Gui, oldView, newView *gocui.View) error {
state.PreviousView = oldView.Name() state.PreviousView = oldView.Name()
} }
newView.Highlight = true newView.Highlight = true
devLog(newView.Name()) devLog("new focused view is " + newView.Name())
if _, err := g.SetCurrentView(newView.Name()); err != nil { if _, err := g.SetCurrentView(newView.Name()); err != nil {
return err return err
} }