1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-07 13:42:01 +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 {
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 {

View File

@ -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

View File

@ -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
}

10
main.go
View File

@ -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...)
}

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
# 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

View File

@ -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
}