-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Harikrishnan Balagopal <[email protected]>
- Loading branch information
1 parent
87d53a2
commit d60ade6
Showing
5 changed files
with
51 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1200,57 +1200,78 @@ func ConvertInterfaceToSliceOfStrings(xI interface{}) ([]string, error) { | |
// GatherGitInfo tries to find the git repo for the path if one exists. | ||
func GatherGitInfo(path string) (repoName, repoDir, repoHostName, repoURL, repoBranch string, err error) { | ||
if finfo, err := os.Stat(path); err != nil { | ||
logrus.Errorf("Failed to stat the path %q Error %q", path, err) | ||
return "", "", "", "", "", err | ||
return "", "", "", "", "", fmt.Errorf("failed to stat the path '%s' . Error %w", path, err) | ||
} else if !finfo.IsDir() { | ||
pathDir := filepath.Dir(path) | ||
logrus.Debugf("The path %q is not a directory. Using %q instead.", path, pathDir) | ||
logrus.Debugf("The path '%s' is not a directory. Using the path '%s' instead.", path, pathDir) | ||
path = pathDir | ||
} | ||
repo, err := git.PlainOpenWithOptions(path, &git.PlainOpenOptions{DetectDotGit: true}) | ||
if err != nil { | ||
logrus.Debugf("Unable to open the path %q as a git repo. Error: %q", path, err) | ||
return "", "", "", "", "", err | ||
return "", "", "", "", "", fmt.Errorf("failed to open the path '%s' as a git repo. Error: %w", path, err) | ||
} | ||
workTree, err := repo.Worktree() | ||
if err != nil { | ||
return "", "", "", "", "", fmt.Errorf("failed to get the repo working tree/directory. Error: %w", err) | ||
} | ||
repoDir = workTree.Filesystem.Root() | ||
ref, err := repo.Head() | ||
if err != nil { | ||
return "", "", "", "", "", fmt.Errorf("failed to get the current branch. Error: %w", err) | ||
} | ||
logrus.Debugf("current branch/tag: %#v", ref) | ||
repoBranch = filepath.Base(string(ref.Name())) | ||
remotes, err := repo.Remotes() | ||
if err != nil || len(remotes) == 0 { | ||
logrus.Debugf("No remotes found at path %q Error: %q", path, err) | ||
logrus.Debugf("failed to find any remote repo urls for the repo at path '%s' . Error: %q", path, err) | ||
logrus.Debugf("git no remotes case - repoName '%s', repoDir '%s', repoHostName '%s', repoURL '%s', repoBranch '%s'", repoName, repoDir, repoHostName, repoURL, repoBranch) | ||
return repoName, repoDir, repoHostName, repoURL, repoBranch, nil | ||
} | ||
var preferredRemote *git.Remote | ||
if preferredRemote = getGitRemoteByName(remotes, "upstream"); preferredRemote == nil { | ||
if preferredRemote = getGitRemoteByName(remotes, "origin"); preferredRemote == nil { | ||
preferredRemote = remotes[0] | ||
} | ||
} | ||
if workTree, err := repo.Worktree(); err == nil { | ||
repoDir = workTree.Filesystem.Root() | ||
} else { | ||
logrus.Debugf("Unable to get the repo directory. Error: %q", err) | ||
} | ||
if ref, err := repo.Head(); err == nil { | ||
repoBranch = filepath.Base(string(ref.Name())) | ||
} else { | ||
logrus.Debugf("Unable to get the current branch. Error: %q", err) | ||
} | ||
if len(preferredRemote.Config().URLs) == 0 { | ||
err = fmt.Errorf("unable to get origins") | ||
logrus.Debugf("%s", err) | ||
} | ||
u := preferredRemote.Config().URLs[0] | ||
if strings.HasPrefix(u, "git") { | ||
parts := strings.Split(u, ":") | ||
if len(parts) == 2 { | ||
u = parts[1] | ||
repoURL = u | ||
if strings.HasPrefix(u, "git@") { | ||
// Example: [email protected]:konveyor/move2kube.git | ||
withoutGitAt := strings.TrimPrefix(u, "git@") | ||
idx := strings.Index(withoutGitAt, ":") | ||
if idx < 0 { | ||
return "", "", "", "", "", fmt.Errorf("failed to parse the remote host url '%s' as a git ssh url. Error: %w", u, err) | ||
} | ||
domain := withoutGitAt[:idx] | ||
rest := withoutGitAt[idx+1:] | ||
newUrl := "https://" + domain + "/" + rest | ||
logrus.Debugf("final parsed git ssh url to normal url: '%s'", newUrl) | ||
giturl, err := url.Parse(newUrl) | ||
if err != nil { | ||
return "", "", "", "", "", fmt.Errorf("failed to parse the remote host url '%s' . Error: %w", newUrl, err) | ||
} | ||
logrus.Debugf("parsed ssh case - giturl: %#v", giturl) | ||
repoHostName = giturl.Host | ||
repoName = filepath.Base(giturl.Path) | ||
repoName = strings.TrimSuffix(repoName, filepath.Ext(repoName)) | ||
logrus.Debugf("git ssh case - repoName '%s', repoDir '%s', repoHostName '%s', repoURL '%s', repoBranch '%s'", repoName, repoDir, repoHostName, repoURL, repoBranch) | ||
return repoName, repoDir, repoHostName, repoURL, repoBranch, nil | ||
} | ||
|
||
giturl, err := url.Parse(u) | ||
if err != nil { | ||
logrus.Debugf("Unable to get origin remote host : %s", err) | ||
return "", "", "", "", "", fmt.Errorf("failed to parse the remote host url '%s' . Error: %w", u, err) | ||
} | ||
logrus.Errorf("parsed normal case - giturl: %#v", giturl) | ||
repoHostName = giturl.Host | ||
repoName = filepath.Base(giturl.Path) | ||
repoName = strings.TrimSuffix(repoName, filepath.Ext(repoName)) | ||
err = nil | ||
return | ||
logrus.Debugf("git normal case - repoName '%s', repoDir '%s', repoHostName '%s', repoURL '%s', repoBranch '%s'", repoName, repoDir, repoHostName, repoURL, repoBranch) | ||
return repoName, repoDir, repoHostName, repoURL, repoBranch, nil | ||
} | ||
|
||
func getGitRemoteByName(remotes []*git.Remote, remoteName string) *git.Remote { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters