GetCommitTreeFiles returns all files that exist in a commit's tree. Unlike GetCommitDartFiles which only returns files changed in a commit, this returns all files that existed at that point in time. Returns absolute paths to all files in the commit tree.
(repoPath, commitID string)
| 13 | // this returns all files that existed at that point in time. |
| 14 | // Returns absolute paths to all files in the commit tree. |
| 15 | func GetCommitTreeFiles(repoPath, commitID string) ([]string, error) { |
| 16 | // Validate the repository path exists |
| 17 | if _, err := os.Stat(repoPath); os.IsNotExist(err) { |
| 18 | return nil, fmt.Errorf("repository path does not exist: %s", repoPath) |
| 19 | } |
| 20 | |
| 21 | // Verify it's a git repository |
| 22 | if !isGitRepository(repoPath) { |
| 23 | return nil, fmt.Errorf("%s is not a git repository (use 'git init' to initialize)", repoPath) |
| 24 | } |
| 25 | |
| 26 | // Validate the commit exists |
| 27 | if err := validateCommit(repoPath, commitID); err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | |
| 31 | // Get the repository root |
| 32 | repoRoot, err := GetRepositoryRoot(repoPath) |
| 33 | if err != nil { |
| 34 | return nil, fmt.Errorf("failed to get repository root: %w", err) |
| 35 | } |
| 36 | |
| 37 | // Use git ls-tree to list all files in the commit tree |
| 38 | cmd := exec.Command("git", "ls-tree", "-r", "--name-only", commitID) |
| 39 | cmd.Dir = repoPath |
| 40 | |
| 41 | var stdout bytes.Buffer |
| 42 | var stderr bytes.Buffer |
| 43 | cmd.Stdout = &stdout |
| 44 | cmd.Stderr = &stderr |
| 45 | |
| 46 | if err := cmd.Run(); err != nil { |
| 47 | if stderr.Len() > 0 { |
| 48 | return nil, fmt.Errorf("git command failed: %s", stderr.String()) |
| 49 | } |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | // Parse the output - one file per line |
| 54 | var files []string |
| 55 | lines := strings.Split(stdout.String(), "\n") |
| 56 | for _, line := range lines { |
| 57 | line = strings.TrimSpace(line) |
| 58 | if line != "" { |
| 59 | files = append(files, line) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Convert to absolute paths |
| 64 | absolutePaths := toAbsolutePaths(repoRoot, files) |
| 65 | |
| 66 | return absolutePaths, nil |
| 67 | } |