aboutsummaryrefslogtreecommitdiffstats
path: root/worktree_status.go
diff options
context:
space:
mode:
authorOleg Sklyar <osklyar@gmx.com>2017-06-19 00:26:14 +0200
committerOleg Sklyar <osklyar@gmx.com>2017-06-19 00:26:14 +0200
commit2f4ac21bad4c14b860a7d5c9d761857cb8d4f89c (patch)
tree869b08e65c35bf80dfd15f665d100e5ca3539917 /worktree_status.go
parent2a00316b65585be2bf68e1ea9c0e42c6af4f5679 (diff)
downloadgo-git-2f4ac21bad4c14b860a7d5c9d761857cb8d4f89c.tar.gz
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{}