-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgit-subtrac.go
109 lines (97 loc) · 2.41 KB
/
git-subtrac.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"fmt"
"github.com/apenwarr/git-subtrac/subtrac"
"github.com/pborman/getopt"
"github.com/go-git/go-git/v5"
"log"
"os"
)
func fatalf(fmt string, args ...interface{}) {
log.Fatalf("git-subtrac: "+fmt, args...)
}
var usage_str = `
Commands:
cid <ref> Print the id of a tracking commit based on the given ref
dump <refs...> Print the cache after loading the given branch ref(s)
update Update all local branches with a matching *.trac branch
`
func usage() {
fmt.Fprintf(os.Stderr, "\n")
getopt.PrintUsage(os.Stderr)
fmt.Fprintf(os.Stderr, usage_str)
}
func usagef(format string, args ...interface{}) {
usage()
fmt.Fprintf(os.Stderr, "\nfatal: "+format+"\n", args...)
os.Exit(99)
}
func main() {
log.SetFlags(0)
infof := log.Printf
getopt.SetUsage(usage)
repodir := getopt.StringLong("git-dir", 'd', ".", "path to git repo", "GIT_DIR")
excludes := getopt.ListLong("exclude", 'x', "commitids to exclude", "commitids...")
autoexclude := getopt.BoolLong("auto-exclude", 0, "auto exclude missing commits")
verbose := getopt.BoolLong("verbose", 'v', "verbose mode")
getopt.Parse()
args := getopt.Args()
if len(args) < 1 {
usagef("no command specified.")
}
var debugf func(fmt string, args ...interface{})
if *verbose {
debugf = infof
} else {
debugf = func(fmt string, args ...interface{}) {}
}
setupOrFatal := func() *subtrac.Cache {
r, err := git.PlainOpen(*repodir)
if err != nil {
fatalf("git: %v: %v\n", *repodir, err)
}
c, err := subtrac.NewCache(*repodir, r, *excludes, *autoexclude, debugf, infof)
if err != nil {
fatalf("NewCache: %v\n", err)
}
return c
}
switch args[0] {
case "update":
if len(args) != 1 {
usagef("command 'update' takes no arguments")
}
c := setupOrFatal()
err := c.UpdateBranchRefs()
if err != nil {
fatalf("%v\n", err)
}
case "cid":
if len(args) != 2 {
usagef("command 'cid' takes exactly 1 argument")
}
c := setupOrFatal()
refname := args[1]
trac, err := c.TracByRef(refname)
if err != nil {
fatalf("%v\n", err)
}
if trac != nil {
fmt.Printf("%v\n", trac.Hash)
}
case "dump":
if len(args) < 2 {
usagef("command 'dump' takes at least 1 argument")
}
c := setupOrFatal()
for _, refname := range args[1:] {
_, err := c.TracByRef(refname)
if err != nil {
fatalf("%v\n", err)
}
}
fmt.Printf("%v\n", c)
default:
usagef("unknown command %v", args[0])
}
}