(ctx *snap.Context, input string)
| 1334 | } |
| 1335 | |
| 1336 | func cloneRepository(ctx *snap.Context, input string) (string, error) { |
| 1337 | owner, repo, cloneURL, err := parseGitHubCloneInfo(input) |
| 1338 | if err != nil { |
| 1339 | return "", err |
| 1340 | } |
| 1341 | |
| 1342 | homeDir, err := os.UserHomeDir() |
| 1343 | if err != nil { |
| 1344 | return "", fmt.Errorf("determine home directory: %w", err) |
| 1345 | } |
| 1346 | |
| 1347 | targetDir := filepath.Join(homeDir, "gh", owner, repo) |
| 1348 | parentDir := filepath.Dir(targetDir) |
| 1349 | if err := os.MkdirAll(parentDir, 0o755); err != nil { |
| 1350 | return "", fmt.Errorf("creating %s: %w", parentDir, err) |
| 1351 | } |
| 1352 | |
| 1353 | if info, err := os.Stat(targetDir); err == nil { |
| 1354 | if info.IsDir() { |
| 1355 | return "", fmt.Errorf("destination %s already exists", targetDir) |
| 1356 | } |
| 1357 | return "", fmt.Errorf("destination %s exists and is not a directory", targetDir) |
| 1358 | } else if !errors.Is(err, os.ErrNotExist) { |
| 1359 | return "", fmt.Errorf("checking %s: %w", targetDir, err) |
| 1360 | } |
| 1361 | |
| 1362 | cmd := exec.Command("git", "clone", cloneURL, targetDir) |
| 1363 | output, err := cmd.CombinedOutput() |
| 1364 | if err != nil { |
| 1365 | trimmed := strings.TrimSpace(string(output)) |
| 1366 | if trimmed != "" { |
| 1367 | fmt.Fprintln(ctx.Stderr(), trimmed) |
| 1368 | } |
| 1369 | return "", fmt.Errorf("git clone failed: %w", err) |
| 1370 | } |
| 1371 | |
| 1372 | return targetDir, nil |
| 1373 | } |
| 1374 | |
| 1375 | func parsePullRequestRef(input string) (string, string, int, error) { |
| 1376 | candidate := strings.TrimSpace(strings.TrimSuffix(input, "/")) |
no test coverage detected