Skip to content

Commit

Permalink
gitindex: interpret SSH URLs (#764)
Browse files Browse the repository at this point in the history
This allows indexing repos that are used for development and therefore have an
SSH 'origin' URL.
  • Loading branch information
hanwen authored Apr 23, 2024
1 parent 734f5b6 commit 6df0554
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions gitindex/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -111,6 +112,11 @@ func FindGitRepos(dir string) ([]string, error) {
// setTemplates fills in URL templates for known git hosting
// sites.
func setTemplates(repo *zoekt.Repository, u *url.URL, typ string) error {
if u.Scheme == "ssh+git" {
u.Scheme = "https"
u.User = nil
}

repo.URL = u.String()
switch typ {
case "gitiles":
Expand Down Expand Up @@ -192,6 +198,8 @@ func configLookupRemoteURL(cfg *config.Config, key string) string {
return rc.URLs[0]
}

var sshRelativeURLRegexp = regexp.MustCompile(`^([^@]+)@([^:]+):(.*)$`)

func setTemplatesFromConfig(desc *zoekt.Repository, repoDir string) error {
repo, err := git.PlainOpen(repoDir)
if err != nil {
Expand Down Expand Up @@ -228,6 +236,14 @@ func setTemplatesFromConfig(desc *zoekt.Repository, repoDir string) error {
if remoteURL == "" {
return nil
}
if sm := sshRelativeURLRegexp.FindStringSubmatch(remoteURL); sm != nil {
user := sm[1]
host := sm[2]
path := sm[3]

remoteURL = fmt.Sprintf("ssh+git://%s@%s/%s", user, host, path)
}

u, err := url.Parse(remoteURL)
if err != nil {
return err
Expand Down
16 changes: 16 additions & 0 deletions gitindex/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,3 +768,19 @@ func runScript(t *testing.T, cwd string, script string) {
t.Fatalf("execution error: %v, output %s", err, out)
}
}

func TestSetTemplate(t *testing.T) {
repositoryDir := t.TempDir()

// setup: initialize the repository and all of its branches
runScript(t, repositoryDir, "git init -b master")
runScript(t, repositoryDir, "git config remote.origin.url [email protected]:sourcegraph/zoekt.git")
desc := zoekt.Repository{}
if err := setTemplatesFromConfig(&desc, repositoryDir); err != nil {
t.Fatalf("setTemplatesFromConfig: %v", err)
}

if got, want := desc.FileURLTemplate, "https://github.com/sourcegraph/zoekt/blob/{{.Version}}/{{.Path}}"; got != want {
t.Errorf("got %q, want %q", got, want)
}
}

0 comments on commit 6df0554

Please sign in to comment.