Rename Name to Path in File and CommitFile

Name was very confusing and misleading.
This commit is contained in:
Stefan Haller 2025-02-28 20:59:09 +01:00
parent 0b5504aa98
commit 2dfc3491bd
22 changed files with 217 additions and 218 deletions

View file

@ -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],
}
})
}

View file

@ -23,7 +23,7 @@ func TestGetCommitFilesFromFilenames(t *testing.T) {
input: "MM\x00Myfile\x00",
output: []*models.CommitFile{
{
Name: "Myfile",
Path: "Myfile",
ChangeStatus: "MM",
},
},
@ -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 ",
},
},
@ -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",
},
},

View file

@ -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
}
@ -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
@ -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
}
}
@ -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) {
@ -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++
}

View file

@ -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,
@ -55,7 +55,7 @@ func TestFileGetStatusFiles(t *testing.T) {
LinesDeleted: 1,
},
{
Name: "file3.txt",
Path: "file3.txt",
HasStagedChanges: true,
HasUnstagedChanges: false,
Tracked: false,
@ -69,7 +69,7 @@ func TestFileGetStatusFiles(t *testing.T) {
LinesDeleted: 2,
},
{
Name: "file2.txt",
Path: "file2.txt",
HasStagedChanges: true,
HasUnstagedChanges: true,
Tracked: false,
@ -83,7 +83,7 @@ func TestFileGetStatusFiles(t *testing.T) {
LinesDeleted: 0,
},
{
Name: "file4.txt",
Path: "file4.txt",
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: false,
@ -97,7 +97,7 @@ func TestFileGetStatusFiles(t *testing.T) {
LinesDeleted: 2,
},
{
Name: "file5.txt",
Path: "file5.txt",
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: true,
@ -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,
@ -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,
@ -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,
@ -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,

View file

@ -92,11 +92,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
}
}
@ -134,13 +134,13 @@ func (self *WorkingTreeCommands) DiscardAllFileChanges(file *models.File) error
if file.ShortStatus == "AA" {
if err := self.cmd.New(
NewGitCmd("checkout").Arg("--ours", "--", file.Name).ToArgv(),
NewGitCmd("checkout").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
}
@ -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
}
@ -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)
@ -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 {
@ -226,7 +226,7 @@ func (self *WorkingTreeCommands) RemoveUntrackedDirFiles(node IFileNode) error {
}
func (self *WorkingTreeCommands) DiscardUnstagedFileChanges(file *models.File) error {
cmdArgs := NewGitCmd("checkout").Arg("--", file.Name).ToArgv()
cmdArgs := NewGitCmd("checkout").Arg("--", file.Path).ToArgv()
return self.cmd.New(cmdArgs).Run()
}

View file

@ -83,7 +83,7 @@ func TestWorkingTreeDiscardAllFileChanges(t *testing.T) {
{
testName: "An error occurred when resetting",
file: &models.File{
Name: "test",
Path: "test",
HasStagedChanges: true,
},
removeFile: func(string) error { return nil },
@ -94,7 +94,7 @@ func TestWorkingTreeDiscardAllFileChanges(t *testing.T) {
{
testName: "An error occurred when removing file",
file: &models.File{
Name: "test",
Path: "test",
Tracked: false,
Added: true,
},
@ -107,7 +107,7 @@ func TestWorkingTreeDiscardAllFileChanges(t *testing.T) {
{
testName: "An error occurred with checkout",
file: &models.File{
Name: "test",
Path: "test",
Tracked: true,
HasStagedChanges: false,
},
@ -119,7 +119,7 @@ func TestWorkingTreeDiscardAllFileChanges(t *testing.T) {
{
testName: "Checkout only",
file: &models.File{
Name: "test",
Path: "test",
Tracked: true,
HasStagedChanges: false,
},
@ -131,7 +131,7 @@ func TestWorkingTreeDiscardAllFileChanges(t *testing.T) {
{
testName: "Reset and checkout staged changes",
file: &models.File{
Name: "test",
Path: "test",
Tracked: true,
HasStagedChanges: true,
},
@ -144,7 +144,7 @@ func TestWorkingTreeDiscardAllFileChanges(t *testing.T) {
{
testName: "Reset and checkout merge conflicts",
file: &models.File{
Name: "test",
Path: "test",
Tracked: true,
HasMergeConflicts: true,
},
@ -157,7 +157,7 @@ func TestWorkingTreeDiscardAllFileChanges(t *testing.T) {
{
testName: "Reset and remove",
file: &models.File{
Name: "test",
Path: "test",
Tracked: false,
Added: true,
HasStagedChanges: true,
@ -173,7 +173,7 @@ func TestWorkingTreeDiscardAllFileChanges(t *testing.T) {
{
testName: "Remove only",
file: &models.File{
Name: "test",
Path: "test",
Tracked: false,
Added: true,
HasStagedChanges: false,
@ -220,7 +220,7 @@ func TestWorkingTreeDiff(t *testing.T) {
{
testName: "Default case",
file: &models.File{
Name: "test.txt",
Path: "test.txt",
HasStagedChanges: false,
Tracked: true,
},
@ -235,7 +235,7 @@ func TestWorkingTreeDiff(t *testing.T) {
{
testName: "cached",
file: &models.File{
Name: "test.txt",
Path: "test.txt",
HasStagedChanges: false,
Tracked: true,
},
@ -250,7 +250,7 @@ func TestWorkingTreeDiff(t *testing.T) {
{
testName: "plain",
file: &models.File{
Name: "test.txt",
Path: "test.txt",
HasStagedChanges: false,
Tracked: true,
},
@ -265,7 +265,7 @@ func TestWorkingTreeDiff(t *testing.T) {
{
testName: "File not tracked and file has no staged changes",
file: &models.File{
Name: "test.txt",
Path: "test.txt",
HasStagedChanges: false,
Tracked: false,
},
@ -280,7 +280,7 @@ func TestWorkingTreeDiff(t *testing.T) {
{
testName: "Default case (ignore whitespace)",
file: &models.File{
Name: "test.txt",
Path: "test.txt",
HasStagedChanges: false,
Tracked: true,
},
@ -295,7 +295,7 @@ func TestWorkingTreeDiff(t *testing.T) {
{
testName: "Show diff with custom context size",
file: &models.File{
Name: "test.txt",
Path: "test.txt",
HasStagedChanges: false,
Tracked: true,
},
@ -310,7 +310,7 @@ func TestWorkingTreeDiff(t *testing.T) {
{
testName: "Show diff with custom similarity threshold",
file: &models.File{
Name: "test.txt",
Path: "test.txt",
HasStagedChanges: false,
Tracked: true,
},
@ -466,7 +466,7 @@ func TestWorkingTreeDiscardUnstagedFileChanges(t *testing.T) {
scenarios := []scenario{
{
testName: "valid case",
file: &models.File{Name: "test.txt"},
file: &models.File{Path: "test.txt"},
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"checkout", "--", "test.txt"}, "", nil),
test: func(err error) {

View file

@ -2,18 +2,17 @@ package models
// CommitFile : A git commit file
type CommitFile struct {
// TODO: rename this to Path
Name string
Path string
ChangeStatus string // e.g. 'A' for added or 'M' for modified. This is based on the result from git diff --name-status
}
func (f *CommitFile) ID() string {
return f.Name
return f.Path
}
func (f *CommitFile) Description() string {
return f.Name
return f.Path
}
func (f *CommitFile) Added() bool {
@ -25,5 +24,5 @@ func (f *CommitFile) Deleted() bool {
}
func (f *CommitFile) GetPath() string {
return f.Name
return f.Path
}

View file

@ -8,8 +8,8 @@ import (
// File : A file from git status
// duplicating this for now
type File struct {
Name string
PreviousName string
Path string
PreviousPath string
HasStagedChanges bool
HasUnstagedChanges bool
Tracked bool
@ -37,14 +37,14 @@ type IFile interface {
}
func (f *File) IsRename() bool {
return f.PreviousName != ""
return f.PreviousPath != ""
}
// Names returns an array containing just the filename, or in the case of a rename, the after filename and the before filename
func (f *File) Names() []string {
result := []string{f.Name}
if f.PreviousName != "" {
result = append(result, f.PreviousName)
result := []string{f.Path}
if f.PreviousPath != "" {
result = append(result, f.PreviousPath)
}
return result
}
@ -55,11 +55,11 @@ func (f *File) Matches(f2 *File) bool {
}
func (f *File) ID() string {
return f.Name
return f.Path
}
func (f *File) Description() string {
return f.Name
return f.Path
}
func (f *File) IsSubmodule(configs []*SubmoduleConfig) bool {
@ -68,7 +68,7 @@ func (f *File) IsSubmodule(configs []*SubmoduleConfig) bool {
func (f *File) SubmoduleConfig(configs []*SubmoduleConfig) *SubmoduleConfig {
for _, config := range configs {
if f.Name == config.Path {
if f.Path == config.Path {
return config
}
}
@ -90,11 +90,11 @@ func (f *File) GetIsTracked() bool {
func (f *File) GetPath() string {
// TODO: remove concept of name; just use path
return f.Name
return f.Path
}
func (f *File) GetPreviousPath() string {
return f.PreviousName
return f.PreviousPath
}
func (f *File) GetIsFile() bool {