Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file tree to file view page #32721

Open
wants to merge 74 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 60 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
181645f
add tree sidebar to file view
kerwin612 Dec 12, 2024
59e46d4
Merge branch 'add-tree-sidebar-2-file-view' into add-file-tree-to-fil…
kerwin612 Dec 13, 2024
c4e7f0c
add tree sidebar to file view
kerwin612 Dec 13, 2024
68ef9ef
Merge branch 'main' into add-file-tree-to-file-view-page
kerwin612 Dec 16, 2024
64b4cf4
Merge branch 'add-file-tree-to-file-view-page' of github.com:kerwin61…
lunny Dec 16, 2024
b60ee86
Add tree
lunny Dec 16, 2024
07fb84f
Merge branch 'main' into add-file-tree-to-file-view-page
kerwin612 Dec 23, 2024
a28b65d
complete first version
lunny Dec 24, 2024
38de8aa
Merge branch 'add-file-tree-to-file-view-page' of github.com:kerwin61…
lunny Dec 24, 2024
a86c9e4
Merge branch 'main' into add-file-tree-to-file-view-page
lunny Dec 24, 2024
e46f7e4
Merge branch 'main' into add-file-tree-to-file-view-page
kerwin612 Dec 24, 2024
023a260
Merge branch 'add-file-tree-to-file-view-page' into add-file-tree-to-…
kerwin612 Dec 24, 2024
6cb22e5
Merge pull request #2 from lunny/add-file-tree-to-file-view-page
kerwin612 Dec 24, 2024
3b3eaec
Fix todo
lunny Dec 24, 2024
f2b1da5
Fix bug
lunny Dec 24, 2024
a1830dc
Merge pull request #3 from lunny/add-file-tree-to-file-view-page
kerwin612 Dec 24, 2024
76a5614
Merge branch 'main' into add-file-tree-to-file-view-page
kerwin612 Dec 24, 2024
158b79e
debug new api /tree
kerwin612 Dec 25, 2024
143249f
change loading
kerwin612 Dec 26, 2024
634fbe0
Merge branch 'main' into add-file-tree-to-file-view-page
kerwin612 Dec 26, 2024
15f4b92
Merge branch 'add-file-tree-to-file-view-page' of github.com:kerwin61…
lunny Dec 26, 2024
2178347
add function for recusive
lunny Dec 26, 2024
9ae1c85
Fix bug
lunny Dec 26, 2024
a19a972
Merge branch 'main' into kerwin612-add-file-tree-to-file-view-page
lunny Dec 26, 2024
e860a36
Add more comment
lunny Dec 26, 2024
1a1fe32
Fix bug
lunny Dec 26, 2024
bf15e18
Fix bug
lunny Dec 26, 2024
91ffb2d
fix bug
kerwin612 Dec 27, 2024
b1b2428
Merge branch 'main' into add-file-tree-to-file-view-page
kerwin612 Jan 3, 2025
fe63c5f
Fix test
lunny Jan 3, 2025
087f0d4
fix
kerwin612 Jan 3, 2025
09000c4
Fix bug
lunny Jan 3, 2025
a1cee9e
Merge branch 'add-file-tree-to-file-view-page' of github.com:kerwin61…
lunny Jan 3, 2025
2a8f9c8
fix
kerwin612 Jan 3, 2025
ad9b126
fix
kerwin612 Jan 6, 2025
c949f42
fix
kerwin612 Jan 6, 2025
64c7e08
Fix bug
lunny Jan 6, 2025
cd3a9af
implement save user ShowFileViewTreeSidebar
lunny Jan 6, 2025
cdf05fb
Add missing changes
lunny Jan 6, 2025
647cd30
Remove unused file
lunny Jan 6, 2025
91f972c
fix
kerwin612 Jan 6, 2025
07e4896
fix lint error
kerwin612 Jan 6, 2025
96e050e
Fix bugs
lunny Jan 6, 2025
1fd53fd
Merge branch 'add-file-tree-to-file-view-page' of github.com:kerwin61…
lunny Jan 6, 2025
6b86304
fix
kerwin612 Jan 6, 2025
a125a58
Fix test
lunny Jan 6, 2025
87ed579
Fix test
lunny Jan 6, 2025
d675289
Fix bug
lunny Jan 6, 2025
2000a67
remove unused code
lunny Jan 6, 2025
335375f
merge main
kerwin612 Jan 10, 2025
34dc5b1
Merge branch 'main' into add-file-tree-to-file-view-page
kerwin612 Jan 10, 2025
be7a6b6
Merge branch 'main' into add-file-tree-to-file-view-page
kerwin612 Jan 10, 2025
3e679d6
fix
kerwin612 Jan 10, 2025
6734237
Merge branch 'upstream_main' into add-file-tree-to-file-view-page
kerwin612 Jan 10, 2025
6441fe5
Merge remote-tracking branch 'refs/remotes/kerwin612/add-file-tree-to…
kerwin612 Jan 10, 2025
f769c8b
Make template simpler
lunny Jan 10, 2025
818ad6a
Merge branch 'add-file-tree-to-file-view-page' of github.com:kerwin61…
lunny Jan 10, 2025
9b95582
fix
kerwin612 Jan 10, 2025
0cafe2a
fix
kerwin612 Jan 10, 2025
c2670db
fix
kerwin612 Jan 10, 2025
62ecf34
Merge branch 'main' into add-file-tree-to-file-view-page
kerwin612 Jan 10, 2025
d4a99e5
fix
kerwin612 Jan 10, 2025
eff3568
Merge branch 'main' into kerwin612-add-file-tree-to-file-view-page
lunny Jan 13, 2025
616fe58
Remove unused code
lunny Jan 13, 2025
3c86322
Fix bug
lunny Jan 13, 2025
67a749f
Some renames and use type instead of isFile
lunny Jan 13, 2025
c0e2fd2
fix
kerwin612 Jan 13, 2025
ab98bfb
Fix bug
lunny Jan 13, 2025
cac5f1c
Merge branch 'add-file-tree-to-file-view-page' of github.com:kerwin61…
lunny Jan 13, 2025
dc63b48
fix
kerwin612 Jan 13, 2025
fe21290
fix
kerwin612 Jan 13, 2025
a55548f
fix
kerwin612 Jan 13, 2025
0b44132
Merge branch 'main' into kerwin612-add-file-tree-to-file-view-page
lunny Jan 13, 2025
4890434
some improvements
lunny Jan 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion models/user/setting_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ const (
// SignupIP is the IP address that the user signed up with
SignupIP = "signup.ip"
// SignupUserAgent is the user agent that the user signed up with
SignupUserAgent = "signup.user_agent"
SignupUserAgent = "signup.user_agent"
SettingsKeyShowFileViewTreeSidebar = "tree.show_file_view_tree_sidebar"
)
24 changes: 22 additions & 2 deletions routers/web/repo/blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ func RefBlame(ctx *context.Context) {
return
}

showFileViewTreeSidebar := true
if ctx.Doer != nil {
v, err := user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyShowFileViewTreeSidebar, "true")
if err != nil {
log.Error("GetUserSetting: %v", err)
} else {
showFileViewTreeSidebar, _ = strconv.ParseBool(v)
}
}
ctx.Data["RepoPreferences"] = &preferencesForm{
ShowFileViewTreeSidebar: showFileViewTreeSidebar,
}

branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
treeLink := branchLink
rawLink := ctx.Repo.RepoLink + "/raw/" + ctx.Repo.BranchNameSubURL()
Expand Down Expand Up @@ -91,9 +104,16 @@ func RefBlame(ctx *context.Context) {
ctx.Data["FileSize"] = fileSize
ctx.Data["FileName"] = blob.Name()

var tplName templates.TplName
if ctx.FormBool("only_content") {
tplName = tplRepoHomeContent
} else {
tplName = tplRepoHome
}

if fileSize >= setting.UI.MaxDisplayFileSize {
ctx.Data["IsFileTooLarge"] = true
ctx.HTML(http.StatusOK, tplRepoHome)
ctx.HTML(http.StatusOK, tplName)
return
}

Expand Down Expand Up @@ -121,7 +141,7 @@ func RefBlame(ctx *context.Context) {

renderBlame(ctx, result.Parts, commitNames)

ctx.HTML(http.StatusOK, tplRepoHome)
ctx.HTML(http.StatusOK, tplName)
}

type blameResult struct {
Expand Down
20 changes: 20 additions & 0 deletions routers/web/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"net/http"
"slices"
"strconv"
"strings"

"code.gitea.io/gitea/models/db"
Expand All @@ -20,6 +21,7 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/optional"
repo_module "code.gitea.io/gitea/modules/repository"
Expand Down Expand Up @@ -758,3 +760,21 @@ func PrepareBranchList(ctx *context.Context) {
}
ctx.Data["Branches"] = brs
}

type preferencesForm struct {
ShowFileViewTreeSidebar bool `json:"show_file_view_tree_sidebar"`
}

func UpdatePreferences(ctx *context.Context) {
form := &preferencesForm{}
if err := json.NewDecoder(ctx.Req.Body).Decode(&form); err != nil {
ctx.ServerError("DecodePreferencesForm", err)
return
}
if err := user_model.SetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyShowFileViewTreeSidebar,
strconv.FormatBool(form.ShowFileViewTreeSidebar)); err != nil {
log.Error("SetUserSetting: %v", err)
}

ctx.JSONOK()
}
28 changes: 28 additions & 0 deletions routers/web/repo/treelist.go → routers/web/repo/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (

"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/services/context"
files_service "code.gitea.io/gitea/services/repository/files"

"github.com/go-enry/go-enry/v2"
)
Expand Down Expand Up @@ -52,3 +54,29 @@ func isExcludedEntry(entry *git.TreeEntry) bool {

return false
}

func Tree(ctx *context.Context) {
treePath := ctx.PathParam("*")
ref := ctx.FormTrim("ref")
recursive := ctx.FormBool("recursive")

gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, ctx.Repo.Repository)
lunny marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
ctx.ServerError("RepositoryFromContextOrOpen", err)
return
}
defer closer.Close()

refName := gitRepo.UnstableGuessRefByShortName(ref)
lunny marked this conversation as resolved.
Show resolved Hide resolved
var results []*files_service.TreeEntry
if !recursive {
results, err = files_service.GetTreeList(ctx, ctx.Repo.Repository, treePath, refName, false)
} else {
results, err = files_service.GetTreeInformation(ctx, ctx.Repo.Repository, treePath, refName)
}
if err != nil {
ctx.ServerError("GetTreeInformation", err)
return
}
ctx.JSON(http.StatusOK, results)
}
13 changes: 7 additions & 6 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ import (
)

const (
tplRepoEMPTY templates.TplName = "repo/empty"
tplRepoHome templates.TplName = "repo/home"
tplRepoViewList templates.TplName = "repo/view_list"
tplWatchers templates.TplName = "repo/watchers"
tplForks templates.TplName = "repo/forks"
tplMigrating templates.TplName = "repo/migrate/migrating"
tplRepoEMPTY templates.TplName = "repo/empty"
tplRepoHome templates.TplName = "repo/home"
tplRepoHomeContent templates.TplName = "repo/home_content"
tplRepoViewList templates.TplName = "repo/view_list"
tplWatchers templates.TplName = "repo/watchers"
tplForks templates.TplName = "repo/forks"
tplMigrating templates.TplName = "repo/migrate/migrating"
)

type fileInfo struct {
Expand Down
21 changes: 20 additions & 1 deletion routers/web/repo/view_home.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"html/template"
"net/http"
"path"
"strconv"
"strings"
"time"

Expand All @@ -17,6 +18,7 @@ import (
access_model "code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo"
unit_model "code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
repo_module "code.gitea.io/gitea/modules/repository"
Expand Down Expand Up @@ -323,6 +325,19 @@ func Home(ctx *context.Context) {
return
}

showFileViewTreeSidebar := true
if ctx.Doer != nil {
v, err := user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyShowFileViewTreeSidebar, "true")
if err != nil {
log.Error("GetUserSetting: %v", err)
} else {
showFileViewTreeSidebar, _ = strconv.ParseBool(v)
}
}
ctx.Data["RepoPreferences"] = &preferencesForm{
ShowFileViewTreeSidebar: showFileViewTreeSidebar,
}

title := ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name
if len(ctx.Repo.Repository.Description) > 0 {
title += ": " + ctx.Repo.Repository.Description
Expand Down Expand Up @@ -392,5 +407,9 @@ func Home(ctx *context.Context) {
}
}

ctx.HTML(http.StatusOK, tplRepoHome)
if ctx.FormBool("only_content") {
ctx.HTML(http.StatusOK, tplRepoHomeContent)
} else {
ctx.HTML(http.StatusOK, tplRepoHome)
}
}
7 changes: 6 additions & 1 deletion routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ func registerRoutes(m *web.Router) {
m.Get("/migrate", repo.Migrate)
m.Post("/migrate", web.Bind(forms.MigrateRepoForm{}), repo.MigratePost)
m.Get("/search", repo.SearchRepo)
m.Put("/preferences", repo.UpdatePreferences)
}, reqSignIn)
// end "/repo": create, migrate, search

Expand Down Expand Up @@ -1156,11 +1157,15 @@ func registerRoutes(m *web.Router) {

m.Group("/{username}/{reponame}", func() {
m.Get("/find/*", repo.FindFiles)
m.Group("/tree-list", func() {
m.Group("/tree-list", func() { // for find files
m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.TreeList)
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.TreeList)
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.TreeList)
})
m.Group("/tree", func() {
m.Get("", repo.Tree)
m.Get("/*", repo.Tree)
})
m.Get("/compare", repo.MustBeNotEmpty, repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff)
m.Combo("/compare/*", repo.MustBeNotEmpty, repo.SetEditorconfigIfExists).
Get(repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff).
Expand Down
18 changes: 18 additions & 0 deletions services/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
unit_model "code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
Expand Down Expand Up @@ -743,6 +744,21 @@ const (
RepoRefBlob
)

func RefTypeName(refType RepoRefType) string {
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
switch refType {
case RepoRefBranch:
return "branch"
case RepoRefTag:
return "tag"
case RepoRefCommit:
return "commit"
case RepoRefBlob:
return "blob"
default:
return "unknown"
}
}

const headRefName = "HEAD"

// RepoRef handles repository reference names when the ref name is not
Expand Down Expand Up @@ -988,6 +1004,8 @@ func RepoRefByType(detectRefType RepoRefType, opts ...RepoRefByTypeOptions) func

ctx.Data["BranchName"] = ctx.Repo.BranchName
ctx.Data["RefName"] = ctx.Repo.RefName
ctx.Data["RefShortName"] = util.Iif(refType == RepoRefCommit, base.ShortSha(ctx.Repo.RefName), ctx.Repo.RefName)
ctx.Data["RefType"] = RefTypeName(refType)
lunny marked this conversation as resolved.
Show resolved Hide resolved
ctx.Data["BranchNameSubURL"] = ctx.Repo.BranchNameSubURL()
ctx.Data["TagName"] = ctx.Repo.TagName
ctx.Data["CommitID"] = ctx.Repo.CommitID
Expand Down
Loading
Loading