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

107 lines
3.5 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-07 12:59:36 +02:00
return c.Run(c.NewCmdObj("git commit --allow-empty --amend --only -m " + c.OSCommand.Quote(name)))
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 {
cmdObj := c.NewCmdObj(fmt.Sprintf("git reset --%s %s", strength, sha)).
// 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").
AddEnvVars(envVars...)
return c.OSCommand.Run(cmdObj)
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-07 12:59:36 +02:00
return c.NewCmdObj(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-07 12:59:36 +02:00
message, err := c.RunWithOutput(c.NewCmdObj("git log -1 --pretty=%s"))
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-07 12:59:36 +02:00
messageWithHeader, err := c.RunWithOutput(c.NewCmdObj(cmdStr))
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-07 12:59:36 +02:00
return c.RunWithOutput(c.NewCmdObj(fmt.Sprintf("git show --no-patch --pretty=format:%%s %s", sha)))
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-07 12:59:36 +02:00
return c.OSCommand.Run(c.AmendHeadCmdObj())
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 {
return c.NewCmdObj("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-09-03 22:37:46 +02:00
contextSize := c.Config.GetUserConfig().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)
return c.NewCmdObj(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-07 12:59:36 +02:00
return c.Run(c.NewCmdObj(fmt.Sprintf("git revert %s", sha)))
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-07 12:59:36 +02:00
return c.Run(c.NewCmdObj(fmt.Sprintf("git revert %s -m %d", sha, parentNumber)))
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
}
cmd, err := c.PrepareInteractiveRebaseCommand("HEAD", todo, false)
if err != nil {
return err
}
2021-12-07 12:59:36 +02:00
return c.OSCommand.Run(cmd)
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-07 12:59:36 +02:00
return c.Run(c.NewCmdObj(fmt.Sprintf("git commit --fixup=%s", sha)))
2020-09-29 12:03:39 +02:00
}