forked from google/osv.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitter.go
More file actions
756 lines (641 loc) · 24.2 KB
/
gitter.go
File metadata and controls
756 lines (641 loc) · 24.2 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
// Package main is the main package for gitter caching service
package main
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"flag"
"fmt"
"io"
"log/slog"
"math"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"os/signal"
"path"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
_ "net/http/pprof" //nolint:gosec // This is a internal only service not public to the internet
"github.com/dgraph-io/ristretto/v2"
"github.com/dustin/go-humanize"
"github.com/google/osv.dev/go/logger"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"golang.org/x/sync/singleflight"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
pb "github.com/google/osv.dev/go/cmd/gitter/pb/repository"
)
type contextKey string
const (
urlKey contextKey = "repoURL"
)
const defaultGitterWorkDir = "/work/gitter"
const persistenceFileName = "last-fetch.json"
const gitStoreFileName = "git-store"
// API Endpoints
var endpointHandlers = map[string]http.HandlerFunc{
"GET /git": gitHandler,
"POST /cache": cacheHandler,
"POST /affected-commits": affectedCommitsHandler,
}
var (
gFetch singleflight.Group
gArchive singleflight.Group
gLoad singleflight.Group
persistencePath = filepath.Join(defaultGitterWorkDir, persistenceFileName)
gitStorePath = filepath.Join(defaultGitterWorkDir, gitStoreFileName)
fetchTimeout time.Duration
semaphore chan struct{} // Request concurrency control
// LRU cache for recently loaded repositories (key: repo URL)
repoCache *ristretto.Cache[string, *Repository]
repoTTL time.Duration
repoCacheMaxCostBytes int64
)
const shutdownTimeout = 10 * time.Second
type SeparatedEvents struct {
Introduced []string
Fixed []string
LastAffected []string
Limit []string
}
func separateEvents(events []*pb.Event) (*SeparatedEvents, error) {
se := &SeparatedEvents{}
for _, event := range events {
switch event.GetEventType() {
case pb.EventType_INTRODUCED:
se.Introduced = append(se.Introduced, event.GetHash())
case pb.EventType_FIXED:
se.Fixed = append(se.Fixed, event.GetHash())
case pb.EventType_LAST_AFFECTED:
se.LastAffected = append(se.LastAffected, event.GetHash())
case pb.EventType_LIMIT:
se.Limit = append(se.Limit, event.GetHash())
default:
return nil, fmt.Errorf("invalid event type: %s", event.GetEventType())
}
}
if len(se.Limit) > 0 && (len(se.Fixed) > 0 || len(se.LastAffected) > 0) {
return nil, errors.New("limit and fixed/last_affected shouldn't exist in the same request")
}
return se, nil
}
// repoLocks is a map of per-repository RWMutexes, with url as the key.
// It coordinates access between write operations (FetchRepo) that modify the git directory on disk
// and read operations (ArchiveRepo, LoadRepository, etc).
// It mainly handles:
// - Ensuring reads and writes are mutually exclusive.
// - Allowing multiple concurrent reads.
var repoLocks sync.Map
func GetRepoLock(repoURL string) *sync.RWMutex {
lock, _ := repoLocks.LoadOrStore(repoURL, &sync.RWMutex{})
return lock.(*sync.RWMutex)
}
// repoCostBytes is the cost function for a repository in the LRU cache.
// The memory cost of a repository is approximated from the num of commits and a base overhead.
func repoCostBytes(repo *Repository) int64 {
// Mutex (8 bytes), string for repo path (say 128 bytes), root commit (assume 1 root only, 32 bytes)
repoOverhead := 168
// Assuming per commit adds:
// - Commit struct (Hash, PatchID, Parent []int of size 1, Tags []string)
// = 20 + 20 + 24 + 8 + 24 + <string len> ~= 128 bytes
// - 1 pointer into []*Commit
// = 8 bytes
// - 1 entry in commitGraph ([][]int, assuming linear history)
// = 24 + 8 = 32 bytes
// - 1 entry to hashToIndex (map[SHA1]int)
// = 20 + 8 ~= 32 bytes
// - 1 entry to patchIDToCommits (map[SHA1][]int, assuming all commits are unique)
// = 20 + 24 + 8 ~= 64 bytes
// TOTAL: 264 bytes -> We round up to 300 for some buffer
costPerCommit := 300
return int64(repoOverhead + len(repo.commits)*costPerCommit)
}
// General guidance is to make NumCounters 10x the cache capacity (in terms of items)
// We're assuming the cache will hold 5000 repositories
const numCounters = int64(10 * 5000)
// InitRepoCache initializes the LRU cache for repositories.
func InitRepoCache() {
var err error
repoCache, err = ristretto.NewCache(&ristretto.Config[string, *Repository]{
NumCounters: numCounters,
MaxCost: repoCacheMaxCostBytes,
BufferItems: 64,
Cost: repoCostBytes,
// Check for TTL expiry every 60 seconds
TtlTickerDurationInSec: 60,
})
if err != nil {
logger.FatalContext(context.Background(), "Failed to initialize repository cache", slog.Any("err", err))
}
}
// CloseRepoCache closes the LRU cache.
func CloseRepoCache() {
if repoCache != nil {
repoCache.Close()
}
}
// prepareCmd prepares the command with context cancellation handled by sending SIGINT.
func prepareCmd(ctx context.Context, dir string, env []string, name string, args ...string) *exec.Cmd {
cmd := exec.CommandContext(ctx, name, args...)
if dir != "" {
cmd.Dir = dir
}
if len(env) > 0 {
cmd.Env = append(os.Environ(), env...)
}
// Use SIGINT instead of SIGKILL for graceful shutdown of subprocesses
cmd.Cancel = func() error {
logger.DebugContext(ctx, "SIGINT sent to command", slog.String("cmd", name), slog.Any("args", args))
return cmd.Process.Signal(syscall.SIGINT)
}
// Ensure it eventually dies if it ignores SIGINT
cmd.WaitDelay = shutdownTimeout / 2
return cmd
}
// runCmd executes a command with context cancellation handled by sending SIGINT.
// It logs cancellation errors separately as requested.
func runCmd(ctx context.Context, dir string, env []string, name string, args ...string) error {
logger.DebugContext(ctx, "Running command", slog.String("cmd", name), slog.Any("args", args))
cmd := prepareCmd(ctx, dir, env, name, args...)
out, err := cmd.CombinedOutput()
if err != nil {
if ctx.Err() != nil {
// Log separately if cancelled
logger.WarnContext(ctx, "Command cancelled", slog.String("cmd", name), slog.Any("err", ctx.Err()))
return fmt.Errorf("command %s cancelled: %w", name, ctx.Err())
}
return fmt.Errorf("command %s failed: %w, output: %s", name, err, out)
}
logger.DebugContext(ctx, "Command completed successfully", slog.String("cmd", name), slog.String("out", string(out)))
return nil
}
// runWithSemaphore runs function after waiting at semaphore for concurrency control
func runWithSemaphore(ctx context.Context, f func() (any, error)) (any, error) {
select {
case semaphore <- struct{}{}:
defer func() { <-semaphore }()
logger.DebugContext(ctx, "Concurrent requests", slog.Int("count", len(semaphore)))
return f()
case <-ctx.Done():
logger.WarnContext(ctx, "Request cancelled while waiting for semaphore")
return nil, ctx.Err()
}
}
func isLocalRequest(r *http.Request) bool {
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
// If SplitHostPort fails, it might be a raw IP (though rare in RemoteAddr),
// or an empty string. Try parsing the whole string as an IP.
host = r.RemoteAddr
}
ip := net.ParseIP(host)
if ip == nil {
return false
}
// Check if it's a loopback address (covers 127.0.0.0/8 and ::1)
return ip.IsLoopback()
}
func prepareURL(r *http.Request, repoURL string) (string, error) {
if repoURL == "" {
return "", errors.New("missing url parameter")
}
u, err := url.Parse(repoURL)
if err != nil {
return "", fmt.Errorf("error parsing url: %w", err)
}
// Convert git://github.com to https://github.com because it times out for some reason
// git protocol on non-github urls works fine
if u.Scheme == "git" && u.Host == "github.com" {
u.Scheme = "https"
}
// Remove query and fragment from the URL
u.RawQuery = ""
u.Fragment = ""
if !isLocalRequest(r) {
if u.Scheme != "http" && u.Scheme != "https" && u.Scheme != "git" {
return "", fmt.Errorf("unsupported protocol: %s", u.Scheme)
}
}
logger.Info("Prepared URL", slog.String("from", repoURL), slog.String("to", u.String()))
return u.String(), nil
}
func getRepoDirName(repoURL string) string {
base := path.Base(repoURL)
base = filepath.Base(base)
base = strings.TrimSuffix(base, ".git")
hash := sha256.Sum256([]byte(repoURL))
return fmt.Sprintf("%s-%s", base, hex.EncodeToString(hash[:]))
}
func isAuthError(err error) bool {
errString := err.Error()
return strings.Contains(errString, "could not read Username") ||
strings.Contains(errString, "Authentication failed") ||
(strings.Contains(strings.ToLower(errString), "repository") && strings.Contains(strings.ToLower(errString), "not found"))
}
func isIndexLockError(err error) bool {
if err == nil {
return false
}
errString := err.Error()
return strings.Contains(errString, "index.lock") && strings.Contains(errString, "File exists")
}
// Helper function to unmarshal request body based on Content-Type (protobuf or JSON)
func unmarshalRequest(r *http.Request, body proto.Message) error {
data, err := io.ReadAll(r.Body)
if err != nil {
return err
}
defer r.Body.Close()
contentType := r.Header.Get("Content-Type")
if contentType == "application/json" {
return protojson.Unmarshal(data, body)
}
// Default to protobuf
return proto.Unmarshal(data, body)
}
// Helper function to marshal response body based on Content-Type (protobuf or JSON)
func marshalResponse(r *http.Request, m proto.Message) ([]byte, error) {
contentType := r.Header.Get("Content-Type")
if contentType == "application/json" {
return protojson.Marshal(m)
}
// Default to protobuf
return proto.Marshal(m)
}
func doFetch(ctx context.Context, w http.ResponseWriter, repoURL string, forceUpdate bool) error {
_, err, _ := gFetch.Do(repoURL, func() (any, error) {
return runWithSemaphore(ctx, func() (any, error) {
return nil, FetchRepo(ctx, repoURL, forceUpdate)
})
})
if err != nil {
logger.ErrorContext(ctx, "Error fetching blob", slog.Any("error", err))
if isAuthError(err) {
http.Error(w, fmt.Sprintf("Error fetching blob: %v", err), http.StatusForbidden)
} else {
http.Error(w, fmt.Sprintf("Error fetching blob: %v", err), http.StatusInternalServerError)
}
return err
}
return nil
}
// getFreshRepo handles fetching and loading of a repository
// If forceUpdate is true, it will always refetch and rebuild the repository (commit graph, patch ID, etc)
// Otherwise, it will use a cache if available
func getFreshRepo(ctx context.Context, w http.ResponseWriter, repoURL string, forceUpdate bool) (*Repository, error) {
repoDirName := getRepoDirName(repoURL)
repoPath := filepath.Join(gitStorePath, repoDirName)
if !forceUpdate {
if repo, ok := repoCache.Get(repoURL); ok {
// repoCache.Get() will not return expired items, so we can safely return the repo
logger.DebugContext(ctx, "Repository already in cache, skipping fetch and load")
return repo, nil
}
}
if err := doFetch(ctx, w, repoURL, forceUpdate); err != nil {
return nil, err
}
repoAny, err, _ := gLoad.Do(repoPath, func() (any, error) {
return runWithSemaphore(ctx, func() (any, error) {
repoLock := GetRepoLock(repoURL)
repoLock.RLock()
defer repoLock.RUnlock()
return LoadRepository(ctx, repoPath)
})
})
if err != nil {
logger.ErrorContext(ctx, "Failed to load repository", slog.Any("error", err))
http.Error(w, fmt.Sprintf("Failed to load repository: %v", err), http.StatusInternalServerError)
return nil, err
}
repo := repoAny.(*Repository)
repoCache.SetWithTTL(repoURL, repo, 0, repoTTL)
return repo, nil
}
func FetchRepo(ctx context.Context, repoURL string, forceUpdate bool) error {
logger.InfoContext(ctx, "Starting fetch repo")
start := time.Now()
repoDirName := getRepoDirName(repoURL)
repoPath := filepath.Join(gitStorePath, repoDirName)
repoLock := GetRepoLock(repoURL)
repoLock.Lock()
defer repoLock.Unlock()
lastFetchMu.Lock()
accessTime, ok := lastFetch[repoURL]
lastFetchMu.Unlock()
// Check if we need to fetch
if forceUpdate || !ok || time.Since(accessTime) > fetchTimeout {
logger.InfoContext(ctx, "Fetching git blob", slog.Duration("sinceAccessTime", time.Since(accessTime)))
if _, err := os.Stat(filepath.Join(repoPath, ".git")); os.IsNotExist(err) {
// Clone
logger.InfoContext(ctx, "Cloning git repository", slog.Duration("sinceAccessTime", time.Since(accessTime)))
err := runCmd(ctx, "", []string{"GIT_TERMINAL_PROMPT=0"}, "git", "clone", "--", repoURL, repoPath)
if err != nil {
return fmt.Errorf("git clone failed: %w", err)
}
} else {
// Fetch/Pull - implementing simple git pull for now, might need reset --hard if we want exact mirrors
// For a generic "get latest", pull is usually sufficient if we treat it as read-only.
// Ideally safely: git fetch origin && git reset --hard origin/HEAD
logger.InfoContext(ctx, "Fetching git repository", slog.Duration("sinceAccessTime", time.Since(accessTime)))
err := runCmd(ctx, repoPath, nil, "git", "fetch", "origin")
if err != nil {
return fmt.Errorf("git fetch failed: %w", err)
}
err = runCmd(ctx, repoPath, nil, "git", "reset", "--hard", "origin/HEAD")
if err != nil && isIndexLockError(err) {
// index.lock exists, likely a previous git reset got terminated and wasn't cleaned up properly.
// We can remove the file and retry the command
logger.WarnContext(ctx, "index.lock exists, attempting to remove and retry")
indexLockPath := filepath.Join(repoPath, ".git", "index.lock")
if err := os.Remove(indexLockPath); err != nil {
return fmt.Errorf("failed to remove index.lock in %s: %w", repoPath, err)
}
// One more attempt at git reset
err = runCmd(ctx, repoPath, nil, "git", "reset", "--hard", "origin/HEAD")
}
if err != nil {
return fmt.Errorf("git reset failed: %w", err)
}
}
updateLastFetch(repoURL)
}
// Double check if the git directory exist
_, err := os.Stat(filepath.Join(repoPath, ".git"))
if err != nil {
if os.IsNotExist(err) {
deleteLastFetch(repoURL)
}
return fmt.Errorf("failed to read file: %w", err)
}
logger.InfoContext(ctx, "Fetch completed", slog.Duration("duration", time.Since(start)))
return nil
}
func ArchiveRepo(ctx context.Context, repoURL string) ([]byte, error) {
repoDirName := getRepoDirName(repoURL)
repoPath := filepath.Join(gitStorePath, repoDirName)
archivePath := repoPath + ".zst"
repoLock := GetRepoLock(repoURL)
repoLock.RLock()
defer repoLock.RUnlock()
lastFetchMu.Lock()
accessTime := lastFetch[repoURL]
lastFetchMu.Unlock()
// Check if archive needs update
// We update if archive does not exist OR if it is older than the last fetch
stats, err := os.Stat(archivePath)
if os.IsNotExist(err) || (err == nil && stats.ModTime().Before(accessTime)) {
logger.InfoContext(ctx, "Archiving git blob")
// Archive
// tar --zstd -cf <archivePath> -C "<gitStorePath>/<repoDirName>" .
// using -C to archive the relative path so it unzips nicely
err := runCmd(ctx, "", nil, "tar", "--zstd", "-cf", archivePath, "-C", filepath.Join(gitStorePath, repoDirName), ".")
if err != nil {
return nil, fmt.Errorf("tar zstd failed: %w", err)
}
}
// If the context is cancelled, still do the fetching stuff, just don't bother returning the result
// As we can still cache the result and reply faster next time.
if ctx.Err() != nil {
return nil, ctx.Err()
}
fileData, err := os.ReadFile(archivePath)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
return fileData, nil
}
func main() {
logger.InitGlobalLogger()
logger.RegisterContextKey(urlKey, "repoURL")
defer logger.Close()
port := flag.Int("port", 8888, "Listen port")
workDir := flag.String("work-dir", defaultGitterWorkDir, "Work directory")
flag.DurationVar(&fetchTimeout, "fetch-timeout", time.Hour, "Fetch timeout duration")
concurrentLimit := flag.Int("concurrent-limit", 100, "Concurrent limit for unique requests")
flag.DurationVar(&repoTTL, "repo-cache-ttl", time.Hour, "Repository LRU cache time-to-live duration")
repoMaxCostStr := flag.String("repo-cache-max-cost", "1GiB", "Repository LRU cache max cost (in bytes)")
flag.Parse()
semaphore = make(chan struct{}, *concurrentLimit)
persistencePath = filepath.Join(*workDir, persistenceFileName)
gitStorePath = filepath.Join(*workDir, gitStoreFileName)
if err := os.MkdirAll(gitStorePath, 0755); err != nil {
logger.Fatal("Failed to create git store path", slog.String("path", gitStorePath), slog.Any("error", err))
}
repoMaxCostUint, err := humanize.ParseBytes(*repoMaxCostStr)
if err != nil {
logger.Fatal("Failed to parse repo cache max cost", slog.String("maxCost", *repoMaxCostStr), slog.Any("error", err))
}
if repoMaxCostUint > math.MaxInt64 {
logger.Fatal("Repo cache max cost too large", slog.Uint64("maxCost", repoMaxCostUint))
}
repoCacheMaxCostBytes = int64(repoMaxCostUint)
loadLastFetchMap()
InitRepoCache()
defer CloseRepoCache()
// Create a context that listens for the interrupt signal from the OS.
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
for endpoint, handler := range endpointHandlers {
http.Handle(endpoint, otelhttp.NewHandler(handler, endpoint))
}
logger.Info("Gitter starting and listening", slog.Int("port", *port))
// --- Server Shutdown Protocol ---
// This is what happens when a kubernetes send a SIGTERM signal:
// 1. Kubernetes sends SIGTERM to the process
// 2. The process receives the signal and prints "Shutting down gracefully..."
// 3. The process calls server.Shutdown(ctx) to close incoming connections, and wait for timeout.
// 4. The context within each request will be automatically cancelled (does not wait for timeout).
// 5. Any subprocesses will be sent SIGINT, with a timeout / 2 duration before SIGKILL.
// 6. The server waits for the timeout to finish processing all requests.
// 7. We save the lastFetch map to disk.
// 8. The process exits
server := &http.Server{
Addr: fmt.Sprintf(":%d", *port),
ReadHeaderTimeout: 3 * time.Second,
BaseContext: func(_ net.Listener) context.Context {
// Return the context tied to the termination signal.
return ctx
},
}
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Error("Gitter failed to start", slog.Int("port", *port), slog.Any("error", err))
}
}()
// Listen for the interrupt signal.
<-ctx.Done()
// Restore default behavior on the interrupt signal and notify user of shutdown.
stop()
logger.Info("Shutting down gracefully, press Ctrl+C again to force")
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
logger.Error("Server forced to shutdown", slog.Any("error", err))
}
saveLastFetchMap()
logger.Info("Server exiting")
}
func gitHandler(w http.ResponseWriter, r *http.Request) {
repoURL, err := prepareURL(r, r.URL.Query().Get("url"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
forceUpdate := r.URL.Query().Get("force-update") == "true"
ctx := context.WithValue(r.Context(), urlKey, repoURL)
logger.InfoContext(ctx, "Received request: /git", slog.Bool("forceUpdate", forceUpdate), slog.String("remoteAddr", r.RemoteAddr))
// Fetch repo first
if err := doFetch(ctx, w, repoURL, forceUpdate); err != nil {
return
}
// Archive repo
fileDataAny, err, _ := gArchive.Do(repoURL, func() (any, error) {
return runWithSemaphore(ctx, func() (any, error) {
return ArchiveRepo(ctx, repoURL)
})
})
if err != nil {
logger.ErrorContext(ctx, "Error archiving blob", slog.Any("error", err))
http.Error(w, fmt.Sprintf("Error archiving blob: %v", err), http.StatusInternalServerError)
return
}
fileData := fileDataAny.([]byte)
w.Header().Set("Content-Type", "application/zstd")
w.Header().Set("Content-Disposition", "attachment; filename=\"git-blob.zst\"")
w.WriteHeader(http.StatusOK)
if _, err := io.Copy(w, bytes.NewReader(fileData)); err != nil {
logger.ErrorContext(ctx, "Error copying file", slog.Any("error", err))
http.Error(w, "Error copying file", http.StatusInternalServerError)
return
}
logger.InfoContext(ctx, "Request completed successfully: /git")
}
func cacheHandler(w http.ResponseWriter, r *http.Request) {
start := time.Now()
body := &pb.CacheRequest{}
if err := unmarshalRequest(r, body); err != nil {
http.Error(w, fmt.Sprintf("Error unmarshaling request: %v", err), http.StatusBadRequest)
return
}
repoURL, err := prepareURL(r, body.GetUrl())
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ctx := context.WithValue(r.Context(), urlKey, repoURL)
logger.InfoContext(ctx, "Received request: /cache")
if _, err := getFreshRepo(ctx, w, repoURL, body.GetForceUpdate()); err != nil {
return
}
w.WriteHeader(http.StatusOK)
logger.InfoContext(ctx, "Request completed successfully: /cache", slog.Duration("duration", time.Since(start)))
}
func affectedCommitsHandler(w http.ResponseWriter, r *http.Request) {
start := time.Now()
body := &pb.AffectedCommitsRequest{}
if err := unmarshalRequest(r, body); err != nil {
http.Error(w, fmt.Sprintf("Error unmarshaling request: %v", err), http.StatusBadRequest)
return
}
repoURL, err := prepareURL(r, body.GetUrl())
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
se, err := separateEvents(body.GetEvents())
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
cherrypickIntro := body.GetDetectCherrypicksIntroduced()
cherrypickFixed := body.GetDetectCherrypicksFixed()
cherrypickLimit := body.GetDetectCherrypicksLimit()
considerAllBranches := body.GetConsiderAllBranches()
// If cherrypick is true, consider all branches must be true
if cherrypickIntro || cherrypickFixed {
considerAllBranches = true
}
ctx := context.WithValue(r.Context(), urlKey, repoURL)
logger.InfoContext(ctx, "Received request: /affected-commits",
slog.Any("introduced", se.Introduced),
slog.Any("fixed", se.Fixed),
slog.Any("last_affected", se.LastAffected),
slog.Any("limit", se.Limit),
slog.Bool("cherrypickIntro", cherrypickIntro),
slog.Bool("cherrypickFixed", cherrypickFixed),
slog.Bool("cherrypickLimit", cherrypickLimit),
slog.Bool("considerAllBranches", considerAllBranches),
)
repo, err := getFreshRepo(ctx, w, repoURL, body.GetForceUpdate())
if err != nil {
return
}
var affectedCommits []*Commit
var cherrypicks cherrypickedHashes
switch {
case len(se.Limit) > 0:
affectedCommits, cherrypicks = repo.Limit(ctx, se, cherrypickIntro, cherrypickLimit)
case considerAllBranches:
affectedCommits, cherrypicks = repo.Affected(ctx, se, cherrypickIntro, cherrypickFixed)
default:
affectedCommits = repo.AffectedSingleBranch(ctx, se)
}
cherryPickedEvents := make([]*pb.Event, 0, len(cherrypicks.Introduced)+len(cherrypicks.Fixed)+len(cherrypicks.Limit))
for _, h := range cherrypicks.Introduced {
cherryPickedEvents = append(cherryPickedEvents, &pb.Event{
EventType: pb.EventType_INTRODUCED,
Hash: h,
})
}
for _, h := range cherrypicks.Fixed {
cherryPickedEvents = append(cherryPickedEvents, &pb.Event{
EventType: pb.EventType_FIXED,
Hash: h,
})
}
for _, h := range cherrypicks.Limit {
cherryPickedEvents = append(cherryPickedEvents, &pb.Event{
EventType: pb.EventType_LIMIT,
Hash: h,
})
}
resp := &pb.AffectedCommitsResponse{
Commits: make([]*pb.Commit, 0, len(affectedCommits)),
Tags: make([]*pb.Ref, 0),
CherryPickedEvents: cherryPickedEvents,
}
for _, c := range affectedCommits {
resp.Commits = append(resp.Commits, &pb.Commit{
Hash: c.Hash[:],
})
for _, tag := range c.Tags {
resp.Tags = append(resp.Tags, &pb.Ref{
Label: tag,
Hash: c.Hash[:],
})
}
}
out, err := marshalResponse(r, resp)
if err != nil {
logger.ErrorContext(ctx, "Error marshaling affected commits", slog.Any("error", err))
http.Error(w, fmt.Sprintf("Error marshaling affected commits: %v", err), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
w.WriteHeader(http.StatusOK)
if _, err := w.Write(out); err != nil {
logger.ErrorContext(ctx, "Error writing response", slog.Any("error", err))
}
logger.InfoContext(ctx, "Request completed successfully: /affected-commits", slog.Duration("duration", time.Since(start)))
}