Skip to content

Commit 2f9e42c

Browse files
committed
Add 'update' command and --autoexclude option.
1 parent 5bae724 commit 2f9e42c

File tree

2 files changed

+72
-12
lines changed

2 files changed

+72
-12
lines changed

git-subtrac.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Usage: %v [-d GIT_DIR] <command>
2121
2222
Commands:
2323
cid <ref> Generate a tracking commit id based on the given ref
24+
update Update all local branches with a matching *.trac branch
2425
`
2526

2627
func usagef(format string, args ...interface{}) {
@@ -34,6 +35,7 @@ func main() {
3435

3536
repodir := getopt.StringLong("git-dir", 'd', ".", "path to git repo")
3637
excludes := getopt.ListLong("exclude", 'x', "", "commitids to exclude")
38+
autoexclude := getopt.BoolLong("auto-exclude", 0, "auto exclude missing commits")
3739
getopt.Parse()
3840

3941
r, err := git.PlainOpen(*repodir)
@@ -46,12 +48,21 @@ func main() {
4648
usagef("no command specified.")
4749
}
4850

51+
c := NewCache(*repodir, r, *excludes, *autoexclude, debugf)
52+
4953
switch args[0] {
54+
case "update":
55+
if len(args) != 1 {
56+
usagef("command 'update' takes no arguments")
57+
}
58+
err := c.UpdateBranchRefs()
59+
if err != nil {
60+
fatalf("%v\n", err)
61+
}
5062
case "cid":
5163
if len(args) != 2 {
52-
usagef("command cid takes exactly 1 argument")
64+
usagef("command 'cid' takes exactly 1 argument")
5365
}
54-
c := NewCache(*repodir, r, *excludes, debugf)
5566
refname := args[1]
5667
trac, err := c.TracByRef(refname)
5768
if err != nil {

subtrac.go

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,24 @@ func (t Trac) String() string {
3737
}
3838

3939
type Cache struct {
40-
debugf func(fmt string, args ...interface{})
41-
repoDir string
42-
repo *git.Repository
43-
excludes map[plumbing.Hash]bool
44-
tracs map[plumbing.Hash]*Trac
40+
debugf func(fmt string, args ...interface{})
41+
repoDir string
42+
repo *git.Repository
43+
autoexclude bool
44+
excludes map[plumbing.Hash]bool
45+
tracs map[plumbing.Hash]*Trac
4546
}
4647

4748
func NewCache(rdir string, r *git.Repository, excludes []string,
49+
autoexclude bool,
4850
debugf func(fmt string, args ...interface{})) *Cache {
4951
c := Cache{
50-
debugf: debugf,
51-
repoDir: rdir,
52-
repo: r,
53-
excludes: make(map[plumbing.Hash]bool),
54-
tracs: make(map[plumbing.Hash]*Trac),
52+
debugf: debugf,
53+
repoDir: rdir,
54+
repo: r,
55+
autoexclude: autoexclude,
56+
excludes: make(map[plumbing.Hash]bool),
57+
tracs: make(map[plumbing.Hash]*Trac),
5558
}
5659
for _, x := range excludes {
5760
hash := plumbing.NewHash(x)
@@ -77,6 +80,49 @@ func (c *Cache) String() string {
7780
return strings.Join(out, "\n")
7881
}
7982

83+
func (c *Cache) UpdateBranchRefs() error {
84+
branchIter, err := c.repo.Branches()
85+
if err != nil {
86+
return fmt.Errorf("GetBranches: %v", err)
87+
}
88+
89+
var branches []*plumbing.Reference
90+
var commits []*object.Commit
91+
err = branchIter.ForEach(func(b *plumbing.Reference) error {
92+
name := string(b.Name())
93+
if strings.HasSuffix(name, ".trac") {
94+
return nil
95+
}
96+
c.debugf("Scanning branch: %v\n", name)
97+
commit, err := c.TracByRef(name)
98+
if err != nil {
99+
return err
100+
} else {
101+
branches = append(branches, b)
102+
commits = append(commits, commit)
103+
}
104+
return nil
105+
})
106+
if err != nil {
107+
return err
108+
}
109+
110+
for i := range branches {
111+
newname := string(branches[i].Name()) + ".trac"
112+
hash := commits[i].Hash
113+
c.debugf("Updating %.10v -> %v\n", hash, newname)
114+
115+
refname := plumbing.ReferenceName(newname)
116+
ref := plumbing.NewHashReference(refname, hash)
117+
err = c.repo.Storer.SetReference(ref)
118+
if err != nil {
119+
return fmt.Errorf("update %v: %v", refname, err)
120+
}
121+
}
122+
123+
return nil
124+
}
125+
80126
func (c *Cache) TracByRef(refname string) (*object.Commit, error) {
81127
h, err := c.repo.ResolveRevision(plumbing.Revision(refname))
82128
if err != nil {
@@ -326,6 +372,9 @@ func (c *Cache) tracTree(path string, tree *object.Tree) (*Trac, error) {
326372
if err != nil {
327373
err = c.tryFetchFromSubmodules(subpath, e.Hash)
328374
if err != nil {
375+
if c.autoexclude {
376+
continue
377+
}
329378
return nil, fmt.Errorf("%v (maybe fetch it manually?)", err)
330379
}
331380
}

0 commit comments

Comments
 (0)