aboutsummaryrefslogtreecommitdiffstats
path: root/worktree_status.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-06-19 10:00:49 +0200
committerGitHub <noreply@github.com>2017-06-19 10:00:49 +0200
commit93633b5767b0d571bedc724364209d30f96a7b17 (patch)
treec53170875387107cbf9945986728f6fb115054b9 /worktree_status.go
parent5d1c674ea818ea534476ed0ca5436e0b513f86a4 (diff)
parent2f4ac21bad4c14b860a7d5c9d761857cb8d4f89c (diff)
downloadgo-git-93633b5767b0d571bedc724364209d30f96a7b17.tar.gz
Merge pull request #429 from silvertern/gitignore
Adds .gitignore support
Diffstat (limited to 'worktree_status.go')
-rw-r--r--worktree_status.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/worktree_status.go b/worktree_status.go
index eb4a83a..7cc4b0f 100644
--- a/worktree_status.go
+++ b/worktree_status.go
@@ -8,6 +8,7 @@ import (
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
+ "gopkg.in/src-d/go-git.v4/plumbing/format/gitignore"
"gopkg.in/src-d/go-git.v4/plumbing/format/index"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/utils/ioutil"
@@ -67,6 +68,8 @@ func (w *Worktree) status(commit plumbing.Hash) (Status, error) {
return nil, err
}
+ right = w.excludeIgnoredChanges(right)
+
for _, ch := range right {
a, err := ch.Action()
if err != nil {
@@ -117,6 +120,35 @@ func (w *Worktree) diffStagingWithWorktree() (merkletrie.Changes, error) {
return merkletrie.DiffTree(from, to, diffTreeIsEquals)
}
+func (w *Worktree) excludeIgnoredChanges(changes merkletrie.Changes) merkletrie.Changes {
+ patterns, err := gitignore.ReadPatterns(w.fs, nil)
+ if err != nil || len(patterns) == 0 {
+ return changes
+ }
+ m := gitignore.NewMatcher(patterns)
+
+ var res merkletrie.Changes
+ for _, ch := range changes {
+ var path []string
+ for _, n := range ch.To {
+ path = append(path, n.Name())
+ }
+ if len(path) == 0 {
+ for _, n := range ch.From {
+ path = append(path, n.Name())
+ }
+ }
+ if len(path) != 0 {
+ isDir := (len(ch.To) > 0 && ch.To.IsDir()) || (len(ch.From) > 0 && ch.From.IsDir())
+ if m.Match(path, isDir) {
+ continue
+ }
+ }
+ res = append(res, ch)
+ }
+ return res
+}
+
func (w *Worktree) getSubmodulesStatus() (map[string]plumbing.Hash, error) {
o := map[string]plumbing.Hash{}