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

Code before:
// ResetAuthor resets the author of the topmost commit
func (self *CommitCommands) ResetAuthor() error {
	cmdArgs := NewGitCmd("commit").
		Arg("--allow-empty", "--only", "--no-edit", "--amend", "--reset-author").
		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("\n\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


Review comment (security):
I think we could be smarter here, and add only one `\n` instead of two if the previous line already is `Co-authored-by:`, for the case that you were pairing on the commit with multiple people.

Code after:
// ResetAuthor resets the author of the topmost commit
func (self *CommitCommands) ResetAuthor() error {
	cmdArgs := NewGitCmd("commit").
		Arg("--allow-empty", "--only", "--no-edit", "--amend", "--reset-author").
		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, author string) error {
	message, err := self.GetCommitMessage(sha)
	if err != nil {
		return err
	}

	message = AddCoAuthorToMessage(message, author)

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

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

func AddCoAuthorToMessage(message string, author string) string {
	subject, body, _ := strings.Cut(message, "\n")

	return strings.TrimSpace(subject) + "\n\n" + AddCoAuthorToDescription(strings.TrimSpace(body), author)
}

func AddCoAuthorToDescription(description string, author string) string {
	if description != "" {
		lines := strings.Split(description, "\n")
		if strings.HasPrefix(lines[len(lines)-1], "Co-authored-by:") {
			description += "\n"
		} else {
			description += "\n\n"
		}
	}

	return description + fmt.Sprintf("Co-authored-by: %s", author)