summaryrefslogtreecommitdiffstats
path: root/modules/git/submodule.go
blob: 10680c242a386a6b61be0415a55139fd21dad706 (plain)
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
// Copyright 2014 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"

	"github.com/gogits/gogs/modules/setting"
)

type SubModule struct {
	Name string
	Url  string
}

// SubModuleFile represents a file with submodule type.
type SubModuleFile struct {
	*Commit

	refUrl string
	refId  string
}

func NewSubModuleFile(c *Commit, refUrl, refId string) *SubModuleFile {
	return &SubModuleFile{
		Commit: c,
		refUrl: refUrl,
		refId:  refId,
	}
}

// RefUrl guesses and returns reference URL.
func (sf *SubModuleFile) RefUrl() string {
	if sf.refUrl == "" {
		return ""
	}

	url := strings.TrimSuffix(sf.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
	}

	// sysuser@xxx:user/repo
	i := strings.Index(url, "@")
	j := strings.LastIndex(url, ":")
	if i > -1 && j > -1 {
		// fix problem with reverse proxy works only with local server
		if strings.Contains(setting.AppUrl, url[i+1:j]) {
			return setting.AppUrl + url[j+1:]
		} else {
			return "http://" + url[i+1:j] + "/" + url[j+1:]
		}
	}

	return url
}

// RefId returns reference ID.
func (sf *SubModuleFile) RefId() string {
	return sf.refId
}