-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgit.go
70 lines (58 loc) · 1.45 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package git
import (
"bytes"
"fmt"
"strings"
"github.com/cli/cli/git"
"github.com/craftamap/bb/internal/run"
)
type BitbucketRepo struct {
RepoOrga string
RepoSlug string
Remote git.Remote
}
func GetBitbucketRepo(remoteName string) (*BitbucketRepo, error) {
remotes, err := git.Remotes()
if err != nil {
return nil, err
}
var selectedRemote git.Remote
for _, remote := range remotes {
if remote.Name == remoteName {
selectedRemote = *remote
}
}
// If no selectedRemote is found, throw an error
if selectedRemote.Name == "" {
return nil, fmt.Errorf("could not find the specified remote %s", remoteName)
}
path := strings.Split(selectedRemote.FetchURL.Path, "/")[1:]
repoOrga := path[0]
repoSlug := path[1]
if selectedRemote.FetchURL.Scheme == "ssh" && strings.HasSuffix(repoSlug, ".git") {
repoSlug = strings.TrimSuffix(repoSlug, ".git")
}
bbrepo := BitbucketRepo{
Remote: selectedRemote,
RepoOrga: repoOrga,
RepoSlug: repoSlug,
}
return &bbrepo, nil
}
func (b *BitbucketRepo) IsBitbucketOrg() bool {
return strings.Contains(b.Remote.FetchURL.String(), "bitbucket")
}
func CurrentHead() (string, error) {
headCmd, err := git.GitCommand("rev-parse", "HEAD")
if err != nil {
return "", err
}
output, err := run.PrepareCmd(headCmd).Output()
return firstLine(output), err
}
func firstLine(output []byte) string {
if i := bytes.IndexAny(output, "\n"); i >= 0 {
return string(output)[0:i]
}
return string(output)
}