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

Code before:

import (
	"fmt"
	"strings"

	"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
)

type CommitCommands struct {
	*GitCommon
}

func NewCommitCommands(gitCommon *GitCommon) *CommitCommands {
	return &CommitCommands{
		GitCommon: gitCommon,
	}
}

// RewordLastCommit rewords the topmost commit with the given message
func (self *CommitCommands) RewordLastCommit(message string) error {
	return self.cmd.New("git commit --allow-empty --amend --only -m " + self.cmd.Quote(message)).Run()
}

// Reset the author of the topmost commit.
func (self *CommitCommands) ResetAuthor() error {
	return self.cmd.New("git commit --allow-empty --no-edit --amend --reset-author").Run()
}

// ResetToCommit reset to commit
func (self *CommitCommands) ResetToCommit(sha string, strength string, envVars []string) error {
	return self.cmd.New(fmt.Sprintf("git reset --%s %s", strength, sha)).
		// 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(message string) oscommands.ICmdObj {
	splitMessage := strings.Split(message, "\n")
	lineArgs := ""
	for _, line := range splitMessage {
		lineArgs += fmt.Sprintf(" -m %s", self.cmd.Quote(line))
	}

	skipHookPrefix := self.UserConfig.Git.SkipHookPrefix
	noVerifyFlag := ""
	if skipHookPrefix != "" && strings.HasPrefix(message, skipHookPrefix) {
		noVerifyFlag = " --no-verify"
	}


Review comment (security):
```suggestion
	return self.cmd.New("git commit --allow-empty --only --no-edit --amend --reset-author").Run()
```

Adding --only will ensure we don't add any extra files from the index to the commit.

Code after:

import (
	"fmt"
	"strings"

	"github.com/go-errors/errors"
	"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
)

type CommitCommands struct {
	*GitCommon
}

func NewCommitCommands(gitCommon *GitCommon) *CommitCommands {
	return &CommitCommands{
		GitCommon: gitCommon,
	}
}

// RewordLastCommit rewords the topmost commit with the given message
func (self *CommitCommands) RewordLastCommit(message string) error {
	return self.cmd.New("git commit --allow-empty --amend --only -m " + self.cmd.Quote(message)).Run()
}

// ResetAuthor resets the author of the topmost commit
func (self *CommitCommands) ResetAuthor() error {
	return self.cmd.New("git commit --allow-empty --only --no-edit --amend --reset-author").Run()
}

// ResetToCommit reset to commit
func (self *CommitCommands) ResetToCommit(sha string, strength string, envVars []string) error {
	return self.cmd.New(fmt.Sprintf("git reset --%s %s", strength, sha)).
		// 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(message string) oscommands.ICmdObj {
	splitMessage := strings.Split(message, "\n")
	lineArgs := ""
	for _, line := range splitMessage {
		lineArgs += fmt.Sprintf(" -m %s", self.cmd.Quote(line))
	}

	skipHookPrefix := self.UserConfig.Git.SkipHookPrefix
	noVerifyFlag := ""
	if skipHookPrefix != "" && strings.HasPrefix(message, skipHookPrefix) {
		noVerifyFlag = " --no-verify"
	}