Skip to content

Commit b2b86ea

Browse files
mrsdizzielafrikslunny
authored
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 <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 6603045 commit b2b86ea

File tree

7 files changed

+49
-9
lines changed

7 files changed

+49
-9
lines changed

routers/repo/commit.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,25 @@ func Diff(ctx *context.Context) {
212212
userName := ctx.Repo.Owner.Name
213213
repoName := ctx.Repo.Repository.Name
214214
commitID := ctx.Params(":sha")
215+
var (
216+
gitRepo *git.Repository
217+
err error
218+
repoPath string
219+
)
220+
221+
if ctx.Data["PageIsUncyclo"] != nil {
222+
gitRepo, err = git.OpenRepository(ctx.Repo.Repository.UncycloPath())
223+
if err != nil {
224+
ctx.ServerError("Repo.GitRepo.GetCommit", err)
225+
return
226+
}
227+
repoPath = ctx.Repo.Repository.UncycloPath()
228+
} else {
229+
gitRepo = ctx.Repo.GitRepo
230+
repoPath = models.RepoPath(userName, repoName)
231+
}
215232

216-
commit, err := ctx.Repo.GitRepo.GetCommit(commitID)
233+
commit, err := gitRepo.GetCommit(commitID)
217234
if err != nil {
218235
if git.IsErrNotExist(err) {
219236
ctx.NotFound("Repo.GitRepo.GetCommit", err)
@@ -233,7 +250,7 @@ func Diff(ctx *context.Context) {
233250

234251
ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)
235252

236-
diff, err := gitdiff.GetDiffCommit(models.RepoPath(userName, repoName),
253+
diff, err := gitdiff.GetDiffCommit(repoPath,
237254
commitID, setting.Git.MaxGitDiffLines,
238255
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
239256
if err != nil {
@@ -258,7 +275,7 @@ func Diff(ctx *context.Context) {
258275

259276
var parentCommit *git.Commit
260277
if commit.ParentCount() > 0 {
261-
parentCommit, err = ctx.Repo.GitRepo.GetCommit(parents[0])
278+
parentCommit, err = gitRepo.GetCommit(parents[0])
262279
if err != nil {
263280
ctx.NotFound("GetParentCommit", err)
264281
return
@@ -298,8 +315,14 @@ func Diff(ctx *context.Context) {
298315

299316
// RawDiff dumps diff results of repository in given commit ID to io.Writer
300317
func RawDiff(ctx *context.Context) {
318+
var repoPath string
319+
if ctx.Data["PageIsUncyclo"] != nil {
320+
repoPath = ctx.Repo.Repository.UncycloPath()
321+
} else {
322+
repoPath = models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
323+
}
301324
if err := git.GetRawDiff(
302-
models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name),
325+
repoPath,
303326
ctx.Params(":sha"),
304327
git.RawDiffType(ctx.Params(":ext")),
305328
ctx.Resp,

routers/repo/wiki.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
245245
ctx.Data["Title"] = pageName
246246
ctx.Data["title"] = pageName
247247
ctx.Data["RequireHighlightJS"] = true
248+
ctx.Data["Username"] = ctx.Repo.Owner.Name
249+
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
248250

249251
//lookup filename in wiki - get filecontent, gitTree entry , real filename
250252
data, entry, pageFilename, noEntry := wikiContentsByName(ctx, commit, pageName)

routers/routes/routes.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,8 @@ func RegisterRoutes(m *macaron.Macaron) {
857857
m.Get("/?:page", repo.Uncyclo)
858858
m.Get("/_pages", repo.UncycloPages)
859859
m.Get("/:page/_revision", repo.UncycloRevision)
860+
m.Get("/commit/:sha([a-f0-9]{7,40})$", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.Diff)
861+
m.Get("/commit/:sha([a-f0-9]{7,40})\\.:ext(patch|diff)", repo.RawDiff)
860862

861863
m.Group("", func() {
862864
m.Combo("/_new").Get(repo.NewUncyclo).
@@ -865,7 +867,9 @@ func RegisterRoutes(m *macaron.Macaron) {
865867
Post(bindIgnErr(auth.NewUncycloForm{}), repo.EditUncycloPost)
866868
m.Post("/:page/delete", repo.DeleteUncycloPagePost)
867869
}, context.RepoMustNotBeArchived(), reqSignIn, reqRepoUncycloWriter)
868-
}, repo.MustEnableUncyclo, context.RepoRef())
870+
}, repo.MustEnableUncyclo, context.RepoRef(), func(ctx *context.Context) {
871+
ctx.Data["PageIsUncyclo"] = true
872+
})
869873

870874
m.Group("/wiki", func() {
871875
m.Get("/raw/*", repo.UncycloRaw)

templates/repo/commit_page.tmpl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
{{end}}
1919
{{end}}
2020
<div class="ui top attached info clearing segment {{$class}}">
21+
{{if not $.PageIsUncyclo}}
2122
<a class="ui floated right blue tiny button" href="{{EscapePound .SourcePath}}">
2223
{{.i18n.Tr "repo.diff.browse_source"}}
2324
</a>
25+
{{end}}
2426
<h3><span class="message-wrapper"><span class="commit-summary" title="{{.Commit.Summary}}">{{RenderCommitMessage .Commit.Message $.RepoLink $.Repository.ComposeMetas}}</span></span>{{template "repo/commit_status" .CommitStatus}}</h3>
2527
{{if IsMultilineCommitMessage .Commit.Message}}
2628
<pre class="commit-body">{{RenderCommitBody .Commit.Message $.RepoLink $.Repository.ComposeMetas}}</pre>
@@ -61,7 +63,11 @@
6163
</div>
6264
<div class="item">
6365
{{range .Parents}}
64-
<a class="ui blue sha label" href="{{$.RepoLink}}/commit/{{.}}">{{ShortSha .}}</a>
66+
{{if $.PageIsUncyclo}}
67+
<a class="ui blue sha label" href="{{$.RepoLink}}/wiki/commit/{{.}}">{{ShortSha .}}</a>
68+
{{else}}
69+
<a class="ui blue sha label" href="{{$.RepoLink}}/commit/{{.}}">{{ShortSha .}}</a>
70+
{{end}}
6571
{{end}}
6672
</div>
6773
{{end}}

templates/repo/commits_list.tmpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
{{$class = (printf "%s%s" $class " isWarning")}}
4040
{{end}}
4141
{{end}}
42-
{{if $.Reponame}}
42+
{{if $.PageIsUncyclo}}
43+
<a href="{{AppSubUrl}}/{{$.Username}}/{{$.Reponame}}/wiki/commit/{{.ID}}" rel="nofollow" class="{{$class}}">
44+
{{else if $.Reponame}}
4345
<a href="{{AppSubUrl}}/{{$.Username}}/{{$.Reponame}}/commit/{{.ID}}" rel="nofollow" class="{{$class}}">
4446
{{else}}
4547
<span class="{{$class}}">

templates/repo/diff/box.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
</div>
7070
<span class="file">{{$file.Name}}</span>
7171
<div>{{$.i18n.Tr "repo.diff.file_suppressed"}}</div>
72-
{{if not $file.IsSubmodule}}
72+
{{if and (not $file.IsSubmodule) (not $.PageIsUncyclo)}}
7373
{{if $file.IsDeleted}}
7474
<a class="ui basic grey tiny button" rel="nofollow" href="{{EscapePound $.BeforeSourcePath}}/{{EscapePound .Name}}">{{$.i18n.Tr "repo.diff.view_file"}}</a>
7575
{{else}}
@@ -103,7 +103,7 @@
103103
{{end}}
104104
</div>
105105
<span class="file">{{if $file.IsRenamed}}{{$file.OldName}} &rarr; {{end}}{{$file.Name}}{{if .IsLFSFile}} ({{$.i18n.Tr "repo.stored_lfs"}}){{end}}</span>
106-
{{if not $file.IsSubmodule}}
106+
{{if and (not $file.IsSubmodule) (not $.PageIsUncyclo)}}
107107
{{if $file.IsDeleted}}
108108
<a class="ui basic grey tiny button" rel="nofollow" href="{{EscapePound $.BeforeSourcePath}}/{{EscapePound .Name}}">{{$.i18n.Tr "repo.diff.view_file"}}</a>
109109
{{else}}

templates/repo/diff/options_dropdown.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
{{if .Issue.Index}}
77
<a class="item" href="{{$.RepoLink}}/pulls/{{.Issue.Index}}.patch" download="{{.Issue.Index}}.patch">{{.i18n.Tr "repo.diff.download_patch"}}</a>
88
<a class="item" href="{{$.RepoLink}}/pulls/{{.Issue.Index}}.diff" download="{{.Issue.Index}}.diff">{{.i18n.Tr "repo.diff.download_diff"}}</a>
9+
{{else if $.PageIsUncyclo}}
10+
<a class="item" href="{{$.RepoLink}}/wiki/commit/{{.Commit.ID.String}}.patch" download="{{ShortSha .Commit.ID.String}}.patch">{{.i18n.Tr "repo.diff.download_patch"}}</a>
11+
<a class="item" href="{{$.RepoLink}}/wiki/commit/{{.Commit.ID.String}}.diff" download="{{ShortSha .Commit.ID.String}}.diff">{{.i18n.Tr "repo.diff.download_diff"}}</a>
912
{{else if .Commit.ID.String}}
1013
<a class="item" href="{{$.RepoLink}}/commit/{{.Commit.ID.String}}.patch" download="{{ShortSha .Commit.ID.String}}.patch">{{.i18n.Tr "repo.diff.download_patch"}}</a>
1114
<a class="item" href="{{$.RepoLink}}/commit/{{.Commit.ID.String}}.diff" download="{{ShortSha .Commit.ID.String}}.diff">{{.i18n.Tr "repo.diff.download_diff"}}</a>

0 commit comments

Comments
 (0)