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

Code before:
	if self.UserConfig.Git.Commit.SignOff {
		return "--signoff"
	} else {
		return ""
	}
}

func (self *CommitCommands) GetCommitMessage(commitSha string) (string, error) {
	cmdArgs := NewGitCmd("rev-list").
		Arg("--format=%B", "--max-count=1", commitSha).
		ToArgv()

	messageWithHeader, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
	message := strings.Join(strings.SplitAfter(messageWithHeader, "\n")[1:], "")
	return strings.TrimSpace(message), err
}

func (self *CommitCommands) GetCommitSubject(commitSha string) (string, error) {
	cmdArgs := NewGitCmd("rev-list").
		Arg("--format=%s", "--max-count=1", commitSha).
		ToArgv()

	rawSubject, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
	subjectArray := strings.Split(strings.TrimSpace(rawSubject), "\n")
	if len(subjectArray) != 2 {
		return "", errors.New("unexpected git output")
	}
	return subjectArray[1], err
}

func (self *CommitCommands) GetCommitDiff(commitSha string) (string, error) {
	cmdArgs := NewGitCmd("show").Arg("--no-color", commitSha).ToArgv()

	diff, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
	return diff, err
}

type Author struct {
	Name  string
	Email string
}

func (self *CommitCommands) GetCommitAuthor(commitSha string) (Author, error) {
	cmdArgs := NewGitCmd("show").
		Arg("--no-patch", "--pretty=format:'%an%x00%ae'", commitSha).
		ToArgv()

	output, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
	if err != nil {
		return Author{}, err
	}

Review comment (refactor):
This block of code has a problem when RunWithOutput returns an error, because it will likely return an empty string for `rawSubject` in that case; but you are not checking the error, and you'll clobber it with "unexpected git output".

Instead of fixing that, I'd suggest to simplify the whole thing by using `git log` instead of `git rev-list`. They are pretty much identical when used with --max-count=1, except that git log doesn't output the `commit <SHA>` line, so this block gets simplified to
```
	rawSubject, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
	return strings.TrimSpace(rawSubject), err
```

We can simplify `GetCommitMessage` in the same way, but don't bother doing this here, I already did it as part of #3170 (not merged yet).

Code after:
	if self.UserConfig.Git.Commit.SignOff {
		return "--signoff"
	} else {
		return ""
	}
}

func (self *CommitCommands) GetCommitMessage(commitSha string) (string, error) {
	cmdArgs := NewGitCmd("rev-list").
		Arg("--format=%B", "--max-count=1", commitSha).
		ToArgv()

	messageWithHeader, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
	message := strings.Join(strings.SplitAfter(messageWithHeader, "\n")[1:], "")
	return strings.TrimSpace(message), err
}

func (self *CommitCommands) GetCommitSubject(commitSha string) (string, error) {
	cmdArgs := NewGitCmd("log").
		Arg("--format=%s", "--max-count=1", commitSha).
		ToArgv()

	subject, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
	return strings.TrimSpace(subject), err
}

func (self *CommitCommands) GetCommitDiff(commitSha string) (string, error) {
	cmdArgs := NewGitCmd("show").Arg("--no-color", commitSha).ToArgv()

	diff, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
	return diff, err
}

type Author struct {
	Name  string
	Email string
}

func (self *CommitCommands) GetCommitAuthor(commitSha string) (Author, error) {
	cmdArgs := NewGitCmd("show").
		Arg("--no-patch", "--pretty=format:'%an%x00%ae'", commitSha).
		ToArgv()

	output, err := self.cmd.New(cmdArgs).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")