summaryrefslogtreecommitdiffstats
path: root/pkg/commands
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2020-08-07 18:27:18 +1000
committerJesse Duffield <jessedduffield@gmail.com>2020-08-07 18:59:56 +1000
commit660cc2f3d1d5253817d52e8991f74e836d3db6ba (patch)
tree6ca6d605851b6f71d27713cf9f5cb5afb2e6eeac /pkg/commands
parent469ac116efde8dd107bc227ef8fc0f1927a629cb (diff)
follow cursor when staging and unstaging a file rename
Diffstat (limited to 'pkg/commands')
-rw-r--r--pkg/commands/file.go20
-rw-r--r--pkg/commands/git.go11
-rw-r--r--pkg/commands/git_test.go2
3 files changed, 27 insertions, 6 deletions
diff --git a/pkg/commands/file.go b/pkg/commands/file.go
index 754b2871f..4a277be22 100644
--- a/pkg/commands/file.go
+++ b/pkg/commands/file.go
@@ -1,6 +1,10 @@
package commands
-import "strings"
+import (
+ "strings"
+
+ "github.com/jesseduffield/lazygit/pkg/utils"
+)
// File : A file from git status
// duplicating this for now
@@ -17,6 +21,18 @@ type File struct {
ShortStatus string // e.g. 'AD', ' A', 'M ', '??'
}
+const RENAME_SEPARATOR = " -> "
+
func (f *File) IsRename() bool {
- return strings.Contains(f.Name, " -> ")
+ return strings.Contains(f.Name, RENAME_SEPARATOR)
+}
+
+// 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 {
+ return strings.Split(f.Name, RENAME_SEPARATOR)
+}
+
+// returns true if the file names are the same or if a a file rename includes the filename of the other
+func (f *File) Matches(f2 *File) bool {
+ return utils.StringArraysOverlap(f.Names(), f2.Names())
}
diff --git a/pkg/commands/git.go b/pkg/commands/git.go
index 20cbd9fb2..249221c1f 100644
--- a/pkg/commands/git.go
+++ b/pkg/commands/git.go
@@ -279,7 +279,7 @@ func (c *GitCommand) StashSave(message string) error {
}
// MergeStatusFiles merge status files
-func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []*File) []*File {
+func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []*File, selectedFile *File) []*File {
if len(oldFiles) == 0 {
return newFiles
}
@@ -290,10 +290,15 @@ func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []*File) []*File {
result := []*File{}
for _, oldFile := range oldFiles {
for newIndex, newFile := range newFiles {
- if oldFile.Name == newFile.Name {
+ if includesInt(appendedIndexes, newIndex) {
+ continue
+ }
+ // if we just staged B and in doing so created 'A -> B' and we are currently have oldFile: A and newFile: 'A -> B', we want to wait until we come across B so the our cursor isn't jumping anywhere
+ waitForMatchingFile := selectedFile != nil && newFile.IsRename() && !selectedFile.IsRename() && newFile.Matches(selectedFile) && !oldFile.Matches(selectedFile)
+
+ if oldFile.Matches(newFile) && !waitForMatchingFile {
result = append(result, newFile)
appendedIndexes = append(appendedIndexes, newIndex)
- break
}
}
}
diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go
index 43accd654..9b0ef5e11 100644
--- a/pkg/commands/git_test.go
+++ b/pkg/commands/git_test.go
@@ -541,7 +541,7 @@ func TestGitCommandMergeStatusFiles(t *testing.T) {
t.Run(s.testName, func(t *testing.T) {
gitCmd := NewDummyGitCommand()
- s.test(gitCmd.MergeStatusFiles(s.oldFiles, s.newFiles))
+ s.test(gitCmd.MergeStatusFiles(s.oldFiles, s.newFiles, nil))
})
}
}