vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
| 1 | package web |
| 2 | |
| 3 | // The Commits tab lists the commits a change request carries, oldest first, so |
| 4 | // a reviewer can read the branch the way it was written and open any commit on |
| 5 | // its own page. It is a list, not a second diff: the commit page already owns |
| 6 | // the files and the patch. |
| 7 | |
| 8 | import ( |
| 9 | "net/http" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | |
| 13 | "rodent.software/vole/core/git" |
| 14 | "rodent.software/vole/metadata" |
| 15 | ) |
| 16 | |
| 17 | // requestCommitPageSize is how many commits one page of the change shows. A |
| 18 | // commit row is one line, so a page can hold more than the issue and change |
| 19 | // lists without becoming a scroll. |
| 20 | const requestCommitPageSize = 50 |
| 21 | |
| 22 | // requestCommitsView is the Commits tab: the page of commits and the pager that |
| 23 | // says where in the branch they sit. |
| 24 | type requestCommitsView struct { |
| 25 | requestSidebarView |
| 26 | Tab string |
| 27 | Commits []git.Commit |
| 28 | Pager pager |
| 29 | } |
| 30 | |
| 31 | // RequestCommits serves the Commits tab for one change. Pagination swaps only |
| 32 | // the list, so a reviewer keeps the request header, tabs, and sidebar in place |
| 33 | // while stepping through the branch. |
| 34 | func (w *Web) RequestCommits(rw http.ResponseWriter, r *http.Request) { |
| 35 | m, ok := w.open(rw, r) |
| 36 | if !ok { |
| 37 | return |
| 38 | } |
| 39 | req, err := w.requestAt(memoOf(r), m, r.PathValue("id")) |
| 40 | if err != nil { |
| 41 | w.notFound(rw, r) |
| 42 | return |
| 43 | } |
| 44 | |
| 45 | change := w.reviewDiff(m, req) |
| 46 | commits, err := requestCommits(m.Git(), req, change) |
| 47 | if err != nil { |
| 48 | w.Logger("request commits %s: %v", req.ID, err) |
| 49 | } |
| 50 | page, _ := strconv.Atoi(r.URL.Query().Get("page")) |
| 51 | pg := newPager(len(commits), requestCommitPageSize, page, func(n int) string { |
| 52 | return requestCommitsURL(m.Name, req.ID, n) |
| 53 | }).forSwap("Commit pages", "#request-commits") |
| 54 | from, to := pg.rows() |
| 55 | commits = commits[from:to] |
| 56 | |
| 57 | view := requestCommitsView{ |
| 58 | requestSidebarView: w.requestSidebar(r, m, req, "commits", "").withLanding(w.landingFrom(m, req, change)), |
| 59 | Tab: "commits", |
| 60 | Commits: commits, |
| 61 | Pager: pg, |
| 62 | } |
| 63 | if htmxRequest(r) { |
| 64 | w.render(rw, "request_commit_list", view, 0) |
| 65 | return |
| 66 | } |
| 67 | w.render(rw, "request_commits", view, 0) |
| 68 | } |
| 69 | |
| 70 | // requestCommits lists the commits a request carries, oldest first. While the |
| 71 | // request is open that is the base..head range; once it has merged, the range |
| 72 | // starts at the recorded pre-landing base and stops at the head, so the landing |
| 73 | // merge commit is not shown as part of what was reviewed. |
| 74 | func requestCommits(g *git.Repo, req *metadata.Request, change reviewChange) ([]git.Commit, error) { |
| 75 | from, to, ok := requestCommitRange(req, change) |
| 76 | if !ok { |
| 77 | return nil, nil |
| 78 | } |
| 79 | return g.LogRange(from, to) |
| 80 | } |
| 81 | |
| 82 | // requestCommitRange names the ends of the range the Commits tab lists. An open |
| 83 | // request's inventory is the three-dot range string; a landed one keeps the base |
| 84 | // tip that was the "before" side of the landing. A landed request written before |
| 85 | // merged_base existed has no range to show: the whole history is not its change. |
| 86 | func requestCommitRange(req *metadata.Request, change reviewChange) (from, to string, ok bool) { |
| 87 | if req.Head == "" { |
| 88 | return "", "", false |
| 89 | } |
| 90 | if base, head, cut := strings.Cut(change.From, "..."); cut { |
| 91 | return base, head, true |
| 92 | } |
| 93 | if change.From == "" || change.To == "" { |
| 94 | return "", "", false |
| 95 | } |
| 96 | return change.From, req.Head, true |
| 97 | } |
| 98 | |
| 99 | func requestCommitsURL(name, id string, page int) string { |
| 100 | u := "/" + name + "/changes/" + id + "/commits" |
| 101 | if page > 1 { |
| 102 | u += "?page=" + strconv.Itoa(page) |
| 103 | } |
| 104 | return u |
| 105 | } |
| 106 |