Repository: jesseduffield/lazygit
File: pkg/commands/git_commands/commit.go (Go)

Code before:
		ToArgv()

	return self.cmd.New(cmdArgs).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 {
	cmdArgs := NewGitCmd("commit").
		Arg("--allow-empty", "--only", "--no-edit", "--amend", "--author="+value).
		ToArgv()

	return self.cmd.New(cmdArgs).Run()
}

// Add a commit's coauthor using Github/Gitlab Co-authored-by metadata. Value is expected to be of the form 'Name <Email>'
func (self *CommitCommands) AddCoAuthor(sha string, value string) error {
	message, err := self.GetCommitMessage(sha)
	if err != nil {
		return err
	}

	message = message + fmt.Sprintf("\nCo-authored-by: %s", value)

	args := []string{"-m", message}
	cmdArgs := NewGitCmd("commit").
		Arg("--allow-empty", "--amend", "--only").
		Arg(args...).
		ToArgv()

	return self.cmd.New(cmdArgs).Run()
}

// ResetToCommit reset to commit
func (self *CommitCommands) ResetToCommit(sha string, strength string, envVars []string) error {
	cmdArgs := NewGitCmd("reset").Arg("--"+strength, sha).ToArgv()

	return self.cmd.New(cmdArgs).
		// 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...).
		Run()
}

func (self *CommitCommands) CommitCmdObj(summary string, description string) oscommands.ICmdObj {
	messageArgs := self.commitMessageArgs(summary, description)

	skipHookPrefix := self.UserConfig.Git.SkipHookPrefix

	cmdArgs := NewGitCmd("commit").
		ArgIf(skipHookPrefix != "" && strings.HasPrefix(summary, skipHookPrefix), "--no-verify").

Review comment (suggestion):
```suggestion
		Arg("--allow-empty", "--amend", "--only", "-m", message).
```
No need to create a separate `args` variable

Code after:
		ToArgv()

	return self.cmd.New(cmdArgs).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 {
	cmdArgs := NewGitCmd("commit").
		Arg("--allow-empty", "--only", "--no-edit", "--amend", "--author="+value).
		ToArgv()

	return self.cmd.New(cmdArgs).Run()
}

// Add a commit's coauthor using Github/Gitlab Co-authored-by metadata. Value is expected to be of the form 'Name <Email>'
func (self *CommitCommands) AddCoAuthor(sha string, value string) error {
	message, err := self.GetCommitMessage(sha)
	if err != nil {
		return err
	}

	message = message + fmt.Sprintf("\nCo-authored-by: %s", value)

	cmdArgs := NewGitCmd("commit").
		Arg("--allow-empty", "--amend", "--only", "-m", message).
		ToArgv()

	return self.cmd.New(cmdArgs).Run()
}

// ResetToCommit reset to commit
func (self *CommitCommands) ResetToCommit(sha string, strength string, envVars []string) error {
	cmdArgs := NewGitCmd("reset").Arg("--"+strength, sha).ToArgv()

	return self.cmd.New(cmdArgs).
		// 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...).
		Run()
}

func (self *CommitCommands) CommitCmdObj(summary string, description string) oscommands.ICmdObj {
	messageArgs := self.commitMessageArgs(summary, description)

	skipHookPrefix := self.UserConfig.Git.SkipHookPrefix

	cmdArgs := NewGitCmd("commit").
		ArgIf(skipHookPrefix != "" && strings.HasPrefix(summary, skipHookPrefix), "--no-verify").
		ArgIf(self.signoffFlag() != "", self.signoffFlag()).
		Arg(messageArgs...).