diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2021-04-12 21:57:43 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-04-17 10:04:49 +1000 |
commit | 9e5f031553dcef0a73afd86cedfa9b7adf68e460 (patch) | |
tree | 750f0dcc4909070e7c1401b2d5806ba7d530bf32 /pkg/gui/filetree | |
parent | dab5ba363cced5e8f5e79968f28e4bb079ea5944 (diff) |
bubble up tracked files in flat file view
Diffstat (limited to 'pkg/gui/filetree')
-rw-r--r-- | pkg/gui/filetree/build_tree.go | 31 | ||||
-rw-r--r-- | pkg/gui/filetree/build_tree_test.go | 46 |
2 files changed, 64 insertions, 13 deletions
diff --git a/pkg/gui/filetree/build_tree.go b/pkg/gui/filetree/build_tree.go index 128ec075c..36034d02d 100644 --- a/pkg/gui/filetree/build_tree.go +++ b/pkg/gui/filetree/build_tree.go @@ -97,10 +97,35 @@ func BuildFlatTreeFromFiles(files []*models.File) *FileNode { rootAux := BuildTreeFromFiles(files) sortedFiles := rootAux.GetLeaves() - // Move merge conflicts to top. This is the one way in which sorting - // differs between flat mode and tree mode + // from top down we have merge conflict files, then tracked file, then untracked + // files. This is the one way in which sorting differs between flat mode and + // tree mode sort.SliceStable(sortedFiles, func(i, j int) bool { - return sortedFiles[i].File != nil && sortedFiles[i].File.HasMergeConflicts && !(sortedFiles[j].File != nil && sortedFiles[j].File.HasMergeConflicts) + iFile := sortedFiles[i].File + jFile := sortedFiles[j].File + + // never going to happen but just to be safe + if iFile == nil || jFile == nil { + return false + } + + if iFile.HasMergeConflicts && !jFile.HasMergeConflicts { + return true + } + + if jFile.HasMergeConflicts && !iFile.HasMergeConflicts { + return false + } + + if iFile.Tracked && !jFile.Tracked { + return true + } + + if jFile.Tracked && !iFile.Tracked { + return false + } + + return false }) return &FileNode{Children: sortedFiles} diff --git a/pkg/gui/filetree/build_tree_test.go b/pkg/gui/filetree/build_tree_test.go index 7f702cd1e..f716fca66 100644 --- a/pkg/gui/filetree/build_tree_test.go +++ b/pkg/gui/filetree/build_tree_test.go @@ -236,33 +236,59 @@ func TestBuildFlatTreeFromFiles(t *testing.T) { }, }, { - name: "paths that can be sorted including a merge conflict file", + name: "tracked, untracked, and conflicted files", files: []*models.File{ { - Name: "z", + Name: "a2", + Tracked: false, + }, + { + Name: "a1", + Tracked: false, + }, + { + Name: "c2", HasMergeConflicts: true, }, { - Name: "b", + Name: "c1", + HasMergeConflicts: true, }, { - Name: "a", + Name: "b2", + Tracked: true, + }, + { + Name: "b1", + Tracked: true, }, }, expected: &FileNode{ Path: "", Children: []*FileNode{ { - File: &models.File{Name: "z", HasMergeConflicts: true}, - Path: "z", + File: &models.File{Name: "c1", HasMergeConflicts: true}, + Path: "c1", }, { - File: &models.File{Name: "a"}, - Path: "a", + File: &models.File{Name: "c2", HasMergeConflicts: true}, + Path: "c2", }, { - File: &models.File{Name: "b"}, - Path: "b", + File: &models.File{Name: "b1", Tracked: true}, + Path: "b1", + }, + { + File: &models.File{Name: "b2", Tracked: true}, + Path: "b2", + }, + { + File: &models.File{Name: "a1", Tracked: false}, + Path: "a1", + }, + { + File: &models.File{Name: "a2", Tracked: false}, + Path: "a2", }, }, }, |