From 016633ef771ca0a9ad1abcdcdbd1b844b5cee877 Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Thu, 17 Jul 2025 09:42:40 +0100 Subject: [PATCH] knotserver/internal: calculate language breakdown on push Change-Id: wuslpzlrxlyoouqtlzwqrsnwlurlywmq Signed-off-by: oppiliappan --- knotserver/git/post_receive.go | 37 +++++++++++++++++++++++++++------- knotserver/internal.go | 6 ++++-- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/knotserver/git/post_receive.go b/knotserver/git/post_receive.go index e2ec8ac..1b3ea94 100644 --- a/knotserver/git/post_receive.go +++ b/knotserver/git/post_receive.go @@ -2,9 +2,11 @@ package git import ( "bufio" + "context" "fmt" "io" "strings" + "time" "tangled.sh/tangled.sh/core/api/tangled" @@ -46,8 +48,9 @@ func ParsePostReceive(buf io.Reader) ([]PostReceiveLine, error) { } type RefUpdateMeta struct { - CommitCount CommitCount - IsDefaultRef bool + CommitCount CommitCount + IsDefaultRef bool + LangBreakdown LangBreakdown } type CommitCount struct { @@ -57,17 +60,25 @@ type CommitCount struct { func (g *GitRepo) RefUpdateMeta(line PostReceiveLine) RefUpdateMeta { commitCount, err := g.newCommitCount(line) if err != nil { - // TODO: non-fatal, log this + // TODO: log this } isDefaultRef, err := g.isDefaultBranch(line) if err != nil { - // TODO: non-fatal, log this + // TODO: log this + } + + ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) + defer cancel() + breakdown, err := g.AnalyzeLanguages(ctx) + if err != nil { + // TODO: log this } return RefUpdateMeta{ - CommitCount: commitCount, - IsDefaultRef: isDefaultRef, + CommitCount: commitCount, + IsDefaultRef: isDefaultRef, + LangBreakdown: breakdown, } } @@ -135,10 +146,22 @@ func (m RefUpdateMeta) AsRecord() tangled.GitRefUpdate_Meta { }) } + var langs []*tangled.GitRefUpdate_Pair + for lang, size := range m.LangBreakdown { + langs = append(langs, &tangled.GitRefUpdate_Pair{ + Lang: lang, + Size: size, + }) + } + langBreakdown := &tangled.GitRefUpdate_Meta_LangBreakdown{ + Inputs: langs, + } + return tangled.GitRefUpdate_Meta{ CommitCount: &tangled.GitRefUpdate_Meta_CommitCount{ ByEmail: byEmail, }, - IsDefaultRef: m.IsDefaultRef, + IsDefaultRef: m.IsDefaultRef, + LangBreakdown: langBreakdown, } } diff --git a/knotserver/internal.go b/knotserver/internal.go index c0665c6..9450e87 100644 --- a/knotserver/internal.go +++ b/knotserver/internal.go @@ -3,6 +3,7 @@ package knotserver import ( "context" "encoding/json" + "fmt" "log/slog" "net/http" "path/filepath" @@ -115,12 +116,13 @@ func (h *InternalHandle) insertRefUpdate(line git.PostReceiveLine, gitUserDid, r return err } - gr, err := git.PlainOpen(repoPath) + gr, err := git.Open(repoPath, line.Ref) if err != nil { - return err + return fmt.Errorf("failed to open git repo at ref %s: %w", line.Ref, err) } meta := gr.RefUpdateMeta(line) + metaRecord := meta.AsRecord() refUpdate := tangled.GitRefUpdate{ -- 2.43.0