1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package web
4
5import (
6 "context"
7 "net/http"
8 "slices"
9
10 "github.com/a-h/templ"
11
12 "bnema.dev/ermite/internal/domain"
13 "bnema.dev/ermite/internal/ports"
14)
15
16// compareDepth bounds the commits read on each side to find where a branch
17// left its target.
18const compareDepth = maxComparePages * pageSize
19
20// compare shows a branch against the default branch of the repository: the
21// commits it is ahead by and, for the owner, the form that opens a merge
22// request.
23//
24// The API has no "diff between two branches" call outside a merge request, so
25// the page shows the diff only when it is exact without one: the branch is
26// ahead by a single commit that sits on the tip of the target (that commit's
27// own diff is then the whole change). Otherwise it lists the commits, which
28// link to their own diffs, and says the whole diff comes with the merge
29// request.
30//
31//hexcheck:ignore suspicious-business-logic-in-adapter builds the compare page from the use case results; the merge rules are the use case's
32func (s *Server) compare(ctx context.Context, r *http.Request) (templ.Component, int, error) {
33 d, err := s.loadRepo(ctx, r.PathValue("repo"))
34 if err != nil {
35 return nil, 0, err
36 }
37 src, rest, ok := resolveRef(d.branches, r.PathValue("rest"))
38 if !ok || rest != "" || d.head == nil {
39 return nil, 0, errNotFound
40 }
41 tgt := *d.head
42 name := d.name()
43
44 p := ComparePage{
45 Source: visibleText(src.ShortName), Target: visibleText(tgt.ShortName),
46 SourceHref: repoHref(name, "tree", src.ShortName), TargetHref: repoHref(name, "tree", tgt.ShortName),
47 CLI: Clone{ID: "cli-create", Lines: []string{
48 "ermite mr create " + shellWord(name) + " --source " + shellWord(src.ShortName) + " --title <title>",
49 }},
50 }
51
52 same := src.ShortName == tgt.ShortName
53 var ahead []domain.Commit
54 exact, behind := false, false
55 if same {
56 p.Note = "this is the default branch: there is nothing to compare it with."
57 p.AheadText = "0 commits"
58 } else {
59 ahead, exact, behind, err = s.commitsAhead(ctx, d.repo.Name, src.CommitID.String(), tgt.CommitID.String())
60 if err != nil {
61 return nil, 0, err
62 }
63 now := s.cfg.Now()
64 for _, c := range ahead {
65 p.Ahead = append(p.Ahead, commitRow(name, c, now))
66 }
67 p.AheadText = plural(len(ahead), "commit", "commits")
68 if !exact {
69 p.AheadText += " or more"
70 }
71 switch {
72 case !exact:
73 p.Note = "the branch left " + p.Target + " too long ago to tell where: only the newest commits are listed."
74 case len(ahead) == 0:
75 p.Note = "nothing to merge: " + p.Target + " already has every commit of " + p.Source + "."
76 case behind:
77 p.Note = p.Target + " has moved on since the branch started: a merge request can only be merged after " +
78 p.Source + " is rebased on " + p.Target + " and pushed again."
79 }
80 }
81
82 // The exact diff: one commit on top of the tip of the target.
83 if exact && !behind && len(ahead) == 1 {
84 res, err := s.getCommit(ctx, d.repo.Name, ahead[0].ID.String())
85 if err != nil {
86 return nil, 0, err
87 }
88 p.HasDiff = true
89 p.Files = diffFiles(res.Diff.Files)
90 p.NFiles, p.Adds, p.Dels = res.Stats.FilesChanged, res.Stats.Additions, res.Stats.Deletions
91 if n := diffNote(res.Diff, len(p.Files), p.NFiles); n != "" {
92 p.Note = n
93 }
94 lines, invisible := diffLines(p.Files)
95 p.Invisible = invisible
96 self := repoHref(name, "compare", src.ShortName)
97 p.DiffView = newDiffView(r, lines, self+"?view=unified", self+"?view=split")
98 } else if len(ahead) > 1 && exact {
99 if p.Note == "" {
100 p.Note = "the whole diff of the branch is on its merge request; each commit links to its own."
101 }
102 }
103
104 // An open request for the same pair, or the form to open one.
105 var existing *domain.MergeRequest
106 if !same {
107 existing, err = s.openRequestFor(ctx, d.repo.Name, src.ShortName, tgt.ShortName)
108 if err != nil {
109 return nil, 0, err
110 }
111 }
112 switch {
113 case existing != nil:
114 p.Existing = &Link{Label: "#" + itoa(int(existing.Number)) + " " + visibleText(existing.Title.String()), Href: mrHref(name, existing.Number)}
115 case SignedIn(ctx) && !same && len(ahead) > 0:
116 title := ""
117 if len(ahead) == 1 {
118 title = ahead[0].Summary
119 } else {
120 title = ahead[len(ahead)-1].Summary
121 }
122 p.Create = &CreateForm{
123 Action: mrBase(name), Source: src.ShortName, Target: tgt.ShortName,
124 Title: cutRunes(title, 120),
125 }
126 }
127
128 l := s.repoLayout(ctx, d, "merge-requests")
129 l.Title = "compare " + p.Source + " · " + name + " · " + s.instance
130 pinRef(&l, target{rev: src.CommitID.String(), name: src.ShortName})
131 l.Crumbs[0].Current = false
132 l.Crumbs = append(l.Crumbs,
133 Crumb{Label: "merge requests", Href: mrBase(name)},
134 Crumb{Label: "compare", Current: false},
135 Crumb{Label: p.Source, Current: true},
136 )
137 l.HasRail = true
138 return Compare(l, p), http.StatusOK, nil
139}
140
141// commitsAhead lists the commits of the source that the target lacks, newest
142// first. exact is false when the walk gave up before finding where the source
143// meets the target. behind is true when the target has commits the source
144// lacks (the tip of the target is not an ancestor of the source).
145//
146//hexcheck:ignore suspicious-business-logic-in-adapter walks the history pages the use case returns to find where the branch left its target; the page needs it, the forge decides nothing
147func (s *Server) commitsAhead(ctx context.Context, repo domain.RepoName, srcTip, tgtTip string) (ahead []domain.Commit, exact, behind bool, err error) {
148 if srcTip == tgtTip {
149 return nil, true, false, nil
150 }
151 onTarget := map[string]bool{}
152 tokenT := ""
153 for len(onTarget) < compareDepth {
154 res, err := s.commitList(ctx, repo, tgtTip, "", pageSize, tokenT)
155 if err != nil {
156 return nil, false, false, err
157 }
158 for _, c := range res.Commits {
159 onTarget[c.ID.String()] = true
160 }
161 if tokenT = res.NextPageToken; tokenT == "" {
162 break
163 }
164 }
165 token := ""
166 for n := 0; n < compareDepth; {
167 res, err := s.commitList(ctx, repo, srcTip, "", pageSize, token)
168 if err != nil {
169 return nil, false, false, err
170 }
171 for _, c := range res.Commits {
172 n++
173 if onTarget[c.ID.String()] {
174 // The first commit of the source that the target has: where the
175 // branch left it. The target moved on when this is not its tip.
176 return ahead, true, c.ID.String() != tgtTip, nil
177 }
178 ahead = append(ahead, c)
179 }
180 if token = res.NextPageToken; token == "" {
181 // The whole history of the source is ahead: no common history.
182 return ahead, true, true, nil
183 }
184 }
185 return ahead, false, !slices.ContainsFunc(ahead, func(c domain.Commit) bool { return c.ID.String() == tgtTip }), nil
186}
187
188// openRequestFor finds the open merge request with this source and target.
189func (s *Server) openRequestFor(ctx context.Context, repo domain.RepoName, source, target string) (*domain.MergeRequest, error) {
190 token := ""
191 for range maxOpenPages {
192 res, err := s.cfg.MergeRequests.List(ctx, principal(ctx), repo, ports.ListMergeRequestsParams{
193 State: domain.MRStateOpen, PageSize: pageSize, PageToken: token,
194 })
195 if err != nil {
196 return nil, err
197 }
198 for i := range res.MergeRequests {
199 if m := &res.MergeRequests[i]; m.Source == source && m.Target == target {
200 return m, nil
201 }
202 }
203 if token = res.NextPageToken; token == "" {
204 break
205 }
206 }
207 return nil, nil
208}
209
210// cutRunes shortens s to at most n characters.
211func cutRunes(s string, n int) string {
212 r := []rune(s)
213 if len(r) <= n {
214 return s
215 }
216 return string(r[:n])
217}