@@ -2,28 +2,17 @@ package main
2
2
3
3
import (
4
4
"fmt"
5
- "github.com/pborman/getopt/v2"
6
5
"gopkg.in/src-d/go-git.v4"
7
6
"gopkg.in/src-d/go-git.v4/config"
8
7
"gopkg.in/src-d/go-git.v4/plumbing"
9
8
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
10
9
"gopkg.in/src-d/go-git.v4/plumbing/object"
11
- "log"
12
- "os"
13
10
"path/filepath"
14
11
"sort"
15
12
"strconv"
16
13
"strings"
17
14
)
18
15
19
- func debugf (fmt string , args ... interface {}) {
20
- log .Printf (fmt , args ... )
21
- }
22
-
23
- func fatalf (fmt string , args ... interface {}) {
24
- log .Fatalf ("git-subtrac: " + fmt , args ... )
25
- }
26
-
27
16
type Trac struct {
28
17
name string
29
18
hash plumbing.Hash
@@ -48,14 +37,17 @@ func (t Trac) String() string {
48
37
}
49
38
50
39
type Cache struct {
40
+ debugf func (fmt string , args ... interface {})
51
41
repoDir string
52
42
repo * git.Repository
53
43
excludes map [plumbing.Hash ]bool
54
44
tracs map [plumbing.Hash ]* Trac
55
45
}
56
46
57
- func NewCache (rdir string , r * git.Repository , excludes []string ) * Cache {
47
+ func NewCache (rdir string , r * git.Repository , excludes []string ,
48
+ debugf func (fmt string , args ... interface {})) * Cache {
58
49
c := Cache {
50
+ debugf : debugf ,
59
51
repoDir : rdir ,
60
52
repo : r ,
61
53
excludes : make (map [plumbing.Hash ]bool ),
@@ -85,7 +77,7 @@ func (c *Cache) String() string {
85
77
return strings .Join (out , "\n " )
86
78
}
87
79
88
- func (c * Cache ) tracByRef (refname string ) (* Trac , error ) {
80
+ func (c * Cache ) TracByRef (refname string ) (* object. Commit , error ) {
89
81
h , err := c .repo .ResolveRevision (plumbing .Revision (refname ))
90
82
if err != nil {
91
83
return nil , fmt .Errorf ("%v: %v" , refname , err )
@@ -94,7 +86,11 @@ func (c *Cache) tracByRef(refname string) (*Trac, error) {
94
86
if err != nil {
95
87
return nil , fmt .Errorf ("%v: %v" , refname , err )
96
88
}
97
- return c .tracCommit (refname , commit )
89
+ tc , err := c .tracCommit (refname , commit )
90
+ if err != nil || tc == nil {
91
+ return nil , err
92
+ }
93
+ return tc .tracCommit , nil
98
94
}
99
95
100
96
// Mercifully, git's content-addressable storage means there are never
@@ -248,7 +244,7 @@ func commitPath(path string, sub int) string {
248
244
}
249
245
250
246
func (c * Cache ) tryFetchFromSubmodules (path string , hash plumbing.Hash ) error {
251
- debugf ("Searching submodules for: %v\n " , path )
247
+ c . debugf ("Searching submodules for: %v\n " , path )
252
248
wt , err := c .repo .Worktree ()
253
249
if err != nil {
254
250
return fmt .Errorf ("git worktree: %v" , err )
@@ -265,7 +261,7 @@ func (c *Cache) tryFetchFromSubmodules(path string, hash plumbing.Hash) error {
265
261
}
266
262
_ , err = subr .CommitObject (hash )
267
263
if err != nil {
268
- debugf (" ...not in %v\n " , subpath )
264
+ c . debugf (" ...not in %v\n " , subpath )
269
265
continue
270
266
}
271
267
brname := fmt .Sprintf ("subtrac-tmp-%v" , hash )
@@ -366,53 +362,6 @@ func (c *Cache) tracTree(path string, tree *object.Tree) (*Trac, error) {
366
362
}
367
363
368
364
func (c * Cache ) add (trac * Trac ) {
369
- debugf (" add %.10v %v\n " , trac .hash , trac .name )
365
+ c . debugf (" add %.10v %v\n " , trac .hash , trac .name )
370
366
c .tracs [trac .hash ] = trac
371
367
}
372
-
373
- var usage_str = `
374
- Usage: %v [-d GIT_DIR] <command>
375
-
376
- Commands:
377
- cid <ref> Generate a tracking commit id based on the given ref
378
- `
379
-
380
- func usagef (format string , args ... interface {}) {
381
- fmt .Fprintf (os .Stderr , usage_str [1 :], os .Args [0 ])
382
- fmt .Fprintf (os .Stderr , "\n fatal: " + format + "\n " , args ... )
383
- os .Exit (99 )
384
- }
385
-
386
- func main () {
387
- log .SetFlags (0 )
388
-
389
- repodir := getopt .StringLong ("git-dir" , 'd' , "." , "path to git repo" )
390
- excludes := getopt .ListLong ("exclude" , 'x' , "" , "commitids to exclude" )
391
- getopt .Parse ()
392
-
393
- r , err := git .PlainOpen (* repodir )
394
- if err != nil {
395
- fatalf ("git: %v: %v\n " , repodir , err )
396
- }
397
-
398
- args := getopt .Args ()
399
- if len (args ) < 1 {
400
- usagef ("no command specified." )
401
- }
402
-
403
- switch args [0 ] {
404
- case "cid" :
405
- if len (args ) != 2 {
406
- usagef ("command cid takes exactly 1 argument" )
407
- }
408
- c := NewCache (* repodir , r , * excludes )
409
- refname := args [1 ]
410
- trac , err := c .tracByRef (refname )
411
- if err != nil {
412
- fatalf ("%v\n " , err )
413
- }
414
- fmt .Printf ("%v\n " , trac .tracCommit .Hash )
415
- default :
416
- usagef ("unknown command %v" , args [0 ])
417
- }
418
- }
0 commit comments