internal/httpd/mrrangediff.go

4a511e42574ead9c92dc73fd320f296cc4dbea07
gitbay/internal/httpd/mrrangediff.go

87 lines · 2371 bytes

3 symbols in this file
 1package httpd
 2
 3import (
 4	"net/http"
 5	"strconv"
 6	"strings"
 7
 8	"gitbay.org/gitbay/internal/protocol"
 9	"gitbay.org/gitbay/internal/store"
10)
11
12type rangeDiffLine struct {
13	Class string
14	Text  string
15}
16
17func parseRangeDiffLines(raw string) []rangeDiffLine {
18	if raw == "" {
19		return nil
20	}
21	rawLines := strings.Split(strings.TrimRight(raw, "\n"), "\n")
22	out := make([]rangeDiffLine, 0, len(rawLines))
23	for _, l := range rawLines {
24		cls := "rd-ctx"
25		switch {
26		case strings.HasPrefix(l, "++"), strings.HasPrefix(l, "+"):
27			cls = "rd-add"
28		case strings.HasPrefix(l, "--"), strings.HasPrefix(l, "-"):
29			cls = "rd-del"
30		case strings.HasPrefix(l, "    +"):
31			cls = "rd-add"
32		case strings.HasPrefix(l, "    -"):
33			cls = "rd-del"
34		case strings.Contains(l, ":  ") && (strings.Contains(l, " ! ") || strings.Contains(l, " = ") || strings.Contains(l, " < ") || strings.Contains(l, " > ")):
35			cls = "rd-hdr"
36		}
37		out = append(out, rangeDiffLine{Class: cls, Text: l})
38	}
39	return out
40}
41
42// mrRangeDiff renders what changed between two revisions of a merge
43// request — the same comparison `mr range-diff` prints on the CLI and
44// the iOS app already show — so a reviewer whose approval a force-push
45// staled can see what moved without leaving the browser (#269).
46func (s *Server) mrRangeDiff(w http.ResponseWriter, r *http.Request) {
47	p, ok := s.repoFor(w, r, "")
48	if !ok {
49		return
50	}
51	p.Tab = "merge requests"
52	n, err := strconv.ParseInt(r.PathValue("n"), 10, 64)
53	if err != nil {
54		s.notFound(w, r)
55		return
56	}
57	m, err := s.st.MRByNumber(p.Repo.ID, n)
58	if err != nil {
59		s.notFound(w, r)
60		return
61	}
62
63	viewer := s.webViewer(r)
64	argv := mrArgs(r, "range-diff")
65	if from := r.URL.Query().Get("from"); from != "" {
66		argv = append(argv, "--from", from)
67	}
68	if to := r.URL.Query().Get("to"); to != "" {
69		argv = append(argv, "--to", to)
70	}
71	// Only an unknown MR 404s (checked above). A bad --from/--to also
72	// resolves to nothing in git, which range-diff reports as
73	// ExitNotFound too, so that result renders on the page instead of
74	// turning a bad query parameter into a 404 (#271).
75	out, msg, code := s.runControlCode(viewer, argv)
76	errMsg := ""
77	if code != protocol.ExitOK {
78		errMsg = msg
79	}
80	s.render(w, "mrrangediff.html", struct {
81		repoPage
82		MR    store.MR
83		Diff  string
84		Lines []rangeDiffLine
85		Error string
86	}{p, m, out, parseRangeDiffLines(out), errMsg})
87}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form