diff --git a/sitesd/sitefeed/feed.go b/sitesd/sitefeed/feed.go index 881e05a50..09fd22565 100644 --- a/sitesd/sitefeed/feed.go +++ b/sitesd/sitefeed/feed.go @@ -2,6 +2,7 @@ package sitefeed import ( "context" + "encoding/base64" "encoding/hex" "encoding/json" "errors" @@ -187,12 +188,20 @@ func (f *Feed) deployContext(ctx context.Context, repoDid string) (*worker.Deplo // branchHeads reads the " " lines the knot packs into the snapshot, keyed by branch name func branchHeads(raw json.RawMessage) (map[string]string, error) { - var record org_tangled.GitRefs + var record struct { + Refs struct { + Bytes string `json:"$bytes"` + } `json:"refs"` + } if err := json.Unmarshal(raw, &record); err != nil { return nil, err } + refs, err := base64.RawStdEncoding.DecodeString(strings.TrimRight(record.Refs.Bytes, "=")) + if err != nil { + return nil, fmt.Errorf("parsing $bytes base64: %w", err) + } heads := make(map[string]string) - for _, line := range strings.Split(string(record.Refs), "\n") { + for _, line := range strings.Split(string(refs), "\n") { sha, name, ok := strings.Cut(line, " ") branch, isHead := strings.CutPrefix(name, "refs/heads/") if !ok || !isHead || branch == "" || !objectID(sha) { diff --git a/sitesd/sitefeed/feed_test.go b/sitesd/sitefeed/feed_test.go index a629e263f..cacd64e79 100644 --- a/sitesd/sitefeed/feed_test.go +++ b/sitesd/sitefeed/feed_test.go @@ -345,6 +345,24 @@ func TestBranchHeads(t *testing.T) { } } +func TestBranchHeadsPaddedBytes(t *testing.T) { + for _, enc := range []*base64.Encoding{base64.RawStdEncoding, base64.StdEncoding} { + // 1 mod 3 bytes pads with two = + blob := feedTestSha + " refs/heads/main\n" + for len(blob)%3 != 1 { + blob += "\n" + } + raw, _ := json.Marshal(map[string]any{"refs": map[string]string{"$bytes": enc.EncodeToString([]byte(blob))}}) + heads, err := branchHeads(raw) + if err != nil { + t.Fatalf("branchHeads: %v", err) + } + if heads["main"] != feedTestSha { + t.Errorf("heads = %v, want main at %s", heads, feedTestSha) + } + } +} + func TestCursorWithoutRedisStartsFromNow(t *testing.T) { db := cursorDB{} if _, err := db.GetLastTimeUs(); err == nil {