Skip to content

Add file tree to file view page #32721

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

Merged
merged 136 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
136 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
4f90719
Merge branch 'main' into add-file-tree-to-file-view-page
kerwin612 Jan 14, 2025
bdd78df
Use the old infrustructure about getting refullname
lunny Jan 14, 2025
fe65658
Merge branch 'add-file-tree-to-file-view-page' of github.com:kerwin61…
lunny Jan 14, 2025
459edfe
fix
kerwin612 Jan 14, 2025
9400d52
fix
kerwin612 Jan 14, 2025
abf95a7
fix
kerwin612 Jan 14, 2025
9dfdb4f
improvement
lunny Jan 14, 2025
b1193e0
fix
kerwin612 Jan 14, 2025
61be52b
finish submodule support
lunny Jan 14, 2025
ca964c1
Merge branch 'main' into kerwin612-add-file-tree-to-file-view-page
lunny Jan 14, 2025
76c82b2
fix
kerwin612 Jan 15, 2025
c06f30d
Merge branch 'main' into add-file-tree-to-file-view-page
kerwin612 Jan 15, 2025
20198ea
fix
kerwin612 Jan 15, 2025
2d644fb
make template simpler
lunny Jan 15, 2025
bb82979
remove duplicated code
lunny Jan 15, 2025
0aa1b86
Merge branch 'main' into add-file-tree-to-file-view-page
kerwin612 Jan 16, 2025
7179916
fix
kerwin612 Jan 16, 2025
40f8de2
Merge branch 'main' into add-file-tree-to-file-view-page
kerwin612 Feb 5, 2025
e632ba8
Merge branch 'main' into add-file-tree-to-file-view-page
kerwin612 Feb 19, 2025
dccffe2
Merge branch 'main' into add-file-tree-to-file-view-page
kerwin612 Feb 20, 2025
a0f69ec
fix
kerwin612 Feb 20, 2025
1136900
Merge branch 'main' into add-file-tree-to-file-view-page
lunny Feb 20, 2025
c14f7a7
merge main
kerwin612 Mar 3, 2025
bf83b2a
Revert some changes, and plan to complete the adaptation of the funct…
kerwin612 Mar 3, 2025
5ce1d58
Merge branch 'upstream_main' into add-file-tree-to-file-view-page
kerwin612 Mar 3, 2025
775d66b
Merge branch 'upstream_main' into add-file-tree-to-file-view-page
kerwin612 Mar 4, 2025
2d5a570
fix
kerwin612 Mar 4, 2025
9fc2ba3
Refactor initViewFileTreeSidebar using the new registerGlobalInitFunc
kerwin612 Mar 4, 2025
a212ff6
fix
kerwin612 Mar 4, 2025
605c5d4
fix
kerwin612 Mar 4, 2025
3cbcd95
fix
kerwin612 Mar 4, 2025
6f2de3e
fix
kerwin612 Mar 4, 2025
31835f4
fix
kerwin612 Mar 5, 2025
3bc3cd8
no window var
wxiaoguang Mar 5, 2025
7db2e20
Merge branch 'main' into add-file-tree-to-file-view-page
wxiaoguang Mar 6, 2025
b9eef42
fix
wxiaoguang Mar 6, 2025
2456a2b
dead code
wxiaoguang Mar 6, 2025
46ab64b
Revert: [Refactor initViewFileTreeSidebar using the new registerGloba…
kerwin612 Mar 10, 2025
1211fcf
fix
kerwin612 Mar 10, 2025
ce6afd3
fix
kerwin612 Mar 10, 2025
9403372
fix
kerwin612 Mar 10, 2025
3b41bea
Split the original repo/home.tmpl into two templates: home and view.
kerwin612 Mar 10, 2025
e761360
Change the layout of the view page from CSS Grid to Flexbox
kerwin612 Mar 10, 2025
f78bdaf
Merge branch 'main' into add-file-tree-to-file-view-page
kerwin612 Mar 10, 2025
daa8282
fix
kerwin612 Mar 11, 2025
c36e6ad
Merge branch 'main' into add-file-tree-to-file-view-page
wxiaoguang Mar 14, 2025
1e765c5
Merge branch 'add-file-tree-to-file-view-page' of github.com:kerwin61…
lunny Mar 14, 2025
4eac502
Some improvements
lunny Mar 14, 2025
b1dde04
clean up
wxiaoguang Mar 15, 2025
14f914c
clean up
wxiaoguang Mar 15, 2025
b63ceb3
fix tree node
wxiaoguang Mar 15, 2025
692c62a
clean up
wxiaoguang Mar 15, 2025
40b61f3
clean up
wxiaoguang Mar 15, 2025
6cab203
clean up
wxiaoguang Mar 15, 2025
8818416
clean up
wxiaoguang Mar 15, 2025
a585ab7
clean up
wxiaoguang Mar 15, 2025
84e2d70
fix nav bug
wxiaoguang Mar 15, 2025
19e23da
fix vue ref
wxiaoguang Mar 15, 2025
1e5bec6
Merge branch 'main' into add-file-tree-to-file-view-page
wxiaoguang Mar 15, 2025
c9e8f8e
fix test
wxiaoguang Mar 15, 2025
b9357a2
fix test
wxiaoguang Mar 15, 2025
7963ebd
fix gogit
wxiaoguang Mar 15, 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: 3 additions & 0 deletions models/user/setting_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
SettingsKeyDiffWhitespaceBehavior = "diff.whitespace_behaviour"
// SettingsKeyShowOutdatedComments is the setting key wether or not to show outdated comments in PRs
SettingsKeyShowOutdatedComments = "comment_code.show_outdated"

// UserActivityPubPrivPem is user's private key
UserActivityPubPrivPem = "activitypub.priv_pem"
// UserActivityPubPubPem is user's public key
Expand All @@ -18,4 +19,6 @@ const (
SignupIP = "signup.ip"
// SignupUserAgent is the user agent that the user signed up with
SignupUserAgent = "signup.user_agent"

SettingsKeyCodeViewShowFileTree = "code_view.show_file_tree"
)
2 changes: 1 addition & 1 deletion modules/git/parse_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func ParseTreeEntries(data []byte) ([]*TreeEntry, error) {
return parseTreeEntries(data, nil)
}

// parseTreeEntries FIXME this function's design is not right, it should make the caller read all data into memory
// parseTreeEntries FIXME this function's design is not right, it should not make the caller read all data into memory
func parseTreeEntries(data []byte, ptree *Tree) ([]*TreeEntry, error) {
entries := make([]*TreeEntry, 0, bytes.Count(data, []byte{'\n'})+1)
for pos := 0; pos < len(data); {
Expand Down
1 change: 1 addition & 0 deletions modules/git/tree_blob_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (t *Tree) GetTreeEntryByPath(relpath string) (*TreeEntry, error) {
return &TreeEntry{
ID: t.ID,
// Type: ObjectTree,
ptree: t,
gogitTreeEntry: &object.TreeEntry{
Name: "",
Mode: filemode.Dir,
Expand Down
60 changes: 22 additions & 38 deletions routers/web/repo/blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,60 +41,45 @@ type blameRow struct {

// RefBlame render blame page
func RefBlame(ctx *context.Context) {
fileName := ctx.Repo.TreePath
if len(fileName) == 0 {
ctx.Data["PageIsViewCode"] = true
ctx.Data["IsBlame"] = true

// Get current entry user currently looking at.
if ctx.Repo.TreePath == "" {
ctx.NotFound(nil)
return
}

branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.RefTypeNameSubURL()
treeLink := branchLink
rawLink := ctx.Repo.RepoLink + "/raw/" + ctx.Repo.RefTypeNameSubURL()

if len(ctx.Repo.TreePath) > 0 {
treeLink += "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
}

var treeNames []string
paths := make([]string, 0, 5)
if len(ctx.Repo.TreePath) > 0 {
treeNames = strings.Split(ctx.Repo.TreePath, "/")
for i := range treeNames {
paths = append(paths, strings.Join(treeNames[:i+1], "/"))
}

ctx.Data["HasParentPath"] = true
if len(paths)-2 >= 0 {
ctx.Data["ParentPath"] = "/" + paths[len(paths)-1]
}
}

// Get current entry user currently looking at.
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
if err != nil {
HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err)
return
}

blob := entry.Blob()
treeNames := strings.Split(ctx.Repo.TreePath, "/")
var paths []string
for i := range treeNames {
paths = append(paths, strings.Join(treeNames[:i+1], "/"))
}

ctx.Data["Paths"] = paths
ctx.Data["TreeLink"] = treeLink
ctx.Data["TreeNames"] = treeNames
ctx.Data["BranchLink"] = branchLink

ctx.Data["RawFileLink"] = rawLink + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
ctx.Data["PageIsViewCode"] = true

ctx.Data["IsBlame"] = true
ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.RefTypeNameSubURL()
ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/raw/" + ctx.Repo.RefTypeNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)

blob := entry.Blob()
fileSize := blob.Size()
ctx.Data["FileSize"] = fileSize
ctx.Data["FileName"] = blob.Name()

tplName := tplRepoViewContent
if !ctx.FormBool("only_content") {
prepareHomeTreeSideBarSwitch(ctx)
tplName = tplRepoView
}

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

Expand All @@ -105,8 +90,7 @@ func RefBlame(ctx *context.Context) {
}

bypassBlameIgnore, _ := strconv.ParseBool(ctx.FormString("bypass-blame-ignore"))

result, err := performBlame(ctx, ctx.Repo.Repository, ctx.Repo.Commit, fileName, bypassBlameIgnore)
result, err := performBlame(ctx, ctx.Repo.Repository, ctx.Repo.Commit, ctx.Repo.TreePath, bypassBlameIgnore)
if err != nil {
ctx.NotFound(err)
return
Expand All @@ -122,7 +106,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
10 changes: 10 additions & 0 deletions routers/web/repo/treelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/gitdiff"
files_service "code.gitea.io/gitea/services/repository/files"

"github.com/go-enry/go-enry/v2"
)
Expand Down Expand Up @@ -84,3 +85,12 @@ func transformDiffTreeForUI(diffTree *gitdiff.DiffTree, filesViewedState map[str

return files
}

func TreeViewNodes(ctx *context.Context) {
results, err := files_service.GetTreeViewNodes(ctx, ctx.Repo.Commit, ctx.Repo.TreePath, ctx.FormString("sub_path"))
if err != nil {
ctx.ServerError("GetTreeViewNodes", err)
return
}
ctx.JSON(http.StatusOK, map[string]any{"fileTreeNodes": results})
}
14 changes: 8 additions & 6 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ 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"
tplRepoView templates.TplName = "repo/view"
tplRepoViewContent templates.TplName = "repo/view_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
25 changes: 24 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 @@ -328,6 +330,19 @@ func handleRepoHomeFeed(ctx *context.Context) bool {
return true
}

func prepareHomeTreeSideBarSwitch(ctx *context.Context) {
showFileTree := true
if ctx.Doer != nil {
v, err := user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyCodeViewShowFileTree, "true")
if err != nil {
log.Error("GetUserSetting: %v", err)
} else {
showFileTree, _ = strconv.ParseBool(v)
}
}
ctx.Data["UserSettingCodeViewShowFileTree"] = showFileTree
}

// Home render repository home page
func Home(ctx *context.Context) {
if handleRepoHomeFeed(ctx) {
Expand All @@ -341,6 +356,8 @@ func Home(ctx *context.Context) {
return
}

prepareHomeTreeSideBarSwitch(ctx)

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 @@ -410,7 +427,13 @@ func Home(ctx *context.Context) {
}
}

ctx.HTML(http.StatusOK, tplRepoHome)
if ctx.FormBool("only_content") {
ctx.HTML(http.StatusOK, tplRepoViewContent)
} else if len(treeNames) != 0 {
ctx.HTML(http.StatusOK, tplRepoView)
} else {
ctx.HTML(http.StatusOK, tplRepoHome)
}
}

func RedirectRepoTreeToSrc(ctx *context.Context) {
Expand Down
26 changes: 26 additions & 0 deletions routers/web/user/setting/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package setting

import (
"net/http"
"strconv"

user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/services/context"
)

func UpdatePreferences(ctx *context.Context) {
type preferencesForm struct {
CodeViewShowFileTree bool `json:"codeViewShowFileTree"`
}
form := &preferencesForm{}
if err := json.NewDecoder(ctx.Req.Body).Decode(&form); err != nil {
ctx.HTTPError(http.StatusBadRequest, "json decode failed")
return
}
_ = user_model.SetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyCodeViewShowFileTree, strconv.FormatBool(form.CodeViewShowFileTree))
ctx.JSONOK()
}
6 changes: 6 additions & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ func registerRoutes(m *web.Router) {
m.Group("/user/settings", func() {
m.Get("", user_setting.Profile)
m.Post("", web.Bind(forms.UpdateProfileForm{}), user_setting.ProfilePost)
m.Post("/update_preferences", user_setting.UpdatePreferences)
m.Get("/change_password", auth.MustChangePassword)
m.Post("/change_password", web.Bind(forms.MustChangePasswordForm{}), auth.MustChangePasswordPost)
m.Post("/avatar", web.Bind(forms.AvatarForm{}), user_setting.AvatarPost)
Expand Down Expand Up @@ -1175,6 +1176,11 @@ func registerRoutes(m *web.Router) {
m.Get("/tag/*", context.RepoRefByType(git.RefTypeTag), repo.TreeList)
m.Get("/commit/*", context.RepoRefByType(git.RefTypeCommit), repo.TreeList)
})
m.Group("/tree-view", func() {
m.Get("/branch/*", context.RepoRefByType(git.RefTypeBranch), repo.TreeViewNodes)
m.Get("/tag/*", context.RepoRefByType(git.RefTypeTag), repo.TreeViewNodes)
m.Get("/commit/*", context.RepoRefByType(git.RefTypeCommit), repo.TreeViewNodes)
})
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
26 changes: 15 additions & 11 deletions services/contexttest/context_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/cache"
git_module "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/reqctx"
"code.gitea.io/gitea/modules/session"
Expand All @@ -30,6 +31,7 @@ import (

"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func mockRequest(t *testing.T, reqPath string) *http.Request {
Expand Down Expand Up @@ -85,7 +87,7 @@ func MockAPIContext(t *testing.T, reqPath string) (*context.APIContext, *httptes
base := context.NewBaseContext(resp, req)
base.Data = middleware.GetContextData(req.Context())
base.Locale = &translation.MockLocale{}
ctx := &context.APIContext{Base: base}
ctx := &context.APIContext{Base: base, Repo: &context.Repository{}}
chiCtx := chi.NewRouteContext()
ctx.SetContextValue(chi.RouteCtxKey, chiCtx)
return ctx, resp
Expand All @@ -106,13 +108,13 @@ func MockPrivateContext(t *testing.T, reqPath string) (*context.PrivateContext,
// LoadRepo load a repo into a test context.
func LoadRepo(t *testing.T, ctx gocontext.Context, repoID int64) {
var doer *user_model.User
repo := &context.Repository{}
var repo *context.Repository
switch ctx := ctx.(type) {
case *context.Context:
ctx.Repo = repo
repo = ctx.Repo
doer = ctx.Doer
case *context.APIContext:
ctx.Repo = repo
repo = ctx.Repo
doer = ctx.Doer
default:
assert.FailNow(t, "context is not *context.Context or *context.APIContext")
Expand Down Expand Up @@ -140,15 +142,17 @@ func LoadRepoCommit(t *testing.T, ctx gocontext.Context) {
}

gitRepo, err := gitrepo.OpenRepository(ctx, repo.Repository)
assert.NoError(t, err)
require.NoError(t, err)
defer gitRepo.Close()
branch, err := gitRepo.GetHEADBranch()
assert.NoError(t, err)
assert.NotNil(t, branch)
if branch != nil {
repo.Commit, err = gitRepo.GetBranchCommit(branch.Name)
assert.NoError(t, err)

if repo.RefFullName == "" {
repo.RefFullName = git_module.RefNameFromBranch(repo.Repository.DefaultBranch)
}
if repo.RefFullName.IsPull() {
repo.BranchName = repo.RefFullName.ShortName()
}
repo.Commit, err = gitRepo.GetCommit(repo.RefFullName.String())
require.NoError(t, err)
}

// LoadUser load a user into a test context
Expand Down
Loading