mirror of https://github.com/pulumi/pulumi.git
87 lines
2.5 KiB
Go
87 lines
2.5 KiB
Go
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
|
|
|
|
package gitutil
|
|
|
|
import (
|
|
"fmt"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/pulumi/pulumi/pkg/util/fsutil"
|
|
git "gopkg.in/src-d/go-git.v4"
|
|
)
|
|
|
|
// GetGitRepository returns the git repository by walking up from the provided directory.
|
|
// If no repository is found, will return (nil, nil).
|
|
func GetGitRepository(dir string) (*git.Repository, error) {
|
|
gitRoot, err := fsutil.WalkUp(dir, func(s string) bool { return filepath.Base(s) == ".git" }, nil)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "searching for git repository from %v", dir)
|
|
}
|
|
if gitRoot == "" {
|
|
return nil, nil
|
|
}
|
|
|
|
// Open the git repo in the .git folder's parent, not the .git folder itself.
|
|
repo, err := git.PlainOpen(path.Join(gitRoot, ".."))
|
|
if err == git.ErrRepositoryNotExists {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "reading git repository")
|
|
}
|
|
return repo, nil
|
|
}
|
|
|
|
// GetGitHubProjectForOrigin returns the GitHub login, and GitHub repo name if the "origin" remote is
|
|
// a GitHub URL.
|
|
func GetGitHubProjectForOrigin(dir string) (string, string, error) {
|
|
repo, err := GetGitRepository(dir)
|
|
if repo == nil {
|
|
return "", "", fmt.Errorf("no git repository found from %v", dir)
|
|
}
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
return GetGitHubProjectForOriginByRepo(repo)
|
|
}
|
|
|
|
// GetGitHubProjectForOriginByRepo returns the GitHub login, and GitHub repo name if the "origin" remote is
|
|
// a GitHub URL.
|
|
func GetGitHubProjectForOriginByRepo(repo *git.Repository) (string, string, error) {
|
|
remote, err := repo.Remote("origin")
|
|
if err != nil {
|
|
return "", "", errors.Wrap(err, "could not read origin information")
|
|
}
|
|
|
|
remoteURL := ""
|
|
if len(remote.Config().URLs) > 0 {
|
|
remoteURL = remote.Config().URLs[0]
|
|
}
|
|
project := ""
|
|
|
|
const GitHubSSHPrefix = "git@github.com:"
|
|
const GitHubHTTPSPrefix = "https://github.com/"
|
|
const GitHubRepositorySuffix = ".git"
|
|
|
|
if strings.HasPrefix(remoteURL, GitHubSSHPrefix) {
|
|
project = trimGitRemoteURL(remoteURL, GitHubSSHPrefix, GitHubRepositorySuffix)
|
|
} else if strings.HasPrefix(remoteURL, GitHubHTTPSPrefix) {
|
|
project = trimGitRemoteURL(remoteURL, GitHubHTTPSPrefix, GitHubRepositorySuffix)
|
|
}
|
|
|
|
split := strings.Split(project, "/")
|
|
|
|
if len(split) != 2 {
|
|
return "", "", errors.Errorf("could not detect GitHub project from url: %v", remote)
|
|
}
|
|
|
|
return split[0], split[1], nil
|
|
}
|
|
|
|
func trimGitRemoteURL(url string, prefix string, suffix string) string {
|
|
return strings.TrimSuffix(strings.TrimPrefix(url, prefix), suffix)
|
|
}
|