2020-09-29 12:03:39 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-09-29 12:28:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2020-09-29 12:03:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RenameCommit renames the topmost commit with the given name
|
|
|
|
func (c *GitCommand) RenameCommit(name string) error {
|
2021-04-06 09:58:48 +02:00
|
|
|
return c.RunCommand("git commit --allow-empty --amend --only -m %s", c.OSCommand.Quote(name))
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResetToCommit reset to commit
|
|
|
|
func (c *GitCommand) ResetToCommit(sha string, strength string, options oscommands.RunCommandOptions) error {
|
|
|
|
return c.OSCommand.RunCommandWithOptions(fmt.Sprintf("git reset --%s %s", strength, sha), options)
|
|
|
|
}
|
|
|
|
|
2021-04-10 03:40:42 +02:00
|
|
|
func (c *GitCommand) CommitCmdStr(message string, flags string) string {
|
2020-10-12 23:16:24 +02:00
|
|
|
splitMessage := strings.Split(message, "\n")
|
|
|
|
lineArgs := ""
|
|
|
|
for _, line := range splitMessage {
|
2021-03-01 16:16:48 +02:00
|
|
|
lineArgs += fmt.Sprintf(" -m %s", c.OSCommand.Quote(line))
|
2020-10-12 23:16:24 +02:00
|
|
|
}
|
|
|
|
|
2021-04-10 03:40:42 +02:00
|
|
|
flagsStr := ""
|
|
|
|
if flags != "" {
|
|
|
|
flagsStr = fmt.Sprintf(" %s", flags)
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2021-04-10 03:40:42 +02:00
|
|
|
return fmt.Sprintf("git commit%s%s", flagsStr, lineArgs)
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GitCommand) GetCommitMessage(commitSha string) (string, error) {
|
|
|
|
cmdStr := "git rev-list --format=%B --max-count=1 " + commitSha
|
|
|
|
messageWithHeader, err := c.OSCommand.RunCommandWithOutput(cmdStr)
|
|
|
|
message := strings.Join(strings.SplitAfter(messageWithHeader, "\n")[1:], "\n")
|
|
|
|
return strings.TrimSpace(message), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// AmendHead amends HEAD with whatever is staged in your working tree
|
2021-04-10 03:40:42 +02:00
|
|
|
func (c *GitCommand) AmendHead() error {
|
|
|
|
return c.OSCommand.RunCommand(c.AmendHeadCmdStr())
|
|
|
|
}
|
2020-09-29 12:03:39 +02:00
|
|
|
|
2021-04-10 03:40:42 +02:00
|
|
|
func (c *GitCommand) AmendHeadCmdStr() string {
|
|
|
|
return "git commit --amend --no-edit --allow-empty"
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GitCommand) ShowCmdStr(sha string, filterPath string) string {
|
|
|
|
filterPathArg := ""
|
|
|
|
if filterPath != "" {
|
|
|
|
filterPathArg = fmt.Sprintf(" -- %s", c.OSCommand.Quote(filterPath))
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("git show --submodule --color=%s --no-renames --stat -p %s %s", c.colorArg(), sha, filterPathArg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revert reverts the selected commit by sha
|
|
|
|
func (c *GitCommand) Revert(sha string) error {
|
2021-04-05 13:08:33 +02:00
|
|
|
return c.RunCommand("git revert %s", sha)
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CherryPickCommits begins an interactive rebase with the given shas being cherry picked onto HEAD
|
|
|
|
func (c *GitCommand) CherryPickCommits(commits []*models.Commit) error {
|
|
|
|
todo := ""
|
|
|
|
for _, commit := range commits {
|
|
|
|
todo = "pick " + commit.Sha + " " + commit.Name + "\n" + todo
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd, err := c.PrepareInteractiveRebaseCommand("HEAD", todo, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.OSCommand.RunPreparedCommand(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFixupCommit creates a commit that fixes up a previous commit
|
|
|
|
func (c *GitCommand) CreateFixupCommit(sha string) error {
|
2021-04-05 13:08:33 +02:00
|
|
|
return c.RunCommand("git commit --fixup=%s", sha)
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|