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

Code before:
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)
}

// ResetToCommit reset to commit
func (self *CommitCommands) ResetToCommit(hash string, strength string, envVars []string) error {
	cmdArgs := NewGitCmd("reset").Arg("--"+strength, hash).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, verify bool) oscommands.ICmdObj {
	messageArgs := self.commitMessageArgs(summary, description)

	cmdArgs := NewGitCmd("commit").
		ArgIf(verify, "--no-verify").
		ArgIf(self.signoffFlag() != "", self.signoffFlag()).
		Arg(messageArgs...).
		ToArgv()

	return self.cmd.New(cmdArgs)
}

func (self *CommitCommands) RewordLastCommitInEditorCmdObj() oscommands.ICmdObj {
	return self.cmd.New(NewGitCmd("commit").Arg("--allow-empty", "--amend", "--only").ToArgv())
}

func (self *CommitCommands) RewordLastCommitInEditorWithMessageFileCmdObj(tmpMessageFile string) oscommands.ICmdObj {
	return self.cmd.New(NewGitCmd("commit").
		Arg("--allow-empty", "--amend", "--only", "--edit", "--file="+tmpMessageFile).ToArgv())
}

func (self *CommitCommands) CommitInEditorWithMessageFileCmdObj(tmpMessageFile string, verify bool) oscommands.ICmdObj {
	return self.cmd.New(NewGitCmd("commit").
		ArgIf(verify, "--no-verify").
		Arg("--edit").
		Arg("--file="+tmpMessageFile).

Review comment (suggestion):
This is a super confusing name for this variable; it's backwards. It should be renamed to `noVerify`, or probably better to `skipHooks` (because variables with `no` in them are an invitation for double negations, which I have a strong aversion against).

But see further discussion below.

Code after:
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)
}

// ResetToCommit reset to commit
func (self *CommitCommands) ResetToCommit(hash string, strength string, envVars []string) error {
	cmdArgs := NewGitCmd("reset").Arg("--"+strength, hash).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, forceSkipHooks bool) oscommands.ICmdObj {
	messageArgs := self.commitMessageArgs(summary, description)
	skipHookPrefix := self.UserConfig().Git.SkipHookPrefix
	cmdArgs := NewGitCmd("commit").
		ArgIf(forceSkipHooks || (skipHookPrefix != "" && strings.HasPrefix(summary, skipHookPrefix)), "--no-verify").
		ArgIf(self.signoffFlag() != "", self.signoffFlag()).
		Arg(messageArgs...).
		ToArgv()

	return self.cmd.New(cmdArgs)
}

func (self *CommitCommands) RewordLastCommitInEditorCmdObj() oscommands.ICmdObj {
	return self.cmd.New(NewGitCmd("commit").Arg("--allow-empty", "--amend", "--only").ToArgv())
}

func (self *CommitCommands) RewordLastCommitInEditorWithMessageFileCmdObj(tmpMessageFile string) oscommands.ICmdObj {
	return self.cmd.New(NewGitCmd("commit").
		Arg("--allow-empty", "--amend", "--only", "--edit", "--file="+tmpMessageFile).ToArgv())
}

func (self *CommitCommands) CommitInEditorWithMessageFileCmdObj(tmpMessageFile string, forceSkipHooks bool) oscommands.ICmdObj {
	return self.cmd.New(NewGitCmd("commit").
		ArgIf(forceSkipHooks, "--no-verify").
		Arg("--edit").
		Arg("--file="+tmpMessageFile).