-
Notifications
You must be signed in to change notification settings - Fork 30
/
gitdiscovery.go
42 lines (36 loc) · 1.22 KB
/
gitdiscovery.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
package gitdiscovery
import (
"fmt"
"os"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient/gitconfig"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient/giturl"
"github.com/pkg/errors"
)
// FindGitURLFromDir tries to find the git clone URL from the given directory
func FindGitURLFromDir(dir string, preferUpstream bool) (string, error) {
_, gitConfDir, err := gitclient.FindGitConfigDir(dir)
if err != nil {
return "", errors.Wrapf(err, "there was a problem obtaining the git config dir of directory %s", dir)
}
if gitConfDir == "" {
// lets use an env var instead
gitURL := os.Getenv("SOURCE_URL")
if gitURL != "" {
return gitURL, nil
}
return "", fmt.Errorf("no .git directory could be found from dir %s", dir)
}
return gitconfig.DiscoverUpstreamGitURL(gitConfDir, preferUpstream)
}
// FindGitInfoFromDir finds the git info from the given dir
func FindGitInfoFromDir(dir string) (*giturl.GitRepository, error) {
gitURL, err := FindGitURLFromDir(dir, false)
if err != nil {
return nil, errors.Wrapf(err, "failed to discover the git URL")
}
if gitURL == "" {
return nil, errors.Errorf("no git URL could be discovered")
}
return giturl.ParseGitURL(gitURL)
}