Skip to content

Commit

Permalink
Always include atleast 2 commits when doing squash and fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
xdjinnx authored and jesseduffield committed Apr 10, 2019
1 parent 3e779bc commit 7ff07e1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
38 changes: 23 additions & 15 deletions pkg/commands/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/i18n/dutch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions pkg/i18n/polish.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 7ff07e1

Please sign in to comment.