Skip to content

Commit d646d75

Browse files
committed
lint
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 71237ef commit d646d75

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

cmd/compose/compose.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,7 @@ func (o *ProjectOptions) toProjectName(dockerCli command.Cli) (string, error) {
204204

205205
func (o *ProjectOptions) ToProject(dockerCli command.Cli, services []string, po ...cli.ProjectOptionsFn) (*types.Project, error) {
206206
if !o.Offline {
207-
var err error
208-
po, err = o.configureRemoteLoaders(dockerCli, po)
209-
if err != nil {
210-
return nil, err
211-
}
207+
po = o.configureRemoteLoaders(dockerCli, po)
212208
}
213209

214210
options, err := o.toProjectOptions(po...)
@@ -255,12 +251,12 @@ func (o *ProjectOptions) ToProject(dockerCli command.Cli, services []string, po
255251
return project, err
256252
}
257253

258-
func (o *ProjectOptions) configureRemoteLoaders(dockerCli command.Cli, po []cli.ProjectOptionsFn) ([]cli.ProjectOptionsFn, error) {
254+
func (o *ProjectOptions) configureRemoteLoaders(dockerCli command.Cli, po []cli.ProjectOptionsFn) []cli.ProjectOptionsFn {
259255
git := remote.NewGitRemoteLoader(o.Offline)
260256
oci := remote.NewOCIRemoteLoader(dockerCli, o.Offline)
261257

262258
po = append(po, cli.WithResourceLoader(git), cli.WithResourceLoader(oci))
263-
return po, nil
259+
return po
264260
}
265261

266262
func (o *ProjectOptions) toProjectOptions(po ...cli.ProjectOptionsFn) (*cli.ProjectOptions, error) {

pkg/remote/git.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,9 @@ func (g gitRemoteLoader) Load(ctx context.Context, path string) (string, error)
8080
ref.Commit = "HEAD" // default branch
8181
}
8282

83-
if !commitSHA.MatchString(ref.Commit) {
84-
cmd := exec.CommandContext(ctx, "git", "ls-remote", "--exit-code", ref.Remote, ref.Commit)
85-
cmd.Env = g.gitCommandEnv()
86-
out, err := cmd.Output()
87-
if err != nil {
88-
if cmd.ProcessState.ExitCode() == 2 {
89-
return "", fmt.Errorf("repository does not contain ref %s, output: %q: %w", path, string(out), err)
90-
}
91-
return "", err
92-
}
93-
if len(out) < 40 {
94-
return "", fmt.Errorf("unexpected git command output: %q", string(out))
95-
}
96-
sha := string(out[:40])
97-
if !commitSHA.MatchString(sha) {
98-
return "", fmt.Errorf("invalid commit sha %q", sha)
99-
}
100-
ref.Commit = sha
83+
err = g.resolveGitRef(ctx, path, ref)
84+
if err != nil {
85+
return "", err
10186
}
10287

10388
cache, err := cacheDir()
@@ -129,6 +114,29 @@ func (g gitRemoteLoader) Load(ctx context.Context, path string) (string, error)
129114
return local, err
130115
}
131116

117+
func (g gitRemoteLoader) resolveGitRef(ctx context.Context, path string, ref *gitutil.GitRef) error {
118+
if !commitSHA.MatchString(ref.Commit) {
119+
cmd := exec.CommandContext(ctx, "git", "ls-remote", "--exit-code", ref.Remote, ref.Commit)
120+
cmd.Env = g.gitCommandEnv()
121+
out, err := cmd.Output()
122+
if err != nil {
123+
if cmd.ProcessState.ExitCode() == 2 {
124+
return fmt.Errorf("repository does not contain ref %s, output: %q: %w", path, string(out), err)
125+
}
126+
return err
127+
}
128+
if len(out) < 40 {
129+
return fmt.Errorf("unexpected git command output: %q", string(out))
130+
}
131+
sha := string(out[:40])
132+
if !commitSHA.MatchString(sha) {
133+
return fmt.Errorf("invalid commit sha %q", sha)
134+
}
135+
ref.Commit = sha
136+
}
137+
return nil
138+
}
139+
132140
func (g gitRemoteLoader) checkout(ctx context.Context, path string, ref *gitutil.GitRef) error {
133141
err := os.MkdirAll(path, 0o700)
134142
if err != nil {

0 commit comments

Comments
 (0)