2020-09-29 12:03:39 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
2022-01-02 01:34:33 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/common"
|
2020-09-29 12:03:39 +02:00
|
|
|
)
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
type CommitCommands struct {
|
|
|
|
*common.Common
|
|
|
|
|
|
|
|
cmd oscommands.ICmdObjBuilder
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCommitCommands(
|
|
|
|
common *common.Common,
|
|
|
|
cmd oscommands.ICmdObjBuilder,
|
|
|
|
) *CommitCommands {
|
|
|
|
return &CommitCommands{
|
|
|
|
Common: common,
|
|
|
|
cmd: cmd,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RewordLastCommit renames the topmost commit with the given name
|
|
|
|
func (self *CommitCommands) RewordLastCommit(name string) error {
|
|
|
|
return self.cmd.New("git commit --allow-empty --amend --only -m " + self.cmd.Quote(name)).Run()
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResetToCommit reset to commit
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *CommitCommands) ResetToCommit(sha string, strength string, envVars []string) error {
|
|
|
|
return self.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
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *CommitCommands) CommitCmdObj(message string, flags string) oscommands.ICmdObj {
|
2020-10-12 23:16:24 +02:00
|
|
|
splitMessage := strings.Split(message, "\n")
|
|
|
|
lineArgs := ""
|
|
|
|
for _, line := range splitMessage {
|
2022-01-02 01:34:33 +02:00
|
|
|
lineArgs += fmt.Sprintf(" -m %s", self.cmd.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
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
return self.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
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *CommitCommands) GetHeadCommitMessage() (string, error) {
|
|
|
|
message, err := self.cmd.New("git log -1 --pretty=%s").DontLog().RunWithOutput()
|
2020-09-29 12:03:39 +02:00
|
|
|
return strings.TrimSpace(message), err
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *CommitCommands) GetCommitMessage(commitSha string) (string, error) {
|
2020-09-29 12:03:39 +02:00
|
|
|
cmdStr := "git rev-list --format=%B --max-count=1 " + commitSha
|
2022-01-02 01:34:33 +02:00
|
|
|
messageWithHeader, err := self.cmd.New(cmdStr).DontLog().RunWithOutput()
|
2020-09-29 12:03:39 +02:00
|
|
|
message := strings.Join(strings.SplitAfter(messageWithHeader, "\n")[1:], "\n")
|
|
|
|
return strings.TrimSpace(message), err
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *CommitCommands) GetCommitMessageFirstLine(sha string) (string, error) {
|
|
|
|
return self.cmd.New(fmt.Sprintf("git show --no-patch --pretty=format:%%s %s", sha)).DontLog().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
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *CommitCommands) AmendHead() error {
|
|
|
|
return self.AmendHeadCmdObj().Run()
|
2021-04-10 03:40:42 +02:00
|
|
|
}
|
2020-09-29 12:03:39 +02:00
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *CommitCommands) AmendHeadCmdObj() oscommands.ICmdObj {
|
|
|
|
return self.cmd.New("git commit --amend --no-edit --allow-empty")
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *CommitCommands) ShowCmdObj(sha string, filterPath string) oscommands.ICmdObj {
|
|
|
|
contextSize := self.UserConfig.Git.DiffContextSize
|
2020-09-29 12:03:39 +02:00
|
|
|
filterPathArg := ""
|
|
|
|
if filterPath != "" {
|
2022-01-02 01:34:33 +02:00
|
|
|
filterPathArg = fmt.Sprintf(" -- %s", self.cmd.Quote(filterPath))
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
2021-12-07 12:59:36 +02:00
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
cmdStr := fmt.Sprintf("git show --submodule --color=%s --unified=%d --no-renames --stat -p %s %s", self.UserConfig.Git.Paging.ColorArg, contextSize, sha, filterPathArg)
|
|
|
|
return self.cmd.New(cmdStr).DontLog()
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Revert reverts the selected commit by sha
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *CommitCommands) Revert(sha string) error {
|
|
|
|
return self.cmd.New(fmt.Sprintf("git revert %s", sha)).Run()
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *CommitCommands) RevertMerge(sha string, parentNumber int) error {
|
|
|
|
return self.cmd.New(fmt.Sprintf("git revert %s -m %d", sha, parentNumber)).Run()
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFixupCommit creates a commit that fixes up a previous commit
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *CommitCommands) CreateFixupCommit(sha string) error {
|
|
|
|
return self.cmd.New(fmt.Sprintf("git commit --fixup=%s", sha)).Run()
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|