~gitpocket/gitpocket

8

refactor(smart): split the git service runner out of the repository handler

commit
5bdc6f72f732cf035f27e6d12f3b66b426568532 browse tree
parent
63d04abd18b3dda61f0429d71ade08bf45510023
author
~james <354576047+[email protected]>
signature
Signed by: ~james SHA256:KlzJ0P0gvJcnOYpaxD5yCUw52ImHttTKRjVAEnnYnKg
date
2026-11-14 11:28 UTC

1 files changed, +21 −9

forge/smart.go modified

@@ -88,14 +88,29 @@ func (s *Server) smart(w http.ResponseWriter, r *http.Request, owner User, name,
8888 return
8989 }
9090 }
91- dir := s.repoDir(repo)
91+ var recv, env []string
92+ limit := int64(0)
93+ if service == "git-receive-pack" {
94+ recv = append(receiveArgs(s.root), pushLimit(s.root, repo.Owner.Name, writer)...)
95+ env = receiveEnv(repo, pusher)
96+ if over, used, quota := overQuota(s.root, repo.Owner.Name); quota > 0 && !over {
97+ limit = quota - used + 1<<20
98+ }
99+ if !writer && (limit == 0 || limit > guestPush+1<<20) {
100+ limit = guestPush + 1<<20
101+ }
102+ }
103+ s.gitService(w, r, s.repoDir(repo), service, rest, recv, env, limit)
104+}
105+
106+func (s *Server) gitService(w http.ResponseWriter, r *http.Request, dir, service, rest string, recv, env []string, limit int64) {
92107 w.Header().Set("Cache-Control", "no-cache")
93108 ctx, cancel := context.WithTimeout(r.Context(), 10*time.Minute)
94109 defer cancel()
95110 if rest == "info/refs" {
96111 argv := []string{strings.TrimPrefix(service, "git-"), "--stateless-rpc", "--advertise-refs", dir}
97112 if service == "git-receive-pack" {
98- argv = append(receiveArgs(s.root), argv...)
113+ argv = append(recv, argv...)
99114 }
100115 cmd := exec.CommandContext(ctx, "git", argv...)
101116 cmd.Env = []string{"HOME=/nonexistent", "PATH=" + os.Getenv("PATH")}
@@ -121,19 +136,16 @@ func (s *Server) smart(w http.ResponseWriter, r *http.Request, owner User, name,
121136 }
122137 argv := []string{strings.TrimPrefix(service, "git-"), "--stateless-rpc", dir}
123138 if service == "git-receive-pack" {
124- argv = append(append(receiveArgs(s.root), pushLimit(s.root, repo.Owner.Name, writer)...), argv...)
125- if over, used, quota := overQuota(s.root, repo.Owner.Name); quota > 0 && !over {
126- body = io.LimitReader(body, quota-used+1<<20)
127- }
128- if !writer {
129- body = io.LimitReader(body, guestPush+1<<20)
139+ argv = append(recv, argv...)
140+ if limit > 0 {
141+ body = io.LimitReader(body, limit)
130142 }
131143 }
132144 cmd := exec.CommandContext(ctx, "git", argv...)
133145 cmd.Stdin = body
134146 cmd.Env = []string{"GIT_PROTOCOL=" + r.Header.Get("Git-Protocol"), "HOME=/nonexistent", "PATH=" + os.Getenv("PATH")}
135147 if service == "git-receive-pack" {
136- cmd.Env = append(cmd.Env, receiveEnv(repo, pusher)...)
148+ cmd.Env = append(cmd.Env, env...)
137149 }
138150 stdout, err := cmd.StdoutPipe()
139151 if err != nil {