From 739f07c98e1254f776a7cb943a3430de32b17d1d Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sun, 13 Nov 2016 00:54:04 -0200 Subject: [PATCH] Remember diff view style (#163) --- cmd/web.go | 6 +++--- models/migrations/migrations.go | 4 +++- models/migrations/v14.go | 12 ++++++++++++ models/user.go | 8 ++++++++ routers/repo/commit.go | 2 -- routers/repo/middlewares.go | 21 +++++++++++++++++++++ routers/repo/pull.go | 1 - 7 files changed, 47 insertions(+), 7 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index 9f45e1a7ff7..8cd3949be19 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -584,7 +584,7 @@ func runWeb(ctx *cli.Context) error { m.Group("/pulls/:index", func() { m.Get("/commits", context.RepoRef(), repo.ViewPullCommits) - m.Get("/files", context.RepoRef(), repo.SetEditorconfigIfExists, repo.ViewPullFiles) + m.Get("/files", context.RepoRef(), repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ViewPullFiles) m.Post("/merge", reqRepoWriter, repo.MergePullRequest) }, repo.MustAllowPulls) @@ -592,12 +592,12 @@ func runWeb(ctx *cli.Context) error { m.Get("/src/*", repo.SetEditorconfigIfExists, repo.Home) m.Get("/raw/*", repo.SingleDownload) m.Get("/commits/*", repo.RefCommits) - m.Get("/commit/:sha([a-f0-9]{7,40})$", repo.SetEditorconfigIfExists, repo.Diff) + m.Get("/commit/:sha([a-f0-9]{7,40})$", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.Diff) m.Get("/forks", repo.Forks) }, context.RepoRef()) m.Get("/commit/:sha([a-f0-9]{7,40})\\.:ext(patch|diff)", repo.RawDiff) - m.Get("/compare/:before([a-z0-9]{40})\\.\\.\\.:after([a-z0-9]{40})", repo.CompareDiff) + m.Get("/compare/:before([a-z0-9]{40})\\.\\.\\.:after([a-z0-9]{40})", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.CompareDiff) }, ignSignIn, context.RepoAssignment(), repo.MustBeNotBare) m.Group("/:username/:reponame", func() { m.Get("/stars", repo.Stars) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index e69cf993b7e..957af42661b 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -72,6 +72,8 @@ var migrations = []Migration{ // v13 -> v14:v0.9.87 NewMigration("set comment updated with created", setCommentUpdatedWithCreated), + + NewMigration("create user column diff view style", createUserColumnDiffViewStyle), } // Migrate database to current version @@ -96,7 +98,7 @@ func Migrate(x *xorm.Engine) error { v := currentVersion.Version if _MIN_DB_VER > v { - log.Fatal(4, `Gogs no longer supports auto-migration from your previously installed version. + log.Fatal(4, `Gogs no longer supports auto-migration from your previously installed version. Please try to upgrade to a lower version (>= v0.6.0) first, then upgrade to current version.`) return nil } diff --git a/models/migrations/v14.go b/models/migrations/v14.go index 79c31133f43..0cdcf1005fc 100644 --- a/models/migrations/v14.go +++ b/models/migrations/v14.go @@ -22,3 +22,15 @@ func setCommentUpdatedWithCreated(x *xorm.Engine) (err error) { } return nil } + +type UserV14 struct { + DiffViewStyle string `xorm:"NOT NULL DEFAULT ''"` +} + +func (*UserV14) TableName() string { + return "user" +} + +func createUserColumnDiffViewStyle(x *xorm.Engine) error { + return x.Sync2(new(UserV14)) +} diff --git a/models/user.go b/models/user.go index d97ced931aa..0b6063fbc90 100644 --- a/models/user.go +++ b/models/user.go @@ -107,6 +107,9 @@ type User struct { NumMembers int Teams []*Team `xorm:"-"` Members []*User `xorm:"-"` + + // Preferences + DiffViewStyle string `xorm:"NOT NULL DEFAULT ''"` } func (u *User) BeforeInsert() { @@ -126,6 +129,11 @@ func (u *User) SetLastLogin() { u.LastLoginUnix = time.Now().Unix() } +func (u *User) UpdateDiffViewStyle(style string) error { + u.DiffViewStyle = style + return UpdateUser(u) +} + func (u *User) AfterSet(colName string, _ xorm.Cell) { switch colName { case "full_name": diff --git a/routers/repo/commit.go b/routers/repo/commit.go index e3170b053df..9706779ee58 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -180,7 +180,6 @@ func Diff(ctx *context.Context) { } ctx.Data["CommitID"] = commitID - ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split" ctx.Data["Username"] = userName ctx.Data["Reponame"] = repoName ctx.Data["IsImageFile"] = commit.IsImageFile @@ -239,7 +238,6 @@ func CompareDiff(ctx *context.Context) { } commits = models.ValidateCommitsWithEmails(commits) - ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split" ctx.Data["CommitRepoLink"] = ctx.Repo.RepoLink ctx.Data["Commits"] = commits ctx.Data["CommitCount"] = commits.Len() diff --git a/routers/repo/middlewares.go b/routers/repo/middlewares.go index 94e007cd208..757a049ab7e 100644 --- a/routers/repo/middlewares.go +++ b/routers/repo/middlewares.go @@ -21,3 +21,24 @@ func SetEditorconfigIfExists(ctx *context.Context) { ctx.Data["Editorconfig"] = ec } + +func SetDiffViewStyle(ctx *context.Context) { + var ( + userStyle = ctx.User.DiffViewStyle + queryStyle = ctx.Query("style") + style string + ) + + if queryStyle == "unified" || queryStyle == "split" { + style = queryStyle + } else if userStyle == "unified" || userStyle == "split" { + style = userStyle + } else { + style = "unified" + } + + ctx.Data["IsSplitStyle"] = style == "split" + if err := ctx.User.UpdateDiffViewStyle(style); err != nil { + ctx.Handle(500, "ErrUpdateDiffViewStyle", err) + } +} diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 83352d7f3c3..6e99dfaed09 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -367,7 +367,6 @@ func ViewPullFiles(ctx *context.Context) { } headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name) - ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split" ctx.Data["Username"] = pull.HeadUserName ctx.Data["Reponame"] = pull.HeadRepo.Name ctx.Data["IsImageFile"] = commit.IsImageFile