server/admin/sync/importer_git_common.go

74 lines
1.6 KiB
Go
Raw Normal View History

package sync
import (
2022-05-24 20:13:38 +00:00
"io"
2022-05-24 19:25:51 +00:00
"net/url"
"os"
2022-05-24 19:25:51 +00:00
"regexp"
"sync"
)
2022-05-24 19:25:51 +00:00
var gitRemoteRe = regexp.MustCompile(`^(?:(?:git@|https://)([\w.@]+)(?:/|:))((?:[\w-_]+)/(?:[\w-_/]+))(?:.git){0,1}(?:(?:/){0,1})$`)
var oneGitPull sync.Mutex
func countFileInDir(dirname string) (int, error) {
files, err := os.ReadDir(dirname)
if err != nil {
return 0, err
}
return len(files), nil
}
2023-05-03 08:54:02 +00:00
func (i GitImporter) Exists(filename string) bool {
return i.li.Exists(filename)
}
func (i GitImporter) toURL(filename string) string {
return i.li.toURL(filename)
}
2022-05-22 23:39:35 +00:00
func (i GitImporter) GetLocalPath(filename ...string) string {
return i.li.GetLocalPath(filename...)
}
func (i GitImporter) importFile(URI string, next func(string, string) (interface{}, error)) (interface{}, error) {
return i.li.importFile(URI, next)
}
2023-11-25 16:13:31 +00:00
func (i GitImporter) GetFile(filename string) (io.Reader, error) {
return i.li.GetFile(filename)
}
2022-05-24 20:13:38 +00:00
func (i GitImporter) writeFile(filename string, reader io.Reader) error {
return i.li.writeFile(filename, reader)
}
2023-11-25 16:13:31 +00:00
func (i GitImporter) ListDir(filename string) ([]string, error) {
return i.li.ListDir(filename)
}
2023-11-25 16:13:31 +00:00
func (i GitImporter) Stat(filename string) (os.FileInfo, error) {
return i.li.Stat(filename)
}
func (i GitImporter) Kind() string {
return "git originated from " + i.Remote + " on " + i.li.Kind()
}
2022-05-24 19:25:51 +00:00
func getForgeBaseLink(remote string) (u *url.URL, err error) {
res := gitRemoteRe.FindStringSubmatch(remote)
u, err = url.Parse(res[2])
u.Scheme = "https"
u.Host = res[1]
return
}
type GitSubmoduleStatus struct {
Hash string `json:"hash"`
Text string `json:"text,omitempty"`
Path string `json:"path"`
Branch string `json:"branch"`
}