1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
package storage
import (
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
type GitCmdAdapter struct {
s3Storage *S3Storage
tempDir string
}
func NewGitCmdAdapter(s3Storage *S3Storage) *GitCmdAdapter {
tempDir, err := os.MkdirTemp("", "giddy-repos-")
if err != nil {
panic(fmt.Sprintf("failed to create temp directory: %v", err))
}
return &GitCmdAdapter{
s3Storage: s3Storage,
tempDir: tempDir,
}
}
func (g *GitCmdAdapter) Cleanup() {
os.RemoveAll(g.tempDir)
}
// GetObject gets a Git object using git cat-file
func (g *GitCmdAdapter) GetObject(ctx context.Context, repo, objectID string) ([]byte, error) {
repoPath, err := g.ensureRepository(ctx, repo)
if err != nil {
return nil, err
}
// Get object type first
typeCmd := exec.CommandContext(ctx, "git", "--git-dir", repoPath, "cat-file", "-t", objectID)
typeOutput, err := typeCmd.Output()
if err != nil {
return nil, fmt.Errorf("git cat-file -t failed for %s: %w", objectID, err)
}
objType := strings.TrimSpace(string(typeOutput))
// Get raw object content using type and objectID
contentCmd := exec.CommandContext(ctx, "git", "--git-dir", repoPath, "cat-file", objType, objectID)
content, err := contentCmd.Output()
if err != nil {
return nil, fmt.Errorf("git cat-file failed for %s: %w", objectID, err)
}
// Format as Git object: "type size\0content"
result := fmt.Sprintf("%s %d\x00", objType, len(content))
return append([]byte(result), content...), nil
}
// GetReachableObjects returns all objects reachable from the given commits
func (g *GitCmdAdapter) GetReachableObjects(ctx context.Context, repo string, wants []string, haves []string) ([]string, error) {
repoPath, err := g.ensureRepository(ctx, repo)
if err != nil {
return nil, err
}
// Use git rev-list to get all reachable objects
args := []string{"--git-dir", repoPath, "rev-list", "--objects"}
args = append(args, wants...)
// Exclude objects reachable from haves
for _, have := range haves {
args = append(args, "^"+have)
}
cmd := exec.CommandContext(ctx, "git", args...)
output, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("git rev-list failed: %w", err)
}
var objects []string
lines := strings.Split(strings.TrimSpace(string(output)), "\n")
for _, line := range lines {
if line == "" {
continue
}
// Each line is: "objectid [path]"
parts := strings.Fields(line)
if len(parts) > 0 {
objects = append(objects, parts[0])
}
}
log.Printf("Found %d reachable objects", len(objects))
return objects, nil
}
func (g *GitCmdAdapter) ensureRepository(ctx context.Context, repo string) (string, error) {
repoPath := filepath.Join(g.tempDir, strings.ReplaceAll(repo, "/", "_"))
// Check if already downloaded (check for any git files)
if _, err := os.Stat(filepath.Join(repoPath, "objects")); err == nil {
log.Printf("Repository %s already downloaded to %s", repo, repoPath)
return repoPath, nil
}
log.Printf("Downloading repository %s from S3 to %s", repo, repoPath)
// Create repo directory
err := os.MkdirAll(repoPath, 0755)
if err != nil {
return "", fmt.Errorf("failed to create repo directory: %w", err)
}
// Download all repository files from S3 with timeout
downloadCtx, cancel := context.WithTimeout(ctx, 300*time.Second)
defer cancel()
err = g.downloadRepositoryFromS3(downloadCtx, repo, repoPath)
if err != nil {
return "", fmt.Errorf("failed to download repository: %w", err)
}
log.Printf("Successfully downloaded repository %s", repo)
return repoPath, nil
}
func (g *GitCmdAdapter) downloadRepositoryFromS3(ctx context.Context, repo, localPath string) error {
// List all objects in the repository
prefix := repo + "/"
paginator := s3.NewListObjectsV2Paginator(g.s3Storage.client, &s3.ListObjectsV2Input{
Bucket: aws.String(g.s3Storage.bucket),
Prefix: aws.String(prefix),
})
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
return fmt.Errorf("failed to list objects: %w", err)
}
for _, obj := range page.Contents {
key := *obj.Key
relativePath := strings.TrimPrefix(key, prefix)
if relativePath == "" {
continue // Skip the prefix itself
}
localFilePath := filepath.Join(localPath, relativePath)
// Create directory if needed
dir := filepath.Dir(localFilePath)
err := os.MkdirAll(dir, 0755)
if err != nil {
return fmt.Errorf("failed to create directory %s: %w", dir, err)
}
// Download the file
err = g.downloadFile(ctx, key, localFilePath)
if err != nil {
return fmt.Errorf("failed to download %s: %w", key, err)
}
log.Printf("Downloaded: %s -> %s", key, localFilePath)
}
}
return nil
}
func (g *GitCmdAdapter) downloadFile(ctx context.Context, s3Key, localPath string) error {
result, err := g.s3Storage.client.GetObject(ctx, &s3.GetObjectInput{
Bucket: aws.String(g.s3Storage.bucket),
Key: aws.String(s3Key),
})
if err != nil {
return err
}
defer result.Body.Close()
file, err := os.Create(localPath)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, result.Body)
return err
}
|