mirror of
https://github.com/jesseduffield/lazygit.git
synced 2024-12-12 11:15:00 +02:00
63dc07fded
By constructing an arg vector manually, we no longer need to quote arguments Mandate that args must be passed when building a command Now you need to provide an args array when building a command. There are a handful of places where we need to deal with a string, such as with user-defined custom commands, and for those we now require that at the callsite they use str.ToArgv to do that. I don't want to provide a method out of the box for it because I want to discourage its use. For some reason we were invoking a command through a shell when amending a commit, and I don't believe we needed to do that as there was nothing user- supplied about the command. So I've switched to using a regular command out- side the shell there
62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package git_commands
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/jesseduffield/generics/slices"
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
"github.com/jesseduffield/lazygit/pkg/common"
|
|
"github.com/samber/lo"
|
|
)
|
|
|
|
type CommitFileLoader struct {
|
|
*common.Common
|
|
cmd oscommands.ICmdObjBuilder
|
|
}
|
|
|
|
func NewCommitFileLoader(common *common.Common, cmd oscommands.ICmdObjBuilder) *CommitFileLoader {
|
|
return &CommitFileLoader{
|
|
Common: common,
|
|
cmd: cmd,
|
|
}
|
|
}
|
|
|
|
// GetFilesInDiff get the specified commit files
|
|
func (self *CommitFileLoader) GetFilesInDiff(from string, to string, reverse bool) ([]*models.CommitFile, error) {
|
|
cmdArgs := NewGitCmd("diff").
|
|
Arg("--submodule").
|
|
Arg("--no-ext-diff").
|
|
Arg("--name-status").
|
|
Arg("-z").
|
|
Arg("--no-renames").
|
|
ArgIf(reverse, "-R").
|
|
Arg(from).
|
|
Arg(to).
|
|
ToArgv()
|
|
|
|
filenames, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return getCommitFilesFromFilenames(filenames), nil
|
|
}
|
|
|
|
// filenames string is something like "MM\x00file1\x00MU\x00file2\x00AA\x00file3\x00"
|
|
// so we need to split it by the null character and then map each status-name pair to a commit file
|
|
func getCommitFilesFromFilenames(filenames string) []*models.CommitFile {
|
|
lines := strings.Split(strings.TrimRight(filenames, "\x00"), "\x00")
|
|
if len(lines) == 1 {
|
|
return []*models.CommitFile{}
|
|
}
|
|
|
|
// typical result looks like 'A my_file' meaning my_file was added
|
|
return slices.Map(lo.Chunk(lines, 2), func(chunk []string) *models.CommitFile {
|
|
return &models.CommitFile{
|
|
ChangeStatus: chunk[0],
|
|
Name: chunk[1],
|
|
}
|
|
})
|
|
}
|