tenseleyflow/shithub / 4f16faa

Browse files

S36: branchesList uses AheadBehindCached keyed on OIDs

Authored by espadonne
SHA
4f16faa5945861d48bed3bb051363412ddbb3148
Parents
9d196ff
Tree
88140e6

1 changed file

StatusFile+-
M internal/web/handlers/repo/branches.go 16 4
internal/web/handlers/repo/branches.gomodified
@@ -49,6 +49,16 @@ func (h *Handlers) branchesList(w http.ResponseWriter, r *http.Request) {
4949
 	rules, _ := h.rq.ListBranchProtectionRules(r.Context(), h.d.Pool, row.ID)
5050
 
5151
 	defaultBranch := row.DefaultBranch
52
+	// Resolve the default's OID once so the cached AheadBehind key is
53
+	// OID-based (immutable across same-OID pushes; cache stays hot
54
+	// when other branches advance).
55
+	defaultOID := ""
56
+	for _, b := range refs.Branches {
57
+		if b.Name == defaultBranch {
58
+			defaultOID = b.OID
59
+			break
60
+		}
61
+	}
5262
 	rows := make([]branchRow, 0, len(refs.Branches))
5363
 	now := time.Now()
5464
 	for _, b := range refs.Branches {
@@ -62,10 +72,12 @@ func (h *Handlers) branchesList(w http.ResponseWriter, r *http.Request) {
6272
 			br.LastWhen = hc.AuthorWhen
6373
 			br.Stale = now.Sub(hc.AuthorWhen) > 90*24*time.Hour
6474
 		}
65
-		if b.Name != defaultBranch {
66
-			ahead, behind, _ := repogit.AheadBehind(r.Context(), gitDir, defaultBranch, b.Name)
67
-			br.Ahead = ahead
68
-			br.Behind = behind
75
+		if b.Name != defaultBranch && defaultOID != "" {
76
+			res, _ := repogit.AheadBehindCached(r.Context(), gitDir, repogit.AheadBehindKey{
77
+				RepoID: row.ID, BaseOID: defaultOID, HeadOID: b.OID,
78
+			})
79
+			br.Ahead = res.Ahead
80
+			br.Behind = res.Behind
6981
 		}
7082
 		br.IsDefault = b.Name == defaultBranch
7183
 		br.Protected = isBranchProtected(rules, b.Name)