Skip to content

Support Github Enterprise (According to hub.*). #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ GitHub remote url open command.
gh-open ~/path/to/repo
#+END_SRC

Github Enterprise is supported:
#+BEGIN_SRC sh
git config hub.host ghe.example.com
git config hub.protocol http
#+END_SRC

Variables are according to [[https://hub.github.com][hub]].

** Requirements

- git command (in PATH)
Expand Down
23 changes: 22 additions & 1 deletion remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,27 @@ func CreateURL(host, user, repo string) (string, error) {
} else if host == "bitbucket.org" {
return fmt.Sprintf("https://%s/%s/%s", host, user, repo), nil
} else {
return "", fmt.Errorf("invalid github or bitbucket host: %s", host)
return CreateURLByHubConfig(host, user, repo)
}
}

func CreateURLByHubConfig(host, user, repo string) (string, error) {
hub_host := GetConfig("hub.host", "")
protocol := GetConfig("hub.protocol", "https")
if hub_host != "" {
if protocol != "https" && protocol != "http" {
return "", fmt.Errorf("unsupported protocol: %s", protocol)
}
return fmt.Sprintf("%s://%s/%s/%s", protocol, host, user, repo), nil
} else {
return "", fmt.Errorf("invalid github (includes enterprise) or bitbucket host: %s", host)
}
}

func GetConfig(name string, default_value string) string {
value, err := exec.Command("git", "config", name).Output()
if err != nil {
return default_value
}
return string(value[:len(value)-1])
}
86 changes: 85 additions & 1 deletion remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"io/ioutil"
"os"
"os/exec"
"regexp"
"syscall"
Expand Down Expand Up @@ -162,7 +163,7 @@ func TestMangleURL(t *testing.T) {
if err == nil {
t.Error("error should be set:", err)
}
if err.Error() != "invalid github or bitbucket host: example.com" {
if err.Error() != "invalid github (includes enterprise) or bitbucket host: example.com" {
t.Error("unexpected error:", err)
}

Expand Down Expand Up @@ -210,3 +211,86 @@ func TestMangleURLforBitBucket(t *testing.T) {
}

}

// - https://username@ghe.example.com/username/repo.git
// Github Enterprise url must set hub.host.
// above url must set hub.protocol.
// - git@ghe.example.com:username/repo.git
// - git://ghe.example.com/username/repo.git

func TestMangleURLforGithubEnterpriseWithoutConfig(t *testing.T) {

// ssh
_, err := MangleURL("git@ghe.example.com/username/repo.git")
if err.Error() != "unsupported remote url: git@ghe.example.com/username/repo.git" {
t.Error("unexpected error:", err)
}

}

func TestMangleURLforGithubEnterpriseWithConfig(t *testing.T) {
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Error("failed to create tempdir:", err)
}
defer syscall.Rmdir(dir)
os.Chdir(dir)

git := exec.Command("git", "init")
git.Dir = dir
if err := git.Run(); err != nil {
t.Error("failed to run git init:", err)
}

git = exec.Command("git", "remote", "add", "origin", "git@ghe.example.com:username/repo.git")
git.Dir = dir
if err := git.Run(); err != nil {
t.Error("failed to run git remote add :", err)
}
SetConfig("hub.host", "ghe.example.com", t)

// https
expected := "https://ghe.example.com/username/repo"
u, err := MangleURL("https://username@ghe.example.com/username/repo.git")
if err != nil {
t.Error("error should be nil:", err)
}
if u != expected {
t.Error("unexpected url:", u)
}

// git
u, err = MangleURL("git@ghe.example.com:username/repo.git")
if err != nil {
t.Error("error should be nil:", err)
}
if u != expected {
t.Error("unexpected url:", u)
}

// set http
SetConfig("hub.protocol", "http", t)
// ssh
expected = "http://ghe.example.com/username/repo"
u, err = MangleURL("git@ghe.example.com:username/repo.git")
if err != nil {
t.Error("error should be nil:", err)
}
if u != expected {
t.Error("unexpected url:", u)
}

// illegal protocol
SetConfig("hub.protocol", "gopher", t)
_, err = MangleURL("git@ghe.example.com:username/repo.git")
if err.Error() != "unsupported protocol: gopher" {
t.Error("unexpected error:", err)
}
}

func SetConfig(name string, value string, t *testing.T) {
git := exec.Command("git", "config", name, value)
if err := git.Run(); err != nil {
t.Error("failed to run git config :", err)
}
}