mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 10:00:56 +00:00
Refactor 7 return values in parseGitUrl to RepoSpec(issue 4866, Task1) (#4900)
* initial commit to refactor 7 return values in parseGitUrl to RepoSpec * fix review comments
This commit is contained in:
committed by
GitHub
parent
e34d5b5608
commit
1e2e7bbc0b
@@ -86,22 +86,19 @@ func NewRepoSpecFromURL(n string) (*RepoSpec, error) {
|
|||||||
if filepath.IsAbs(n) {
|
if filepath.IsAbs(n) {
|
||||||
return nil, fmt.Errorf("uri looks like abs path: %s", n)
|
return nil, fmt.Errorf("uri looks like abs path: %s", n)
|
||||||
}
|
}
|
||||||
host, orgRepo, path, gitRef, gitSubmodules, suffix, gitTimeout := parseGitURL(n)
|
repoSpecVal := parseGitURL(n)
|
||||||
if orgRepo == "" {
|
if repoSpecVal.OrgRepo == "" {
|
||||||
return nil, fmt.Errorf("url lacks orgRepo: %s", n)
|
return nil, fmt.Errorf("url lacks orgRepo: %s", n)
|
||||||
}
|
}
|
||||||
if host == "" {
|
if repoSpecVal.Host == "" {
|
||||||
return nil, fmt.Errorf("url lacks host: %s", n)
|
return nil, fmt.Errorf("url lacks host: %s", n)
|
||||||
}
|
}
|
||||||
cleanedPath := filepath.Clean(strings.TrimPrefix(path, string(filepath.Separator)))
|
cleanedPath := filepath.Clean(strings.TrimPrefix(repoSpecVal.Path, string(filepath.Separator)))
|
||||||
if pathElements := strings.Split(cleanedPath, string(filepath.Separator)); len(pathElements) > 0 &&
|
if pathElements := strings.Split(cleanedPath, string(filepath.Separator)); len(pathElements) > 0 &&
|
||||||
pathElements[0] == filesys.ParentDir {
|
pathElements[0] == filesys.ParentDir {
|
||||||
return nil, fmt.Errorf("url path exits repo: %s", n)
|
return nil, fmt.Errorf("url path exits repo: %s", n)
|
||||||
}
|
}
|
||||||
return &RepoSpec{
|
return repoSpecVal, nil
|
||||||
raw: n, Host: host, OrgRepo: orgRepo,
|
|
||||||
Dir: notCloned, Path: path, Ref: gitRef, GitSuffix: suffix,
|
|
||||||
Submodules: gitSubmodules, Timeout: gitTimeout}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -112,68 +109,68 @@ const (
|
|||||||
|
|
||||||
// From strings like git@github.com:someOrg/someRepo.git or
|
// From strings like git@github.com:someOrg/someRepo.git or
|
||||||
// https://github.com/someOrg/someRepo?ref=someHash, extract
|
// https://github.com/someOrg/someRepo?ref=someHash, extract
|
||||||
// the parts.
|
// the different parts of URL , set into a RepoSpec object and return RepoSpec object.
|
||||||
func parseGitURL(n string) (
|
func parseGitURL(n string) *RepoSpec {
|
||||||
host string, orgRepo string, path string, gitRef string, gitSubmodules bool, gitSuff string, gitTimeout time.Duration) {
|
repoSpec := &RepoSpec{raw: n, Dir: notCloned, Timeout: defaultTimeout, Submodules: defaultSubmodules}
|
||||||
// parse query first
|
// parse query first
|
||||||
// safe because according to rfc3986: ? only allowed in query
|
// safe because according to rfc3986: ? only allowed in query
|
||||||
// and not recognized %-encoded
|
// and not recognized %-encoded
|
||||||
beforeQuery, query, _ := strings.Cut(n, "?")
|
beforeQuery, query, _ := strings.Cut(n, "?")
|
||||||
n = beforeQuery
|
n = beforeQuery
|
||||||
// if no query, defaults returned
|
// if no query, defaults returned
|
||||||
gitRef, gitTimeout, gitSubmodules = parseQuery(query)
|
repoSpec.Ref, repoSpec.Timeout, repoSpec.Submodules = parseQuery(query)
|
||||||
|
|
||||||
if strings.Contains(n, gitDelimiter) {
|
if strings.Contains(n, gitDelimiter) {
|
||||||
index := strings.Index(n, gitDelimiter)
|
index := strings.Index(n, gitDelimiter)
|
||||||
// Adding _git/ to host
|
// Adding _git/ to host
|
||||||
host = normalizeGitHostSpec(n[:index+len(gitDelimiter)])
|
repoSpec.Host = normalizeGitHostSpec(n[:index+len(gitDelimiter)])
|
||||||
orgRepo = strings.Split(n[index+len(gitDelimiter):], "/")[0]
|
repoSpec.OrgRepo = strings.Split(n[index+len(gitDelimiter):], "/")[0]
|
||||||
path = parsePath(n[index+len(gitDelimiter)+len(orgRepo):])
|
repoSpec.Path = parsePath(n[index+len(gitDelimiter)+len(repoSpec.OrgRepo):])
|
||||||
return
|
return repoSpec
|
||||||
}
|
}
|
||||||
host, n = parseHostSpec(n)
|
repoSpec.Host, n = parseHostSpec(n)
|
||||||
isLocal := strings.HasPrefix(host, "file://")
|
isLocal := strings.HasPrefix(repoSpec.Host, "file://")
|
||||||
if !isLocal {
|
if !isLocal {
|
||||||
gitSuff = gitSuffix
|
repoSpec.GitSuffix = gitSuffix
|
||||||
}
|
}
|
||||||
if strings.Contains(n, gitSuffix) {
|
if strings.Contains(n, gitSuffix) {
|
||||||
gitSuff = gitSuffix
|
repoSpec.GitSuffix = gitSuffix
|
||||||
index := strings.Index(n, gitSuffix)
|
index := strings.Index(n, gitSuffix)
|
||||||
orgRepo = n[0:index]
|
repoSpec.OrgRepo = n[0:index]
|
||||||
n = n[index+len(gitSuffix):]
|
n = n[index+len(gitSuffix):]
|
||||||
if len(n) > 0 && n[0] == '/' {
|
if len(n) > 0 && n[0] == '/' {
|
||||||
n = n[1:]
|
n = n[1:]
|
||||||
}
|
}
|
||||||
path = parsePath(n)
|
repoSpec.Path = parsePath(n)
|
||||||
return
|
return repoSpec
|
||||||
}
|
}
|
||||||
|
|
||||||
if isLocal {
|
if isLocal {
|
||||||
if idx := strings.Index(n, "//"); idx > 0 {
|
if idx := strings.Index(n, "//"); idx > 0 {
|
||||||
orgRepo = n[:idx]
|
repoSpec.OrgRepo = n[:idx]
|
||||||
n = n[idx+2:]
|
n = n[idx+2:]
|
||||||
path = parsePath(n)
|
repoSpec.Path = parsePath(n)
|
||||||
return
|
return repoSpec
|
||||||
}
|
}
|
||||||
orgRepo = parsePath(n)
|
repoSpec.OrgRepo = parsePath(n)
|
||||||
return
|
return repoSpec
|
||||||
}
|
}
|
||||||
|
|
||||||
i := strings.Index(n, "/")
|
i := strings.Index(n, "/")
|
||||||
if i < 1 {
|
if i < 1 {
|
||||||
path = parsePath(n)
|
repoSpec.Path = parsePath(n)
|
||||||
return
|
return repoSpec
|
||||||
}
|
}
|
||||||
j := strings.Index(n[i+1:], "/")
|
j := strings.Index(n[i+1:], "/")
|
||||||
if j >= 0 {
|
if j >= 0 {
|
||||||
j += i + 1
|
j += i + 1
|
||||||
orgRepo = n[:j]
|
repoSpec.OrgRepo = n[:j]
|
||||||
path = parsePath(n[j+1:])
|
repoSpec.Path = parsePath(n[j+1:])
|
||||||
return
|
return repoSpec
|
||||||
}
|
}
|
||||||
path = ""
|
repoSpec.Path = ""
|
||||||
orgRepo = parsePath(n)
|
repoSpec.OrgRepo = parsePath(n)
|
||||||
return host, orgRepo, path, gitRef, gitSubmodules, gitSuff, gitTimeout
|
return repoSpec
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clone git submodules by default.
|
// Clone git submodules by default.
|
||||||
|
|||||||
Reference in New Issue
Block a user