Skip to content

Commit 31394d8

Browse files
committed
Support a .trac-excludes file in the repo directory.
1 parent c2bc443 commit 31394d8

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

git-subtrac.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ func main() {
5959
debugf = func(fmt string, args ...interface{}) {}
6060
}
6161

62-
c := NewCache(*repodir, r, *excludes, *autoexclude, debugf, infof)
62+
c, err := NewCache(*repodir, r, *excludes, *autoexclude, debugf, infof)
63+
if err != nil {
64+
fatalf("NewCache: %v\n", err)
65+
}
6366

6467
switch args[0] {
6568
case "update":

subtrac.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bufio"
45
"fmt"
56
"gopkg.in/src-d/go-git.v4"
67
"gopkg.in/src-d/go-git.v4/config"
@@ -51,7 +52,7 @@ type Cache struct {
5152

5253
func NewCache(rdir string, r *git.Repository, excludes []string,
5354
autoexclude bool,
54-
debugf, infof func(fmt string, args ...interface{})) *Cache {
55+
debugf, infof func(fmt string, args ...interface{})) (*Cache, error) {
5556
c := Cache{
5657
debugf: debugf,
5758
infof: infof,
@@ -61,11 +62,40 @@ func NewCache(rdir string, r *git.Repository, excludes []string,
6162
excludes: make(map[plumbing.Hash]bool),
6263
tracs: make(map[plumbing.Hash]*Trac),
6364
}
65+
6466
for _, x := range excludes {
6567
hash := plumbing.NewHash(x)
6668
c.exclude(hash)
6769
}
68-
return &c
70+
71+
wt, err := r.Worktree()
72+
if err != nil {
73+
return nil, fmt.Errorf("git worktree: %v", err)
74+
}
75+
f, err := wt.Filesystem.Open(".trac-excludes")
76+
if err == nil { // file might not exist, but that's ok
77+
r := bufio.NewReader(f)
78+
for {
79+
line, err := r.ReadString('\n')
80+
if err != nil {
81+
break
82+
}
83+
// trim comments
84+
pos := strings.Index(line, "#")
85+
if pos >= 0 {
86+
line = line[:pos]
87+
}
88+
// trim whitespace
89+
line = strings.TrimSpace(line)
90+
91+
if line != "" {
92+
hash := plumbing.NewHash(line)
93+
c.exclude(hash)
94+
}
95+
}
96+
}
97+
98+
return &c, nil
6999
}
70100

71101
func (c *Cache) String() string {
@@ -89,7 +119,6 @@ func (c *Cache) String() string {
89119
func (c *Cache) exclude(hash plumbing.Hash) {
90120
if !c.excludes[hash] {
91121
c.excludes[hash] = true
92-
c.infof("Excluding %v\n", hash)
93122
}
94123
}
95124

@@ -460,6 +489,7 @@ func (c *Cache) tracTree(path string, tree *object.Tree) (*Trac, error) {
460489
err = c.tryFetchFromSubmodules(subpath, e.Hash)
461490
if err != nil {
462491
if c.autoexclude {
492+
c.infof("Excluding %v\n", e.Hash)
463493
c.exclude(e.Hash)
464494
continue
465495
}

0 commit comments

Comments
 (0)