blob: 5c098a1216a76e781c6e9782fbf9da40716f1b2b (
plain)
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
|
package main
import (
"log"
"os"
"os/exec"
"strings"
"unicode"
)
func main() {
gitArgs := []string{"switch"}
if requiresDetachFlag(os.Args) {
gitArgs = append(gitArgs, "--detach")
}
firstPositionalArg := true
for i, argv := range os.Args {
if i == 0 {
continue // argv[0] is a program name.
}
if firstPositionalArg && !strings.HasPrefix(argv, "-") {
if isInt(argv) {
argv = "feature/" + argv
}
firstPositionalArg = false
}
gitArgs = append(gitArgs, argv)
}
cmd := exec.Command("git", gitArgs...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
switch err.(type) {
case *exec.ExitError:
// Do nothing here because Git has already reported the error.
default:
log.Fatal(err)
}
}
os.Exit(cmd.ProcessState.ExitCode())
}
func requiresDetachFlag(argv []string) bool {
argc := len(argv)
if argc == 1 {
return false
}
firstArg := argv[1]
// Example: origin/main, upstream/develop
if strings.HasPrefix(firstArg, "origin/") || strings.HasPrefix(firstArg, "upstream/") {
return true
}
// Example: 1234, cafebabe
if len(firstArg) >= 4 {
for _, c := range firstArg {
if !unicode.Is(unicode.ASCII_Hex_Digit, c) {
return false
}
}
return true
}
return false
}
func isInt(s string) bool {
for _, c := range s {
if !unicode.IsDigit(c) {
return false
}
}
return true
}
|