Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/gitcheckout: resolve submodule relative URL into absolute one from remote origin #1289

Merged
merged 1 commit into from
Aug 15, 2024
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
73 changes: 73 additions & 0 deletions internal/cli/cmd/cluster/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cluster

import (
"fmt"
"path"
"path/filepath"
"strings"
)

type gitScheme string

const (
SSH gitScheme = "ssh"
HTTPS gitScheme = "https"
)

type giturl struct {
scheme gitScheme
hostname, owner, repository string
}

func (g giturl) URL() string {
if g.scheme == SSH {
return fmt.Sprintf("git@%s:%s/%s.git", g.hostname, g.owner, g.repository)
} else {
return fmt.Sprintf("https://%s/%s/%s.git", g.hostname, g.owner, g.repository)
}
}

func parseGitUrl(url string) (giturl, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably not relevant in this particular case but I think file:// also exists (which can be implied by being left out) and git://
https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols

switch {
case strings.HasPrefix(url, "https://"):
urlSegments := strings.Split(url, "/")
return giturl{
scheme: HTTPS,
hostname: urlSegments[1],
owner: path.Join(urlSegments[2 : len(urlSegments)-1]...),
repository: strings.TrimSuffix(urlSegments[len(urlSegments)-1], ".git"),
}, nil
case strings.HasPrefix(url, "git@"):
urlSegments := strings.Split(url, ":")
ownerAndRepoName := strings.Split(urlSegments[1], "/")
return giturl{
scheme: SSH,
hostname: strings.TrimPrefix(urlSegments[0], "git@"),
owner: path.Join(ownerAndRepoName[:len(ownerAndRepoName)-1]...),
repository: strings.TrimSuffix(ownerAndRepoName[len(ownerAndRepoName)-1], ".git"),
}, nil
}

return giturl{}, fmt.Errorf("url %q is not git url", url)
}

func isRelativeUrl(url string) bool {
return strings.HasPrefix(url, "./") || strings.HasPrefix(url, "../")
}

func resolveRelativeRemoteUrl(remoteUrl, originRemoteUrl string) (giturl, error) {
origin, err := parseGitUrl(originRemoteUrl)
if err != nil {
return giturl{}, err
}

combined := filepath.Join(origin.owner, origin.repository, remoteUrl)
resolved := filepath.Clean(combined)
resolvedOwnerRepoName := strings.Split(resolved, "/")
return giturl{
scheme: origin.scheme,
hostname: origin.hostname,
owner: filepath.Join(resolvedOwnerRepoName[:len(resolvedOwnerRepoName)-1]...),
repository: strings.TrimSuffix(resolvedOwnerRepoName[len(resolvedOwnerRepoName)-1], ".git"),
}, nil
}
39 changes: 39 additions & 0 deletions internal/cli/cmd/cluster/gitcheckout.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,23 @@ func (p *processor) doProcessRepo(ctx context.Context, job processRepoJob) error
if job.recursionDepth > p.maxRecurseDepth {
return fmt.Errorf("Reached max nesting level: %d", job.recursionDepth)
}

submodules, err := getSubmodules(ctx, job.repoPath)
if err != nil {
return err
}

// NSL-3898: get origin repository to be able to resolve potential relative submodule URL
origin, err := getRepoRemoteOrigin(ctx)
if err != nil {
return err
}

submodules, err = resolveRelativeRemoteUrls(submodules, origin)
if err != nil {
return err
}

mirrorReadyChan := make(chan ensureMirrorResult, len(submodules))
for _, submod := range submodules {
fmt.Fprintf(console.Info(ctx), "N%d: In %s: Found submodule %s -> %s\n", job.recursionDepth, job.repoPath, submod.relativePath, submod.remoteUrl)
Expand Down Expand Up @@ -526,6 +538,33 @@ func inRepoGit(repoPath string, args ...string) *exec.Cmd {
return exec.Command("git", allArgs...)
}

func getRepoRemoteOrigin(ctx context.Context) (string, error) {
cmd := exec.Command("git", "config", "--get", "remote.origin.url")
fmt.Fprintf(console.Debug(ctx), "exec: %s\n", strings.Join(cmd.Args, " "))
output, err := cmd.CombinedOutput()
if err != nil {
return "", err
}

return strings.Trim(string(output), string([]rune{'\n', ' '})), nil
}

func resolveRelativeRemoteUrls(submodules []submodule, originRemoteUrl string) ([]submodule, error) {
for i, sub := range submodules {
if isRelativeUrl(sub.remoteUrl) {
resolvedUrl, err := resolveRelativeRemoteUrl(sub.remoteUrl, originRemoteUrl)
if err != nil {
return nil, err
}

sub.remoteUrl = resolvedUrl.URL()
submodules[i] = sub
}
}

return submodules, nil
}

func getSubmodules(ctx context.Context, repoPath string) ([]submodule, error) {
cmd := inRepoGit(repoPath, "config", "--file", ".gitmodules", "--get-regexp", "submodule\\.")
fmt.Fprintf(console.Debug(ctx), "exec: %s\n", strings.Join(cmd.Args, " "))
Expand Down
Loading