Skip to content

Commit 1e765c5

Browse files
committed
Merge branch 'add-file-tree-to-file-view-page' of github.com:kerwin612/gitea into kerwin612-add-file-tree-to-file-view-page
2 parents 65e2411 + c36e6ad commit 1e765c5

File tree

21 files changed

+1125
-115
lines changed

21 files changed

+1125
-115
lines changed

models/user/setting_keys.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ const (
1717
// SignupIP is the IP address that the user signed up with
1818
SignupIP = "signup.ip"
1919
// SignupUserAgent is the user agent that the user signed up with
20-
SignupUserAgent = "signup.user_agent"
20+
SignupUserAgent = "signup.user_agent"
21+
SettingsKeyShowFileViewTreeSidebar = "tree.show_file_view_tree_sidebar"
2122
)

routers/web/repo/blame.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ func RefBlame(ctx *context.Context) {
4747
return
4848
}
4949

50+
prepareHomeTreeSideBarSwitch(ctx)
51+
5052
branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.RefTypeNameSubURL()
5153
treeLink := branchLink
5254
rawLink := ctx.Repo.RepoLink + "/raw/" + ctx.Repo.RefTypeNameSubURL()
@@ -92,9 +94,16 @@ func RefBlame(ctx *context.Context) {
9294
ctx.Data["FileSize"] = fileSize
9395
ctx.Data["FileName"] = blob.Name()
9496

97+
var tplName templates.TplName
98+
if ctx.FormBool("only_content") {
99+
tplName = tplRepoViewContent
100+
} else {
101+
tplName = tplRepoView
102+
}
103+
95104
if fileSize >= setting.UI.MaxDisplayFileSize {
96105
ctx.Data["IsFileTooLarge"] = true
97-
ctx.HTML(http.StatusOK, tplRepoHome)
106+
ctx.HTML(http.StatusOK, tplName)
98107
return
99108
}
100109

@@ -122,7 +131,7 @@ func RefBlame(ctx *context.Context) {
122131

123132
renderBlame(ctx, result.Parts, commitNames)
124133

125-
ctx.HTML(http.StatusOK, tplRepoHome)
134+
ctx.HTML(http.StatusOK, tplName)
126135
}
127136

128137
type blameResult struct {

routers/web/repo/repo.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"net/http"
1111
"slices"
12+
"strconv"
1213
"strings"
1314

1415
"code.gitea.io/gitea/models/db"
@@ -20,6 +21,7 @@ import (
2021
user_model "code.gitea.io/gitea/models/user"
2122
"code.gitea.io/gitea/modules/cache"
2223
"code.gitea.io/gitea/modules/git"
24+
"code.gitea.io/gitea/modules/json"
2325
"code.gitea.io/gitea/modules/log"
2426
"code.gitea.io/gitea/modules/optional"
2527
repo_module "code.gitea.io/gitea/modules/repository"
@@ -648,3 +650,21 @@ func PrepareBranchList(ctx *context.Context) {
648650
}
649651
ctx.Data["Branches"] = brs
650652
}
653+
654+
type preferencesForm struct {
655+
ShowFileViewTreeSidebar bool `json:"show_file_view_tree_sidebar"`
656+
}
657+
658+
func UpdatePreferences(ctx *context.Context) {
659+
form := &preferencesForm{}
660+
if err := json.NewDecoder(ctx.Req.Body).Decode(&form); err != nil {
661+
ctx.ServerError("DecodePreferencesForm", err)
662+
return
663+
}
664+
if err := user_model.SetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyShowFileViewTreeSidebar,
665+
strconv.FormatBool(form.ShowFileViewTreeSidebar)); err != nil {
666+
log.Error("SetUserSetting: %v", err)
667+
}
668+
669+
ctx.JSONOK()
670+
}

routers/web/repo/treelist.go renamed to routers/web/repo/tree.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
package repo
55

66
import (
7+
"errors"
78
"net/http"
89

910
pull_model "code.gitea.io/gitea/models/pull"
1011
"code.gitea.io/gitea/modules/base"
1112
"code.gitea.io/gitea/modules/git"
1213
"code.gitea.io/gitea/services/context"
1314
"code.gitea.io/gitea/services/gitdiff"
15+
files_service "code.gitea.io/gitea/services/repository/files"
1416

1517
"github.com/go-enry/go-enry/v2"
1618
)
@@ -84,3 +86,25 @@ func transformDiffTreeForUI(diffTree *gitdiff.DiffTree, filesViewedState map[str
8486

8587
return files
8688
}
89+
90+
func Tree(ctx *context.Context) {
91+
recursive := ctx.FormBool("recursive")
92+
93+
if ctx.Repo.RefFullName == "" {
94+
ctx.ServerError("RefFullName", errors.New("ref_name is invalid"))
95+
return
96+
}
97+
98+
var results []*files_service.TreeViewNode
99+
var err error
100+
if !recursive {
101+
results, err = files_service.GetTreeList(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Repo.TreePath, ctx.Repo.RefFullName, false)
102+
} else {
103+
results, err = files_service.GetTreeInformation(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Repo.TreePath, ctx.Repo.RefFullName)
104+
}
105+
if err != nil {
106+
ctx.ServerError("GetTreeInformation", err)
107+
return
108+
}
109+
ctx.JSON(http.StatusOK, results)
110+
}

routers/web/repo/view.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ import (
4747
)
4848

4949
const (
50-
tplRepoEMPTY templates.TplName = "repo/empty"
51-
tplRepoHome templates.TplName = "repo/home"
52-
tplRepoViewList templates.TplName = "repo/view_list"
53-
tplWatchers templates.TplName = "repo/watchers"
54-
tplForks templates.TplName = "repo/forks"
55-
tplMigrating templates.TplName = "repo/migrate/migrating"
50+
tplRepoEMPTY templates.TplName = "repo/empty"
51+
tplRepoHome templates.TplName = "repo/home"
52+
tplRepoView templates.TplName = "repo/view"
53+
tplRepoViewContent templates.TplName = "repo/view_content"
54+
tplRepoViewList templates.TplName = "repo/view_list"
55+
tplWatchers templates.TplName = "repo/watchers"
56+
tplForks templates.TplName = "repo/forks"
57+
tplMigrating templates.TplName = "repo/migrate/migrating"
5658
)
5759

5860
type fileInfo struct {

routers/web/repo/view_home.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"html/template"
1010
"net/http"
1111
"path"
12+
"strconv"
1213
"strings"
1314
"time"
1415

@@ -17,6 +18,7 @@ import (
1718
access_model "code.gitea.io/gitea/models/perm/access"
1819
repo_model "code.gitea.io/gitea/models/repo"
1920
unit_model "code.gitea.io/gitea/models/unit"
21+
user_model "code.gitea.io/gitea/models/user"
2022
"code.gitea.io/gitea/modules/git"
2123
"code.gitea.io/gitea/modules/log"
2224
repo_module "code.gitea.io/gitea/modules/repository"
@@ -328,6 +330,21 @@ func handleRepoHomeFeed(ctx *context.Context) bool {
328330
return true
329331
}
330332

333+
func prepareHomeTreeSideBarSwitch(ctx *context.Context) {
334+
showFileViewTreeSidebar := true
335+
if ctx.Doer != nil {
336+
v, err := user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyShowFileViewTreeSidebar, "true")
337+
if err != nil {
338+
log.Error("GetUserSetting: %v", err)
339+
} else {
340+
showFileViewTreeSidebar, _ = strconv.ParseBool(v)
341+
}
342+
}
343+
ctx.Data["RepoPreferences"] = &preferencesForm{
344+
ShowFileViewTreeSidebar: showFileViewTreeSidebar,
345+
}
346+
}
347+
331348
// Home render repository home page
332349
func Home(ctx *context.Context) {
333350
if handleRepoHomeFeed(ctx) {
@@ -341,6 +358,8 @@ func Home(ctx *context.Context) {
341358
return
342359
}
343360

361+
prepareHomeTreeSideBarSwitch(ctx)
362+
344363
title := ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name
345364
if len(ctx.Repo.Repository.Description) > 0 {
346365
title += ": " + ctx.Repo.Repository.Description
@@ -410,7 +429,13 @@ func Home(ctx *context.Context) {
410429
}
411430
}
412431

413-
ctx.HTML(http.StatusOK, tplRepoHome)
432+
if ctx.FormBool("only_content") {
433+
ctx.HTML(http.StatusOK, tplRepoViewContent)
434+
} else if len(treeNames) != 0 {
435+
ctx.HTML(http.StatusOK, tplRepoView)
436+
} else {
437+
ctx.HTML(http.StatusOK, tplRepoHome)
438+
}
414439
}
415440

416441
func RedirectRepoTreeToSrc(ctx *context.Context) {

routers/web/web.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,7 @@ func registerRoutes(m *web.Router) {
10011001
m.Get("/migrate", repo.Migrate)
10021002
m.Post("/migrate", web.Bind(forms.MigrateRepoForm{}), repo.MigratePost)
10031003
m.Get("/search", repo.SearchRepo)
1004+
m.Put("/preferences", repo.UpdatePreferences)
10041005
}, reqSignIn)
10051006
// end "/repo": create, migrate, search
10061007

@@ -1175,6 +1176,11 @@ func registerRoutes(m *web.Router) {
11751176
m.Get("/tag/*", context.RepoRefByType(git.RefTypeTag), repo.TreeList)
11761177
m.Get("/commit/*", context.RepoRefByType(git.RefTypeCommit), repo.TreeList)
11771178
})
1179+
m.Group("/tree", func() {
1180+
m.Get("/branch/*", context.RepoRefByType(git.RefTypeBranch), repo.Tree)
1181+
m.Get("/tag/*", context.RepoRefByType(git.RefTypeTag), repo.Tree)
1182+
m.Get("/commit/*", context.RepoRefByType(git.RefTypeCommit), repo.Tree)
1183+
})
11781184
m.Get("/compare", repo.MustBeNotEmpty, repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff)
11791185
m.Combo("/compare/*", repo.MustBeNotEmpty, repo.SetEditorconfigIfExists).
11801186
Get(repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff).

0 commit comments

Comments
 (0)