summaryrefslogtreecommitdiffstats
path: root/pkg/commands/git_commands/file_loader.go
diff options
context:
space:
mode:
authorsudoburt <sudoburt@proton.me>2022-11-10 21:19:29 -0500
committerJesse Duffield <jessedduffield@gmail.com>2022-11-14 18:11:45 +1100
commit3e73dacce3365b540b278c3e1afb9bd8059d1ce7 (patch)
treed2706dc236491f7839ff5119e4eb60409e5bdbd8 /pkg/commands/git_commands/file_loader.go
parentf67824b349fdbdcd1779e260f80bd26a446a4702 (diff)
Merge loaders package into git_commands package
Diffstat (limited to 'pkg/commands/git_commands/file_loader.go')
-rw-r--r--pkg/commands/git_commands/file_loader.go123
1 files changed, 123 insertions, 0 deletions
diff --git a/pkg/commands/git_commands/file_loader.go b/pkg/commands/git_commands/file_loader.go
new file mode 100644
index 000000000..9f33b811a
--- /dev/null
+++ b/pkg/commands/git_commands/file_loader.go
@@ -0,0 +1,123 @@
+package git_commands
+
+import (
+ "fmt"
+ "strings"
+
+ "github.com/jesseduffield/lazygit/pkg/commands/models"
+ "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
+ "github.com/jesseduffield/lazygit/pkg/common"
+)
+
+type FileLoaderConfig interface {
+ GetShowUntrackedFiles() string
+}
+
+type FileLoader struct {
+ *common.Common
+ cmd oscommands.ICmdObjBuilder
+ config FileLoaderConfig
+ getFileType func(string) string
+}
+
+func NewFileLoader(cmn *common.Common, cmd oscommands.ICmdObjBuilder, config FileLoaderConfig) *FileLoader {
+ return &FileLoader{
+ Common: cmn,
+ cmd: cmd,
+ getFileType: oscommands.FileType,
+ config: config,
+ }
+}
+
+type GetStatusFileOptions struct {
+ NoRenames bool
+}
+
+func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File {
+ // check if config wants us ignoring untracked files
+ untrackedFilesSetting := self.config.GetShowUntrackedFiles()
+
+ if untrackedFilesSetting == "" {
+ untrackedFilesSetting = "all"
+ }
+ untrackedFilesArg := fmt.Sprintf("--untracked-files=%s", untrackedFilesSetting)
+
+ statuses, err := self.GitStatus(GitStatusOptions{NoRenames: opts.NoRenames, UntrackedFilesArg: untrackedFilesArg})
+ if err != nil {
+ self.Log.Error(err)
+ }
+ files := []*models.File{}
+
+ for _, status := range statuses {
+ if strings.HasPrefix(status.StatusString, "warning") {
+ self.Log.Warningf("warning when calling git status: %s", status.StatusString)
+ continue
+ }
+
+ file := &models.File{
+ Name: status.Name,
+ PreviousName: status.PreviousName,
+ DisplayString: status.StatusString,
+ Type: self.getFileType(status.Name),
+ }
+
+ models.SetStatusFields(file, status.Change)
+ files = append(files, file)
+ }
+
+ return files
+}
+
+// GitStatus returns the file status of the repo
+type GitStatusOptions struct {
+ NoRenames bool
+ UntrackedFilesArg string
+}
+
+type FileStatus struct {
+ StatusString string
+ Change string // ??, MM, AM, ...
+ Name string
+ PreviousName string
+}
+
+func (c *FileLoader) GitStatus(opts GitStatusOptions) ([]FileStatus, error) {
+ noRenamesFlag := ""
+ if opts.NoRenames {
+ noRenamesFlag = " --no-renames"
+ }
+
+ statusLines, _, err := c.cmd.New(fmt.Sprintf("git status %s --porcelain -z%s", opts.UntrackedFilesArg, noRenamesFlag)).DontLog().RunWithOutputs()
+ if err != nil {
+ return []FileStatus{}, err
+ }
+
+ splitLines := strings.Split(statusLines, "\x00")
+ response := []FileStatus{}
+
+ for i := 0; i < len(splitLines); i++ {
+ 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 = splitLines[i+1]
+ status.StatusString = fmt.Sprintf("%s %s -> %s", status.Change, status.PreviousName, status.Name)
+ i++
+ }
+
+ response = append(response, status)
+ }
+
+ return response, nil
+}