-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.go
44 lines (37 loc) · 932 Bytes
/
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
package pkg
import (
"fmt"
"net/url"
"github.com/gogs/git-module"
"github.com/mitchellh/go-homedir"
giturls "github.com/whilp/git-urls"
)
func GitRemoteURLGet0(r *git.Repository, remote string) (*url.URL, error) {
urlString, err := r.RemoteGetURL(remote)
if err != nil {
return nil, err
}
return giturls.Parse(urlString[0]) // [0]: MVP
}
// if empty, path is working dir
func gitOpen(name string) (*git.Repository, error) {
path, err := homedir.Expand(name)
if err != nil {
return nil, fmt.Errorf("expanding path %s: %w", name, err)
}
// can I haz git?
if _, err := git.BinVersion(); err != nil {
return nil, fmt.Errorf("git binary: %w", err)
}
// parse path
path, err = PWDIfEmpty(path)
if err != nil {
return nil, fmt.Errorf("getting working directory: %w", err)
}
// open repo
r, err := git.Open(path)
if err != nil {
return nil, fmt.Errorf("opening git repo: %w", err)
}
return r, nil
}