Skip to content

Commit 1551bd2

Browse files
committed
Add --exclude option to exclude submodule commitids when necessary.
1 parent 6a21040 commit 1551bd2

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

subtrac.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,20 @@ type Cache struct {
5252
tracs map[plumbing.Hash]*Trac
5353
}
5454

55-
func NewCache(rdir string, r *git.Repository) *Cache {
55+
func NewCache(rdir string, r *git.Repository, excludes []string) *Cache {
5656
c := Cache{
5757
repoDir: rdir,
5858
repo: r,
5959
tracs: make(map[plumbing.Hash]*Trac),
6060
}
61+
for _, x := range excludes {
62+
hash := plumbing.NewHash(x)
63+
trac := &Trac{
64+
name: "[excluded]",
65+
hash: hash,
66+
}
67+
c.add(trac)
68+
}
6169
return &c
6270
}
6371

@@ -143,7 +151,7 @@ func commitPath(path string, sub int) string {
143151
}
144152

145153
func (c *Cache) tryFetchFromSubmodules(path string, hash plumbing.Hash) error {
146-
debugf("Searching submodules for: %.10v %v\n", hash, path)
154+
debugf("Searching submodules for: %v\n", path)
147155
wt, err := c.repo.Worktree()
148156
if err != nil {
149157
return fmt.Errorf("git worktree: %v", err)
@@ -200,7 +208,7 @@ func (c *Cache) tryFetchFromSubmodules(path string, hash plumbing.Hash) error {
200208
}
201209
return nil
202210
}
203-
return fmt.Errorf("%v: %.10v not found.", path, hash)
211+
return fmt.Errorf("%v: %v not found.", path, hash)
204212
}
205213

206214
func (c *Cache) tracTree(path string, tree *object.Tree) (*Trac, error) {
@@ -210,6 +218,10 @@ func (c *Cache) tracTree(path string, tree *object.Tree) (*Trac, error) {
210218
}
211219
for _, e := range tree.Entries {
212220
if e.Mode == filemode.Submodule {
221+
if c.tracs[e.Hash] != nil {
222+
// already handled
223+
continue
224+
}
213225
subpath := fmt.Sprintf("%s%s@%.10v", path, e.Name, e.Hash)
214226
sc, err := c.repo.CommitObject(e.Hash)
215227
if err != nil {
@@ -269,6 +281,7 @@ func main() {
269281
log.SetFlags(0)
270282

271283
repodir := getopt.StringLong("git-dir", 'd', ".", "path to git repo")
284+
excludes := getopt.ListLong("exclude", 'x', "", "commitids to exclude")
272285
getopt.Parse()
273286

274287
r, err := git.PlainOpen(*repodir)
@@ -286,7 +299,7 @@ func main() {
286299
if len(args) != 2 {
287300
usagef("command cid takes exactly 1 argument")
288301
}
289-
c := NewCache(*repodir, r)
302+
c := NewCache(*repodir, r, *excludes)
290303
refname := args[1]
291304
_, err = c.tracByRef(refname)
292305
if err != nil {

0 commit comments

Comments
 (0)