Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

fix submodule has port #136

Merged
merged 1 commit into from
Jan 23, 2019
Merged
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
16 changes: 12 additions & 4 deletions submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ func NewSubModuleFile(c *Commit, refURL, refID string) *SubModuleFile {
}
}

// RefURL guesses and returns reference URL.
func (sf *SubModuleFile) RefURL(urlPrefix string, parentPath string) string {
if sf.refURL == "" {
func getRefURL(refURL, urlPrefix, parentPath string) string {
if refURL == "" {
return ""
}

url := strings.TrimSuffix(sf.refURL, ".git")
url := strings.TrimSuffix(refURL, ".git")

// git://xxx/user/repo
if strings.HasPrefix(url, "git://") {
Expand Down Expand Up @@ -67,12 +66,21 @@ func (sf *SubModuleFile) RefURL(urlPrefix string, parentPath string) string {
if strings.Contains(urlPrefix, url[i+1:j]) {
return urlPrefix + url[j+1:]
}
if strings.HasPrefix(url, "ssh://") || strings.HasPrefix(url, "git+ssh://") {
k := strings.Index(url[j+1:], "/")
return "http://" + url[i+1:j] + "/" + url[j+1:][k+1:]
}
return "http://" + url[i+1:j] + "/" + url[j+1:]
}

return url
}

// RefURL guesses and returns reference URL.
func (sf *SubModuleFile) RefURL(urlPrefix string, parentPath string) string {
return getRefURL(sf.refURL, urlPrefix, parentPath)
}

// RefID returns reference ID.
func (sf *SubModuleFile) RefID() string {
return sf.refID
Expand Down
29 changes: 29 additions & 0 deletions submodule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2018 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package git

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetRefURL(t *testing.T) {
var kases = []struct {
refURL string
prefixURL string
parentPath string
expect string
}{
{"git://github.com/user1/repo1", "/", "/", "http://github.com/user1/repo1"},
{"https://localhost/user1/repo1.git", "/", "/", "https://localhost/user1/repo1"},
{"[email protected]/user1/repo1.git", "/", "/", "[email protected]/user1/repo1"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we prefix it with http if it doesn't contain a scheme ? and drop the user ?
I am not blocking on this as it improve the existing func.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean [email protected]/user1/repo1.git => http://github.com/user1/repo1.git ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

{"ssh://[email protected]:2222/zefie/lge_g6_kernel_scripts.git", "/", "/", "http://git.zefie.net/zefie/lge_g6_kernel_scripts"},
}

for _, kase := range kases {
assert.EqualValues(t, kase.expect, getRefURL(kase.refURL, kase.prefixURL, kase.parentPath))
}
}