From 7ff07e145474b1ef3465db94dd57c0aa82353bfb Mon Sep 17 00:00:00 2001 From: Peter Lundberg Date: Sun, 7 Apr 2019 01:10:58 +0200 Subject: [PATCH] Always include atleast 2 commits when doing squash and fixup --- pkg/commands/git.go | 38 +++++++++++++++++++++++--------------- pkg/i18n/dutch.go | 3 +++ pkg/i18n/english.go | 3 +++ pkg/i18n/polish.go | 3 +++ 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 9024e1479..e3fe4ba74 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -613,12 +613,12 @@ func (c *GitCommand) GenericMerge(commandType string, command string) error { } func (c *GitCommand) RewordCommit(commits []*Commit, index int) (*exec.Cmd, error) { - todo, err := c.GenerateGenericRebaseTodo(commits, index, "reword") + todo, sha, err := c.GenerateGenericRebaseTodo(commits, index, "reword") if err != nil { return nil, err } - return c.PrepareInteractiveRebaseCommand(commits[index+1].Sha, todo, false) + return c.PrepareInteractiveRebaseCommand(sha, todo, false) } func (c *GitCommand) MoveCommitDown(commits []*Commit, index int) error { @@ -643,12 +643,12 @@ func (c *GitCommand) MoveCommitDown(commits []*Commit, index int) error { } func (c *GitCommand) InteractiveRebase(commits []*Commit, index int, action string) error { - todo, err := c.GenerateGenericRebaseTodo(commits, index, action) + todo, sha, err := c.GenerateGenericRebaseTodo(commits, index, action) if err != nil { return err } - cmd, err := c.PrepareInteractiveRebaseCommand(commits[index+1].Sha, todo, true) + cmd, err := c.PrepareInteractiveRebaseCommand(sha, todo, true) if err != nil { return err } @@ -702,21 +702,31 @@ func (c *GitCommand) SoftReset(baseSha string) error { return c.OSCommand.RunCommand("git reset --soft " + baseSha) } -func (c *GitCommand) GenerateGenericRebaseTodo(commits []*Commit, index int, action string) (string, error) { - if len(commits) <= index+1 { - // assuming they aren't picking the bottom commit - return "", errors.New(c.Tr.SLocalize("CannotRebaseOntoFirstCommit")) +func (c *GitCommand) GenerateGenericRebaseTodo(commits []*Commit, actionIndex int, action string) (string, string, error) { + baseIndex := actionIndex + 1 + + if len(commits) <= baseIndex { + return "", "", errors.New(c.Tr.SLocalize("CannotRebaseOntoFirstCommit")) + } + + if action == "squash" || action == "fixup" { + baseIndex++ + + if len(commits) <= baseIndex { + return "", "", errors.New(c.Tr.SLocalize("CannotSquashOntoSecondCommit")) + } } todo := "" - for i, commit := range commits[0 : index+1] { + for i, commit := range commits[0:baseIndex] { a := "pick" - if i == index { + if i == actionIndex { a = action } todo = a + " " + commit.Sha + " " + commit.Name + "\n" + todo } - return todo, nil + + return todo, commits[baseIndex].Sha, nil } // AmendTo amends the given commit with whatever files are staged @@ -845,14 +855,12 @@ func (c *GitCommand) DiscardOldFileChanges(commits []*Commit, commitIndex int, f return errors.New(c.Tr.SLocalize("DisabledForGPG")) } - commitSha := commits[commitIndex].Sha - - todo, err := c.GenerateGenericRebaseTodo(commits, commitIndex, "edit") + todo, sha, err := c.GenerateGenericRebaseTodo(commits, commitIndex, "edit") if err != nil { return err } - cmd, err := c.PrepareInteractiveRebaseCommand(commitSha+"^", todo, true) + cmd, err := c.PrepareInteractiveRebaseCommand(sha+"^", todo, true) if err != nil { return err } diff --git a/pkg/i18n/dutch.go b/pkg/i18n/dutch.go index a7c6efd23..3da767bf3 100644 --- a/pkg/i18n/dutch.go +++ b/pkg/i18n/dutch.go @@ -565,6 +565,9 @@ func addDutch(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "CannotRebaseOntoFirstCommit", Other: "You cannot interactive rebase onto the first commit", + }, &i18n.Message{ + ID: "CannotSquashOntoSecondCommit", + Other: "You cannot squash/fixup onto the second commit", }, &i18n.Message{ ID: "Donate", Other: "Donate", diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index b64cc582f..3b61ac3a7 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -588,6 +588,9 @@ func addEnglish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "CannotRebaseOntoFirstCommit", Other: "You cannot interactive rebase onto the first commit", + }, &i18n.Message{ + ID: "CannotSquashOntoSecondCommit", + Other: "You cannot squash/fixup onto the second commit", }, &i18n.Message{ ID: "Donate", Other: "Donate", diff --git a/pkg/i18n/polish.go b/pkg/i18n/polish.go index fc17323b9..372f4c090 100644 --- a/pkg/i18n/polish.go +++ b/pkg/i18n/polish.go @@ -548,6 +548,9 @@ func addPolish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "CannotRebaseOntoFirstCommit", Other: "You cannot interactive rebase onto the first commit", + }, &i18n.Message{ + ID: "CannotSquashOntoSecondCommit", + Other: "You cannot squash/fixup onto the second commit", }, &i18n.Message{ ID: "Donate", Other: "Donate",