@@ -163,11 +163,16 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) error {
case len(elems) == 2 && elems[1] == "commits":
return h.ChangeCommitsHandler(w, req, changeID)
// "/{changeID}/files".
case len(elems) == 2 && elems[1] == "files":
return h.ChangeFilesHandler(w, req, changeID)
return h.ChangeFilesHandler(w, req, changeID, "")
// "/{changeID}/files/{commitID}".
case len(elems) == 3 && elems[1] == "files":
commitID := elems[2]
return h.ChangeFilesHandler(w, req, changeID, commitID)
default:
return httperror.HTTP{Code: http.StatusNotFound, Err: errors.New("no route")}
}
}
@@ -336,11 +341,11 @@ func (h *handler) ChangeCommitsHandler(w http.ResponseWriter, req *http.Request,
}
state.Change, err = h.is.Get(req.Context(), state.RepoSpec, state.IssueID)
if err != nil {
return err
}
cs, err := h.is.ListCommits(req.Context(), state.RepoSpec, state.IssueID)
cs, err := h.is.ListCommits(req.Context(), state.RepoSpec, state.IssueID, nil)
if err != nil {
return err
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
err = h.static.ExecuteTemplate(w, "change-commits.html.tmpl", &state)
@@ -357,11 +362,11 @@ func (h *handler) ChangeCommitsHandler(w http.ResponseWriter, req *http.Request,
}
_, err = io.WriteString(w, `</body></html>`)
return err
}
func (h *handler) ChangeFilesHandler(w http.ResponseWriter, req *http.Request, changeID uint64) error {
func (h *handler) ChangeFilesHandler(w http.ResponseWriter, req *http.Request, changeID uint64, commitID string) error {
if req.Method != http.MethodGet {
return httperror.Method{Allowed: []string{http.MethodGet}}
}
state, err := h.state(req, changeID)
if err != nil {
@@ -369,11 +374,32 @@ func (h *handler) ChangeFilesHandler(w http.ResponseWriter, req *http.Request, c
}
state.Change, err = h.is.Get(req.Context(), state.RepoSpec, state.IssueID)
if err != nil {
return err
}
rawDiff, err := h.is.GetDiff(req.Context(), state.RepoSpec, state.IssueID)
var (
opt *changes.ListCommitsOptions
commit commitMessage
)
if commitID != "" {
opt = &changes.ListCommitsOptions{
Commit: commitID,
}
cs, err := h.is.ListCommits(req.Context(), state.RepoSpec, state.IssueID, opt)
if err != nil {
return err
}
subject, body := splitCommitMessage(cs[0].Message)
commit = commitMessage{
CommitHash: cs[0].SHA,
Subject: subject,
Body: body,
Author: cs[0].Author,
AuthorTime: cs[0].AuthorTime,
}
}
rawDiff, err := h.is.GetDiff(req.Context(), state.RepoSpec, state.IssueID, opt)
if err != nil {
return err
}
fileDiffs, err := diff.ParseMultiFileDiff(rawDiff)
if err != nil {
@@ -382,10 +408,16 @@ func (h *handler) ChangeFilesHandler(w http.ResponseWriter, req *http.Request, c
w.Header().Set("Content-Type", "text/html; charset=utf-8")
err = h.static.ExecuteTemplate(w, "change-files.html.tmpl", &state)
if err != nil {
return err
}
if commitID != "" {
err = h.static.ExecuteTemplate(w, "CommitMessage", commit)
if err != nil {
return err
}
}
for _, f := range fileDiffs {
err = h.static.ExecuteTemplate(w, "FileDiff", fileDiff{FileDiff: f})
if err != nil {
return err
}