From 6d7b3ae1a86ec28b2b829c786f323a1662293e10 Mon Sep 17 00:00:00 2001 From: vnkmpf Date: Tue, 9 Feb 2021 20:59:23 +0100 Subject: [PATCH 1/5] Add whitespace to commit view --- routers/repo/commit.go | 13 ++++++++++--- routers/routes/web.go | 2 +- services/gitdiff/gitdiff.go | 8 +++++++- templates/repo/diff/box.tmpl | 12 ++---------- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/routers/repo/commit.go b/routers/repo/commit.go index c411d247e2e59..9f6074589006c 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -262,6 +262,12 @@ func Diff(ctx *context.Context) { ctx.Data["RequireSimpleMDE"] = true ctx.Data["RequireTribute"] = true + whitespaceFlags := map[string]string{ + "ignore-all": "-w", + "ignore-change": "-b", + "ignore-eol": "--ignore-space-at-eol", + "": ""} + userName := ctx.Repo.Owner.Name repoName := ctx.Repo.Repository.Name commitID := ctx.Params(":sha") @@ -304,11 +310,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, + whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)]) if err != nil { - ctx.NotFound("GetDiffCommit", err) + ctx.NotFound("GetDiffCommitWithWhitespaceBehavior", err) return } diff --git a/routers/routes/web.go b/routers/routes/web.go index 9e3e690fb965f..ba2caaae9e708 100644 --- a/routers/routes/web.go +++ b/routers/routes/web.go @@ -968,7 +968,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 81b92f71686dd..e79186756d4af 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 diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl index f01795ad15b60..cb523e6e51a60 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" .}} From d48b2c082e12da13f6c28809ea6baa7eaf80715b Mon Sep 17 00:00:00 2001 From: vnkmpf Date: Tue, 9 Feb 2021 21:33:05 +0100 Subject: [PATCH 2/5] Add whitespace to /compare/a...b --- routers/repo/compare.go | 19 ++++++++++++++----- routers/repo/pull.go | 12 ++++++++++-- routers/routes/web.go | 4 ++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/routers/repo/compare.go b/routers/repo/compare.go index 218f712469530..7a99ce9e0bcc5 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,20 @@ 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) + + whitespaceFlags := map[string]string{ + "ignore-all": "-w", + "ignore-change": "-b", + "ignore-eol": "--ignore-space-at-eol", + "": ""} + 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, + whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)]) if ctx.Written() { return } diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 69860021025b9..8ceabe64bdf8e 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -968,6 +968,12 @@ func CompareAndPullRequestPost(ctx *context.Context) { ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled upload.AddUploadContext(ctx, "comment") + whitespaceFlags := map[string]string{ + "ignore-all": "-w", + "ignore-change": "-b", + "ignore-eol": "--ignore-space-at-eol", + "": ""} + var ( repo = ctx.Repo.Repository attachments []string @@ -993,7 +999,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, + whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)]) if ctx.Written() { return } @@ -1003,7 +1010,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, + whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)]) if ctx.Written() { return } diff --git a/routers/routes/web.go b/routers/routes/web.go index ba2caaae9e708..03a805cae42db 100644 --- a/routers/routes/web.go +++ b/routers/routes/web.go @@ -687,8 +687,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 From 5467b03e11c7b446f9f184f7cd09358baa92134a Mon Sep 17 00:00:00 2001 From: vnkmpf Date: Tue, 9 Feb 2021 22:35:10 +0100 Subject: [PATCH 3/5] Move repeated whitespaceFlags to gitdiff --- routers/repo/commit.go | 8 +------- routers/repo/compare.go | 8 +------- routers/repo/pull.go | 18 +++--------------- services/gitdiff/gitdiff.go | 11 +++++++++++ 4 files changed, 16 insertions(+), 29 deletions(-) diff --git a/routers/repo/commit.go b/routers/repo/commit.go index 9f6074589006c..c3ee6b5acc0dd 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -262,12 +262,6 @@ func Diff(ctx *context.Context) { ctx.Data["RequireSimpleMDE"] = true ctx.Data["RequireTribute"] = true - whitespaceFlags := map[string]string{ - "ignore-all": "-w", - "ignore-change": "-b", - "ignore-eol": "--ignore-space-at-eol", - "": ""} - userName := ctx.Repo.Owner.Name repoName := ctx.Repo.Repository.Name commitID := ctx.Params(":sha") @@ -313,7 +307,7 @@ func Diff(ctx *context.Context) { diff, err := gitdiff.GetDiffCommitWithWhitespaceBehavior(repoPath, commitID, setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, - whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)]) + gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string))) if err != nil { ctx.NotFound("GetDiffCommitWithWhitespaceBehavior", err) return diff --git a/routers/repo/compare.go b/routers/repo/compare.go index 7a99ce9e0bcc5..4869e5919a94b 100644 --- a/routers/repo/compare.go +++ b/routers/repo/compare.go @@ -532,19 +532,13 @@ func getBranchesForRepo(user *models.User, repo *models.Repository) (bool, []str func CompareDiff(ctx *context.Context) { headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch := ParseCompareInfo(ctx) - whitespaceFlags := map[string]string{ - "ignore-all": "-w", - "ignore-change": "-b", - "ignore-eol": "--ignore-space-at-eol", - "": ""} - if ctx.Written() { return } defer headGitRepo.Close() nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch, - whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)]) + gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string))) if ctx.Written() { return } diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 8ceabe64bdf8e..d3dd9e4fb5442 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 @@ -968,12 +962,6 @@ func CompareAndPullRequestPost(ctx *context.Context) { ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled upload.AddUploadContext(ctx, "comment") - whitespaceFlags := map[string]string{ - "ignore-all": "-w", - "ignore-change": "-b", - "ignore-eol": "--ignore-space-at-eol", - "": ""} - var ( repo = ctx.Repo.Repository attachments []string @@ -1000,7 +988,7 @@ 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, - whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)]) + gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string))) if ctx.Written() { return } @@ -1011,7 +999,7 @@ func CompareAndPullRequestPost(ctx *context.Context) { if util.IsEmptyString(form.Title) { PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch, - whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)]) + gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string))) if ctx.Written() { return } diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index e79186756d4af..9ae3c0acd466f 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -1001,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] +} From 95cff5bad0ceaefc6b663041c3eaa0be1ff3d486 Mon Sep 17 00:00:00 2001 From: vnkmpf Date: Tue, 9 Feb 2021 22:53:25 +0100 Subject: [PATCH 4/5] Add whitespace for wiki pages --- routers/routes/web.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/routes/web.go b/routers/routes/web.go index 03a805cae42db..8c9be0da051d5 100644 --- a/routers/routes/web.go +++ b/routers/routes/web.go @@ -876,7 +876,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() { From 6a2dd28da1768e97342573424902629200b0589e Mon Sep 17 00:00:00 2001 From: vnkmpf Date: Thu, 11 Feb 2021 18:39:14 +0100 Subject: [PATCH 5/5] Fix syntax --- services/gitdiff/gitdiff.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 9ae3c0acd466f..d706dc99c9479 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -1003,7 +1003,7 @@ func CommentMustAsDiff(c *models.Comment) *Diff { } // GetWhitespaceFlag returns git diff flag for treating whitespaces -func GetWhitespaceFlag(whiteSpaceBehavior string) (string) { +func GetWhitespaceFlag(whiteSpaceBehavior string) string { whitespaceFlags := map[string]string{ "ignore-all": "-w", "ignore-change": "-b",