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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
package github
import (
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/github/hub/git"
"github.com/github/hub/utils"
)
type Project struct {
Name string
Owner string
Host string
Protocol string
}
func (p Project) String() string {
return fmt.Sprintf("%s/%s", p.Owner, p.Name)
}
func (p *Project) SameAs(other *Project) bool {
return strings.EqualFold(p.Owner, other.Owner) &&
strings.EqualFold(p.Name, other.Name) &&
strings.EqualFold(p.Host, other.Host)
}
func (p *Project) WebURL(name, owner, path string) string {
if owner == "" {
owner = p.Owner
}
if name == "" {
name = p.Name
}
ownerWithName := fmt.Sprintf("%s/%s", owner, name)
if strings.Contains(ownerWithName, ".wiki") {
ownerWithName = strings.TrimSuffix(ownerWithName, ".wiki")
if path != "wiki" {
if strings.HasPrefix(path, "commits") {
path = "_history"
} else if path != "" {
path = fmt.Sprintf("_%s", path)
}
if path != "" {
path = utils.ConcatPaths("wiki", path)
} else {
path = "wiki"
}
}
}
url := fmt.Sprintf("%s://%s", p.Protocol, utils.ConcatPaths(p.Host, ownerWithName))
if path != "" {
url = utils.ConcatPaths(url, path)
}
return url
}
func (p *Project) GitURL(name, owner string, isSSH bool) (url string) {
if name == "" {
name = p.Name
}
if owner == "" {
owner = p.Owner
}
host := rawHost(p.Host)
if preferredProtocol() == "https" {
url = fmt.Sprintf("https://%s/%s/%s.git", host, owner, name)
} else if isSSH || preferredProtocol() == "ssh" {
url = fmt.Sprintf("git@%s:%s/%s.git", host, owner, name)
} else {
url = fmt.Sprintf("git://%s/%s/%s.git", host, owner, name)
}
return url
}
// Remove the scheme from host when the host url is absolute.
func rawHost(host string) string {
u, err := url.Parse(host)
utils.Check(err)
if u.IsAbs() {
return u.Host
} else {
return u.Path
}
}
func preferredProtocol() string {
userProtocol := os.Getenv("HUB_PROTOCOL")
if userProtocol == "" {
userProtocol, _ = git.Config("hub.protocol")
}
return userProtocol
}
func NewProjectFromRepo(repo *Repository) (p *Project, err error) {
url, err := url.Parse(repo.HtmlUrl)
if err != nil {
return
}
p, err = NewProjectFromURL(url)
return
}
func NewProjectFromURL(url *url.URL) (p *Project, err error) {
if !knownGitHubHostsInclude(url.Host) {
err = &GithubHostError{url}
return
}
parts := strings.SplitN(url.Path, "/", 4)
if len(parts) <= 2 {
err = fmt.Errorf("Invalid GitHub URL: %s", url)
return
}
name := strings.TrimSuffix(parts[2], ".git")
p = newProject(parts[1], name, url.Host, url.Scheme)
return
}
func NewProject(owner, name, host string) *Project {
return newProject(owner, name, host, "")
}
func newProject(owner, name, host, protocol string) *Project {
if strings.Contains(owner, "/") {
result := strings.SplitN(owner, "/", 2)
owner = result[0]
if name == "" {
name = result[1]
}
} else if strings.Contains(name, "/") {
result := strings.SplitN(name, "/", 2)
if owner == "" {
owner = result[0]
}
name = result[1]
}
if host == "" {
host = DefaultGitHubHost()
}
if host == "ssh.github.com" {
host = GitHubHost
}
if protocol != "http" && protocol != "https" {
protocol = ""
}
if protocol == "" {
h := CurrentConfig().Find(host)
if h != nil {
protocol = h.Protocol
}
}
if protocol == "" {
protocol = "https"
}
if owner == "" {
h := CurrentConfig().Find(host)
if h != nil {
owner = h.User
}
}
return &Project{
Name: name,
Owner: owner,
Host: host,
Protocol: protocol,
}
}
func SanitizeProjectName(name string) string {
name = filepath.Base(name)
return strings.Replace(name, " ", "-", -1)
}
|