From 7cdcef8c931ea77dab3345b5c936c26b19a69c2d Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 2 Jun 2018 09:05:20 +1000 Subject: [PATCH] picking up new files upon file refresh --- gitcommands.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/gitcommands.go b/gitcommands.go index 3cadaa08f..6ebfabcfc 100644 --- a/gitcommands.go +++ b/gitcommands.go @@ -70,7 +70,18 @@ func Map(vs []string, f func(string) string) []string { return vsm } -func includes(list []string, a string) bool { +func includesString(list []string, a string) bool { + for _, b := range list { + if b == a { + return true + } + } + return false +} + +// not sure how to genericise this because []interface{} doesn't accept e.g. +// []int arguments +func includesInt(list []int, a int) bool { for _, b := range list { if b == a { return true @@ -84,15 +95,27 @@ func mergeGitStatusFiles(oldGitFiles, newGitFiles []GitFile) []GitFile { return newGitFiles } + appendedIndexes := make([]int, 0) + + // retain position of files we already could see result := make([]GitFile, 0) for _, oldGitFile := range oldGitFiles { - for _, newGitFile := range newGitFiles { + for newIndex, newGitFile := range newGitFiles { if oldGitFile.Name == newGitFile.Name { result = append(result, newGitFile) + appendedIndexes = append(appendedIndexes, newIndex) break } } } + + // append any new files to the end + for index, newGitFile := range newGitFiles { + if !includesInt(appendedIndexes, index) { + result = append(result, newGitFile) + } + } + return result } @@ -230,7 +253,7 @@ func getCommits() []Commit { for _, line := range lines { splitLine := strings.Split(line, " ") sha := splitLine[0] - pushed := includes(pushables, sha) + pushed := includesString(pushables, sha) commits = append(commits, Commit{ Sha: sha, Name: strings.Join(splitLine[1:], " "),