2022-11-11 04:19:29 +02:00
|
|
|
package git_commands
|
2020-09-29 11:22:26 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-09-29 12:28:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2021-12-30 08:19:01 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/common"
|
2020-09-29 11:22:26 +02:00
|
|
|
)
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
type FileLoaderConfig interface {
|
|
|
|
GetShowUntrackedFiles() string
|
|
|
|
}
|
|
|
|
|
2021-12-30 08:19:01 +02:00
|
|
|
type FileLoader struct {
|
|
|
|
*common.Common
|
|
|
|
cmd oscommands.ICmdObjBuilder
|
2022-01-02 01:34:33 +02:00
|
|
|
config FileLoaderConfig
|
2021-12-30 08:19:01 +02:00
|
|
|
getFileType func(string) string
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:34:33 +02:00
|
|
|
func NewFileLoader(cmn *common.Common, cmd oscommands.ICmdObjBuilder, config FileLoaderConfig) *FileLoader {
|
2021-12-30 08:19:01 +02:00
|
|
|
return &FileLoader{
|
|
|
|
Common: cmn,
|
|
|
|
cmd: cmd,
|
|
|
|
getFileType: oscommands.FileType,
|
2022-01-02 01:34:33 +02:00
|
|
|
config: config,
|
2021-12-30 08:19:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 12:03:39 +02:00
|
|
|
type GetStatusFileOptions struct {
|
|
|
|
NoRenames bool
|
|
|
|
}
|
|
|
|
|
2021-12-30 08:19:01 +02:00
|
|
|
func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File {
|
2020-09-29 11:22:26 +02:00
|
|
|
// check if config wants us ignoring untracked files
|
2022-01-02 01:34:33 +02:00
|
|
|
untrackedFilesSetting := self.config.GetShowUntrackedFiles()
|
2020-09-29 11:22:26 +02:00
|
|
|
|
|
|
|
if untrackedFilesSetting == "" {
|
|
|
|
untrackedFilesSetting = "all"
|
|
|
|
}
|
|
|
|
untrackedFilesArg := fmt.Sprintf("--untracked-files=%s", untrackedFilesSetting)
|
|
|
|
|
2023-05-19 12:18:02 +02:00
|
|
|
statuses, err := self.gitStatus(GitStatusOptions{NoRenames: opts.NoRenames, UntrackedFilesArg: untrackedFilesArg})
|
2020-09-29 11:22:26 +02:00
|
|
|
if err != nil {
|
2021-12-30 08:19:01 +02:00
|
|
|
self.Log.Error(err)
|
2020-09-29 11:22:26 +02:00
|
|
|
}
|
|
|
|
files := []*models.File{}
|
|
|
|
|
2021-08-16 16:15:37 +02:00
|
|
|
for _, status := range statuses {
|
|
|
|
if strings.HasPrefix(status.StatusString, "warning") {
|
2021-12-30 08:19:01 +02:00
|
|
|
self.Log.Warningf("warning when calling git status: %s", status.StatusString)
|
2020-09-29 11:22:26 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
file := &models.File{
|
2022-07-31 05:52:56 +02:00
|
|
|
Name: status.Name,
|
|
|
|
PreviousName: status.PreviousName,
|
|
|
|
DisplayString: status.StatusString,
|
|
|
|
Type: self.getFileType(status.Name),
|
2020-09-29 11:22:26 +02:00
|
|
|
}
|
2022-07-31 05:52:56 +02:00
|
|
|
|
|
|
|
models.SetStatusFields(file, status.Change)
|
2020-09-29 11:22:26 +02:00
|
|
|
files = append(files, file)
|
|
|
|
}
|
2020-11-15 01:45:55 +02:00
|
|
|
|
2020-09-29 11:22:26 +02:00
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2021-08-16 16:15:37 +02:00
|
|
|
// GitStatus returns the file status of the repo
|
2020-09-29 11:22:26 +02:00
|
|
|
type GitStatusOptions struct {
|
|
|
|
NoRenames bool
|
|
|
|
UntrackedFilesArg string
|
|
|
|
}
|
|
|
|
|
2021-08-16 16:15:37 +02:00
|
|
|
type FileStatus struct {
|
|
|
|
StatusString string
|
|
|
|
Change string // ??, MM, AM, ...
|
|
|
|
Name string
|
|
|
|
PreviousName string
|
|
|
|
}
|
|
|
|
|
2023-05-19 12:18:02 +02:00
|
|
|
func (c *FileLoader) gitStatus(opts GitStatusOptions) ([]FileStatus, error) {
|
2023-05-21 09:00:29 +02:00
|
|
|
cmdArgs := NewGitCmd("status").
|
2023-05-19 12:18:02 +02:00
|
|
|
Arg(opts.UntrackedFilesArg).
|
|
|
|
Arg("--porcelain").
|
|
|
|
Arg("-z").
|
|
|
|
ArgIf(opts.NoRenames, "--no-renames").
|
2023-05-21 09:00:29 +02:00
|
|
|
ToArgv()
|
2020-09-29 11:22:26 +02:00
|
|
|
|
2023-05-21 09:00:29 +02:00
|
|
|
statusLines, _, err := c.cmd.New(cmdArgs).DontLog().RunWithOutputs()
|
2021-03-02 04:51:03 +02:00
|
|
|
if err != nil {
|
2021-08-16 16:15:37 +02:00
|
|
|
return []FileStatus{}, err
|
2021-03-02 04:51:03 +02:00
|
|
|
}
|
|
|
|
|
2021-03-14 04:20:54 +02:00
|
|
|
splitLines := strings.Split(statusLines, "\x00")
|
2021-08-16 16:15:37 +02:00
|
|
|
response := []FileStatus{}
|
2021-07-22 12:02:41 +02:00
|
|
|
|
|
|
|
for i := 0; i < len(splitLines); i++ {
|
2021-03-14 04:20:54 +02:00
|
|
|
original := splitLines[i]
|
2021-08-16 16:15:37 +02:00
|
|
|
|
|
|
|
if len(original) < 3 {
|
2021-07-22 12:02:41 +02:00
|
|
|
continue
|
2021-08-16 16:15:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
status := FileStatus{
|
|
|
|
StatusString: original,
|
|
|
|
Change: original[:2],
|
|
|
|
Name: original[3:],
|
|
|
|
PreviousName: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(status.Change, "R") {
|
2021-07-22 12:02:41 +02:00
|
|
|
// if a line starts with 'R' then the next line is the original file.
|
2022-04-14 14:24:08 +02:00
|
|
|
status.PreviousName = splitLines[i+1]
|
2021-08-16 16:15:37 +02:00
|
|
|
status.StatusString = fmt.Sprintf("%s %s -> %s", status.Change, status.PreviousName, status.Name)
|
2021-07-22 12:02:41 +02:00
|
|
|
i++
|
2021-03-14 04:20:54 +02:00
|
|
|
}
|
2021-08-16 16:15:37 +02:00
|
|
|
|
|
|
|
response = append(response, status)
|
2021-03-14 04:20:54 +02:00
|
|
|
}
|
|
|
|
|
2021-07-22 12:02:41 +02:00
|
|
|
return response, nil
|
2020-09-29 11:22:26 +02:00
|
|
|
}
|