Skip to content

Commit f699d19

Browse files
Switch to creating a tempdir and specifying that for cloning
1 parent 383ca96 commit f699d19

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

internal/cmd/fetcher/main.go

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func recreateNPMPackageLock(ctx context.Context, plugin createdPlugin) error {
125125
// recreateSwiftPackageResolved resolves Swift package dependencies for plugins that use Swift packages.
126126
// It clones the git repository specified in the Dockerfile, runs 'swift package resolve',
127127
// and moves the generated Package.resolved file to the version directory.
128-
func recreateSwiftPackageResolved(ctx context.Context, plugin createdPlugin) error {
128+
func recreateSwiftPackageResolved(ctx context.Context, plugin createdPlugin) (retErr error) {
129129
versionDir := filepath.Join(plugin.pluginDir, plugin.newVersion)
130130
packageResolved := filepath.Join(versionDir, "Package.resolved")
131131
_, err := os.Stat(packageResolved)
@@ -164,52 +164,40 @@ func recreateSwiftPackageResolved(ctx context.Context, plugin createdPlugin) err
164164

165165
log.Printf("resolving Swift package for %s/%s:%s", plugin.org, plugin.name, plugin.newVersion)
166166

167-
// Execute the git clone command
168-
cmd := exec.CommandContext(ctx, "sh", "-c", gitCloneCmd)
167+
// Create a tempdir for cloning the repo
168+
tmpDir, err := os.MkdirTemp("", "swift-repo-*")
169+
if err != nil {
170+
return fmt.Errorf("creating tmp dir: %w", err)
171+
}
172+
defer func() {
173+
retErr = errors.Join(retErr, os.RemoveAll(tmpDir))
174+
}()
175+
176+
// Execute the git clone command, cloning to the tmpDir
177+
cmd := exec.CommandContext(ctx, "sh", "-c", gitCloneCmd, "--", tmpDir)
169178
cmd.Dir = versionDir
170179
cmd.Stdout = os.Stdout
171180
cmd.Stderr = os.Stderr
172181
if err := cmd.Run(); err != nil {
173182
return fmt.Errorf("failed to run git clone: %w", err)
174183
}
175184

176-
// Extract the repository name from the git clone command to determine the directory
177-
parts := strings.Fields(gitCloneCmd)
178-
var repoDir string
179-
for _, part := range parts {
180-
if strings.HasPrefix(part, "https://") {
181-
// Extract directory name from URL (e.g., "repo.git" or "repo")
182-
repoName := filepath.Base(part)
183-
repoName = strings.TrimSuffix(repoName, ".git")
184-
repoDir = filepath.Join(versionDir, repoName)
185-
break
186-
}
187-
}
188-
if repoDir == "" {
189-
return errors.New("failed to determine repository directory from git clone command")
190-
}
191-
192185
// Run `swift package resolve` in the cloned directory
193186
cmd = exec.CommandContext(ctx, "swift", "package", "resolve")
194-
cmd.Dir = repoDir
187+
cmd.Dir = tmpDir
195188
cmd.Stdout = os.Stdout
196189
cmd.Stderr = os.Stderr
197190
if err := cmd.Run(); err != nil {
198191
return fmt.Errorf("failed to run swift package resolve: %w", err)
199192
}
200193

201194
// Move the Package.resolved file from the cloned directory to the version directory
202-
src := filepath.Join(repoDir, "Package.resolved")
195+
src := filepath.Join(tmpDir, "Package.resolved")
203196
dest := packageResolved
204197
if err := os.Rename(src, dest); err != nil {
205198
return fmt.Errorf("failed to move Package.resolved: %w", err)
206199
}
207200

208-
// Remove the cloned repo
209-
if err := os.RemoveAll(repoDir); err != nil {
210-
return fmt.Errorf("removing cloned repo: %w", err)
211-
}
212-
213201
return nil
214202
}
215203

0 commit comments

Comments
 (0)