1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-10 04:07:18 +02:00
lazygit/pkg/commands/commits.go

106 lines
3.4 KiB
Go
Raw Normal View History

2020-09-29 12:03:39 +02:00
package commands
import (
"fmt"
"strings"
"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-12-29 05:33:38 +02:00
return c.Cmd.New("git commit --allow-empty --amend --only -m " + c.OSCommand.Quote(name)).Run()
2020-09-29 12:03:39 +02:00
}
// ResetToCommit reset to commit
2021-12-07 12:59:36 +02:00
func (c *GitCommand) ResetToCommit(sha string, strength string, envVars []string) error {
2021-12-29 05:33:38 +02:00
return c.Cmd.New(fmt.Sprintf("git reset --%s %s", strength, sha)).
2021-12-07 12:59:36 +02:00
// prevents git from prompting us for input which would freeze the program
// TODO: see if this is actually needed here
AddEnvVars("GIT_TERMINAL_PROMPT=0").
2021-12-29 05:33:38 +02:00
AddEnvVars(envVars...).
Run()
2020-09-29 12:03:39 +02:00
}
2021-12-07 12:59:36 +02:00
func (c *GitCommand) CommitCmdObj(message string, flags string) oscommands.ICmdObj {
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))
}
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-12-29 05:33:38 +02:00
return c.Cmd.New(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) {
2021-12-29 05:33:38 +02:00
message, err := c.Cmd.New("git log -1 --pretty=%s").RunWithOutput()
2020-09-29 12:03:39 +02:00
return strings.TrimSpace(message), err
}
func (c *GitCommand) GetCommitMessage(commitSha string) (string, error) {
cmdStr := "git rev-list --format=%B --max-count=1 " + commitSha
2021-12-29 05:33:38 +02:00
messageWithHeader, err := c.Cmd.New(cmdStr).RunWithOutput()
2020-09-29 12:03:39 +02:00
message := strings.Join(strings.SplitAfter(messageWithHeader, "\n")[1:], "\n")
return strings.TrimSpace(message), err
}
2021-06-05 08:39:59 +02:00
func (c *GitCommand) GetCommitMessageFirstLine(sha string) (string, error) {
2021-12-29 05:33:38 +02:00
return c.Cmd.New(fmt.Sprintf("git show --no-patch --pretty=format:%%s %s", sha)).RunWithOutput()
2021-06-05 08:39:59 +02:00
}
2020-09-29 12:03:39 +02:00
// AmendHead amends HEAD with whatever is staged in your working tree
2021-04-10 03:40:42 +02:00
func (c *GitCommand) AmendHead() error {
2021-12-29 05:33:38 +02:00
return c.AmendHeadCmdObj().Run()
2021-04-10 03:40:42 +02:00
}
2020-09-29 12:03:39 +02:00
2021-12-07 12:59:36 +02:00
func (c *GitCommand) AmendHeadCmdObj() oscommands.ICmdObj {
2021-12-29 05:33:38 +02:00
return c.Cmd.New("git commit --amend --no-edit --allow-empty")
2020-09-29 12:03:39 +02:00
}
2021-12-07 12:59:36 +02:00
func (c *GitCommand) ShowCmdObj(sha string, filterPath string) oscommands.ICmdObj {
2021-12-29 02:41:33 +02:00
contextSize := c.UserConfig.Git.DiffContextSize
2020-09-29 12:03:39 +02:00
filterPathArg := ""
if filterPath != "" {
filterPathArg = fmt.Sprintf(" -- %s", c.OSCommand.Quote(filterPath))
}
2021-12-07 12:59:36 +02:00
cmdStr := fmt.Sprintf("git show --submodule --color=%s --unified=%d --no-renames --stat -p %s %s", c.colorArg(), contextSize, sha, filterPathArg)
2021-12-29 05:33:38 +02:00
return c.Cmd.New(cmdStr)
2020-09-29 12:03:39 +02:00
}
// Revert reverts the selected commit by sha
func (c *GitCommand) Revert(sha string) error {
2021-12-29 05:33:38 +02:00
return c.Cmd.New(fmt.Sprintf("git revert %s", sha)).Run()
2020-09-29 12:03:39 +02:00
}
2021-06-05 08:39:59 +02:00
func (c *GitCommand) RevertMerge(sha string, parentNumber int) error {
2021-12-29 05:33:38 +02:00
return c.Cmd.New(fmt.Sprintf("git revert %s -m %d", sha, parentNumber)).Run()
2021-06-05 08:39:59 +02:00
}
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
}
2021-12-29 05:33:38 +02:00
cmdObj, err := c.PrepareInteractiveRebaseCommand("HEAD", todo, false)
2020-09-29 12:03:39 +02:00
if err != nil {
return err
}
2021-12-29 05:33:38 +02:00
return cmdObj.Run()
2020-09-29 12:03:39 +02:00
}
// CreateFixupCommit creates a commit that fixes up a previous commit
func (c *GitCommand) CreateFixupCommit(sha string) error {
2021-12-29 05:33:38 +02:00
return c.Cmd.New(fmt.Sprintf("git commit --fixup=%s", sha)).Run()
2020-09-29 12:03:39 +02:00
}