Skip to content

Commit 487f2ee

Browse files
authored
Whitespace in commits (#14650)
* Add whitespace to commit view * Add whitespace to /compare/a...b * Move repeated whitespaceFlags to gitdiff * Add whitespace for wiki pages
1 parent a3cc842 commit 487f2ee

File tree

6 files changed

+41
-32
lines changed

6 files changed

+41
-32
lines changed

routers/repo/commit.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,12 @@ func Diff(ctx *context.Context) {
304304
ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)
305305
ctx.Data["CommitStatuses"] = statuses
306306

307-
diff, err := gitdiff.GetDiffCommit(repoPath,
307+
diff, err := gitdiff.GetDiffCommitWithWhitespaceBehavior(repoPath,
308308
commitID, setting.Git.MaxGitDiffLines,
309-
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
309+
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
310+
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
310311
if err != nil {
311-
ctx.NotFound("GetDiffCommit", err)
312+
ctx.NotFound("GetDiffCommitWithWhitespaceBehavior", err)
312313
return
313314
}
314315

routers/repo/compare.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,8 @@ func PrepareCompareDiff(
411411
headRepo *models.Repository,
412412
headGitRepo *git.Repository,
413413
compareInfo *git.CompareInfo,
414-
baseBranch, headBranch string) bool {
414+
baseBranch, headBranch string,
415+
whitespaceBehavior string) bool {
415416

416417
var (
417418
repo = ctx.Repo.Repository
@@ -442,11 +443,11 @@ func PrepareCompareDiff(
442443
return true
443444
}
444445

445-
diff, err := gitdiff.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
446+
diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(models.RepoPath(headUser.Name, headRepo.Name),
446447
compareInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
447-
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
448+
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, whitespaceBehavior)
448449
if err != nil {
449-
ctx.ServerError("GetDiffRange", err)
450+
ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err)
450451
return false
451452
}
452453
ctx.Data["Diff"] = diff
@@ -530,12 +531,14 @@ func getBranchesForRepo(user *models.User, repo *models.Repository) (bool, []str
530531
// CompareDiff show different from one commit to another commit
531532
func CompareDiff(ctx *context.Context) {
532533
headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
534+
533535
if ctx.Written() {
534536
return
535537
}
536538
defer headGitRepo.Close()
537539

538-
nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch)
540+
nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch,
541+
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
539542
if ctx.Written() {
540543
return
541544
}

routers/repo/pull.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,6 @@ func ViewPullFiles(ctx *context.Context) {
581581
}
582582
pull := issue.PullRequest
583583

584-
whitespaceFlags := map[string]string{
585-
"ignore-all": "-w",
586-
"ignore-change": "-b",
587-
"ignore-eol": "--ignore-space-at-eol",
588-
"": ""}
589-
590584
var (
591585
diffRepoPath string
592586
startCommitID string
@@ -629,7 +623,7 @@ func ViewPullFiles(ctx *context.Context) {
629623
diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
630624
startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
631625
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
632-
whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)])
626+
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
633627
if err != nil {
634628
ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err)
635629
return
@@ -993,7 +987,8 @@ func CompareAndPullRequestPost(ctx *context.Context) {
993987

994988
// This stage is already stop creating new pull request, so it does not matter if it has
995989
// something to compare or not.
996-
PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
990+
PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch,
991+
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
997992
if ctx.Written() {
998993
return
999994
}
@@ -1003,7 +998,8 @@ func CompareAndPullRequestPost(ctx *context.Context) {
1003998
}
1004999

10051000
if util.IsEmptyString(form.Title) {
1006-
PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
1001+
PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch,
1002+
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
10071003
if ctx.Written() {
10081004
return
10091005
}

routers/routes/web.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,8 @@ func RegisterRoutes(m *web.Route) {
695695
m.Get("/{id}", repo.MilestoneIssuesAndPulls)
696696
}, reqRepoIssuesOrPullsReader, context.RepoRef())
697697
m.Combo("/compare/*", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists).
698-
Get(ignSignIn, repo.SetDiffViewStyle, repo.CompareDiff).
699-
Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, bindIgnErr(auth.CreateIssueForm{}), repo.CompareAndPullRequestPost)
698+
Get(ignSignIn, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff).
699+
Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, bindIgnErr(auth.CreateIssueForm{}), repo.SetWhitespaceBehavior, repo.CompareAndPullRequestPost)
700700
}, context.RepoAssignment(), context.UnitTypes())
701701

702702
// Grouping for those endpoints that do require authentication
@@ -885,7 +885,7 @@ func RegisterRoutes(m *web.Route) {
885885
m.Get("/{page}", repo.Wiki)
886886
m.Get("/_pages", repo.WikiPages)
887887
m.Get("/{page}/_revision", repo.WikiRevision)
888-
m.Get("/commit/{sha:[a-f0-9]{7,40}}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.Diff)
888+
m.Get("/commit/{sha:[a-f0-9]{7,40}}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.Diff)
889889
m.Get("/commit/{sha:[a-f0-9]{7,40}}.{:patch|diff}", repo.RawDiff)
890890

891891
m.Group("", func() {
@@ -977,7 +977,7 @@ func RegisterRoutes(m *web.Route) {
977977

978978
m.Group("", func() {
979979
m.Get("/graph", repo.Graph)
980-
m.Get("/commit/{sha:([a-f0-9]{7,40})$}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.Diff)
980+
m.Get("/commit/{sha:([a-f0-9]{7,40})$}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.Diff)
981981
}, repo.MustBeNotEmpty, context.RepoRef(), reqRepoCodeReader)
982982

983983
m.Group("/src", func() {

services/gitdiff/gitdiff.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,13 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID
967967

968968
// GetDiffCommit builds a Diff representing the given commitID.
969969
func GetDiffCommit(repoPath, commitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) {
970-
return GetDiffRange(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles)
970+
return GetDiffRangeWithWhitespaceBehavior(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles, "")
971+
}
972+
973+
// GetDiffCommitWithWhitespaceBehavior builds a Diff representing the given commitID.
974+
// The whitespaceBehavior is either an empty string or a git flag
975+
func GetDiffCommitWithWhitespaceBehavior(repoPath, commitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {
976+
return GetDiffRangeWithWhitespaceBehavior(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles, whitespaceBehavior)
971977
}
972978

973979
// CommentAsDiff returns c.Patch as *Diff
@@ -995,3 +1001,14 @@ func CommentMustAsDiff(c *models.Comment) *Diff {
9951001
}
9961002
return diff
9971003
}
1004+
1005+
// GetWhitespaceFlag returns git diff flag for treating whitespaces
1006+
func GetWhitespaceFlag(whiteSpaceBehavior string) string {
1007+
whitespaceFlags := map[string]string{
1008+
"ignore-all": "-w",
1009+
"ignore-change": "-b",
1010+
"ignore-eol": "--ignore-space-at-eol",
1011+
"": ""}
1012+
1013+
return whitespaceFlags[whiteSpaceBehavior]
1014+
}

templates/repo/diff/box.tmpl

+2-10
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
<div class="diff-detail-box diff-box sticky">
33
<div>
44
<div class="ui right">
5-
{{if .PageIsPullFiles}}
6-
{{template "repo/diff/whitespace_dropdown" .}}
7-
{{else}}
8-
<a class="ui tiny basic toggle button" href="?style={{if .IsSplitStyle}}unified{{else}}split{{end}}">{{ if .IsSplitStyle }}{{.i18n.Tr "repo.diff.show_unified_view"}}{{else}}{{.i18n.Tr "repo.diff.show_split_view"}}{{end}}</a>
9-
{{end}}
5+
{{template "repo/diff/whitespace_dropdown" .}}
106
{{template "repo/diff/options_dropdown" .}}
117
{{if and .PageIsPullFiles $.SignedUserID (not .IsArchived)}}
128
{{template "repo/diff/new_review" .}}
@@ -22,11 +18,7 @@
2218
{{svg "octicon-diff" 16 "mr-2"}}{{.i18n.Tr "repo.diff.stats_desc" .Diff.NumFiles .Diff.TotalAddition .Diff.TotalDeletion | Str2html}}
2319
</div>
2420
<div class="diff-detail-actions df ac">
25-
{{if .PageIsPullFiles}}
26-
{{template "repo/diff/whitespace_dropdown" .}}
27-
{{else}}
28-
<a class="ui tiny basic toggle button" href="?style={{if .IsSplitStyle}}unified{{else}}split{{end}}">{{ if .IsSplitStyle }}{{.i18n.Tr "repo.diff.show_unified_view"}}{{else}}{{.i18n.Tr "repo.diff.show_split_view"}}{{end}}</a>
29-
{{end}}
21+
{{template "repo/diff/whitespace_dropdown" .}}
3022
{{template "repo/diff/options_dropdown" .}}
3123
{{if and .PageIsPullFiles $.SignedUserID (not .IsArchived)}}
3224
{{template "repo/diff/new_review" .}}

0 commit comments

Comments
 (0)