aboutsummaryrefslogtreecommitdiff
path: root/http_handle_repo_index.go
blob: c253fa95d5839df554b5d43ad245cd16cfa1cbc1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>

package main

import (
	"net/http"
	"strings"

	"go.lindenii.runxiyu.org/forge/git2c"
	"go.lindenii.runxiyu.org/forge/render"
)

type commitDisplay struct {
	Hash    string
	Author  string
	Email   string
	Date    string
	Message string
}

// httpHandleRepoIndex provides the front page of a repo using git2d.
func (s *server) httpHandleRepoIndex(w http.ResponseWriter, req *http.Request, params map[string]any) {
	repoName := params["repo_name"].(string)
	groupPath := params["group_path"].([]string)

	_, repoPath, _, _, _, _, _ := s.getRepoInfo(req.Context(), groupPath, repoName, "") // TODO: Don't use getRepoInfo

	var notes []string
	if strings.Contains(repoName, "\n") || sliceContainsNewlines(groupPath) {
		notes = append(notes, "Path contains newlines; HTTP Git access impossible")
	}

	client, err := git2c.NewClient(s.config.Git.Socket)
	if err != nil {
		errorPage500(w, params, err.Error())
		return
	}
	defer client.Close()

	commits, readme, err := client.Cmd1(repoPath)
	if err != nil {
		errorPage500(w, params, err.Error())
		return
	}

	params["commits"] = commits
	params["readme_filename"] = readme.Filename
	_, params["readme"] = render.Readme(readme.Content, readme.Filename)
	params["notes"] = notes

	renderTemplate(w, "repo_index", params)

	// TODO: Caching
}