浏览代码

lint

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 2 年之前
父节点
当前提交
d646d757a2
共有 2 个文件被更改,包括 29 次插入25 次删除
  1. 3 7
      cmd/compose/compose.go
  2. 26 18
      pkg/remote/git.go

+ 3 - 7
cmd/compose/compose.go

@@ -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) {

+ 26 - 18
pkg/remote/git.go

@@ -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 {