Skip to content

Commit ca112f0

Browse files
nemoinhotechknowlogick
authored andcommitted
Add whitespace handling to PR-comparsion (#4683)
* Add whitespace handling to PR-comparsion In a PR we have to keep an eye on a lot of different things. But sometimes the bare code is the key-thing we want to care about and just don't want to care about fixed indention on some places. Especially if we follow the pathfinder rule we face a lot of these situations because these changes don't break the code in many languages but improve the readability a lot. So this change introduce a fine graned button to adjust the way how the reviewer want to see whitespace-changes within the code. The possibilities reflect the possibilities from git itself except of the `--ignore-blank-lines` flag because that one is also handled by `-b` and is really rare. Signed-off-by: Felix Nehrke <[email protected]>
1 parent 03e558c commit ca112f0

File tree

7 files changed

+91
-15
lines changed

7 files changed

+91
-15
lines changed

models/git_diff.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,13 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
633633
// passing the empty string as beforeCommitID returns a diff from the
634634
// parent commit.
635635
func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) {
636+
return GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID, maxLines, maxLineCharacters, maxFiles, "")
637+
}
638+
639+
// GetDiffRangeWithWhitespaceBehavior builds a Diff between two commits of a repository.
640+
// Passing the empty string as beforeCommitID returns a diff from the parent commit.
641+
// The whitespaceBehavior is either an empty string or a git flag
642+
func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {
636643
gitRepo, err := git.OpenRepository(repoPath)
637644
if err != nil {
638645
return nil, err
@@ -644,17 +651,21 @@ func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxL
644651
}
645652

646653
var cmd *exec.Cmd
647-
// if "after" commit given
648-
if len(beforeCommitID) == 0 {
649-
// First commit of repository.
650-
if commit.ParentCount() == 0 {
651-
cmd = exec.Command("git", "show", afterCommitID)
652-
} else {
653-
c, _ := commit.Parent(0)
654-
cmd = exec.Command("git", "diff", "-M", c.ID.String(), afterCommitID)
655-
}
654+
if len(beforeCommitID) == 0 && commit.ParentCount() == 0 {
655+
cmd = exec.Command("git", "show", afterCommitID)
656656
} else {
657-
cmd = exec.Command("git", "diff", "-M", beforeCommitID, afterCommitID)
657+
actualBeforeCommitID := beforeCommitID
658+
if len(actualBeforeCommitID) == 0 {
659+
parentCommit, _ := commit.Parent(0)
660+
actualBeforeCommitID = parentCommit.ID.String()
661+
}
662+
diffArgs := []string{"diff", "-M"}
663+
if len(whitespaceBehavior) != 0 {
664+
diffArgs = append(diffArgs, whitespaceBehavior)
665+
}
666+
diffArgs = append(diffArgs, actualBeforeCommitID)
667+
diffArgs = append(diffArgs, afterCommitID)
668+
cmd = exec.Command("git", diffArgs...)
658669
}
659670
cmd.Dir = repoPath
660671
cmd.Stderr = os.Stderr

options/locale/locale_en-US.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,11 @@ diff.data_not_available = Diff Content Not Available
11521152
diff.show_diff_stats = Show Diff Stats
11531153
diff.show_split_view = Split View
11541154
diff.show_unified_view = Unified View
1155+
diff.whitespace_button = Whitespace
1156+
diff.whitespace_show_everything = Show all changes
1157+
diff.whitespace_ignore_all_whitespace = Ignore whitespace when comparing lines
1158+
diff.whitespace_ignore_amount_changes = Ignore changes in amount of whitespace
1159+
diff.whitespace_ignore_at_eol = Ignore changes in whitespace at EOL
11551160
diff.stats_desc = <strong> %d changed files</strong> with <strong>%d additions</strong> and <strong>%d deletions</strong>
11561161
diff.bin = BIN
11571162
diff.view_file = View File

routers/repo/middlewares.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,14 @@ func SetDiffViewStyle(ctx *context.Context) {
5050
ctx.ServerError("ErrUpdateDiffViewStyle", err)
5151
}
5252
}
53+
54+
// SetWhitespaceBehavior set whitespace behavior as render variable
55+
func SetWhitespaceBehavior(ctx *context.Context) {
56+
whitespaceBehavior := ctx.Query("whitespace")
57+
switch whitespaceBehavior {
58+
case "ignore-all", "ignore-eol", "ignore-change":
59+
ctx.Data["WhitespaceBehavior"] = whitespaceBehavior
60+
default:
61+
ctx.Data["WhitespaceBehavior"] = ""
62+
}
63+
}

routers/repo/pull.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,12 @@ func ViewPullFiles(ctx *context.Context) {
390390
}
391391
pull := issue.PullRequest
392392

393+
whitespaceFlags := map[string]string{
394+
"ignore-all": "-w",
395+
"ignore-change": "-b",
396+
"ignore-eol": "--ignore-space-at-eol",
397+
"": ""}
398+
393399
var (
394400
diffRepoPath string
395401
startCommitID string
@@ -455,11 +461,12 @@ func ViewPullFiles(ctx *context.Context) {
455461
ctx.Data["Reponame"] = pull.HeadRepo.Name
456462
}
457463

458-
diff, err := models.GetDiffRange(diffRepoPath,
464+
diff, err := models.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
459465
startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
460-
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
466+
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
467+
whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)])
461468
if err != nil {
462-
ctx.ServerError("GetDiffRange", err)
469+
ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err)
463470
return
464471
}
465472

routers/routes/routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ func RegisterRoutes(m *macaron.Macaron) {
674674
m.Post("/merge", reqRepoWriter, bindIgnErr(auth.MergePullRequestForm{}), repo.MergePullRequest)
675675
m.Post("/cleanup", context.RepoRef(), repo.CleanUpPullRequest)
676676
m.Group("/files", func() {
677-
m.Get("", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ViewPullFiles)
677+
m.Get("", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.ViewPullFiles)
678678
m.Group("/reviews", func() {
679679
m.Post("/comments", bindIgnErr(auth.CodeCommentForm{}), repo.CreateCodeComment)
680680
m.Post("/submit", bindIgnErr(auth.SubmitReviewForm{}), repo.SubmitReview)

templates/repo/diff/box.tmpl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
{{if .DiffNotAvailable}}
2+
<div class="diff-detail-box diff-box ui sticky">
3+
<div>
4+
<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}}
10+
<a class="ui tiny basic toggle button" data-target="#diff-files">{{.i18n.Tr "repo.diff.show_diff_stats"}}</a>
11+
{{if and .PageIsPullFiles $.SignedUserID}}
12+
{{template "repo/diff/new_review" .}}
13+
{{end}}
14+
</div>
15+
</div>
16+
</div>
217
<h4>{{.i18n.Tr "repo.diff.data_not_available"}}</h4>
318
{{else}}
419
<div class="diff-detail-box diff-box ui sticky">
520
<div>
621
<i class="fa fa-retweet"></i>
722
{{.i18n.Tr "repo.diff.stats_desc" .Diff.NumFiles .Diff.TotalAddition .Diff.TotalDeletion | Str2html}}
823
<div class="ui right">
9-
<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>
24+
{{if .PageIsPullFiles}}
25+
{{template "repo/diff/whitespace_dropdown" .}}
26+
{{else}}
27+
<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>
28+
{{end}}
1029
<a class="ui tiny basic toggle button" data-target="#diff-files">{{.i18n.Tr "repo.diff.show_diff_stats"}}</a>
1130
{{if and .PageIsPullFiles $.SignedUserID}}
1231
{{template "repo/diff/new_review" .}}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<div class="ui dropdown tiny button">
2+
{{.i18n.Tr "repo.diff.whitespace_button"}}
3+
<i class="dropdown icon"></i>
4+
<div class="menu">
5+
<a class="item" href="?style={{if .IsSplitStyle}}split{{else}}unified{{end}}&whitespace=">
6+
<i class="circle {{ if eq .WhitespaceBehavior "" }}dot{{else}}outline{{end}} icon"></i>
7+
{{.i18n.Tr "repo.diff.whitespace_show_everything"}}
8+
</a>
9+
<a class="item" href="?style={{if .IsSplitStyle}}split{{else}}unified{{end}}&whitespace=ignore-all">
10+
<i class="circle {{ if eq .WhitespaceBehavior "ignore-all" }}dot{{else}}outline{{end}} icon"></i>
11+
{{.i18n.Tr "repo.diff.whitespace_ignore_all_whitespace"}}
12+
</a>
13+
<a class="item" href="?style={{if .IsSplitStyle}}split{{else}}unified{{end}}&whitespace=ignore-change">
14+
<i class="circle {{ if eq .WhitespaceBehavior "ignore-change" }}dot{{else}}outline{{end}} icon"></i>
15+
{{.i18n.Tr "repo.diff.whitespace_ignore_amount_changes"}}
16+
</a>
17+
<a class="item" href="?style={{if .IsSplitStyle}}split{{else}}unified{{end}}&whitespace=ignore-eol">
18+
<i class="circle {{ if eq .WhitespaceBehavior "ignore-eol" }}dot{{else}}outline{{end}} icon"></i>
19+
{{.i18n.Tr "repo.diff.whitespace_ignore_at_eol"}}
20+
</a>
21+
</div>
22+
</div>
23+
<a class="ui tiny basic toggle button" href="?style={{if .IsSplitStyle}}unified{{else}}split{{end}}&whitespace={{$.WhitespaceBehavior}}">{{ if .IsSplitStyle }}{{.i18n.Tr "repo.diff.show_unified_view"}}{{else}}{{.i18n.Tr "repo.diff.show_split_view"}}{{end}}</a>

0 commit comments

Comments
 (0)