aboutsummaryrefslogtreecommitdiff
path: root/http_handle_repo_upload_pack.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-03-06 15:17:57 +0800
committerRunxi Yu <me@runxiyu.org>2025-03-06 20:07:48 +0800
commit8ed0dbe4201a58b00d6f3743178f4cbe5328e2b0 (patch)
treea2f33fccac42b554b1176741d00c1d0cd2d0dceb /http_handle_repo_upload_pack.go
parentsql: Add purge and test scripts (diff)
downloadforge-8ed0dbe4201a58b00d6f3743178f4cbe5328e2b0.tar.gz
forge-8ed0dbe4201a58b00d6f3743178f4cbe5328e2b0.tar.zst
forge-8ed0dbe4201a58b00d6f3743178f4cbe5328e2b0.zip
*: Support subgroups via SQL recursion
Diffstat (limited to 'http_handle_repo_upload_pack.go')
-rw-r--r--http_handle_repo_upload_pack.go44
1 files changed, 39 insertions, 5 deletions
diff --git a/http_handle_repo_upload_pack.go b/http_handle_repo_upload_pack.go
index b273da9..4d3ec09 100644
--- a/http_handle_repo_upload_pack.go
+++ b/http_handle_repo_upload_pack.go
@@ -8,24 +8,58 @@ import (
"net/http"
"os"
"os/exec"
+
+ "github.com/jackc/pgx/v5/pgtype"
)
func handle_upload_pack(w http.ResponseWriter, r *http.Request, params map[string]any) (err error) {
- var group_name, repo_name string
+ var group_path []string
+ var repo_name string
var repo_path string
var stdout io.ReadCloser
var stdin io.WriteCloser
var cmd *exec.Cmd
- group_name, repo_name = params["group_name"].(string), params["repo_name"].(string)
+ group_path, repo_name = params["group_path"].([]string), params["repo_name"].(string)
- if err = database.QueryRow(r.Context(),
- "SELECT r.filesystem_path FROM repos r JOIN groups g ON r.group_id = g.id WHERE g.name = $1 AND r.name = $2;",
- group_name, repo_name,
+ if err := database.QueryRow(r.Context(), `
+ WITH RECURSIVE group_path_cte AS (
+ -- Start: match the first name in the path where parent_group IS NULL
+ SELECT
+ id,
+ parent_group,
+ name,
+ 1 AS depth
+ FROM groups
+ WHERE name = ($1::text[])[1]
+ AND parent_group IS NULL
+
+ UNION ALL
+
+ -- Recurse: jion next segment of the path
+ SELECT
+ g.id,
+ g.parent_group,
+ g.name,
+ group_path_cte.depth + 1
+ FROM groups g
+ JOIN group_path_cte ON g.parent_group = group_path_cte.id
+ WHERE g.name = ($1::text[])[group_path_cte.depth + 1]
+ AND group_path_cte.depth + 1 <= cardinality($1::text[])
+ )
+ SELECT r.filesystem_path
+ FROM group_path_cte c
+ JOIN repos r ON r.group_id = c.id
+ WHERE c.depth = cardinality($1::text[])
+ AND r.name = $2
+ `,
+ pgtype.FlatArray[string](group_path),
+ repo_name,
).Scan(&repo_path); err != nil {
return err
}
+
w.Header().Set("Content-Type", "application/x-git-upload-pack-result")
w.Header().Set("Connection", "Keep-Alive")
w.Header().Set("Transfer-Encoding", "chunked")