Skip to content

Commit 739f07c

Browse files
andreyneringlunny
authored andcommitted
Remember diff view style (#163)
1 parent bd76e15 commit 739f07c

File tree

7 files changed

+47
-7
lines changed

7 files changed

+47
-7
lines changed

cmd/web.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,20 +584,20 @@ func runWeb(ctx *cli.Context) error {
584584

585585
m.Group("/pulls/:index", func() {
586586
m.Get("/commits", context.RepoRef(), repo.ViewPullCommits)
587-
m.Get("/files", context.RepoRef(), repo.SetEditorconfigIfExists, repo.ViewPullFiles)
587+
m.Get("/files", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ViewPullFiles)
588588
m.Post("/merge", reqRepoWriter, repo.MergePullRequest)
589589
}, repo.MustAllowPulls)
590590

591591
m.Group("", func() {
592592
m.Get("/src/*", repo.SetEditorconfigIfExists, repo.Home)
593593
m.Get("/raw/*", repo.SingleDownload)
594594
m.Get("/commits/*", repo.RefCommits)
595-
m.Get("/commit/:sha([a-f0-9]{7,40})$", repo.SetEditorconfigIfExists, repo.Diff)
595+
m.Get("/commit/:sha([a-f0-9]{7,40})$", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.Diff)
596596
m.Get("/forks", repo.Forks)
597597
}, context.RepoRef())
598598
m.Get("/commit/:sha([a-f0-9]{7,40})\\.:ext(patch|diff)", repo.RawDiff)
599599

600-
m.Get("/compare/:before([a-z0-9]{40})\\.\\.\\.:after([a-z0-9]{40})", repo.CompareDiff)
600+
m.Get("/compare/:before([a-z0-9]{40})\\.\\.\\.:after([a-z0-9]{40})", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.CompareDiff)
601601
}, ignSignIn, context.RepoAssignment(), repo.MustBeNotBare)
602602
m.Group("/:username/:reponame", func() {
603603
m.Get("/stars", repo.Stars)

models/migrations/migrations.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ var migrations = []Migration{
7272

7373
// v13 -> v14:v0.9.87
7474
NewMigration("set comment updated with created", setCommentUpdatedWithCreated),
75+
76+
NewMigration("create user column diff view style", createUserColumnDiffViewStyle),
7577
}
7678

7779
// Migrate database to current version
@@ -96,7 +98,7 @@ func Migrate(x *xorm.Engine) error {
9698

9799
v := currentVersion.Version
98100
if _MIN_DB_VER > v {
99-
log.Fatal(4, `Gogs no longer supports auto-migration from your previously installed version.
101+
log.Fatal(4, `Gogs no longer supports auto-migration from your previously installed version.
100102
Please try to upgrade to a lower version (>= v0.6.0) first, then upgrade to current version.`)
101103
return nil
102104
}

models/migrations/v14.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,15 @@ func setCommentUpdatedWithCreated(x *xorm.Engine) (err error) {
2222
}
2323
return nil
2424
}
25+
26+
type UserV14 struct {
27+
DiffViewStyle string `xorm:"NOT NULL DEFAULT ''"`
28+
}
29+
30+
func (*UserV14) TableName() string {
31+
return "user"
32+
}
33+
34+
func createUserColumnDiffViewStyle(x *xorm.Engine) error {
35+
return x.Sync2(new(UserV14))
36+
}

models/user.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ type User struct {
107107
NumMembers int
108108
Teams []*Team `xorm:"-"`
109109
Members []*User `xorm:"-"`
110+
111+
// Preferences
112+
DiffViewStyle string `xorm:"NOT NULL DEFAULT ''"`
110113
}
111114

112115
func (u *User) BeforeInsert() {
@@ -126,6 +129,11 @@ func (u *User) SetLastLogin() {
126129
u.LastLoginUnix = time.Now().Unix()
127130
}
128131

132+
func (u *User) UpdateDiffViewStyle(style string) error {
133+
u.DiffViewStyle = style
134+
return UpdateUser(u)
135+
}
136+
129137
func (u *User) AfterSet(colName string, _ xorm.Cell) {
130138
switch colName {
131139
case "full_name":

routers/repo/commit.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ func Diff(ctx *context.Context) {
180180
}
181181

182182
ctx.Data["CommitID"] = commitID
183-
ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
184183
ctx.Data["Username"] = userName
185184
ctx.Data["Reponame"] = repoName
186185
ctx.Data["IsImageFile"] = commit.IsImageFile
@@ -239,7 +238,6 @@ func CompareDiff(ctx *context.Context) {
239238
}
240239
commits = models.ValidateCommitsWithEmails(commits)
241240

242-
ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
243241
ctx.Data["CommitRepoLink"] = ctx.Repo.RepoLink
244242
ctx.Data["Commits"] = commits
245243
ctx.Data["CommitCount"] = commits.Len()

routers/repo/middlewares.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,24 @@ func SetEditorconfigIfExists(ctx *context.Context) {
2121

2222
ctx.Data["Editorconfig"] = ec
2323
}
24+
25+
func SetDiffViewStyle(ctx *context.Context) {
26+
var (
27+
userStyle = ctx.User.DiffViewStyle
28+
queryStyle = ctx.Query("style")
29+
style string
30+
)
31+
32+
if queryStyle == "unified" || queryStyle == "split" {
33+
style = queryStyle
34+
} else if userStyle == "unified" || userStyle == "split" {
35+
style = userStyle
36+
} else {
37+
style = "unified"
38+
}
39+
40+
ctx.Data["IsSplitStyle"] = style == "split"
41+
if err := ctx.User.UpdateDiffViewStyle(style); err != nil {
42+
ctx.Handle(500, "ErrUpdateDiffViewStyle", err)
43+
}
44+
}

routers/repo/pull.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,6 @@ func ViewPullFiles(ctx *context.Context) {
367367
}
368368

369369
headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
370-
ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
371370
ctx.Data["Username"] = pull.HeadUserName
372371
ctx.Data["Reponame"] = pull.HeadRepo.Name
373372
ctx.Data["IsImageFile"] = commit.IsImageFile

0 commit comments

Comments
 (0)