summaryrefslogtreecommitdiffstats
path: root/modules/git/submodule.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2019-03-27 17:33:00 +0800
committerGitHub <noreply@github.com>2019-03-27 17:33:00 +0800
commitd578b71d61ee8131e8abf7f538b93d8c6cc6fe6d (patch)
treef0bc12702264f2400f1b4308e06f9aa631fe3cca /modules/git/submodule.go
parentd056bf300ff5ebd89d8b0035722c94a3b08ac745 (diff)
downloadgitea-d578b71d61ee8131e8abf7f538b93d8c6cc6fe6d.tar.gz
gitea-d578b71d61ee8131e8abf7f538b93d8c6cc6fe6d.zip
move code.gitea.io/git to code.gitea.io/gitea/modules/git (#6364)
* move code.gitea.io/git to code.gitea.io/gitea/modules/git * fix imports * fix fmt * fix misspell * remove wrong tests data * fix unit tests * fix tests * fix tests * fix tests * fix tests * fix tests * enable Debug to trace the failure tests * fix tests * fix tests * fix tests * fix tests * fix tests * comment commit count tests since git clone depth is 50 * fix tests * update from code.gitea.io/git * revert change to makefile
Diffstat (limited to 'modules/git/submodule.go')
-rw-r--r--modules/git/submodule.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/modules/git/submodule.go b/modules/git/submodule.go
new file mode 100644
index 0000000000..294df3986a
--- /dev/null
+++ b/modules/git/submodule.go
@@ -0,0 +1,87 @@
+// Copyright 2015 The Gogs 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 "strings"
+
+// SubModule submodule is a reference on git repository
+type SubModule struct {
+ Name string
+ URL string
+}
+
+// SubModuleFile represents a file with submodule type.
+type SubModuleFile struct {
+ *Commit
+
+ refURL string
+ refID string
+}
+
+// NewSubModuleFile create a new submodule file
+func NewSubModuleFile(c *Commit, refURL, refID string) *SubModuleFile {
+ return &SubModuleFile{
+ Commit: c,
+ refURL: refURL,
+ refID: refID,
+ }
+}
+
+func getRefURL(refURL, urlPrefix, parentPath string) string {
+ if refURL == "" {
+ return ""
+ }
+
+ url := strings.TrimSuffix(refURL, ".git")
+
+ // git://xxx/user/repo
+ if strings.HasPrefix(url, "git://") {
+ return "http://" + strings.TrimPrefix(url, "git://")
+ }
+
+ // http[s]://xxx/user/repo
+ if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
+ return url
+ }
+
+ // Relative url prefix check (according to git submodule documentation)
+ if strings.HasPrefix(url, "./") || strings.HasPrefix(url, "../") {
+ // ...construct and return correct submodule url here...
+ idx := strings.Index(parentPath, "/src/")
+ if idx == -1 {
+ return url
+ }
+ return strings.TrimSuffix(urlPrefix, "/") + parentPath[:idx] + "/" + url
+ }
+
+ // sysuser@xxx:user/repo
+ i := strings.Index(url, "@")
+ j := strings.LastIndex(url, ":")
+
+ // Only process when i < j because git+ssh://git@git.forwardbias.in/npploader.git
+ if i > -1 && j > -1 && i < j {
+ // fix problem with reverse proxy works only with local server
+ 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
+}