-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathforeachref.go
More file actions
51 lines (43 loc) · 1.2 KB
/
Copy pathforeachref.go
File metadata and controls
51 lines (43 loc) · 1.2 KB
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
package cmd
import (
"flag"
"fmt"
"os"
"strings"
"github.com/driusan/dgit/git"
)
func ForEachRef(c *git.Client, args []string) error {
flags := flag.NewFlagSet("for-each-ref", flag.ExitOnError)
flags.SetOutput(flag.CommandLine.Output())
flags.Usage = func() {
flag.Usage()
fmt.Fprintf(flag.CommandLine.Output(), "\n\nOptions:\n")
flags.PrintDefaults()
}
// These flags can be moved out of these lists and below as proper flags as they are implemented
for _, sf := range []string{"format"} {
flags.Var(newNotimplStringValue(), sf, "Not implemented")
}
flags.Parse(args)
refs, err := git.ShowRef(c, git.ShowRefOptions{}, []string{})
if err != nil {
return err
}
patterns := flags.Args()
for _, ref := range refs {
if len(patterns) == 0 {
fmt.Printf("%s %s\t%s\n", ref.Value, "commit", ref.Name) // FIXME check if commit or other object type
} else {
for _, pattern := range patterns {
if strings.HasPrefix(ref.Name, pattern) { // FIXME support actual patterns and match only on path segments
fmt.Printf("%s %s\t%s\n", ref.Value, "commit", ref.Name) // FIXME check if commit or other object type
break
}
}
}
}
if len(refs) == 0 {
os.Exit(1)
}
return nil
}