Skip to content

Commit 1107462

Browse files
authored
Add root node in file tree (#4346)
- **PR Description** If files at the root level of the repository have changed, there was no way to see the combined diff of all of them. Fix this by inserting a `/` item in this case. This is useful in repositories such as git.git, which have all their `.c` files at root level (gasp). If only files inside some folder have changed, the root item gets compressed away automatically, so nothing changes for that case (which I guess should be more common for most people). Addresses #4331. - **Please check if the PR fulfills these requirements** * [x] Cheatsheets are up-to-date (run `go generate ./...`) * [x] Code has been formatted (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#code-formatting)) * [x] Tests have been added/updated (see [here](https://github.com/jesseduffield/lazygit/blob/master/pkg/integration/README.md) for the integration test guide) * [ ] Text is internationalised (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#internationalisation)) * [ ] If a new UserConfig entry was added, make sure it can be hot-reloaded (see [here](https://github.com/jesseduffield/lazygit/blob/master/docs/dev/Codebase_Guide.md#using-userconfig)) * [ ] Docs have been updated if necessary * [x] You've read through your own file changes for silly mistakes etc
2 parents e8d3a7a + 2645952 commit 1107462

File tree

78 files changed

+964
-747
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+964
-747
lines changed

pkg/commands/git_commands/commit_file_loader.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func getCommitFilesFromFilenames(filenames string) []*models.CommitFile {
5555
return lo.Map(lo.Chunk(lines, 2), func(chunk []string, _ int) *models.CommitFile {
5656
return &models.CommitFile{
5757
ChangeStatus: chunk[0],
58-
Name: chunk[1],
58+
Path: chunk[1],
5959
}
6060
})
6161
}

pkg/commands/git_commands/commit_file_loader_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestGetCommitFilesFromFilenames(t *testing.T) {
2323
input: "MM\x00Myfile\x00",
2424
output: []*models.CommitFile{
2525
{
26-
Name: "Myfile",
26+
Path: "Myfile",
2727
ChangeStatus: "MM",
2828
},
2929
},
@@ -33,11 +33,11 @@ func TestGetCommitFilesFromFilenames(t *testing.T) {
3333
input: "MM\x00Myfile\x00M \x00MyOtherFile\x00",
3434
output: []*models.CommitFile{
3535
{
36-
Name: "Myfile",
36+
Path: "Myfile",
3737
ChangeStatus: "MM",
3838
},
3939
{
40-
Name: "MyOtherFile",
40+
Path: "MyOtherFile",
4141
ChangeStatus: "M ",
4242
},
4343
},
@@ -47,15 +47,15 @@ func TestGetCommitFilesFromFilenames(t *testing.T) {
4747
input: "MM\x00Myfile\x00M \x00MyOtherFile\x00 M\x00YetAnother\x00",
4848
output: []*models.CommitFile{
4949
{
50-
Name: "Myfile",
50+
Path: "Myfile",
5151
ChangeStatus: "MM",
5252
},
5353
{
54-
Name: "MyOtherFile",
54+
Path: "MyOtherFile",
5555
ChangeStatus: "M ",
5656
},
5757
{
58-
Name: "YetAnother",
58+
Path: "YetAnother",
5959
ChangeStatus: " M",
6060
},
6161
},

pkg/commands/git_commands/file_loader.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File
6868
}
6969

7070
file := &models.File{
71-
Name: status.Name,
72-
PreviousName: status.PreviousName,
71+
Path: status.Path,
72+
PreviousPath: status.PreviousPath,
7373
DisplayString: status.StatusString,
7474
}
7575

76-
if diff, ok := fileDiffs[status.Name]; ok {
76+
if diff, ok := fileDiffs[status.Path]; ok {
7777
file.LinesAdded = diff.LinesAdded
7878
file.LinesDeleted = diff.LinesDeleted
7979
}
@@ -87,7 +87,7 @@ func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File
8787
worktreePaths := linkedWortkreePaths(self.Fs, self.repoPaths.RepoGitDirPath())
8888
for _, file := range files {
8989
for _, worktreePath := range worktreePaths {
90-
absFilePath, err := filepath.Abs(file.Name)
90+
absFilePath, err := filepath.Abs(file.Path)
9191
if err != nil {
9292
self.Log.Error(err)
9393
continue
@@ -96,7 +96,7 @@ func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File
9696
file.IsWorktree = true
9797
// `git status` renders this worktree as a folder with a trailing slash but we'll represent it as a singular worktree
9898
// If we include the slash, it will be rendered as a folder with a null file inside.
99-
file.Name = strings.TrimSuffix(file.Name, "/")
99+
file.Path = strings.TrimSuffix(file.Path, "/")
100100
break
101101
}
102102
}
@@ -153,8 +153,8 @@ type GitStatusOptions struct {
153153
type FileStatus struct {
154154
StatusString string
155155
Change string // ??, MM, AM, ...
156-
Name string
157-
PreviousName string
156+
Path string
157+
PreviousPath string
158158
}
159159

160160
func (fileLoader *FileLoader) gitDiffNumStat() (string, error) {
@@ -197,14 +197,14 @@ func (self *FileLoader) gitStatus(opts GitStatusOptions) ([]FileStatus, error) {
197197
status := FileStatus{
198198
StatusString: original,
199199
Change: original[:2],
200-
Name: original[3:],
201-
PreviousName: "",
200+
Path: original[3:],
201+
PreviousPath: "",
202202
}
203203

204204
if strings.HasPrefix(status.Change, "R") {
205205
// if a line starts with 'R' then the next line is the original file.
206-
status.PreviousName = splitLines[i+1]
207-
status.StatusString = fmt.Sprintf("%s %s -> %s", status.Change, status.PreviousName, status.Name)
206+
status.PreviousPath = splitLines[i+1]
207+
status.StatusString = fmt.Sprintf("%s %s -> %s", status.Change, status.PreviousPath, status.Path)
208208
i++
209209
}
210210

pkg/commands/git_commands/file_loader_test.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestFileGetStatusFiles(t *testing.T) {
4141
showNumstatInFilesView: true,
4242
expectedFiles: []*models.File{
4343
{
44-
Name: "file1.txt",
44+
Path: "file1.txt",
4545
HasStagedChanges: true,
4646
HasUnstagedChanges: true,
4747
Tracked: true,
@@ -55,7 +55,7 @@ func TestFileGetStatusFiles(t *testing.T) {
5555
LinesDeleted: 1,
5656
},
5757
{
58-
Name: "file3.txt",
58+
Path: "file3.txt",
5959
HasStagedChanges: true,
6060
HasUnstagedChanges: false,
6161
Tracked: false,
@@ -69,7 +69,7 @@ func TestFileGetStatusFiles(t *testing.T) {
6969
LinesDeleted: 2,
7070
},
7171
{
72-
Name: "file2.txt",
72+
Path: "file2.txt",
7373
HasStagedChanges: true,
7474
HasUnstagedChanges: true,
7575
Tracked: false,
@@ -83,7 +83,7 @@ func TestFileGetStatusFiles(t *testing.T) {
8383
LinesDeleted: 0,
8484
},
8585
{
86-
Name: "file4.txt",
86+
Path: "file4.txt",
8787
HasStagedChanges: false,
8888
HasUnstagedChanges: true,
8989
Tracked: false,
@@ -97,7 +97,7 @@ func TestFileGetStatusFiles(t *testing.T) {
9797
LinesDeleted: 2,
9898
},
9999
{
100-
Name: "file5.txt",
100+
Path: "file5.txt",
101101
HasStagedChanges: false,
102102
HasUnstagedChanges: true,
103103
Tracked: true,
@@ -119,7 +119,7 @@ func TestFileGetStatusFiles(t *testing.T) {
119119
ExpectGitArgs([]string{"status", "--untracked-files=yes", "--porcelain", "-z", "--find-renames=50%"}, "MM a\nb.txt", nil),
120120
expectedFiles: []*models.File{
121121
{
122-
Name: "a\nb.txt",
122+
Path: "a\nb.txt",
123123
HasStagedChanges: true,
124124
HasUnstagedChanges: true,
125125
Tracked: true,
@@ -142,8 +142,8 @@ func TestFileGetStatusFiles(t *testing.T) {
142142
),
143143
expectedFiles: []*models.File{
144144
{
145-
Name: "after1.txt",
146-
PreviousName: "before1.txt",
145+
Path: "after1.txt",
146+
PreviousPath: "before1.txt",
147147
HasStagedChanges: true,
148148
HasUnstagedChanges: false,
149149
Tracked: true,
@@ -155,8 +155,8 @@ func TestFileGetStatusFiles(t *testing.T) {
155155
ShortStatus: "R ",
156156
},
157157
{
158-
Name: "after2.txt",
159-
PreviousName: "before2.txt",
158+
Path: "after2.txt",
159+
PreviousPath: "before2.txt",
160160
HasStagedChanges: true,
161161
HasUnstagedChanges: true,
162162
Tracked: true,
@@ -179,7 +179,7 @@ func TestFileGetStatusFiles(t *testing.T) {
179179
),
180180
expectedFiles: []*models.File{
181181
{
182-
Name: "a -> b.txt",
182+
Path: "a -> b.txt",
183183
HasStagedChanges: false,
184184
HasUnstagedChanges: true,
185185
Tracked: false,

pkg/commands/git_commands/working_tree.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ func (self *WorkingTreeCommands) BeforeAndAfterFileForRename(file *models.File)
9292
var beforeFile *models.File
9393
var afterFile *models.File
9494
for _, f := range filesWithoutRenames {
95-
if f.Name == file.PreviousName {
95+
if f.Path == file.PreviousPath {
9696
beforeFile = f
9797
}
9898

99-
if f.Name == file.Name {
99+
if f.Path == file.Path {
100100
afterFile = f
101101
}
102102
}
@@ -134,13 +134,13 @@ func (self *WorkingTreeCommands) DiscardAllFileChanges(file *models.File) error
134134

135135
if file.ShortStatus == "AA" {
136136
if err := self.cmd.New(
137-
NewGitCmd("checkout").Arg("--ours", "--", file.Name).ToArgv(),
137+
NewGitCmd("checkout").Arg("--ours", "--", file.Path).ToArgv(),
138138
).Run(); err != nil {
139139
return err
140140
}
141141

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

150150
if file.ShortStatus == "DU" {
151151
return self.cmd.New(
152-
NewGitCmd("rm").Arg("--", file.Name).ToArgv(),
152+
NewGitCmd("rm").Arg("--", file.Path).ToArgv(),
153153
).Run()
154154
}
155155

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

169169
if file.Added {
170-
return self.os.RemoveFile(file.Name)
170+
return self.os.RemoveFile(file.Path)
171171
}
172172

173173
return self.DiscardUnstagedFileChanges(file)
@@ -199,7 +199,7 @@ func (self *WorkingTreeCommands) DiscardUnstagedDirChanges(node IFileNode) error
199199
}
200200
} else {
201201
if file.Added && !file.HasStagedChanges {
202-
return self.os.RemoveFile(file.Name)
202+
return self.os.RemoveFile(file.Path)
203203
}
204204

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

228228
func (self *WorkingTreeCommands) DiscardUnstagedFileChanges(file *models.File) error {
229-
cmdArgs := NewGitCmd("checkout").Arg("--", file.Name).ToArgv()
229+
cmdArgs := NewGitCmd("checkout").Arg("--", file.Path).ToArgv()
230230
return self.cmd.New(cmdArgs).Run()
231231
}
232232

0 commit comments

Comments
 (0)