Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions go/cmd/gitter/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,16 @@
logger.Info("Loaded lastFetch map", slog.Int("entry_count", len(lastFetch)))
}

func saveRepositoryCache(cachePath string, repo *Repository) error {
func saveRepositoryCache(cachePath string, repo *Repository) (int, error) {
logger.Info("Saving repository cache", slog.String("path", cachePath))

cache := &pb.RepositoryCache{}
emptyPatchID := SHA1{}
for _, commit := range repo.commits {
// Only save commits that have a patch ID
if commit.PatchID == emptyPatchID {
continue
}
cache.Commits = append(cache.Commits, &pb.CommitDetail{
Hash: commit.Hash[:],
PatchId: commit.PatchID[:],
Expand All @@ -99,10 +104,14 @@

data, err := proto.Marshal(cache)
if err != nil {
return err
return 0, err
}

if err := os.WriteFile(cachePath, data, 0600); err != nil {

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
return 0, err
}

return os.WriteFile(cachePath, data, 0600)
return len(cache.Commits), nil
}

func loadRepositoryCache(cachePath string) (*pb.RepositoryCache, error) {
Expand Down
12 changes: 8 additions & 4 deletions go/cmd/gitter/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,21 @@ func LoadRepository(ctx context.Context, repoPath string) (*Repository, error) {
return nil, fmt.Errorf("failed to build commit graph: %w", err)
}

var patchIDErr error
if len(newCommits) > 0 {
if err := repo.calculatePatchIDs(ctx, newCommits); err != nil {
return nil, fmt.Errorf("failed to calculate patch id for commits: %w", err)
}
patchIDErr = repo.calculatePatchIDs(ctx, newCommits)
}

// Save cache
if err := saveRepositoryCache(cachePath, repo); err != nil {
patchIDCount, err := saveRepositoryCache(cachePath, repo)
if err != nil {
logger.ErrorContext(ctx, "Failed to save repository cache", slog.Any("err", err))
}

if patchIDErr != nil {
return nil, fmt.Errorf("failed to fully calculate patch id for commits: %w, saved %d", patchIDErr, patchIDCount)
}

return repo, nil
}

Expand Down
Loading