aboutsummaryrefslogtreecommitdiff
path: root/forged/internal/incoming/web/handlers/repo/index.go
blob: c2cb24a2f3050787ba3e8a1b9fcb572895dfa1b9 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

package repo

import (
	"bytes"
	"fmt"
	"html/template"
	"log/slog"
	"net/http"
	"net/url"
	"path/filepath"
	"strings"

	"github.com/yuin/goldmark"
	"github.com/yuin/goldmark/extension"
	"go.lindenii.runxiyu.org/forge/forged/internal/common/misc"
	"go.lindenii.runxiyu.org/forge/forged/internal/database/queries"
	wtypes "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/types"
	"go.lindenii.runxiyu.org/forge/forged/internal/ipc/git2c"
)

func (h *HTTP) Index(w http.ResponseWriter, r *http.Request, v wtypes.Vars) {
	base := wtypes.Base(r)
	repoName := v["repo"]
	slog.Info("repo index", "group_path", base.GroupPath, "repo", repoName)

	var userID int64
	if base.UserID != "" {
		_, _ = fmt.Sscan(base.UserID, &userID)
	}
	grp, err := base.Global.Queries.GetGroupByPath(r.Context(), queries.GetGroupByPathParams{
		Column1: base.GroupPath,
		UserID:  userID,
	})
	if err != nil {
		slog.Error("get group by path", "error", err)
		http.Error(w, "Group not found", http.StatusNotFound)
		return
	}

	repoRow, err := base.Global.Queries.GetRepoByGroupAndName(r.Context(), queries.GetRepoByGroupAndNameParams{
		GroupID: grp.ID,
		Name:    repoName,
	})
	if err != nil {
		slog.Error("get repo by name", "error", err)
		http.Error(w, "Repository not found", http.StatusNotFound)
		return
	}

	repoPath := filepath.Join(base.Global.Config.Git.RepoDir, fmt.Sprintf("%d.git", repoRow.ID))

	var commits []git2c.Commit
	var readme template.HTML
	var commitsErr error
	var readmeFile *git2c.FilenameContents
	var cerr error
	client, err := git2c.NewClient(r.Context(), base.Global.Config.Git.Socket)
	if err == nil {
		defer func() { _ = client.Close() }()
		commits, readmeFile, cerr = client.CmdIndex(repoPath)
		if cerr != nil {
			commitsErr = cerr
			slog.Error("git2d CmdIndex failed", "error", cerr, "path", repoPath)
		} else if readmeFile != nil {
			nameLower := strings.ToLower(readmeFile.Filename)
			if strings.HasSuffix(nameLower, ".md") || strings.HasSuffix(nameLower, ".markdown") || nameLower == "readme" {
				md := goldmark.New(
					goldmark.WithExtensions(extension.GFM),
				)
				var buf bytes.Buffer
				if err := md.Convert(readmeFile.Content, &buf); err == nil {
					readme = template.HTML(buf.String())
				} else {
					readme = template.HTML(template.HTMLEscapeString(string(readmeFile.Content)))
				}
			} else {
				readme = template.HTML(template.HTMLEscapeString(string(readmeFile.Content)))
			}
		}
	} else {
		commitsErr = err
		slog.Error("git2d connect failed", "error", err)
	}

	sshRoot := strings.TrimSuffix(base.Global.Config.SSH.Root, "/")
	httpRoot := strings.TrimSuffix(base.Global.Config.Web.Root, "/")
	pathPart := misc.SegmentsToURL(base.GroupPath) + "/-/repos/" + url.PathEscape(repoRow.Name)
	sshURL := ""
	httpURL := ""
	if sshRoot != "" {
		sshURL = sshRoot + "/" + pathPart
	}
	if httpRoot != "" {
		httpURL = httpRoot + "/" + pathPart
	}

	var notes []string
	if len(commits) == 0 && commitsErr == nil {
		notes = append(notes, "This repository has no commits yet.")
	}
	if readme == template.HTML("") {
		notes = append(notes, "No README found in the default branch.")
	}
	if sshURL == "" && httpURL == "" {
		notes = append(notes, "Clone URLs not configured (missing SSH root and HTTP root).")
	}

	cloneURL := sshURL
	if cloneURL == "" {
		cloneURL = httpURL
	}

	data := map[string]any{
		"BaseData":         base,
		"group_path":       base.GroupPath,
		"repo_name":        repoRow.Name,
		"repo_description": repoRow.Description,
		"ssh_clone_url":    cloneURL,
		"ref_name":         base.RefName,
		"commits":          commits,
		"commits_err":      &commitsErr,
		"readme":           readme,
		"notes":            notes,
		"global": map[string]any{
			"forge_title": base.Global.ForgeTitle,
		},
	}
	if err := h.r.Render(w, "repo_index", data); err != nil {
		slog.Error("render repo index", "error", err)
		http.Error(w, "Internal Server Error", http.StatusInternalServerError)
	}
}