From 487f2ee41cb06f0173b8bf12bd6a78408c75fabf Mon Sep 17 00:00:00 2001 From: vnkmpf Date: Sat, 13 Feb 2021 05:35:43 +0100 Subject: 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 --- routers/repo/commit.go | 7 ++++--- routers/repo/compare.go | 13 ++++++++----- routers/repo/pull.go | 14 +++++--------- routers/routes/web.go | 8 ++++---- services/gitdiff/gitdiff.go | 19 ++++++++++++++++++- templates/repo/diff/box.tmpl | 12 ++---------- 6 files changed, 41 insertions(+), 32 deletions(-) diff --git a/routers/repo/commit.go b/routers/repo/commit.go index c411d247e2..c3ee6b5acc 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -304,11 +304,12 @@ func Diff(ctx *context.Context) { ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses) ctx.Data["CommitStatuses"] = statuses - diff, err := gitdiff.GetDiffCommit(repoPath, + diff, err := gitdiff.GetDiffCommitWithWhitespaceBehavior(repoPath, commitID, setting.Git.MaxGitDiffLines, - setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles) + setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, + gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string))) if err != nil { - ctx.NotFound("GetDiffCommit", err) + ctx.NotFound("GetDiffCommitWithWhitespaceBehavior", err) return } diff --git a/routers/repo/compare.go b/routers/repo/compare.go index 218f712469..4869e5919a 100644 --- a/routers/repo/compare.go +++ b/routers/repo/compare.go @@ -411,7 +411,8 @@ func PrepareCompareDiff( headRepo *models.Repository, headGitRepo *git.Repository, compareInfo *git.CompareInfo, - baseBranch, headBranch string) bool { + baseBranch, headBranch string, + whitespaceBehavior string) bool { var ( repo = ctx.Repo.Repository @@ -442,11 +443,11 @@ func PrepareCompareDiff( return true } - diff, err := gitdiff.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name), + diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(models.RepoPath(headUser.Name, headRepo.Name), compareInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines, - setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles) + setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, whitespaceBehavior) if err != nil { - ctx.ServerError("GetDiffRange", err) + ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err) return false } ctx.Data["Diff"] = diff @@ -530,12 +531,14 @@ func getBranchesForRepo(user *models.User, repo *models.Repository) (bool, []str // CompareDiff show different from one commit to another commit func CompareDiff(ctx *context.Context) { headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch := ParseCompareInfo(ctx) + if ctx.Written() { return } defer headGitRepo.Close() - nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch) + nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch, + gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string))) if ctx.Written() { return } diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 6986002102..d3dd9e4fb5 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -581,12 +581,6 @@ func ViewPullFiles(ctx *context.Context) { } pull := issue.PullRequest - whitespaceFlags := map[string]string{ - "ignore-all": "-w", - "ignore-change": "-b", - "ignore-eol": "--ignore-space-at-eol", - "": ""} - var ( diffRepoPath string startCommitID string @@ -629,7 +623,7 @@ func ViewPullFiles(ctx *context.Context) { diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(diffRepoPath, startCommitID, endCommitID, setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, - whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)]) + gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string))) if err != nil { ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err) return @@ -993,7 +987,8 @@ func CompareAndPullRequestPost(ctx *context.Context) { // This stage is already stop creating new pull request, so it does not matter if it has // something to compare or not. - PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch) + PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch, + gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string))) if ctx.Written() { return } @@ -1003,7 +998,8 @@ func CompareAndPullRequestPost(ctx *context.Context) { } if util.IsEmptyString(form.Title) { - PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch) + PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch, + gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string))) if ctx.Written() { return } diff --git a/routers/routes/web.go b/routers/routes/web.go index 389e050376..a6af64f874 100644 --- a/routers/routes/web.go +++ b/routers/routes/web.go @@ -695,8 +695,8 @@ func RegisterRoutes(m *web.Route) { m.Get("/{id}", repo.MilestoneIssuesAndPulls) }, reqRepoIssuesOrPullsReader, context.RepoRef()) m.Combo("/compare/*", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists). - Get(ignSignIn, repo.SetDiffViewStyle, repo.CompareDiff). - Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, bindIgnErr(auth.CreateIssueForm{}), repo.CompareAndPullRequestPost) + Get(ignSignIn, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff). + Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, bindIgnErr(auth.CreateIssueForm{}), repo.SetWhitespaceBehavior, repo.CompareAndPullRequestPost) }, context.RepoAssignment(), context.UnitTypes()) // Grouping for those endpoints that do require authentication @@ -885,7 +885,7 @@ func RegisterRoutes(m *web.Route) { 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}}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.Diff) m.Get("/commit/{sha:[a-f0-9]{7,40}}.{:patch|diff}", repo.RawDiff) m.Group("", func() { @@ -977,7 +977,7 @@ func RegisterRoutes(m *web.Route) { m.Group("", func() { m.Get("/graph", repo.Graph) - m.Get("/commit/{sha:([a-f0-9]{7,40})$}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.Diff) + m.Get("/commit/{sha:([a-f0-9]{7,40})$}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.Diff) }, repo.MustBeNotEmpty, context.RepoRef(), reqRepoCodeReader) m.Group("/src", func() { diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 81b92f7168..d706dc99c9 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -967,7 +967,13 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID // GetDiffCommit builds a Diff representing the given commitID. func GetDiffCommit(repoPath, commitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) { - return GetDiffRange(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles) + return GetDiffRangeWithWhitespaceBehavior(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles, "") +} + +// GetDiffCommitWithWhitespaceBehavior builds a Diff representing the given commitID. +// The whitespaceBehavior is either an empty string or a git flag +func GetDiffCommitWithWhitespaceBehavior(repoPath, commitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) { + return GetDiffRangeWithWhitespaceBehavior(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles, whitespaceBehavior) } // CommentAsDiff returns c.Patch as *Diff @@ -995,3 +1001,14 @@ func CommentMustAsDiff(c *models.Comment) *Diff { } return diff } + +// GetWhitespaceFlag returns git diff flag for treating whitespaces +func GetWhitespaceFlag(whiteSpaceBehavior string) string { + whitespaceFlags := map[string]string{ + "ignore-all": "-w", + "ignore-change": "-b", + "ignore-eol": "--ignore-space-at-eol", + "": ""} + + return whitespaceFlags[whiteSpaceBehavior] +} diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl index 5110129b8d..f93dac9b4d 100644 --- a/templates/repo/diff/box.tmpl +++ b/templates/repo/diff/box.tmpl @@ -2,11 +2,7 @@
- {{if .PageIsPullFiles}} - {{template "repo/diff/whitespace_dropdown" .}} - {{else}} - {{ if .IsSplitStyle }}{{.i18n.Tr "repo.diff.show_unified_view"}}{{else}}{{.i18n.Tr "repo.diff.show_split_view"}}{{end}} - {{end}} + {{template "repo/diff/whitespace_dropdown" .}} {{template "repo/diff/options_dropdown" .}} {{if and .PageIsPullFiles $.SignedUserID (not .IsArchived)}} {{template "repo/diff/new_review" .}} @@ -22,11 +18,7 @@ {{svg "octicon-diff" 16 "mr-2"}}{{.i18n.Tr "repo.diff.stats_desc" .Diff.NumFiles .Diff.TotalAddition .Diff.TotalDeletion | Str2html}}
- {{if .PageIsPullFiles}} - {{template "repo/diff/whitespace_dropdown" .}} - {{else}} - {{ if .IsSplitStyle }}{{.i18n.Tr "repo.diff.show_unified_view"}}{{else}}{{.i18n.Tr "repo.diff.show_split_view"}}{{end}} - {{end}} + {{template "repo/diff/whitespace_dropdown" .}} {{template "repo/diff/options_dropdown" .}} {{if and .PageIsPullFiles $.SignedUserID (not .IsArchived)}} {{template "repo/diff/new_review" .}} -- cgit v1.2.3