From b2b86ea8701cab901cd9e6a02c918aa5c61748d6 Mon Sep 17 00:00:00 2001 From: mrsdizzie Date: Sat, 16 May 2020 12:38:40 -0400 Subject: [PATCH] Support view individual commit for wiki pages (#11415) Currently you can see a list of commit history for wiki pages but aren't able to view the commit diff itself. This adds the feature to view an individual commit to a wiki repo. Closes #8999 Co-authored-by: Lauris BH Co-authored-by: Lunny Xiao --- routers/repo/commit.go | 31 ++++++++++++++++++++--- routers/repo/wiki.go | 2 ++ routers/routes/routes.go | 6 ++++- templates/repo/commit_page.tmpl | 8 +++++- templates/repo/commits_list.tmpl | 4 ++- templates/repo/diff/box.tmpl | 4 +-- templates/repo/diff/options_dropdown.tmpl | 3 +++ 7 files changed, 49 insertions(+), 9 deletions(-) diff --git a/routers/repo/commit.go b/routers/repo/commit.go index 77439f887..a7a8d30d0 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -212,8 +212,25 @@ func Diff(ctx *context.Context) { userName := ctx.Repo.Owner.Name repoName := ctx.Repo.Repository.Name commitID := ctx.Params(":sha") + var ( + gitRepo *git.Repository + err error + repoPath string + ) + + if ctx.Data["PageIsWiki"] != nil { + gitRepo, err = git.OpenRepository(ctx.Repo.Repository.WikiPath()) + if err != nil { + ctx.ServerError("Repo.GitRepo.GetCommit", err) + return + } + repoPath = ctx.Repo.Repository.WikiPath() + } else { + gitRepo = ctx.Repo.GitRepo + repoPath = models.RepoPath(userName, repoName) + } - commit, err := ctx.Repo.GitRepo.GetCommit(commitID) + commit, err := gitRepo.GetCommit(commitID) if err != nil { if git.IsErrNotExist(err) { ctx.NotFound("Repo.GitRepo.GetCommit", err) @@ -233,7 +250,7 @@ func Diff(ctx *context.Context) { ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses) - diff, err := gitdiff.GetDiffCommit(models.RepoPath(userName, repoName), + diff, err := gitdiff.GetDiffCommit(repoPath, commitID, setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles) if err != nil { @@ -258,7 +275,7 @@ func Diff(ctx *context.Context) { var parentCommit *git.Commit if commit.ParentCount() > 0 { - parentCommit, err = ctx.Repo.GitRepo.GetCommit(parents[0]) + parentCommit, err = gitRepo.GetCommit(parents[0]) if err != nil { ctx.NotFound("GetParentCommit", err) return @@ -298,8 +315,14 @@ func Diff(ctx *context.Context) { // RawDiff dumps diff results of repository in given commit ID to io.Writer func RawDiff(ctx *context.Context) { + var repoPath string + if ctx.Data["PageIsWiki"] != nil { + repoPath = ctx.Repo.Repository.WikiPath() + } else { + repoPath = models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) + } if err := git.GetRawDiff( - models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name), + repoPath, ctx.Params(":sha"), git.RawDiffType(ctx.Params(":ext")), ctx.Resp, diff --git a/routers/repo/wiki.go b/routers/repo/wiki.go index a01498fb0..5da01f21a 100644 --- a/routers/repo/wiki.go +++ b/routers/repo/wiki.go @@ -245,6 +245,8 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) ctx.Data["Title"] = pageName ctx.Data["title"] = pageName ctx.Data["RequireHighlightJS"] = true + ctx.Data["Username"] = ctx.Repo.Owner.Name + ctx.Data["Reponame"] = ctx.Repo.Repository.Name //lookup filename in wiki - get filecontent, gitTree entry , real filename data, entry, pageFilename, noEntry := wikiContentsByName(ctx, commit, pageName) diff --git a/routers/routes/routes.go b/routers/routes/routes.go index a51c1e17a..7f409eb57 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -857,6 +857,8 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/?:page", repo.Wiki) m.Get("/_pages", repo.WikiPages) m.Get("/:page/_revision", repo.WikiRevision) + m.Get("/commit/:sha([a-f0-9]{7,40})$", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.Diff) + m.Get("/commit/:sha([a-f0-9]{7,40})\\.:ext(patch|diff)", repo.RawDiff) m.Group("", func() { m.Combo("/_new").Get(repo.NewWiki). @@ -865,7 +867,9 @@ func RegisterRoutes(m *macaron.Macaron) { Post(bindIgnErr(auth.NewWikiForm{}), repo.EditWikiPost) m.Post("/:page/delete", repo.DeleteWikiPagePost) }, context.RepoMustNotBeArchived(), reqSignIn, reqRepoWikiWriter) - }, repo.MustEnableWiki, context.RepoRef()) + }, repo.MustEnableWiki, context.RepoRef(), func(ctx *context.Context) { + ctx.Data["PageIsWiki"] = true + }) m.Group("/wiki", func() { m.Get("/raw/*", repo.WikiRaw) diff --git a/templates/repo/commit_page.tmpl b/templates/repo/commit_page.tmpl index 372cc4fb9..f19292fd9 100644 --- a/templates/repo/commit_page.tmpl +++ b/templates/repo/commit_page.tmpl @@ -18,9 +18,11 @@ {{end}} {{end}}
+ {{if not $.PageIsWiki}} {{.i18n.Tr "repo.diff.browse_source"}} + {{end}}

{{RenderCommitMessage .Commit.Message $.RepoLink $.Repository.ComposeMetas}}{{template "repo/commit_status" .CommitStatus}}

{{if IsMultilineCommitMessage .Commit.Message}}
{{RenderCommitBody .Commit.Message $.RepoLink $.Repository.ComposeMetas}}
@@ -61,7 +63,11 @@
{{range .Parents}} - {{ShortSha .}} + {{if $.PageIsWiki}} + {{ShortSha .}} + {{else}} + {{ShortSha .}} + {{end}} {{end}}
{{end}} diff --git a/templates/repo/commits_list.tmpl b/templates/repo/commits_list.tmpl index 92da6c1b2..ad2369c04 100644 --- a/templates/repo/commits_list.tmpl +++ b/templates/repo/commits_list.tmpl @@ -39,7 +39,9 @@ {{$class = (printf "%s%s" $class " isWarning")}} {{end}} {{end}} - {{if $.Reponame}} + {{if $.PageIsWiki}} + + {{else if $.Reponame}} {{else}} diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl index a78935052..b6d1fdf3a 100644 --- a/templates/repo/diff/box.tmpl +++ b/templates/repo/diff/box.tmpl @@ -69,7 +69,7 @@ {{$file.Name}}
{{$.i18n.Tr "repo.diff.file_suppressed"}}
- {{if not $file.IsSubmodule}} + {{if and (not $file.IsSubmodule) (not $.PageIsWiki)}} {{if $file.IsDeleted}}
{{$.i18n.Tr "repo.diff.view_file"}} {{else}} @@ -103,7 +103,7 @@ {{end}} {{if $file.IsRenamed}}{{$file.OldName}} → {{end}}{{$file.Name}}{{if .IsLFSFile}} ({{$.i18n.Tr "repo.stored_lfs"}}){{end}} - {{if not $file.IsSubmodule}} + {{if and (not $file.IsSubmodule) (not $.PageIsWiki)}} {{if $file.IsDeleted}} {{$.i18n.Tr "repo.diff.view_file"}} {{else}} diff --git a/templates/repo/diff/options_dropdown.tmpl b/templates/repo/diff/options_dropdown.tmpl index 4bc6a3b99..8dcf39824 100644 --- a/templates/repo/diff/options_dropdown.tmpl +++ b/templates/repo/diff/options_dropdown.tmpl @@ -6,6 +6,9 @@ {{if .Issue.Index}} {{.i18n.Tr "repo.diff.download_patch"}} {{.i18n.Tr "repo.diff.download_diff"}} + {{else if $.PageIsWiki}} + {{.i18n.Tr "repo.diff.download_patch"}} + {{.i18n.Tr "repo.diff.download_diff"}} {{else if .Commit.ID.String}} {{.i18n.Tr "repo.diff.download_patch"}} {{.i18n.Tr "repo.diff.download_diff"}}