git/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 git
import (
"os"
"strings"
"j3s.sh/zoa/color"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
)
const (
clonePath = "/var/lib/zoa/repo"
)
func GitMode(command string) bool {
// TODO: support file-based git remotes
// all current remote git url types:
// ssh://[user@]host.xz[:port]/path/to/repo.git/
// git://host.xz[:port]/path/to/repo.git/
// http[s]://host.xz[:port]/path/to/repo.git/
// ftp[s]://host.xz[:port]/path/to/repo.git/
// [user@]host.xz:path/to/repo.git/
if strings.Contains(command, "://") {
// this assumption totally won't bite my ass later
return true
}
// scp-mode
if strings.Contains(command, "@") {
return true
}
// todo: rewrite this and make it less shit lmao
return false
}
func Clone(repo string, branch string) error {
// for now, take the uber naive blow + clone
// technique
os.RemoveAll(clonePath)
// Tempdir to clone the repository
cloneopts := &git.CloneOptions{
URL: repo,
}
msg := "-> cloning " + repo
if branch != "" {
msg += "@" + branch
cloneopts.ReferenceName = plumbing.ReferenceName(branch)
}
color.BluePrintln(msg)
_, err := git.PlainClone(clonePath, false, cloneopts)
return err
}