2022-11-11 04:19:29 +02:00
|
|
|
package git_commands
|
2021-12-30 08:19:01 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/common"
|
2022-03-19 07:34:46 +02:00
|
|
|
"github.com/samber/lo"
|
2021-12-30 08:19:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("diff").
|
2023-05-19 12:18:02 +02:00
|
|
|
Arg("--submodule").
|
|
|
|
Arg("--no-ext-diff").
|
|
|
|
Arg("--name-status").
|
|
|
|
Arg("-z").
|
|
|
|
Arg("--no-renames").
|
|
|
|
ArgIf(reverse, "-R").
|
|
|
|
Arg(from).
|
|
|
|
Arg(to).
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv()
|
2023-05-19 12:18:02 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
filenames, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
|
2021-12-30 08:19:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-19 07:34:46 +02:00
|
|
|
return getCommitFilesFromFilenames(filenames), nil
|
2021-12-30 08:19:01 +02:00
|
|
|
}
|
|
|
|
|
2022-03-19 07:34:46 +02:00
|
|
|
// 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 {
|
2021-12-30 08:19:01 +02:00
|
|
|
lines := strings.Split(strings.TrimRight(filenames, "\x00"), "\x00")
|
2022-03-19 07:34:46 +02:00
|
|
|
if len(lines) == 1 {
|
|
|
|
return []*models.CommitFile{}
|
2021-12-30 08:19:01 +02:00
|
|
|
}
|
|
|
|
|
2022-03-19 07:34:46 +02:00
|
|
|
// typical result looks like 'A my_file' meaning my_file was added
|
2023-07-24 05:06:42 +02:00
|
|
|
return lo.Map(lo.Chunk(lines, 2), func(chunk []string, _ int) *models.CommitFile {
|
2022-03-19 07:34:46 +02:00
|
|
|
return &models.CommitFile{
|
|
|
|
ChangeStatus: chunk[0],
|
|
|
|
Name: chunk[1],
|
|
|
|
}
|
|
|
|
})
|
2021-12-30 08:19:01 +02:00
|
|
|
}
|