1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/commands/loaders/files.go

138 lines
3.8 KiB
Go
Raw Normal View History

2021-12-30 08:19:01 +02:00
package loaders
2020-09-29 11:22:26 +02:00
import (
"fmt"
"strings"
"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"
2022-03-19 03:26:30 +02:00
"github.com/samber/lo"
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)
2021-12-30 08:19:01 +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{}
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
}
change := status.Change
2020-09-29 11:22:26 +02:00
stagedChange := change[0:1]
unstagedChange := change[1:2]
2022-03-19 03:26:30 +02:00
untracked := lo.Contains([]string{"??", "A ", "AM"}, change)
hasNoStagedChanges := lo.Contains([]string{" ", "U", "?"}, stagedChange)
hasInlineMergeConflicts := lo.Contains([]string{"UU", "AA"}, change)
hasMergeConflicts := hasInlineMergeConflicts || lo.Contains([]string{"DD", "AU", "UA", "UD", "DU"}, change)
2020-09-29 11:22:26 +02:00
file := &models.File{
Name: status.Name,
PreviousName: status.PreviousName,
DisplayString: status.StatusString,
2020-09-29 11:22:26 +02:00
HasStagedChanges: !hasNoStagedChanges,
HasUnstagedChanges: unstagedChange != " ",
Tracked: !untracked,
Deleted: unstagedChange == "D" || stagedChange == "D",
2021-03-20 03:07:11 +02:00
Added: unstagedChange == "A" || untracked,
2020-09-29 11:22:26 +02:00
HasMergeConflicts: hasMergeConflicts,
HasInlineMergeConflicts: hasInlineMergeConflicts,
2021-12-30 08:19:01 +02:00
Type: self.getFileType(status.Name),
2020-09-29 11:22:26 +02:00
ShortStatus: change,
}
files = append(files, file)
}
2020-09-29 11:22:26 +02:00
return files
}
// GitStatus returns the file status of the repo
2020-09-29 11:22:26 +02:00
type GitStatusOptions struct {
NoRenames bool
UntrackedFilesArg string
}
type FileStatus struct {
StatusString string
Change string // ??, MM, AM, ...
Name string
PreviousName string
}
2021-12-30 08:19:01 +02:00
func (c *FileLoader) GitStatus(opts GitStatusOptions) ([]FileStatus, error) {
2020-09-29 11:22:26 +02:00
noRenamesFlag := ""
if opts.NoRenames {
2021-12-30 08:19:01 +02:00
noRenamesFlag = " --no-renames"
2020-09-29 11:22:26 +02:00
}
2022-01-05 02:57:32 +02:00
statusLines, err := c.cmd.New(fmt.Sprintf("git status %s --porcelain -z%s", opts.UntrackedFilesArg, noRenamesFlag)).DontLog().RunWithOutput()
if err != nil {
return []FileStatus{}, err
}
2021-03-14 04:20:54 +02:00
splitLines := strings.Split(statusLines, "\x00")
response := []FileStatus{}
for i := 0; i < len(splitLines); i++ {
2021-03-14 04:20:54 +02:00
original := splitLines[i]
if len(original) < 3 {
continue
}
status := FileStatus{
StatusString: original,
Change: original[:2],
Name: original[3:],
PreviousName: "",
}
if strings.HasPrefix(status.Change, "R") {
// if a line starts with 'R' then the next line is the original file.
status.PreviousName = strings.TrimSpace(splitLines[i+1])
status.StatusString = fmt.Sprintf("%s %s -> %s", status.Change, status.PreviousName, status.Name)
i++
2021-03-14 04:20:54 +02:00
}
response = append(response, status)
2021-03-14 04:20:54 +02:00
}
return response, nil
2020-09-29 11:22:26 +02:00
}