Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-10-31 08:46:45 +01:00 committed by Nicolas De loof
parent 71237ef62b
commit d646d757a2
2 changed files with 29 additions and 25 deletions

View File

@ -204,11 +204,7 @@ func (o *ProjectOptions) toProjectName(dockerCli command.Cli) (string, error) {
func (o *ProjectOptions) ToProject(dockerCli command.Cli, services []string, po ...cli.ProjectOptionsFn) (*types.Project, error) {
if !o.Offline {
var err error
po, err = o.configureRemoteLoaders(dockerCli, po)
if err != nil {
return nil, err
}
po = o.configureRemoteLoaders(dockerCli, po)
}
options, err := o.toProjectOptions(po...)
@ -255,12 +251,12 @@ func (o *ProjectOptions) ToProject(dockerCli command.Cli, services []string, po
return project, err
}
func (o *ProjectOptions) configureRemoteLoaders(dockerCli command.Cli, po []cli.ProjectOptionsFn) ([]cli.ProjectOptionsFn, error) {
func (o *ProjectOptions) configureRemoteLoaders(dockerCli command.Cli, po []cli.ProjectOptionsFn) []cli.ProjectOptionsFn {
git := remote.NewGitRemoteLoader(o.Offline)
oci := remote.NewOCIRemoteLoader(dockerCli, o.Offline)
po = append(po, cli.WithResourceLoader(git), cli.WithResourceLoader(oci))
return po, nil
return po
}
func (o *ProjectOptions) toProjectOptions(po ...cli.ProjectOptionsFn) (*cli.ProjectOptions, error) {

View File

@ -80,24 +80,9 @@ func (g gitRemoteLoader) Load(ctx context.Context, path string) (string, error)
ref.Commit = "HEAD" // default branch
}
if !commitSHA.MatchString(ref.Commit) {
cmd := exec.CommandContext(ctx, "git", "ls-remote", "--exit-code", ref.Remote, ref.Commit)
cmd.Env = g.gitCommandEnv()
out, err := cmd.Output()
if err != nil {
if cmd.ProcessState.ExitCode() == 2 {
return "", fmt.Errorf("repository does not contain ref %s, output: %q: %w", path, string(out), err)
}
return "", err
}
if len(out) < 40 {
return "", fmt.Errorf("unexpected git command output: %q", string(out))
}
sha := string(out[:40])
if !commitSHA.MatchString(sha) {
return "", fmt.Errorf("invalid commit sha %q", sha)
}
ref.Commit = sha
err = g.resolveGitRef(ctx, path, ref)
if err != nil {
return "", err
}
cache, err := cacheDir()
@ -129,6 +114,29 @@ func (g gitRemoteLoader) Load(ctx context.Context, path string) (string, error)
return local, err
}
func (g gitRemoteLoader) resolveGitRef(ctx context.Context, path string, ref *gitutil.GitRef) error {
if !commitSHA.MatchString(ref.Commit) {
cmd := exec.CommandContext(ctx, "git", "ls-remote", "--exit-code", ref.Remote, ref.Commit)
cmd.Env = g.gitCommandEnv()
out, err := cmd.Output()
if err != nil {
if cmd.ProcessState.ExitCode() == 2 {
return fmt.Errorf("repository does not contain ref %s, output: %q: %w", path, string(out), err)
}
return err
}
if len(out) < 40 {
return fmt.Errorf("unexpected git command output: %q", string(out))
}
sha := string(out[:40])
if !commitSHA.MatchString(sha) {
return fmt.Errorf("invalid commit sha %q", sha)
}
ref.Commit = sha
}
return nil
}
func (g gitRemoteLoader) checkout(ctx context.Context, path string, ref *gitutil.GitRef) error {
err := os.MkdirAll(path, 0o700)
if err != nil {