mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-01-12 04:23:03 +02:00
Add option to split patch into a new commit
Add GetHeadCommitMessage to read the subject of the HEAD commit Create PullPatchIntoNewCommit based heavily on PullPatchIntoIndex to split the current patch from its commit and apply it in a separate commit immediately after. WIP to Squash - Fill format string with format string WIP
This commit is contained in:
parent
3dd33b65a0
commit
7ed8ee160d
@ -441,6 +441,13 @@ func (c *GitCommand) Commit(message string, flags string) (*exec.Cmd, error) {
|
|||||||
return nil, c.OSCommand.RunCommand(command)
|
return nil, c.OSCommand.RunCommand(command)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the subject of the HEAD commit
|
||||||
|
func (c *GitCommand) GetHeadCommitMessage() (string, error) {
|
||||||
|
cmdStr := "git log -1 --pretty=%s"
|
||||||
|
message, err := c.OSCommand.RunCommandWithOutput(cmdStr)
|
||||||
|
return strings.TrimSpace(message), err
|
||||||
|
}
|
||||||
|
|
||||||
// AmendHead amends HEAD with whatever is staged in your working tree
|
// AmendHead amends HEAD with whatever is staged in your working tree
|
||||||
func (c *GitCommand) AmendHead() (*exec.Cmd, error) {
|
func (c *GitCommand) AmendHead() (*exec.Cmd, error) {
|
||||||
command := "git commit --amend --no-edit --allow-empty"
|
command := "git commit --amend --no-edit --allow-empty"
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import "github.com/go-errors/errors"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/go-errors/errors"
|
||||||
|
)
|
||||||
|
|
||||||
// DeletePatchesFromCommit applies a patch in reverse for a commit
|
// DeletePatchesFromCommit applies a patch in reverse for a commit
|
||||||
func (c *GitCommand) DeletePatchesFromCommit(commits []*Commit, commitIndex int, p *PatchManager) error {
|
func (c *GitCommand) DeletePatchesFromCommit(commits []*Commit, commitIndex int, p *PatchManager) error {
|
||||||
@ -183,3 +186,43 @@ func (c *GitCommand) PullPatchIntoIndex(commits []*Commit, commitIdx int, p *Pat
|
|||||||
|
|
||||||
return c.GenericMerge("rebase", "continue")
|
return c.GenericMerge("rebase", "continue")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *GitCommand) PullPatchIntoNewCommit(commits []*Commit, commitIdx int, p *PatchManager) error {
|
||||||
|
if err := c.BeginInteractiveRebaseForCommit(commits, commitIdx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := p.ApplyPatches(true); err != nil {
|
||||||
|
if err := c.GenericMerge("rebase", "abort"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// amend the commit
|
||||||
|
if _, err := c.AmendHead(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// add patches to index
|
||||||
|
if err := p.ApplyPatches(false); err != nil {
|
||||||
|
if err := c.GenericMerge("rebase", "abort"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
head_message, _ := c.GetHeadCommitMessage()
|
||||||
|
new_message := fmt.Sprintf("Split from \"%s\"", head_message)
|
||||||
|
_, err := c.Commit(new_message, "")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.onSuccessfulContinue != nil {
|
||||||
|
return errors.New("You are midway through another rebase operation. Please abort to start again")
|
||||||
|
}
|
||||||
|
|
||||||
|
c.PatchManager.Reset()
|
||||||
|
return c.GenericMerge("rebase", "continue")
|
||||||
|
}
|
||||||
|
@ -20,6 +20,10 @@ func (gui *Gui) handleCreatePatchOptionsMenu(g *gocui.Gui, v *gocui.View) error
|
|||||||
displayString: "pull patch out into index",
|
displayString: "pull patch out into index",
|
||||||
onPress: gui.handlePullPatchIntoWorkingTree,
|
onPress: gui.handlePullPatchIntoWorkingTree,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
displayString: "pull patch into new commit",
|
||||||
|
onPress: gui.handlePullPatchIntoNewCommit,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
displayString: "apply patch",
|
displayString: "apply patch",
|
||||||
onPress: func() error { return gui.handleApplyPatch(false) },
|
onPress: func() error { return gui.handleApplyPatch(false) },
|
||||||
@ -137,6 +141,22 @@ func (gui *Gui) handlePullPatchIntoWorkingTree() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gui *Gui) handlePullPatchIntoNewCommit() error {
|
||||||
|
if ok, err := gui.validateNormalWorkingTreeState(); !ok {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := gui.returnFocusFromLineByLinePanelIfNecessary(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return gui.WithWaitingStatus(gui.Tr.SLocalize("RebasingStatus"), func() error {
|
||||||
|
commitIndex := gui.getPatchCommitIndex()
|
||||||
|
err := gui.GitCommand.PullPatchIntoNewCommit(gui.State.Commits, commitIndex, gui.GitCommand.PatchManager)
|
||||||
|
return gui.handleGenericMergeCommandResult(err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleApplyPatch(reverse bool) error {
|
func (gui *Gui) handleApplyPatch(reverse bool) error {
|
||||||
if err := gui.returnFocusFromLineByLinePanelIfNecessary(); err != nil {
|
if err := gui.returnFocusFromLineByLinePanelIfNecessary(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
Loading…
Reference in New Issue
Block a user