aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorViktor Suprun <popsul1993@gmail.com>2022-02-08 17:15:04 +1100
committerGitHub <noreply@github.com>2022-02-08 14:15:04 +0800
commit4d939845d20d377e06bce5b02667ff21f69c3beb (patch)
tree3cfc0c54b5930a71c404961c26e7ae9e76060ffb
parent60f203385e6f27fae47f3cc8c5d71309f4fd88dc (diff)
downloadgitea-4d939845d20d377e06bce5b02667ff21f69c3beb.tar.gz
gitea-4d939845d20d377e06bce5b02667ff21f69c3beb.zip
Added auto-save whitespace behavior if it changed manually (#15566)
-rw-r--r--models/user/setting_keys.go4
-rw-r--r--routers/web/repo/middlewares.go24
-rw-r--r--services/gitdiff/gitdiff.go10
-rw-r--r--templates/repo/diff/whitespace_dropdown.tmpl4
4 files changed, 33 insertions, 9 deletions
diff --git a/models/user/setting_keys.go b/models/user/setting_keys.go
index 458b78e357..109b5dd916 100644
--- a/models/user/setting_keys.go
+++ b/models/user/setting_keys.go
@@ -5,6 +5,8 @@
package user
const (
- // SettingsKeyHiddenCommentTypes is the settings key for hidden comment types
+ // SettingsKeyHiddenCommentTypes is the setting key for hidden comment types
SettingsKeyHiddenCommentTypes = "issue.hidden_comment_types"
+ // SettingsKeyDiffWhitespaceBehavior is the setting key for whitespace behavior of diff
+ SettingsKeyDiffWhitespaceBehavior = "diff.whitespace_behaviour"
)
diff --git a/routers/web/repo/middlewares.go b/routers/web/repo/middlewares.go
index 3ee9d1a3e9..c99f0ee8f9 100644
--- a/routers/web/repo/middlewares.go
+++ b/routers/web/repo/middlewares.go
@@ -63,11 +63,29 @@ func SetDiffViewStyle(ctx *context.Context) {
// SetWhitespaceBehavior set whitespace behavior as render variable
func SetWhitespaceBehavior(ctx *context.Context) {
+ const defaultWhitespaceBehavior = "show-all"
whitespaceBehavior := ctx.FormString("whitespace")
switch whitespaceBehavior {
- case "ignore-all", "ignore-eol", "ignore-change":
- ctx.Data["WhitespaceBehavior"] = whitespaceBehavior
+ case "", "ignore-all", "ignore-eol", "ignore-change":
+ break
default:
- ctx.Data["WhitespaceBehavior"] = ""
+ whitespaceBehavior = defaultWhitespaceBehavior
+ }
+ if ctx.IsSigned {
+ userWhitespaceBehavior, err := user_model.GetUserSetting(ctx.User.ID, user_model.SettingsKeyDiffWhitespaceBehavior, defaultWhitespaceBehavior)
+ if err == nil {
+ if whitespaceBehavior == "" {
+ whitespaceBehavior = userWhitespaceBehavior
+ } else if whitespaceBehavior != userWhitespaceBehavior {
+ _ = user_model.SetUserSetting(ctx.User.ID, user_model.SettingsKeyDiffWhitespaceBehavior, whitespaceBehavior)
+ }
+ } // else: we can ignore the error safely
+ }
+
+ // these behaviors are for gitdiff.GetWhitespaceFlag
+ if whitespaceBehavior == "" {
+ ctx.Data["WhitespaceBehavior"] = defaultWhitespaceBehavior
+ } else {
+ ctx.Data["WhitespaceBehavior"] = whitespaceBehavior
}
}
diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go
index 0fcb361619..017341d63f 100644
--- a/services/gitdiff/gitdiff.go
+++ b/services/gitdiff/gitdiff.go
@@ -1532,13 +1532,17 @@ 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",
"ignore-eol": "--ignore-space-at-eol",
- "": "",
+ "show-all": "",
}
- return whitespaceFlags[whiteSpaceBehavior]
+ if flag, ok := whitespaceFlags[whitespaceBehavior]; ok {
+ return flag
+ }
+ log.Warn("unknown whitespace behavior: %q, default to 'show-all'", whitespaceBehavior)
+ return ""
}
diff --git a/templates/repo/diff/whitespace_dropdown.tmpl b/templates/repo/diff/whitespace_dropdown.tmpl
index 1e0e046925..7ea4238d6d 100644
--- a/templates/repo/diff/whitespace_dropdown.tmpl
+++ b/templates/repo/diff/whitespace_dropdown.tmpl
@@ -2,8 +2,8 @@
{{.i18n.Tr "repo.diff.whitespace_button"}}
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
<div class="menu">
- <a class="item" href="?style={{if .IsSplitStyle}}split{{else}}unified{{end}}&whitespace=">
- <i class="circle {{ if eq .WhitespaceBehavior "" }}dot{{else}}outline{{end}} icon"></i>
+ <a class="item" href="?style={{if .IsSplitStyle}}split{{else}}unified{{end}}&whitespace=show-all">
+ <i class="circle {{ if eq .WhitespaceBehavior "show-all" }}dot{{else}}outline{{end}} icon"></i>
{{.i18n.Tr "repo.diff.whitespace_show_everything"}}
</a>
<a class="item" href="?style={{if .IsSplitStyle}}split{{else}}unified{{end}}&whitespace=ignore-all">