-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
136 lines (108 loc) · 2.99 KB
/
main.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"regexp"
"strings"
)
var ( // flags
remoteName string
quiet bool
)
func init() {
flag.StringVar(&remoteName, "remote", "origin", "Specify git remote, defaults to 'origin'")
flag.BoolVar(&quiet, "q", false, "Quiet mode")
flag.Parse()
if quiet { // Mute logging
log.SetOutput(ioutil.Discard)
}
}
func main() {
_, err := exec.LookPath("git")
if err != nil {
log.Fatalln(err)
}
if output, err := exec.Command("git", "status").CombinedOutput(); err != nil {
log.Fatal(string(output))
}
remote := func() string {
if len(os.Args) > 1 {
return os.Args[1]
}
remote, err := exec.Command("git", "remote", "get-url", remoteName).Output()
if err != nil {
log.Fatalf("Remote '%s' not found", highlight(remoteName))
}
return strings.TrimSpace(string(remote))
}()
urlParts := urlParts(remote)
config := map[string]string{}
// Merge config values, more deep links have higher force
for n := range urlParts {
subsection := strings.Join(urlParts[:n+1], "/")
values := gitSubsectionConfig(subsection)
for k, v := range values {
config[k] = v
}
}
// Set resolved config values
if len(config) == 0 {
log.Printf("No values matched for '%s'", highlight(remote))
}
for name, value := range config {
if err := exec.Command("git", "config", name, value).Run(); err != nil {
log.Fatalln(err)
}
}
status()
}
// urlParts returns essential parts of git remote. Removes schema, auth and splitting into parts.
func urlParts(url string) []string {
substrAfter := func(s string, sub string) string {
index := strings.Index(url, sub)
if index < 0 {
return s
}
return s[index+len(sub):]
}
url = strings.TrimSpace(url)
url = substrAfter(url, "://")
url = substrAfter(url, "@")
return regexp.MustCompile("[:/@]+").Split(url, -1)
}
// gitSubsectionConfig picks config params filtered by subsection, errors are ignored. Returns key-values pairs or an empty map
func gitSubsectionConfig(subsection string) (values map[string]string) {
values = map[string]string{}
section := fmt.Sprintf(".*\\.%s\\.", subsection)
bytes, err := exec.Command("git", "config", "--global", "--get-regexp", section).Output()
if err != nil {
return
}
scanner := bufio.NewScanner(
strings.NewReader(string(bytes)),
)
for scanner.Scan() {
kv := strings.SplitN(scanner.Text(), " ", 2) // Split into key and value
// Remove subsection from key
key := strings.Replace(kv[0], ("." + subsection), "", 1)
value := kv[1]
values[key] = value
}
return
}
// status prints git commiter name and email
func status() {
name, _ := exec.Command("git", "config", "-z", "user.name").Output()
email, _ := exec.Command("git", "config", "-z", "user.email").Output()
author := fmt.Sprintf("%s <%s>", string(name), string(email))
log.Printf("Committing as %s", highlight(author))
}
// highlight returns text wrapped in ANSI coloring
func highlight(text string) string {
return fmt.Sprintf("\033[0;32m%s\033[0m", text)
}