2022-01-08 05:00:36 +02:00
|
|
|
package git_commands
|
2020-09-29 12:03:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-05-01 06:14:29 +02:00
|
|
|
"github.com/go-errors/errors"
|
2020-09-29 12:03:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
|
|
)
|
|
|
|
|
2023-01-21 13:38:14 +02:00
|
|
|
var ErrInvalidCommitIndex = errors.New("invalid commit index")
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
type CommitCommands struct {
|
2022-01-18 12:26:21 +02:00
|
|
|
*GitCommon
|
2022-01-02 01:34:33 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
func NewCommitCommands(gitCommon *GitCommon) *CommitCommands {
|
2022-01-02 01:34:33 +02:00
|
|
|
return &CommitCommands{
|
2022-01-18 12:26:21 +02:00
|
|
|
GitCommon: gitCommon,
|
2022-01-02 01:34:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 20:22:11 +02:00
|
|
|
// ResetAuthor resets the author of the topmost commit
|
2022-04-22 16:01:30 +02:00
|
|
|
func (self *CommitCommands) ResetAuthor() error {
|
2022-05-07 16:19:42 +02:00
|
|
|
return self.cmd.New("git commit --allow-empty --only --no-edit --amend --reset-author").Run()
|
2022-04-22 16:01:30 +02:00
|
|
|
}
|
|
|
|
|
2022-05-08 13:05:01 +02:00
|
|
|
// Sets the commit's author to the supplied value. Value is expected to be of the form 'Name <Email>'
|
|
|
|
func (self *CommitCommands) SetAuthor(value string) error {
|
|
|
|
commandStr := fmt.Sprintf("git commit --allow-empty --only --no-edit --amend --author=%s", self.cmd.Quote(value))
|
|
|
|
return self.cmd.New(commandStr).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-07 10:19:24 +02:00
|
|
|
func (self *CommitCommands) CommitCmdObj(message string) oscommands.ICmdObj {
|
2023-01-21 13:38:14 +02:00
|
|
|
messageArgs := self.commitMessageArgs(message)
|
2020-10-12 23:16:24 +02:00
|
|
|
|
2022-01-07 10:19:24 +02:00
|
|
|
skipHookPrefix := self.UserConfig.Git.SkipHookPrefix
|
|
|
|
noVerifyFlag := ""
|
|
|
|
if skipHookPrefix != "" && strings.HasPrefix(message, skipHookPrefix) {
|
|
|
|
noVerifyFlag = " --no-verify"
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2023-01-21 13:38:14 +02:00
|
|
|
return self.cmd.New(fmt.Sprintf("git commit%s%s%s", noVerifyFlag, self.signoffFlag(), messageArgs))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RewordLastCommit rewords the topmost commit with the given message
|
|
|
|
func (self *CommitCommands) RewordLastCommit(message string) error {
|
|
|
|
messageArgs := self.commitMessageArgs(message)
|
|
|
|
return self.cmd.New(fmt.Sprintf("git commit --allow-empty --amend --only%s", messageArgs)).Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitCommands) commitMessageArgs(message string) string {
|
|
|
|
msg, description, _ := strings.Cut(message, "\n")
|
|
|
|
descriptionArgs := ""
|
|
|
|
if description != "" {
|
|
|
|
descriptionArgs = fmt.Sprintf(" -m %s", self.cmd.Quote(description))
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(" -m %s%s", self.cmd.Quote(msg), descriptionArgs)
|
2022-01-07 10:19:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// runs git commit without the -m argument meaning it will invoke the user's editor
|
|
|
|
func (self *CommitCommands) CommitEditorCmdObj() oscommands.ICmdObj {
|
2023-01-01 02:31:46 +02:00
|
|
|
return self.cmd.New(fmt.Sprintf("git commit%s%s", self.signoffFlag(), self.verboseFlag()))
|
2022-01-07 10:19:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitCommands) signoffFlag() string {
|
|
|
|
if self.UserConfig.Git.Commit.SignOff {
|
|
|
|
return " --signoff"
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2023-01-01 02:31:46 +02:00
|
|
|
func (self *CommitCommands) verboseFlag() string {
|
2023-01-01 17:49:59 +02:00
|
|
|
switch self.config.UserConfig.Git.Commit.Verbose {
|
|
|
|
case "always":
|
2023-01-01 02:31:46 +02:00
|
|
|
return " --verbose"
|
2023-01-01 17:49:59 +02:00
|
|
|
case "never":
|
|
|
|
return " --no-verbose"
|
|
|
|
default:
|
2023-01-01 02:31:46 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
2022-03-21 14:29:34 +02:00
|
|
|
message := strings.Join(strings.SplitAfter(messageWithHeader, "\n")[1:], "")
|
2020-09-29 12:03:39 +02:00
|
|
|
return strings.TrimSpace(message), err
|
|
|
|
}
|
|
|
|
|
2022-03-24 22:04:33 +02:00
|
|
|
func (self *CommitCommands) GetCommitDiff(commitSha string) (string, error) {
|
|
|
|
cmdStr := "git show --no-color " + commitSha
|
|
|
|
diff, err := self.cmd.New(cmdStr).DontLog().RunWithOutput()
|
|
|
|
return diff, err
|
|
|
|
}
|
|
|
|
|
2022-05-01 06:14:29 +02:00
|
|
|
type Author struct {
|
|
|
|
Name string
|
|
|
|
Email string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitCommands) GetCommitAuthor(commitSha string) (Author, error) {
|
2022-05-07 11:06:51 +02:00
|
|
|
cmdStr := "git show --no-patch --pretty=format:'%an%x00%ae' " + commitSha
|
2022-05-01 06:14:29 +02:00
|
|
|
output, err := self.cmd.New(cmdStr).DontLog().RunWithOutput()
|
|
|
|
if err != nil {
|
|
|
|
return Author{}, err
|
|
|
|
}
|
|
|
|
|
2022-05-07 11:06:51 +02:00
|
|
|
split := strings.SplitN(strings.TrimSpace(output), "\x00", 2)
|
2022-05-01 06:14:29 +02:00
|
|
|
if len(split) < 2 {
|
|
|
|
return Author{}, errors.New("unexpected git output")
|
|
|
|
}
|
|
|
|
|
|
|
|
author := Author{Name: split[0], Email: split[1]}
|
|
|
|
return author, err
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *CommitCommands) GetCommitMessageFirstLine(sha string) (string, error) {
|
2022-01-19 09:32:27 +02:00
|
|
|
return self.GetCommitMessagesFirstLine([]string{sha})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitCommands) GetCommitMessagesFirstLine(shas []string) (string, error) {
|
|
|
|
return self.cmd.New(
|
|
|
|
fmt.Sprintf("git show --no-patch --pretty=format:%%s %s", strings.Join(shas, " ")),
|
|
|
|
).DontLog().RunWithOutput()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitCommands) GetCommitsOneline(shas []string) (string, error) {
|
|
|
|
return self.cmd.New(
|
|
|
|
fmt.Sprintf("git show --no-patch --oneline %s", strings.Join(shas, " ")),
|
|
|
|
).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
|
|
|
}
|
|
|
|
|
2023-02-03 21:20:20 +02:00
|
|
|
func (self *CommitCommands) ShowCmdObj(sha string, filterPath string, ignoreWhitespace bool) oscommands.ICmdObj {
|
2022-01-02 01:34:33 +02:00
|
|
|
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
|
|
|
}
|
2023-02-03 21:20:20 +02:00
|
|
|
ignoreWhitespaceArg := ""
|
|
|
|
if ignoreWhitespace {
|
|
|
|
ignoreWhitespaceArg = " --ignore-all-space"
|
|
|
|
}
|
2021-12-07 12:59:36 +02:00
|
|
|
|
2023-04-13 11:57:35 +02:00
|
|
|
cmdStr := fmt.Sprintf("git show --submodule --color=%s --unified=%d --stat -p %s%s%s",
|
2023-02-03 21:20:20 +02:00
|
|
|
self.UserConfig.Git.Paging.ColorArg, contextSize, sha, ignoreWhitespaceArg, filterPathArg)
|
2022-01-02 01:34:33 +02:00
|
|
|
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
|
|
|
}
|
2023-01-21 13:38:14 +02:00
|
|
|
|
|
|
|
// a value of 0 means the head commit, 1 is the parent commit, etc
|
|
|
|
func (self *CommitCommands) GetCommitMessageFromHistory(value int) (string, error) {
|
|
|
|
hash, _ := self.cmd.New(fmt.Sprintf("git log -1 --skip=%d --pretty=%%H", value)).DontLog().RunWithOutput()
|
|
|
|
formattedHash := strings.TrimSpace(hash)
|
|
|
|
if len(formattedHash) == 0 {
|
|
|
|
return "", ErrInvalidCommitIndex
|
|
|
|
}
|
|
|
|
return self.GetCommitMessage(formattedHash)
|
|
|
|
}
|