MCPcopy Create free account
hub / github.com/github/gh-aw / Set

Method Set

pkg/parser/import_cache.go:96–139  ·  view source on GitHub ↗

Set stores a new cache entry by saving the content to the cache directory sha parameter should be the resolved commit SHA

(owner, repo, path, sha string, content []byte)

Source from the content-addressed store, hash-verified

94// Set stores a new cache entry by saving the content to the cache directory
95// sha parameter should be the resolved commit SHA
96func (c *ImportCache) Set(owner, repo, path, sha string, content []byte) (string, error) {
97 importCacheLog.Printf("Setting cache entry: %s/%s/%s@%s, size=%d bytes", owner, repo, path, sha, len(content))
98
99 // Validate file size (max 10MB)
100 const maxFileSize = 10 * 1024 * 1024
101 if len(content) > maxFileSize {
102 importCacheLog.Printf("Cache set rejected: file size %d exceeds max %d bytes", len(content), maxFileSize)
103 return "", fmt.Errorf("file size (%d bytes) exceeds maximum allowed size (%d bytes)", len(content), maxFileSize)
104 }
105
106 // Validate path components to prevent path traversal
107 if err := validatePathComponents(owner, repo, path, sha); err != nil {
108 importCacheLog.Printf("Cache set rejected: invalid path components: %v", err)
109 return "", fmt.Errorf("invalid path components: %w", err)
110 }
111
112 // Use SHA in path for consistent caching
113 // This ensures that different refs pointing to the same commit reuse the same cache
114 sanitizedPath := sanitizePath(path)
115 relativeCachePath := filepath.Join(ImportCacheDir, owner, repo, sha, sanitizedPath)
116 fullCachePath := filepath.Join(c.baseDir, relativeCachePath)
117
118 // Ensure directory exists
119 dir := filepath.Dir(fullCachePath)
120 if err := os.MkdirAll(dir, constants.DirPermSensitive); err != nil {
121 importCacheLog.Printf("Failed to create cache directory: %v", err)
122 return "", err
123 }
124
125 // Ensure .gitattributes file exists in cache root
126 if err := c.ensureGitAttributes(); err != nil {
127 importCacheLog.Printf("Failed to ensure .gitattributes: %v", err)
128 // Non-fatal error - continue with caching
129 }
130
131 // Write content to cache file
132 if err := os.WriteFile(fullCachePath, content, constants.FilePermSensitive); err != nil {
133 importCacheLog.Printf("Failed to write cache file: %v", err)
134 return "", err
135 }
136
137 importCacheLog.Printf("Cached import: %s/%s/%s@%s -> %s", owner, repo, path, sha, fullCachePath)
138 return fullCachePath, nil
139}
140
141// GetCacheDir returns the base cache directory path
142func (c *ImportCache) GetCacheDir() string {

Calls 5

ensureGitAttributesMethod · 0.95
validatePathComponentsFunction · 0.85
sanitizePathFunction · 0.85
PrintfMethod · 0.45
ErrorfMethod · 0.45