1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-16 11:37:01 +02:00
lazygit/pkg/commands/git_commands/commit.go

253 lines
7.0 KiB
Go
Raw Normal View History

2022-01-08 05:00:36 +02:00
package git_commands
2020-09-29 12:03:39 +02:00
import (
"fmt"
"strings"
"github.com/go-errors/errors"
2020-09-29 12:03:39 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
)
var ErrInvalidCommitIndex = errors.New("invalid commit index")
2022-01-02 01:34:33 +02:00
type CommitCommands struct {
*GitCommon
2022-01-02 01:34:33 +02:00
}
func NewCommitCommands(gitCommon *GitCommon) *CommitCommands {
2022-01-02 01:34:33 +02:00
return &CommitCommands{
GitCommon: gitCommon,
2022-01-02 01:34:33 +02:00
}
}
// ResetAuthor resets the author of the topmost commit
func (self *CommitCommands) ResetAuthor() error {
cmdStr := NewGitCmd("commit").
Arg("--allow-empty", "--only", "--no-edit", "--amend", "--reset-author").
ToString()
return self.cmd.New(cmdStr).Run()
}
// 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 {
cmdStr := NewGitCmd("commit").
Arg("--allow-empty", "--only", "--no-edit", "--amend", "--author="+self.cmd.Quote(value)).
ToString()
return self.cmd.New(cmdStr).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 {
cmdStr := NewGitCmd("reset").Arg("--"+strength, sha).ToString()
return self.cmd.New(cmdStr).
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
}
func (self *CommitCommands) CommitCmdObj(message string) oscommands.ICmdObj {
messageArgs := self.commitMessageArgs(message)
skipHookPrefix := self.UserConfig.Git.SkipHookPrefix
2020-09-29 12:03:39 +02:00
cmdStr := NewGitCmd("commit").
ArgIf(skipHookPrefix != "" && strings.HasPrefix(message, skipHookPrefix), "--no-verify").
ArgIf(self.signoffFlag() != "", self.signoffFlag()).
Arg(messageArgs...).
ToString()
return self.cmd.New(cmdStr)
}
// RewordLastCommit rewords the topmost commit with the given message
func (self *CommitCommands) RewordLastCommit(message string) error {
messageArgs := self.commitMessageArgs(message)
cmdStr := NewGitCmd("commit").
Arg("--allow-empty", "--amend", "--only").
Arg(messageArgs...).
ToString()
return self.cmd.New(cmdStr).Run()
}
func (self *CommitCommands) commitMessageArgs(message string) []string {
msg, description, _ := strings.Cut(message, "\n")
args := []string{"-m", self.cmd.Quote(msg)}
if description != "" {
args = append(args, "-m", self.cmd.Quote(description))
}
return args
}
// runs git commit without the -m argument meaning it will invoke the user's editor
func (self *CommitCommands) CommitEditorCmdObj() oscommands.ICmdObj {
cmdStr := NewGitCmd("commit").
ArgIf(self.signoffFlag() != "", self.signoffFlag()).
ArgIf(self.verboseFlag() != "", self.verboseFlag()).
ToString()
return self.cmd.New(cmdStr)
}
func (self *CommitCommands) signoffFlag() string {
if self.UserConfig.Git.Commit.SignOff {
return "--signoff"
} else {
return ""
}
2020-09-29 12:03:39 +02:00
}
func (self *CommitCommands) verboseFlag() string {
switch self.config.UserConfig.Git.Commit.Verbose {
case "always":
return "--verbose"
case "never":
return "--no-verbose"
default:
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) {
cmdStr := NewGitCmd("log").Arg("-1", "--pretty=%s").ToString()
message, err := self.cmd.New(cmdStr).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) {
cmdStr := NewGitCmd("rev-list").
Arg("--format=%B", "--max-count=1", commitSha).
ToString()
2022-01-02 01:34:33 +02:00
messageWithHeader, err := self.cmd.New(cmdStr).DontLog().RunWithOutput()
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 := NewGitCmd("show").Arg("--no-color", commitSha).ToString()
2022-03-24 22:04:33 +02:00
diff, err := self.cmd.New(cmdStr).DontLog().RunWithOutput()
return diff, err
}
type Author struct {
Name string
Email string
}
func (self *CommitCommands) GetCommitAuthor(commitSha string) (Author, error) {
cmdStr := NewGitCmd("show").
Arg("--no-patch", "--pretty=format:'%an%x00%ae'", commitSha).
ToString()
output, err := self.cmd.New(cmdStr).DontLog().RunWithOutput()
if err != nil {
return Author{}, err
}
split := strings.SplitN(strings.TrimSpace(output), "\x00", 2)
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) {
cmdStr := NewGitCmd("show").
Arg("--no-patch", "--pretty=format:%s").
Arg(shas...).
ToString()
return self.cmd.New(cmdStr).DontLog().RunWithOutput()
2022-01-19 09:32:27 +02:00
}
func (self *CommitCommands) GetCommitsOneline(shas []string) (string, error) {
cmdStr := NewGitCmd("show").
Arg("--no-patch", "--oneline").
Arg(shas...).
ToString()
return self.cmd.New(cmdStr).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 {
cmdStr := NewGitCmd("commit").
Arg("--amend", "--no-edit", "--allow-empty").
ToString()
return self.cmd.New(cmdStr)
2020-09-29 12:03:39 +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
2021-12-07 12:59:36 +02:00
cmdStr := NewGitCmd("show").
Arg("--submodule").
Arg("--color="+self.UserConfig.Git.Paging.ColorArg).
Arg(fmt.Sprintf("--unified=%d", contextSize)).
Arg("--stat").
Arg("-p").
Arg(sha).
ArgIf(ignoreWhitespace, "--ignore-all-space").
ArgIf(filterPath != "", "--", self.cmd.Quote(filterPath)).
ToString()
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 {
cmdStr := NewGitCmd("revert").Arg(sha).ToString()
return self.cmd.New(cmdStr).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 {
cmdStr := NewGitCmd("revert").Arg(sha, "-m", fmt.Sprintf("%d", parentNumber)).
ToString()
return self.cmd.New(cmdStr).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 {
cmdStr := NewGitCmd("commit").Arg("--fixup=" + sha).ToString()
return self.cmd.New(cmdStr).Run()
2020-09-29 12:03:39 +02:00
}
// a value of 0 means the head commit, 1 is the parent commit, etc
func (self *CommitCommands) GetCommitMessageFromHistory(value int) (string, error) {
cmdStr := NewGitCmd("log").Arg("-1", fmt.Sprintf("--skip=%d", value), "--pretty=%H").
ToString()
hash, _ := self.cmd.New(cmdStr).DontLog().RunWithOutput()
formattedHash := strings.TrimSpace(hash)
if len(formattedHash) == 0 {
return "", ErrInvalidCommitIndex
}
return self.GetCommitMessage(formattedHash)
}