/
git.go
56 lines (48 loc) · 1.42 KB
/
git.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
package main
import (
"fmt"
"strings"
)
var isGitRepoAsserted = false
func assertGitRepo() {
if isGitRepoAsserted {
return
}
val, err := execNoPanic("git rev-parse --is-inside-work-tree")
if err == nil && strings.TrimSpace(val) == "true" {
isGitRepoAsserted = true
return
}
shouldGitInit := readConsoleOptionInput("This is not a valid git repo, do you want to 'git init'?", "n", []string{"y", "n"})
if shouldGitInit == "y" {
execOrPanic("git init")
} else {
panic("hackit-quickstart needs to be called from a valid git repo.")
}
assertGitRepo()
}
func addGitSubmodule(submodule string, directory string) {
assertGitRepo()
execOrPanic(fmt.Sprintf("git submodule add \"%v\" ./%v", submodule, directory))
execOrPanic("git submodule update --init")
}
func readGitOriginURL() string {
assertGitRepo()
repoURL, err := execNoPanic("git config --get remote.origin.url")
if err != nil {
repoURL = readConsolStringInput("Could not find remote 'origin', please enter origin url")
if repoURL == "" {
panic("git origin url required")
}
execOrPanicWithMessage("git remote add origin "+repoURL, "Failed to add origin to git repo")
return readGitOriginURL()
}
repoURL = strings.TrimSpace(repoURL)
if strings.HasPrefix(repoURL, "git@") {
repoURL = "https://" + strings.Replace(repoURL[4:len(repoURL)], ":", "/", -1)
}
if strings.HasSuffix(repoURL, ".git") {
repoURL = repoURL[0 : len(repoURL)-4]
}
return repoURL
}