From 73e740d1ba45e6c01988953234f8e217b811d1dc Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 11 Aug 2018 15:04:02 +1000 Subject: [PATCH] clean up fixup code reduce log clutter add log dumping with spew --- commits_panel.go | 34 ++++++++--- files_panel.go | 4 -- gitcommands.go | 59 ++++++++----------- main.go | 10 ++++ test/lots_of_commits.sh | 28 +++++++++ ...nerate_basic_repo.sh => merge_conflict.sh} | 2 +- view_helpers.go | 2 +- 7 files changed, 91 insertions(+), 48 deletions(-) create mode 100755 test/lots_of_commits.sh rename test/{generate_basic_repo.sh => merge_conflict.sh} (95%) diff --git a/commits_panel.go b/commits_panel.go index b222d346c..2b9ccbf87 100644 --- a/commits_panel.go +++ b/commits_panel.go @@ -42,7 +42,6 @@ func refreshCommits(g *gocui.Gui) 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 { commit, err := getSelectedCommit(g) - devLog(commit) if err != nil { panic(err) } @@ -106,23 +105,40 @@ func handleCommitSquashDown(g *gocui.Gui, v *gocui.View) error { 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 { if len(state.Commits) == 1 { 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] commit, err := getSelectedCommit(g) if err != nil { return err } - if output, err := gitSquashFixupCommit(branch.Name, commit.Sha); err != nil { - return createErrorPanel(g, output) - } - if err := refreshCommits(g); err != nil { - panic(err) - } - refreshStatus(g) - return handleCommitSelect(g, v) + 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 { + return createErrorPanel(g, output) + } + if err := refreshCommits(g); err != nil { + panic(err) + } + refreshStatus(g) + return handleCommitSelect(g, v) + }, nil) + return nil } func handleRenameCommit(g *gocui.Gui, v *gocui.View) error { diff --git a/files_panel.go b/files_panel.go index 32ed36ad4..e645c6cb6 100644 --- a/files_panel.go +++ b/files_panel.go @@ -295,7 +295,6 @@ func refreshFiles(g *gocui.Gui) error { } func pullFiles(g *gocui.Gui, v *gocui.View) error { - devLog("pulling...") createMessagePanel(g, v, "", "Pulling...") go func() { if output, err := gitPull(); err != nil { @@ -304,7 +303,6 @@ func pullFiles(g *gocui.Gui, v *gocui.View) error { closeConfirmationPrompt(g) refreshCommits(g) refreshStatus(g) - devLog("pulled.") } refreshFiles(g) }() @@ -312,7 +310,6 @@ func pullFiles(g *gocui.Gui, v *gocui.View) error { } func pushFiles(g *gocui.Gui, v *gocui.View) error { - devLog("pushing...") createMessagePanel(g, v, "", "Pushing...") go func() { if output, err := gitPush(); err != nil { @@ -321,7 +318,6 @@ func pushFiles(g *gocui.Gui, v *gocui.View) error { closeConfirmationPrompt(g) refreshCommits(g) refreshStatus(g) - devLog("pushed.") } }() return nil diff --git a/gitcommands.go b/gitcommands.go index 59331ad53..3db1df1ee 100644 --- a/gitcommands.go +++ b/gitcommands.go @@ -119,6 +119,12 @@ func mergeGitStatusFiles(oldGitFiles, newGitFiles []GitFile) []GitFile { 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) { commandLog(command) @@ -212,13 +218,9 @@ func getGitStatusFiles() []GitFile { Deleted: unstagedChange == "D" || stagedChange == "D", HasMergeConflicts: change == "UU", } - devLog("tracked", gitFile.Tracked) - devLog("hasUnstagedChanges", gitFile.HasUnstagedChanges) - devLog("HasStagedChanges", gitFile.HasStagedChanges) - devLog("DisplayString", gitFile.DisplayString) gitFiles = append(gitFiles, gitFile) } - devLog(gitFiles) + objectLog(gitFiles) return gitFiles } @@ -258,7 +260,6 @@ func sanitisedCommandOutput(output []byte, err error) (string, error) { func runCommand(command string) (string, error) { commandLog(command) splitCmd := strings.Split(command, " ") - devLog(splitCmd) cmdOut, err := exec.Command(splitCmd[0], splitCmd[1:]...).CombinedOutput() return sanitisedCommandOutput(cmdOut, err) } @@ -419,7 +420,6 @@ func unStageFile(file string, tracked bool) error { } else { command = "git rm --cached " } - devLog(command) _, err := runCommand(command + file) return err } @@ -488,35 +488,28 @@ func gitSquashPreviousTwoCommits(message 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 := "" - output, err := runDirectCommand("git checkout -q " + shaValue) - ret += output - if err != nil { - goto FIXUP_ERROR + for _, command := range commands { + devLog(command) + output, err := runDirectCommand(command) + ret += output + if err != nil { + devLog(ret) + break + } } - output, err = runDirectCommand("git reset --soft " + shaValue + "^") - ret += output if err != nil { - goto FIXUP_ERROR - } - output, err = runDirectCommand("git commit --amend -C " + shaValue + "^") - ret += output - 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 + // We are already in an error state here so we're just going to append + // the output of these commands + ret += runDirectCommandIgnoringError("git branch -d " + shaValue) + ret += runDirectCommandIgnoringError("git checkout " + branchName) } return ret, err } diff --git a/main.go b/main.go index c918c7348..426f47eee 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,8 @@ import ( "os/user" "path/filepath" + "github.com/davecgh/go-spew/spew" + "github.com/jesseduffield/gocui" git "gopkg.in/src-d/go-git.v4" ) @@ -47,6 +49,14 @@ func devLog(objects ...interface{}) { localLog("development.log", objects...) } +func objectLog(object interface{}) { + if !*debuggingFlag { + return + } + str := spew.Sdump(object) + localLog("development.log", str) +} + func commandLog(objects ...interface{}) { localLog("commands.log", objects...) } diff --git a/test/lots_of_commits.sh b/test/lots_of_commits.sh new file mode 100755 index 000000000..d85034f4e --- /dev/null +++ b/test/lots_of_commits.sh @@ -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 diff --git a/test/generate_basic_repo.sh b/test/merge_conflict.sh similarity index 95% rename from test/generate_basic_repo.sh rename to test/merge_conflict.sh index ef04333c7..9bd12bf56 100755 --- a/test/generate_basic_repo.sh +++ b/test/merge_conflict.sh @@ -4,7 +4,7 @@ # on the master branch and if we try and merge master we get a merge conflict # 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 # -x means print out simple commands before running them diff --git a/view_helpers.go b/view_helpers.go index bb8b86da7..0d7d7618c 100644 --- a/view_helpers.go +++ b/view_helpers.go @@ -107,7 +107,7 @@ func switchFocus(g *gocui.Gui, oldView, newView *gocui.View) error { state.PreviousView = oldView.Name() } newView.Highlight = true - devLog(newView.Name()) + devLog("new focused view is " + newView.Name()) if _, err := g.SetCurrentView(newView.Name()); err != nil { return err }