1package web
2
3import (
4 "net/http"
5
6 "github.com/charmbracelet/log/v2"
7 "github.com/charmbracelet/soft-serve/git"
8 "github.com/charmbracelet/soft-serve/pkg/config"
9 "github.com/charmbracelet/soft-serve/pkg/proto"
10 "github.com/gorilla/mux"
11)
12
13// CommitData contains data for rendering individual commit view.
14type CommitData struct {
15 Repo proto.Repository
16 DefaultBranch string
17 Commit *git.Commit
18 Diff *git.Diff
19 ParentIDs []string
20 ActiveTab string
21 ServerName string
22}
23
24func repoCommit(w http.ResponseWriter, r *http.Request) {
25 ctx := r.Context()
26 logger := log.FromContext(ctx)
27 cfg := config.FromContext(ctx)
28 repo := proto.RepositoryFromContext(ctx)
29 vars := mux.Vars(r)
30 hash := vars["hash"]
31
32 gr, err := openRepository(repo)
33 if err != nil {
34 logger.Debug("failed to open repository", "repo", repo.Name(), "err", err)
35 renderInternalServerError(w, r)
36 return
37 }
38
39 commit, err := gr.CatFileCommit(hash)
40 if err != nil {
41 logger.Debug("failed to get commit", "repo", repo.Name(), "hash", hash, "err", err)
42 renderNotFound(w, r)
43 return
44 }
45
46 diff, err := gr.Diff(commit)
47 if err != nil {
48 logger.Debug("failed to get diff", "repo", repo.Name(), "hash", hash, "err", err)
49 renderInternalServerError(w, r)
50 return
51 }
52
53 defaultBranch := getDefaultBranch(gr)
54
55 parentIDs := make([]string, commit.ParentsCount())
56 for i := 0; i < commit.ParentsCount(); i++ {
57 parentID, err := commit.ParentID(i)
58 if err == nil && parentID != nil {
59 parentIDs[i] = parentID.String()
60 }
61 }
62
63 data := CommitData{
64 Repo: repo,
65 DefaultBranch: defaultBranch,
66 Commit: commit,
67 Diff: diff,
68 ParentIDs: parentIDs,
69 ActiveTab: "commits",
70 ServerName: cfg.Name,
71 }
72
73 renderHTML(w, "commit.html", data)
74}