Lindenii Project Forge
Login
Commit info
ID3f44e5773e9edd3044cd5235a36a06d01de13e31
AuthorRunxi Yu<me@runxiyu.org>
Author dateFri, 14 Feb 2025 11:29:49 +0800
CommitterRunxi Yu<me@runxiyu.org>
Committer dateFri, 14 Feb 2025 11:31:32 +0800
Actions
Get patch
repo_log: Use parameters for refspec
package main

import (
	"net/http"

	"github.com/go-git/go-git/v5/plumbing"
)

// TODO: I probably shouldn't include *all* commits here...
func handle_repo_log(w http.ResponseWriter, r *http.Request, params map[string]any) {
	group_name, repo_name, ref_name := params["group_name"].(string), params["repo_name"].(string), params["ref_name"].(string)
	repo, description, err := open_git_repo(r.Context(), group_name, repo_name)
	repo, description, err := open_git_repo(r.Context(), params["group_name"].(string), params["repo_name"].(string))
	if err != nil {
		http.Error(w, "Error opening repo: "+err.Error(), http.StatusInternalServerError)
		return
	}
	params["repo_description"] = description
	ref, err := repo.Reference(plumbing.NewBranchReferenceName(ref_name), true)

	ref_hash, err := get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string))
	if err != nil {
		http.Error(w, "Error getting repo reference: "+err.Error(), http.StatusInternalServerError)
		http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError)
		return
	}
	ref_hash := ref.Hash()

	commits, err := get_recent_commits(repo, ref_hash, -1)
	if err != nil {
		http.Error(w, "Error getting recent commits: "+err.Error(), http.StatusInternalServerError)
		return
	}
	params["commits"] = commits

	render_template(w, "repo_log", params)
}
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.Debug("Incoming HTTP: " + r.RemoteAddr + " " + r.Method + " " + r.RequestURI)

	segments, _, err := parse_request_uri(r.RequestURI)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	non_empty_last_segments_len := len(segments)
	trailing_slash := false
	if segments[len(segments)-1] == "" {
		non_empty_last_segments_len--
		trailing_slash = true
	}

	if segments[0] == ":" {
		if len(segments) < 2 {
			http.Error(w, "Blank system endpoint", http.StatusNotFound)
			return
		} else if len(segments) == 2 && !trailing_slash {
			http.Redirect(w, r, r.URL.Path+"/", http.StatusSeeOther)
			return
		}

		switch segments[1] {
		case "static":
			static_handler.ServeHTTP(w, r)
			return
		case "source":
			source_handler.ServeHTTP(w, r)
			return
		}
	}

	params := make(map[string]any)
	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
		}
	}

	switch {
	case non_empty_last_segments_len == 0:
		handle_index(w, r, params)
	case separator_index == -1:
		http.Error(w, "Group indexing hasn't been implemented yet", http.StatusNotImplemented)
	case non_empty_last_segments_len == separator_index+1:
		http.Error(w, "Group root hasn't been implemented yet", http.StatusNotImplemented)
	case non_empty_last_segments_len == separator_index+2:
		if !trailing_slash {
			http.Redirect(w, r, r.URL.Path+"/", http.StatusSeeOther)
			return
		}
		module_type := segments[separator_index+1]
		params["group_name"] = segments[0]
		switch module_type {
		case "repos":
			handle_group_repos(w, r, params)
		default:
			http.Error(w, fmt.Sprintf("Unknown module type: %s", module_type), http.StatusNotFound)
		}
	default:
		module_type := segments[separator_index+1]
		module_name := segments[separator_index+2]
		params["group_name"] = segments[0]
		switch module_type {
		case "repos":
			params["repo_name"] = module_name
			params["ref_type"], params["ref_name"], err = get_param_ref_and_type(r)
			if 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 non_empty_last_segments_len == separator_index+3 {
				if !trailing_slash {
					http.Redirect(w, r, r.URL.Path+"/", http.StatusSeeOther)
					return
				}
				handle_repo_index(w, r, params)
				return
			}
			repo_feature := segments[separator_index+3]
			switch repo_feature {
			case "info":
				handle_repo_info(w, r, params)
			case "tree":
				params["rest"] = strings.Join(segments[separator_index+4:], "/")
				handle_repo_tree(w, r, params)
			case "raw":
				params["rest"] = strings.Join(segments[separator_index+4:], "/")
				handle_repo_raw(w, r, params)
			case "log":
				if non_empty_last_segments_len > separator_index+5 {
				if non_empty_last_segments_len > separator_index+4 {
					http.Error(w, "Too many parameters", http.StatusBadRequest)
					return
				} else if non_empty_last_segments_len < separator_index+5 {
					http.Error(w, "Insufficient parameters", http.StatusBadRequest)
					return
				}
				if trailing_slash {
					http.Redirect(w, r, strings.TrimSuffix(r.URL.Path, "/"), http.StatusSeeOther)
				if !trailing_slash {
					http.Redirect(w, r, r.URL.Path+"/", http.StatusSeeOther)
					return
				}
				params["ref_name"] = segments[separator_index+4]
				handle_repo_log(w, r, params)
			case "commit":
				if trailing_slash {
					http.Redirect(w, r, strings.TrimSuffix(r.URL.Path, "/"), http.StatusSeeOther)
					return
				}
				params["commit_id"] = segments[separator_index+4]
				handle_repo_commit(w, r, params)
			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)
		}
	}
}

var err_bad_request = errors.New("Bad Request")
{{- define "repo_index" -}}
<!DOCTYPE html>
<html lang="en">
	<head>
		{{ template "head_common" . }}
		<title>{{ .group_name }}/repos/{{ .repo_name }} &ndash; Lindenii Forge</title>
	</head>
	<body class="repo-index">
		{{ template "header" . }}
		<div class="padding-wrapper">
			<table id="repo-info-table">
				<thead>
					<tr class="title-row">
						<th colspan="2">Repo Info</th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<th scope="row">Name</th>
						<td>{{ .repo_name }}</td>
					</tr>
					<tr>
						<th scope="row">Clone</th>
						<td><code>git clone {{ .clone_url }}</code></td>
					</tr>
					{{ if .repo_description }}
						<tr>
							<th scope="row">Description</th>
							<td>{{ .repo_description }}</td>
						</tr>
					{{ end }}
				</tbody>
			</table>
		</div>
		<div class="padding-wrapper scroll">
			<input id="toggle-table-recent-commits" type="checkbox" class="toggle-table-off" />
			<table id="recent-commits" class="wide">
				<thead>
					<tr class="title-row">
						<th colspan="3"><label for="toggle-table-recent-commits">Recent Commits (<a href="log/{{ .ref_name }}/">see all</a>)</label></th>
						<th colspan="3"><label for="toggle-table-recent-commits">Recent Commits (<a href="log/{{ if .ref_type }}?{{ .ref_type }}={{ .ref_name }}{{ end }}">see all</a>)</label></th>
					</tr>
				</thead>
				<tbody>
					{{- range .commits }}
						<tr>
							<td class="commit-title"><a href="commit/{{ .ID }}">{{ .Message | first_line }}</a></td>
							<td class="commit-author">
								<a class="email-name" href="mailto:{{ .Author.Email }}">{{ .Author.Name }}</a>
							</td>
							<td class="commit-time">
								{{ .Author.When.Format "2006-01-02 15:04:05 -0700" }}
							</td>
						</tr>
					{{- end }}
				</tbody>
			</table>
		</div>
		<div class="padding-wrapper scroll">
			<input id="toggle-table-file-tree" type="checkbox" class="toggle-table-off" />
			<table id="file-tree" class="wide">
				<thead>
					<tr class="title-row">
						<th colspan="3"><label for="toggle-table-file-tree">/{{ if .ref_name }} on {{ .ref_name }}{{ end }}</label></th>
					</tr>
				</thead>
				<tbody>
					{{- $ref_type := .ref_type }}
					{{- $ref := .ref_name }}
					{{- range .files }}
						<tr>
							<td class="file-mode">{{ .Mode }}</td>
							<td class="file-name"><a href="tree/{{ .Name }}{{ if not .Is_file }}/{{ end }}{{ if $ref_type }}?{{ $ref_type }}={{ $ref }}{{ end }}">{{ .Name }}</a>{{ if not .Is_file }}/{{ end }}</td>
							<td class="file-size">{{ .Size }}</td>
						</tr>
					{{- end }}
				</tbody>
			</table>
		</div>
		<div class="padding-wrapper">
			<div id="refs">
			</div>
		</div>
		<div class="padding-wrapper">
			{{ if .readme }}
				<input id="toggle-table-readme" type="checkbox" class="toggle-table-off" />
				<table class="wide">
					<thead>
						<tr class="title-row">
							<th><label for="toggle-table-readme">{{ .readme_filename }}</label></th>
						</tr>
					</thead>
					<tbody>
						<tr>
							<td id="readme">
								{{ .readme -}}
							</td>
						</tr>
					</tbody>
				</table>
			{{ end }}
		</div>
		<footer>
			{{ template "footer" . }}
		</footer>
	</body>
</html>
{{- end -}}
{{- define "repo_log" -}}
<!DOCTYPE html>
<html lang="en">
	<head>
		{{ template "head_common" . }}
		<title>Log of {{ .group_name }}/repos/{{ .repo_name }} &ndash; Lindenii Forge</title>
	</head>
	<body class="repo-log">
		{{ template "header" . }}
		<div class="scroll">
			<table id="commits" class="wide">
				<thead>
					<tr class="title-row">
						<th colspan="4">Commits on {{ .ref_name }}</th>
						<th colspan="4">Commits {{ if .ref_name }} on {{ .ref_name }}{{ end }}</th>
					</tr>
					<tr>
						<th scope="col">ID</th>
						<th scope="col">Title</th>
						<th scope="col">Author</th>
						<th scope="col">Time</th>
					</tr>
				</thead>
				<tbody>
					{{- range .commits }}
						<tr>
							<td class="commit-id"><a href="../commit/{{ .ID }}">{{ .ID }}</a></td>
							<td class="commit-title">{{ .Message | first_line }}</td>
							<td class="commit-author">
								<a class="email-name" href="mailto:{{ .Author.Email }}">{{ .Author.Name }}</a>
							</td>
							<td class="commit-time">
								{{ .Author.When.Format "2006-01-02 15:04:05 -0700" }}
							</td>
						</tr>
					{{- end }}
				</tbody>
			</table>
		</div>
		<footer>
			{{ template "footer" . }}
		</footer>
	</body>
</html>
{{- end -}}