package server import ( "bytes" "fmt" "os" "os/exec" "path/filepath" "strings" "git.a-wan.net/alan/pipelines/internal/stage" ) // landingAccess derives delivery paths from Git's verified worktree metadata, // never from stage input. A missing canonical main worktree fails closed. func landingAccess(workspace string) (stage.LandingAccess, error) { workspace, err := canonicalDirectory(workspace) if err != nil { return stage.LandingAccess{}, fmt.Errorf("delivery workspace: %w", err) } gitDir, err := gitOutput(workspace, "rev-parse", "--git-dir") if err != nil { return stage.LandingAccess{}, fmt.Errorf("delivery workspace is not a Git worktree: %w", err) } commonDir, err := gitOutput(workspace, "rev-parse", "--git-common-dir") if err != nil { return stage.LandingAccess{}, fmt.Errorf("delivery repository metadata: %w", err) } gitDir, err = canonicalDirectory(resolveGitPath(workspace, gitDir)) if err != nil { return stage.LandingAccess{}, fmt.Errorf("delivery Git metadata: %w", err) } commonDir, err = canonicalDirectory(resolveGitPath(workspace, commonDir)) if err != nil { return stage.LandingAccess{}, fmt.Errorf("delivery common Git metadata: %w", err) } main, err := mainWorktree(workspace, commonDir) if err != nil { return stage.LandingAccess{}, err } // Delivery originates in a linked item worktree. Its metadata must be under // the repository's canonical common directory, never a standalone or foreign // checkout. Git's common directory is the bounded capability needed for refs, // worktree state, and the landing lock. if !pathWithin(filepath.Join(commonDir, "worktrees"), gitDir) { return stage.LandingAccess{}, fmt.Errorf("delivery workspace is not a linked Git worktree") } return stage.LandingAccess{MainWorktree: main, GitMetadata: commonDir, LockPath: filepath.Join(commonDir, "pipelines-landing.lock")}, nil } func mainWorktree(workspace, commonDir string) (string, error) { list, err := gitOutput(workspace, "worktree", "list", "--porcelain") if err != nil { return "", fmt.Errorf("list repository worktrees: %w", err) } for _, record := range strings.Split(strings.TrimSpace(list), "\n\n") { lines := strings.Split(record, "\n") if len(lines) == 0 || !strings.HasPrefix(lines[0], "worktree ") || !worktreeHasBranch(lines, "refs/heads/main") { continue } candidate, err := canonicalDirectory(strings.TrimPrefix(lines[0], "worktree ")) if err != nil || candidate == workspace { continue } candidateGitDir, err := gitOutput(candidate, "rev-parse", "--git-dir") if err != nil { continue } candidateGitDir, err = canonicalDirectory(resolveGitPath(candidate, candidateGitDir)) if err != nil || candidateGitDir != commonDir { continue } candidateCommon, err := gitOutput(candidate, "rev-parse", "--git-common-dir") if err != nil { continue } candidateCommon, err = canonicalDirectory(resolveGitPath(candidate, candidateCommon)) if err == nil && candidateCommon == commonDir { return candidate, nil } } return "", fmt.Errorf("no canonical main worktree in the delivery repository") } func worktreeHasBranch(lines []string, branch string) bool { for _, line := range lines[1:] { if line == "branch "+branch { return true } } return false } func canonicalDirectory(path string) (string, error) { resolved, err := filepath.EvalSymlinks(path) if err != nil { return "", err } info, err := os.Stat(resolved) if err != nil { return "", err } if !info.IsDir() { return "", fmt.Errorf("not a directory") } return filepath.Clean(resolved), nil } func resolveGitPath(workspace, value string) string { if filepath.IsAbs(value) { return value } return filepath.Join(workspace, value) } func pathWithin(parent, child string) bool { rel, err := filepath.Rel(parent, child) return err == nil && rel != "." && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) } func gitOutput(directory string, args ...string) (string, error) { cmd := exec.Command("git", append([]string{"-C", directory}, args...)...) var stderr bytes.Buffer cmd.Stderr = &stderr data, err := cmd.Output() if err != nil { return "", fmt.Errorf("git %s: %w: %s", strings.Join(args, " "), err, strings.TrimSpace(stderr.String())) } return strings.TrimSpace(string(data)), nil }