Skip to content

Commit 5140fd7

Browse files
authored
Merge branch 'master' into git_simple_pr
2 parents 300765d + 487f2ee commit 5140fd7

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
@@ -589,12 +589,6 @@ func ViewPullFiles(ctx *context.Context) {
589589
}
590590
pull := issue.PullRequest
591591

592-
whitespaceFlags := map[string]string{
593-
"ignore-all": "-w",
594-
"ignore-change": "-b",
595-
"ignore-eol": "--ignore-space-at-eol",
596-
"": ""}
597-
598592
var (
599593
diffRepoPath string
600594
startCommitID string
@@ -637,7 +631,7 @@ func ViewPullFiles(ctx *context.Context) {
637631
diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
638632
startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
639633
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
640-
whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)])
634+
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
641635
if err != nil {
642636
ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err)
643637
return
@@ -1001,7 +995,8 @@ func CompareAndPullRequestPost(ctx *context.Context) {
1001995

1002996
// This stage is already stop creating new pull request, so it does not matter if it has
1003997
// something to compare or not.
1004-
PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
998+
PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch,
999+
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
10051000
if ctx.Written() {
10061001
return
10071002
}
@@ -1011,7 +1006,8 @@ func CompareAndPullRequestPost(ctx *context.Context) {
10111006
}
10121007

10131008
if util.IsEmptyString(form.Title) {
1014-
PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
1009+
PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch,
1010+
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
10151011
if ctx.Written() {
10161012
return
10171013
}

routers/routes/web.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,8 @@ func RegisterRoutes(m *web.Route) {
711711
m.Get("/{id}", repo.MilestoneIssuesAndPulls)
712712
}, reqRepoIssuesOrPullsReader, context.RepoRef())
713713
m.Combo("/compare/*", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists).
714-
Get(ignSignIn, repo.SetDiffViewStyle, repo.CompareDiff).
715-
Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, bindIgnErr(auth.CreateIssueForm{}), repo.CompareAndPullRequestPost)
714+
Get(ignSignIn, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff).
715+
Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, bindIgnErr(auth.CreateIssueForm{}), repo.SetWhitespaceBehavior, repo.CompareAndPullRequestPost)
716716
}, context.RepoAssignment(), context.UnitTypes())
717717

718718
// Grouping for those endpoints that do require authentication
@@ -901,7 +901,7 @@ func RegisterRoutes(m *web.Route) {
901901
m.Get("/{page}", repo.Wiki)
902902
m.Get("/_pages", repo.WikiPages)
903903
m.Get("/{page}/_revision", repo.WikiRevision)
904-
m.Get("/commit/{sha:[a-f0-9]{7,40}}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.Diff)
904+
m.Get("/commit/{sha:[a-f0-9]{7,40}}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.Diff)
905905
m.Get("/commit/{sha:[a-f0-9]{7,40}}.{:patch|diff}", repo.RawDiff)
906906

907907
m.Group("", func() {
@@ -993,7 +993,7 @@ func RegisterRoutes(m *web.Route) {
993993

994994
m.Group("", func() {
995995
m.Get("/graph", repo.Graph)
996-
m.Get("/commit/{sha:([a-f0-9]{7,40})$}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.Diff)
996+
m.Get("/commit/{sha:([a-f0-9]{7,40})$}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.Diff)
997997
}, repo.MustBeNotEmpty, context.RepoRef(), reqRepoCodeReader)
998998

999999
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)