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 {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("commit").
|
2023-05-19 12:18:02 +02:00
|
|
|
Arg("--allow-empty", "--only", "--no-edit", "--amend", "--reset-author").
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).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 {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("commit").
|
|
|
|
Arg("--allow-empty", "--only", "--no-edit", "--amend", "--author="+value).
|
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).Run()
|
2022-05-08 13:05:01 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("reset").Arg("--"+strength, sha).ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).
|
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
|
|
|
}
|
|
|
|
|
2023-07-22 06:05:42 +02:00
|
|
|
func (self *CommitCommands) CommitCmdObj(summary string, description string) oscommands.ICmdObj {
|
|
|
|
messageArgs := self.commitMessageArgs(summary, description)
|
2020-10-12 23:16:24 +02:00
|
|
|
|
2022-01-07 10:19:24 +02:00
|
|
|
skipHookPrefix := self.UserConfig.Git.SkipHookPrefix
|
2020-09-29 12:03:39 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("commit").
|
2023-07-22 06:05:42 +02:00
|
|
|
ArgIf(skipHookPrefix != "" && strings.HasPrefix(summary, skipHookPrefix), "--no-verify").
|
2023-05-19 12:18:02 +02:00
|
|
|
ArgIf(self.signoffFlag() != "", self.signoffFlag()).
|
|
|
|
Arg(messageArgs...).
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitCommands) RewordLastCommitInEditorCmdObj() oscommands.ICmdObj {
|
|
|
|
return self.cmd.New(NewGitCmd("commit").Arg("--allow-empty", "--amend", "--only").ToArgv())
|
2023-01-21 13:38:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RewordLastCommit rewords the topmost commit with the given message
|
2023-07-22 06:05:42 +02:00
|
|
|
func (self *CommitCommands) RewordLastCommit(summary string, description string) error {
|
|
|
|
messageArgs := self.commitMessageArgs(summary, description)
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("commit").
|
2023-05-19 12:18:02 +02:00
|
|
|
Arg("--allow-empty", "--amend", "--only").
|
|
|
|
Arg(messageArgs...).
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).Run()
|
2023-01-21 13:38:14 +02:00
|
|
|
}
|
|
|
|
|
2023-07-22 06:05:42 +02:00
|
|
|
func (self *CommitCommands) commitMessageArgs(summary string, description string) []string {
|
|
|
|
args := []string{"-m", summary}
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-01-21 13:38:14 +02:00
|
|
|
if description != "" {
|
2023-05-21 09:00:29 +02:00
|
|
|
args = append(args, "-m", description)
|
2023-01-21 13:38:14 +02:00
|
|
|
}
|
|
|
|
|
2023-05-19 12:18:02 +02:00
|
|
|
return args
|
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-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("commit").
|
2023-05-19 12:18:02 +02:00
|
|
|
ArgIf(self.signoffFlag() != "", self.signoffFlag()).
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs)
|
2022-01-07 10:19:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitCommands) signoffFlag() string {
|
|
|
|
if self.UserConfig.Git.Commit.SignOff {
|
2023-05-19 12:18:02 +02:00
|
|
|
return "--signoff"
|
2022-01-07 10:19:24 +02:00
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
2020-09-29 12:03:39 +02:00
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func (self *CommitCommands) GetCommitMessage(commitSha string) (string, error) {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("rev-list").
|
2023-05-19 12:18:02 +02:00
|
|
|
Arg("--format=%B", "--max-count=1", commitSha).
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
messageWithHeader, err := self.cmd.New(cmdArgs).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) {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("show").Arg("--no-color", commitSha).ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
diff, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
|
2022-03-24 22:04:33 +02:00
|
|
|
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) {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("show").
|
2023-05-19 12:18:02 +02:00
|
|
|
Arg("--no-patch", "--pretty=format:'%an%x00%ae'", commitSha).
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
output, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
|
2022-05-01 06:14:29 +02:00
|
|
|
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) {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("show").
|
2023-05-19 12:18:02 +02:00
|
|
|
Arg("--no-patch", "--pretty=format:%s").
|
|
|
|
Arg(shas...).
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).DontLog().RunWithOutput()
|
2022-01-19 09:32:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitCommands) GetCommitsOneline(shas []string) (string, error) {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("show").
|
2023-05-19 12:18:02 +02:00
|
|
|
Arg("--no-patch", "--oneline").
|
|
|
|
Arg(shas...).
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).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 {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("commit").
|
2023-05-19 12:18:02 +02:00
|
|
|
Arg("--amend", "--no-edit", "--allow-empty").
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs)
|
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
|
2021-12-07 12:59:36 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("show").
|
2023-05-19 12:18:02 +02:00
|
|
|
Arg("--submodule").
|
|
|
|
Arg("--color="+self.UserConfig.Git.Paging.ColorArg).
|
|
|
|
Arg(fmt.Sprintf("--unified=%d", contextSize)).
|
|
|
|
Arg("--stat").
|
2023-06-28 09:44:04 +02:00
|
|
|
Arg("--decorate").
|
2023-05-19 12:18:02 +02:00
|
|
|
Arg("-p").
|
|
|
|
Arg(sha).
|
|
|
|
ArgIf(ignoreWhitespace, "--ignore-all-space").
|
2023-05-21 09:00:29 +02:00
|
|
|
ArgIf(filterPath != "", "--", filterPath).
|
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).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 {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("revert").Arg(sha).ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).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 {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("revert").Arg(sha, "-m", fmt.Sprintf("%d", parentNumber)).
|
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).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 {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("commit").Arg("--fixup=" + sha).ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
return self.cmd.New(cmdArgs).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) {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("log").Arg("-1", fmt.Sprintf("--skip=%d", value), "--pretty=%H").
|
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
hash, _ := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
|
2023-01-21 13:38:14 +02:00
|
|
|
formattedHash := strings.TrimSpace(hash)
|
|
|
|
if len(formattedHash) == 0 {
|
|
|
|
return "", ErrInvalidCommitIndex
|
|
|
|
}
|
|
|
|
return self.GetCommitMessage(formattedHash)
|
|
|
|
}
|