Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add root node in file tree #4346

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/commands/git_commands/commit_file_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func getCommitFilesFromFilenames(filenames string) []*models.CommitFile {
return lo.Map(lo.Chunk(lines, 2), func(chunk []string, _ int) *models.CommitFile {
return &models.CommitFile{
ChangeStatus: chunk[0],
Name: chunk[1],
Path: chunk[1],
}
})
}
12 changes: 6 additions & 6 deletions pkg/commands/git_commands/commit_file_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestGetCommitFilesFromFilenames(t *testing.T) {
input: "MM\x00Myfile\x00",
output: []*models.CommitFile{
{
Name: "Myfile",
Path: "Myfile",
ChangeStatus: "MM",
},
},
Expand All @@ -33,11 +33,11 @@ func TestGetCommitFilesFromFilenames(t *testing.T) {
input: "MM\x00Myfile\x00M \x00MyOtherFile\x00",
output: []*models.CommitFile{
{
Name: "Myfile",
Path: "Myfile",
ChangeStatus: "MM",
},
{
Name: "MyOtherFile",
Path: "MyOtherFile",
ChangeStatus: "M ",
},
},
Expand All @@ -47,15 +47,15 @@ func TestGetCommitFilesFromFilenames(t *testing.T) {
input: "MM\x00Myfile\x00M \x00MyOtherFile\x00 M\x00YetAnother\x00",
output: []*models.CommitFile{
{
Name: "Myfile",
Path: "Myfile",
ChangeStatus: "MM",
},
{
Name: "MyOtherFile",
Path: "MyOtherFile",
ChangeStatus: "M ",
},
{
Name: "YetAnother",
Path: "YetAnother",
ChangeStatus: " M",
},
},
Expand Down
22 changes: 11 additions & 11 deletions pkg/commands/git_commands/file_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File
}

file := &models.File{
Name: status.Name,
PreviousName: status.PreviousName,
Path: status.Path,
PreviousPath: status.PreviousPath,
DisplayString: status.StatusString,
}

if diff, ok := fileDiffs[status.Name]; ok {
if diff, ok := fileDiffs[status.Path]; ok {
file.LinesAdded = diff.LinesAdded
file.LinesDeleted = diff.LinesDeleted
}
Expand All @@ -87,7 +87,7 @@ func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File
worktreePaths := linkedWortkreePaths(self.Fs, self.repoPaths.RepoGitDirPath())
for _, file := range files {
for _, worktreePath := range worktreePaths {
absFilePath, err := filepath.Abs(file.Name)
absFilePath, err := filepath.Abs(file.Path)
if err != nil {
self.Log.Error(err)
continue
Expand All @@ -96,7 +96,7 @@ func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File
file.IsWorktree = true
// `git status` renders this worktree as a folder with a trailing slash but we'll represent it as a singular worktree
// If we include the slash, it will be rendered as a folder with a null file inside.
file.Name = strings.TrimSuffix(file.Name, "/")
file.Path = strings.TrimSuffix(file.Path, "/")
break
}
}
Expand Down Expand Up @@ -153,8 +153,8 @@ type GitStatusOptions struct {
type FileStatus struct {
StatusString string
Change string // ??, MM, AM, ...
Name string
PreviousName string
Path string
PreviousPath string
}

func (fileLoader *FileLoader) gitDiffNumStat() (string, error) {
Expand Down Expand Up @@ -197,14 +197,14 @@ func (self *FileLoader) gitStatus(opts GitStatusOptions) ([]FileStatus, error) {
status := FileStatus{
StatusString: original,
Change: original[:2],
Name: original[3:],
PreviousName: "",
Path: original[3:],
PreviousPath: "",
}

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)
status.PreviousPath = splitLines[i+1]
status.StatusString = fmt.Sprintf("%s %s -> %s", status.Change, status.PreviousPath, status.Path)
i++
}

Expand Down
22 changes: 11 additions & 11 deletions pkg/commands/git_commands/file_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestFileGetStatusFiles(t *testing.T) {
showNumstatInFilesView: true,
expectedFiles: []*models.File{
{
Name: "file1.txt",
Path: "file1.txt",
HasStagedChanges: true,
HasUnstagedChanges: true,
Tracked: true,
Expand All @@ -55,7 +55,7 @@ func TestFileGetStatusFiles(t *testing.T) {
LinesDeleted: 1,
},
{
Name: "file3.txt",
Path: "file3.txt",
HasStagedChanges: true,
HasUnstagedChanges: false,
Tracked: false,
Expand All @@ -69,7 +69,7 @@ func TestFileGetStatusFiles(t *testing.T) {
LinesDeleted: 2,
},
{
Name: "file2.txt",
Path: "file2.txt",
HasStagedChanges: true,
HasUnstagedChanges: true,
Tracked: false,
Expand All @@ -83,7 +83,7 @@ func TestFileGetStatusFiles(t *testing.T) {
LinesDeleted: 0,
},
{
Name: "file4.txt",
Path: "file4.txt",
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: false,
Expand All @@ -97,7 +97,7 @@ func TestFileGetStatusFiles(t *testing.T) {
LinesDeleted: 2,
},
{
Name: "file5.txt",
Path: "file5.txt",
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: true,
Expand All @@ -119,7 +119,7 @@ func TestFileGetStatusFiles(t *testing.T) {
ExpectGitArgs([]string{"status", "--untracked-files=yes", "--porcelain", "-z", "--find-renames=50%"}, "MM a\nb.txt", nil),
expectedFiles: []*models.File{
{
Name: "a\nb.txt",
Path: "a\nb.txt",
HasStagedChanges: true,
HasUnstagedChanges: true,
Tracked: true,
Expand All @@ -142,8 +142,8 @@ func TestFileGetStatusFiles(t *testing.T) {
),
expectedFiles: []*models.File{
{
Name: "after1.txt",
PreviousName: "before1.txt",
Path: "after1.txt",
PreviousPath: "before1.txt",
HasStagedChanges: true,
HasUnstagedChanges: false,
Tracked: true,
Expand All @@ -155,8 +155,8 @@ func TestFileGetStatusFiles(t *testing.T) {
ShortStatus: "R ",
},
{
Name: "after2.txt",
PreviousName: "before2.txt",
Path: "after2.txt",
PreviousPath: "before2.txt",
HasStagedChanges: true,
HasUnstagedChanges: true,
Tracked: true,
Expand All @@ -179,7 +179,7 @@ func TestFileGetStatusFiles(t *testing.T) {
),
expectedFiles: []*models.File{
{
Name: "a -> b.txt",
Path: "a -> b.txt",
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: false,
Expand Down
18 changes: 9 additions & 9 deletions pkg/commands/git_commands/working_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ func (self *WorkingTreeCommands) BeforeAndAfterFileForRename(file *models.File)
var beforeFile *models.File
var afterFile *models.File
for _, f := range filesWithoutRenames {
if f.Name == file.PreviousName {
if f.Path == file.PreviousPath {
beforeFile = f
}

if f.Name == file.Name {
if f.Path == file.Path {
afterFile = f
}
}
Expand Down Expand Up @@ -134,13 +134,13 @@ func (self *WorkingTreeCommands) DiscardAllFileChanges(file *models.File) error

if file.ShortStatus == "AA" {
if err := self.cmd.New(
newCheckoutCommand().Arg("--ours", "--", file.Name).ToArgv(),
newCheckoutCommand().Arg("--ours", "--", file.Path).ToArgv(),
).Run(); err != nil {
return err
}

if err := self.cmd.New(
NewGitCmd("add").Arg("--", file.Name).ToArgv(),
NewGitCmd("add").Arg("--", file.Path).ToArgv(),
).Run(); err != nil {
return err
}
Expand All @@ -149,14 +149,14 @@ func (self *WorkingTreeCommands) DiscardAllFileChanges(file *models.File) error

if file.ShortStatus == "DU" {
return self.cmd.New(
NewGitCmd("rm").Arg("--", file.Name).ToArgv(),
NewGitCmd("rm").Arg("--", file.Path).ToArgv(),
).Run()
}

// if the file isn't tracked, we assume you want to delete it
if file.HasStagedChanges || file.HasMergeConflicts {
if err := self.cmd.New(
NewGitCmd("reset").Arg("--", file.Name).ToArgv(),
NewGitCmd("reset").Arg("--", file.Path).ToArgv(),
).Run(); err != nil {
return err
}
Expand All @@ -167,7 +167,7 @@ func (self *WorkingTreeCommands) DiscardAllFileChanges(file *models.File) error
}

if file.Added {
return self.os.RemoveFile(file.Name)
return self.os.RemoveFile(file.Path)
}

return self.DiscardUnstagedFileChanges(file)
Expand Down Expand Up @@ -199,7 +199,7 @@ func (self *WorkingTreeCommands) DiscardUnstagedDirChanges(node IFileNode) error
}
} else {
if file.Added && !file.HasStagedChanges {
return self.os.RemoveFile(file.Name)
return self.os.RemoveFile(file.Path)
}

if err := self.DiscardUnstagedFileChanges(file); err != nil {
Expand All @@ -226,7 +226,7 @@ func (self *WorkingTreeCommands) RemoveUntrackedDirFiles(node IFileNode) error {
}

func (self *WorkingTreeCommands) DiscardUnstagedFileChanges(file *models.File) error {
cmdArgs := newCheckoutCommand().Arg("--", file.Name).ToArgv()
cmdArgs := newCheckoutCommand().Arg("--", file.Path).ToArgv()
return self.cmd.New(cmdArgs).Run()
}

Expand Down
Loading
Loading