Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

knotserver/internal: calculate language breakdown on push

Signed-off-by: oppiliappan <me@oppi.li>

authored by

oppiliappan and committed by
Tangled
1f4f8258 9b690ac3

+34 -9
+30 -7
knotserver/git/post_receive.go
··· 2 2 3 3 import ( 4 4 "bufio" 5 + "context" 5 6 "fmt" 6 7 "io" 7 8 "strings" 9 + "time" 8 10 9 11 "tangled.sh/tangled.sh/core/api/tangled" 10 12 ··· 48 46 } 49 47 50 48 type RefUpdateMeta struct { 51 - CommitCount CommitCount 52 - IsDefaultRef bool 49 + CommitCount CommitCount 50 + IsDefaultRef bool 51 + LangBreakdown LangBreakdown 53 52 } 54 53 55 54 type CommitCount struct { ··· 60 57 func (g *GitRepo) RefUpdateMeta(line PostReceiveLine) RefUpdateMeta { 61 58 commitCount, err := g.newCommitCount(line) 62 59 if err != nil { 63 - // TODO: non-fatal, log this 60 + // TODO: log this 64 61 } 65 62 66 63 isDefaultRef, err := g.isDefaultBranch(line) 67 64 if err != nil { 68 - // TODO: non-fatal, log this 65 + // TODO: log this 66 + } 67 + 68 + ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) 69 + defer cancel() 70 + breakdown, err := g.AnalyzeLanguages(ctx) 71 + if err != nil { 72 + // TODO: log this 69 73 } 70 74 71 75 return RefUpdateMeta{ 72 - CommitCount: commitCount, 73 - IsDefaultRef: isDefaultRef, 76 + CommitCount: commitCount, 77 + IsDefaultRef: isDefaultRef, 78 + LangBreakdown: breakdown, 74 79 } 75 80 } 76 81 ··· 146 135 }) 147 136 } 148 137 138 + var langs []*tangled.GitRefUpdate_Pair 139 + for lang, size := range m.LangBreakdown { 140 + langs = append(langs, &tangled.GitRefUpdate_Pair{ 141 + Lang: lang, 142 + Size: size, 143 + }) 144 + } 145 + langBreakdown := &tangled.GitRefUpdate_Meta_LangBreakdown{ 146 + Inputs: langs, 147 + } 148 + 149 149 return tangled.GitRefUpdate_Meta{ 150 150 CommitCount: &tangled.GitRefUpdate_Meta_CommitCount{ 151 151 ByEmail: byEmail, 152 152 }, 153 - IsDefaultRef: m.IsDefaultRef, 153 + IsDefaultRef: m.IsDefaultRef, 154 + LangBreakdown: langBreakdown, 154 155 } 155 156 }
+4 -2
knotserver/internal.go
··· 3 3 import ( 4 4 "context" 5 5 "encoding/json" 6 + "fmt" 6 7 "log/slog" 7 8 "net/http" 8 9 "path/filepath" ··· 116 115 return err 117 116 } 118 117 119 - gr, err := git.PlainOpen(repoPath) 118 + gr, err := git.Open(repoPath, line.Ref) 120 119 if err != nil { 121 - return err 120 + return fmt.Errorf("failed to open git repo at ref %s: %w", line.Ref, err) 122 121 } 123 122 124 123 meta := gr.RefUpdateMeta(line) 124 + 125 125 metaRecord := meta.AsRecord() 126 126 127 127 refUpdate := tangled.GitRefUpdate{