Skip to content

Commit 5bae724

Browse files
committed
Move some files around so we can have a binary named git-subtrac.
If it's installed with 'go install', this makes git automatically find it as 'git subtrac'.
1 parent 66cd610 commit 5bae724

File tree

3 files changed

+78
-65
lines changed

3 files changed

+78
-65
lines changed

git-subtrac.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/pborman/getopt/v2"
6+
"gopkg.in/src-d/go-git.v4"
7+
"log"
8+
"os"
9+
)
10+
11+
func debugf(fmt string, args ...interface{}) {
12+
log.Printf(fmt, args...)
13+
}
14+
15+
func fatalf(fmt string, args ...interface{}) {
16+
log.Fatalf("git-subtrac: "+fmt, args...)
17+
}
18+
19+
var usage_str = `
20+
Usage: %v [-d GIT_DIR] <command>
21+
22+
Commands:
23+
cid <ref> Generate a tracking commit id based on the given ref
24+
`
25+
26+
func usagef(format string, args ...interface{}) {
27+
fmt.Fprintf(os.Stderr, usage_str[1:], os.Args[0])
28+
fmt.Fprintf(os.Stderr, "\nfatal: "+format+"\n", args...)
29+
os.Exit(99)
30+
}
31+
32+
func main() {
33+
log.SetFlags(0)
34+
35+
repodir := getopt.StringLong("git-dir", 'd', ".", "path to git repo")
36+
excludes := getopt.ListLong("exclude", 'x', "", "commitids to exclude")
37+
getopt.Parse()
38+
39+
r, err := git.PlainOpen(*repodir)
40+
if err != nil {
41+
fatalf("git: %v: %v\n", repodir, err)
42+
}
43+
44+
args := getopt.Args()
45+
if len(args) < 1 {
46+
usagef("no command specified.")
47+
}
48+
49+
switch args[0] {
50+
case "cid":
51+
if len(args) != 2 {
52+
usagef("command cid takes exactly 1 argument")
53+
}
54+
c := NewCache(*repodir, r, *excludes, debugf)
55+
refname := args[1]
56+
trac, err := c.TracByRef(refname)
57+
if err != nil {
58+
fatalf("%v\n", err)
59+
}
60+
fmt.Printf("%v\n", trac.Hash)
61+
default:
62+
usagef("unknown command %v", args[0])
63+
}
64+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/apenwarr/git_subtrac
1+
module github.com/apenwarr/git-subtrac
22

33
go 1.12
44

subtrac.go

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,17 @@ package main
22

33
import (
44
"fmt"
5-
"github.com/pborman/getopt/v2"
65
"gopkg.in/src-d/go-git.v4"
76
"gopkg.in/src-d/go-git.v4/config"
87
"gopkg.in/src-d/go-git.v4/plumbing"
98
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
109
"gopkg.in/src-d/go-git.v4/plumbing/object"
11-
"log"
12-
"os"
1310
"path/filepath"
1411
"sort"
1512
"strconv"
1613
"strings"
1714
)
1815

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-
2716
type Trac struct {
2817
name string
2918
hash plumbing.Hash
@@ -48,14 +37,17 @@ func (t Trac) String() string {
4837
}
4938

5039
type Cache struct {
40+
debugf func(fmt string, args ...interface{})
5141
repoDir string
5242
repo *git.Repository
5343
excludes map[plumbing.Hash]bool
5444
tracs map[plumbing.Hash]*Trac
5545
}
5646

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 {
5849
c := Cache{
50+
debugf: debugf,
5951
repoDir: rdir,
6052
repo: r,
6153
excludes: make(map[plumbing.Hash]bool),
@@ -85,7 +77,7 @@ func (c *Cache) String() string {
8577
return strings.Join(out, "\n")
8678
}
8779

88-
func (c *Cache) tracByRef(refname string) (*Trac, error) {
80+
func (c *Cache) TracByRef(refname string) (*object.Commit, error) {
8981
h, err := c.repo.ResolveRevision(plumbing.Revision(refname))
9082
if err != nil {
9183
return nil, fmt.Errorf("%v: %v", refname, err)
@@ -94,7 +86,11 @@ func (c *Cache) tracByRef(refname string) (*Trac, error) {
9486
if err != nil {
9587
return nil, fmt.Errorf("%v: %v", refname, err)
9688
}
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
9894
}
9995

10096
// Mercifully, git's content-addressable storage means there are never
@@ -248,7 +244,7 @@ func commitPath(path string, sub int) string {
248244
}
249245

250246
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)
252248
wt, err := c.repo.Worktree()
253249
if err != nil {
254250
return fmt.Errorf("git worktree: %v", err)
@@ -265,7 +261,7 @@ func (c *Cache) tryFetchFromSubmodules(path string, hash plumbing.Hash) error {
265261
}
266262
_, err = subr.CommitObject(hash)
267263
if err != nil {
268-
debugf(" ...not in %v\n", subpath)
264+
c.debugf(" ...not in %v\n", subpath)
269265
continue
270266
}
271267
brname := fmt.Sprintf("subtrac-tmp-%v", hash)
@@ -366,53 +362,6 @@ func (c *Cache) tracTree(path string, tree *object.Tree) (*Trac, error) {
366362
}
367363

368364
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)
370366
c.tracs[trac.hash] = trac
371367
}
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, "\nfatal: "+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

Comments
 (0)