Lindenii Project Forge
*: Reformat
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-FileContributor: Runxi Yu <https://runxiyu.org> package main import ( "context" "errors" "io" "os" "strings"
"github.com/jackc/pgx/v5/pgtype"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object"
"github.com/jackc/pgx/v5/pgtype"
) // open_git_repo opens a git repository by group and repo name. func open_git_repo(ctx context.Context, group_path []string, repo_name string) (repo *git.Repository, description string, repo_id int, err error) { var fs_path string err = database.QueryRow(ctx, ` 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: join 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, COALESCE(r.description, ''), r.id FROM group_path_cte g JOIN repos r ON r.group_id = g.id WHERE g.depth = cardinality($1::text[]) AND r.name = $2 `, pgtype.FlatArray[string](group_path), repo_name).Scan(&fs_path, &description, &repo_id) if err != nil { return } repo, err = git.PlainOpen(fs_path) return } // go-git's tree entries are not friendly for use in HTML templates. type display_git_tree_entry_t struct { Name string Mode string Size int64 Is_file bool Is_subtree bool } func build_display_git_tree(tree *object.Tree) (display_git_tree []display_git_tree_entry_t) { for _, entry := range tree.Entries { display_git_tree_entry := display_git_tree_entry_t{} var err error var os_mode os.FileMode if os_mode, err = entry.Mode.ToOSFileMode(); err != nil { display_git_tree_entry.Mode = "x---------" } else { display_git_tree_entry.Mode = os_mode.String() } display_git_tree_entry.Is_file = entry.Mode.IsFile() if display_git_tree_entry.Size, err = tree.Size(entry.Name); err != nil { display_git_tree_entry.Size = 0 } display_git_tree_entry.Name = strings.TrimPrefix(entry.Name, "/") display_git_tree = append(display_git_tree, display_git_tree_entry) } return display_git_tree } func get_recent_commits(repo *git.Repository, head_hash plumbing.Hash, number_of_commits int) (recent_commits []*object.Commit, err error) { var commit_iter object.CommitIter var this_recent_commit *object.Commit commit_iter, err = repo.Log(&git.LogOptions{From: head_hash}) if err != nil { return nil, err } recent_commits = make([]*object.Commit, 0) defer commit_iter.Close() if number_of_commits < 0 { for { this_recent_commit, err = commit_iter.Next() if errors.Is(err, io.EOF) { return recent_commits, nil } else if err != nil { return nil, err } recent_commits = append(recent_commits, this_recent_commit) } } else { for range number_of_commits { this_recent_commit, err = commit_iter.Next() if errors.Is(err, io.EOF) { return recent_commits, nil } else if err != nil { return nil, err } recent_commits = append(recent_commits, this_recent_commit) } } return recent_commits, err } func get_patch_from_commit(commit_object *object.Commit) (parent_commit_hash plumbing.Hash, patch *object.Patch, err error) { var parent_commit_object *object.Commit var commit_tree *object.Tree parent_commit_object, err = commit_object.Parent(0) if errors.Is(err, object.ErrParentNotFound) { if commit_tree, err = commit_object.Tree(); err != nil { return } if patch, err = (&object.Tree{}).Patch(commit_tree); err != nil { return } } else if err != nil { return } else { parent_commit_hash = parent_commit_object.Hash if patch, err = parent_commit_object.Patch(commit_object); err != nil { return } } return }
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-FileContributor: Runxi Yu <https://runxiyu.org> package main import ( "fmt" "net/http" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" ) func handle_group_index(w http.ResponseWriter, r *http.Request, params map[string]any) { var group_path []string var repos []name_desc_t var subgroups []name_desc_t var err error group_path = params["group_path"].([]string) // Repos var rows pgx.Rows rows, err = database.Query(r.Context(), ` WITH RECURSIVE group_path_cte AS ( SELECT id, parent_group, name, 1 AS depth FROM groups WHERE name = ($1::text[])[1] AND parent_group IS NULL UNION ALL 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.name, COALESCE(r.description, '') FROM group_path_cte c JOIN repos r ON r.group_id = c.id WHERE c.depth = cardinality($1::text[]) `, pgtype.FlatArray[string](group_path), ) if err != nil { http.Error(w, "Error getting repos: "+err.Error(), http.StatusInternalServerError) return } defer rows.Close() for rows.Next() { var name, description string if err = rows.Scan(&name, &description); err != nil { http.Error(w, "Error getting repos: "+err.Error(), http.StatusInternalServerError) return } repos = append(repos, name_desc_t{name, description}) } if err = rows.Err(); err != nil { http.Error(w, "Error getting repos: "+err.Error(), http.StatusInternalServerError) return } // Subgroups rows, err = database.Query(r.Context(), ` WITH RECURSIVE group_path_cte AS ( SELECT id, parent_group, name, 1 AS depth FROM groups WHERE name = ($1::text[])[1] AND parent_group IS NULL UNION ALL 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 g.name, COALESCE(g.description, '') FROM group_path_cte c JOIN groups g ON g.parent_group = c.id WHERE c.depth = cardinality($1::text[]) `, pgtype.FlatArray[string](group_path), ) if err != nil { http.Error(w, "Error getting subgroups: "+err.Error(), http.StatusInternalServerError) return } defer rows.Close() for rows.Next() { var name, description string if err = rows.Scan(&name, &description); err != nil { http.Error(w, "Error getting subgroups: "+err.Error(), http.StatusInternalServerError) return } subgroups = append(subgroups, name_desc_t{name, description}) } if err = rows.Err(); err != nil { http.Error(w, "Error getting subgroups: "+err.Error(), http.StatusInternalServerError) return } params["repos"] = repos params["subgroups"] = subgroups fmt.Println(group_path) render_template(w, "group", params) }
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-FileContributor: Runxi Yu <https://runxiyu.org> package main import ( "io" "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_path []string var repo_name string var repo_path string var stdout io.ReadCloser var stdin io.WriteCloser var cmd *exec.Cmd group_path, repo_name = params["group_path"].([]string), params["repo_name"].(string) 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") w.WriteHeader(http.StatusOK) cmd = exec.Command("git", "upload-pack", "--stateless-rpc", repo_path) cmd.Env = append(os.Environ(), "LINDENII_FORGE_HOOKS_SOCKET_PATH="+config.Hooks.Socket) if stdout, err = cmd.StdoutPipe(); err != nil { return err } cmd.Stderr = cmd.Stdout defer func() { _ = stdout.Close() }() if stdin, err = cmd.StdinPipe(); err != nil { return err } defer func() { _ = stdin.Close() }() if err = cmd.Start(); err != nil { return err } if _, err = io.Copy(stdin, r.Body); err != nil { return err } if err = stdin.Close(); err != nil { return err } if _, err = io.Copy(w, stdout); err != nil { return err } if err = cmd.Wait(); err != nil { return err } return nil }
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-FileContributor: Runxi Yu <https://runxiyu.org> package main import ( "errors" "fmt" "net/http" "strconv" "strings" "github.com/jackc/pgx/v5" "go.lindenii.runxiyu.org/lindenii-common/clog" ) type http_router_t struct{} func (router *http_router_t) ServeHTTP(w http.ResponseWriter, r *http.Request) { clog.Info("Incoming HTTP: " + r.RemoteAddr + " " + r.Method + " " + r.RequestURI) var segments []string var err error var non_empty_last_segments_len int var separator_index int params := make(map[string]any) if segments, _, err = parse_request_uri(r.RequestURI); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } non_empty_last_segments_len = len(segments) if segments[len(segments)-1] == "" { non_empty_last_segments_len-- } if segments[0] == ":" { if len(segments) < 2 { http.Error(w, "Blank system endpoint", http.StatusNotFound) return } else if len(segments) == 2 && redirect_with_slash(w, r) { return } switch segments[1] { case "static": static_handler.ServeHTTP(w, r) return case "source": source_handler.ServeHTTP(w, r) return } } params["url_segments"] = segments params["global"] = global_data var _user_id int // 0 for none _user_id, params["username"], err = get_user_info_from_request(r) if errors.Is(err, http.ErrNoCookie) { } else if errors.Is(err, pgx.ErrNoRows) { } else if err != nil { http.Error(w, "Error getting user info from request: "+err.Error(), http.StatusInternalServerError) return } if _user_id == 0 { params["user_id"] = "" } else { params["user_id"] = strconv.Itoa(_user_id) } if segments[0] == ":" { switch segments[1] { case "login": handle_login(w, r, params) return case "users": handle_users(w, r, params) return default: http.Error(w, fmt.Sprintf("Unknown system module type: %s", segments[1]), http.StatusNotFound) return } } separator_index = -1 for i, part := range segments { if part == ":" { separator_index = i break } } params["separator_index"] = separator_index var group_path []string var module_type string var module_name string if separator_index > 0 { group_path = segments[:separator_index] } else {
group_path = segments[:len(segments) - 1]
group_path = segments[:len(segments)-1]
} params["group_path"] = group_path switch { case non_empty_last_segments_len == 0: handle_index(w, r, params) case separator_index == -1: if redirect_with_slash(w, r) { return } handle_group_index(w, r, params) case non_empty_last_segments_len == separator_index+1: http.Error(w, "Illegal path 1", http.StatusNotImplemented) return case non_empty_last_segments_len == separator_index+2: http.Error(w, "Illegal path 2", http.StatusNotImplemented) return default: module_type = segments[separator_index+1] module_name = segments[separator_index+2] switch module_type { case "repos": params["repo_name"] = module_name if non_empty_last_segments_len > separator_index+3 { switch segments[separator_index+3] { case "info": if err = handle_repo_info(w, r, params); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } return case "git-upload-pack": if err = handle_upload_pack(w, r, params); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } return } } if params["ref_type"], params["ref_name"], err = get_param_ref_and_type(r); err != nil { if errors.Is(err, err_no_ref_spec) { params["ref_type"] = "" } else { http.Error(w, "Error querying ref type: "+err.Error(), http.StatusInternalServerError) return } } // TODO: subgroups if params["repo"], params["repo_description"], params["repo_id"], err = open_git_repo(r.Context(), group_path, module_name); err != nil { http.Error(w, "Error opening repo: "+err.Error(), http.StatusInternalServerError) return } fmt.Println(non_empty_last_segments_len, separator_index, segments) if non_empty_last_segments_len == separator_index+3 { if redirect_with_slash(w, r) {
return
return
} handle_repo_index(w, r, params) return } repo_feature := segments[separator_index+3] switch repo_feature { case "tree": params["rest"] = strings.Join(segments[separator_index+4:], "/") if len(segments) < separator_index+5 && redirect_with_slash(w, r) { return } handle_repo_tree(w, r, params) case "raw": params["rest"] = strings.Join(segments[separator_index+4:], "/") if len(segments) < separator_index+5 && redirect_with_slash(w, r) { return } handle_repo_raw(w, r, params) case "log": if non_empty_last_segments_len > separator_index+4 { http.Error(w, "Too many parameters", http.StatusBadRequest) return } if redirect_with_slash(w, r) { return } handle_repo_log(w, r, params) case "commit": if redirect_without_slash(w, r) { return } params["commit_id"] = segments[separator_index+4] handle_repo_commit(w, r, params) case "contrib": if redirect_with_slash(w, r) { return } switch non_empty_last_segments_len { case separator_index + 4: handle_repo_contrib_index(w, r, params) case separator_index + 5: params["mr_id"] = segments[separator_index+4] handle_repo_contrib_one(w, r, params) default: http.Error(w, "Too many parameters", http.StatusBadRequest) } default: http.Error(w, fmt.Sprintf("Unknown repo feature: %s", repo_feature), http.StatusNotFound) } default: http.Error(w, fmt.Sprintf("Unknown module type: %s", module_type), http.StatusNotFound) } } }