From d83aecd2e4adf9ad62abe5e6ee600904c3552c64 Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Tue, 16 Mar 2021 17:34:24 +0800 Subject: [PATCH 01/28] Create finding files page ui in repo page --- options/locale/locale_en-US.ini | 1 + routers/repo/find.go | 58 +++++++++++++++++++++++++++++++++ routers/routes/web.go | 1 + templates/repo/find/files.tmpl | 52 +++++++++++++++++++++++++++++ templates/repo/home.tmpl | 5 +++ 5 files changed, 117 insertions(+) create mode 100644 routers/repo/find.go create mode 100644 templates/repo/find/files.tmpl diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 0ee8e7ab0c33..9df50a341ebc 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2564,6 +2564,7 @@ publish_release = `released "%[4]s" at %[4]s for %[3]s#%[2]s` review_dismissed_reason = Reason: create_branch = created branch %[3]s in %[4]s +find_file = Go to file [tool] ago = %s ago diff --git a/routers/repo/find.go b/routers/repo/find.go new file mode 100644 index 000000000000..f6f153c0eff3 --- /dev/null +++ b/routers/repo/find.go @@ -0,0 +1,58 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package repo + +import ( + "fmt" + + _ "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/base" + "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/context" +) + +const ( + tplFindFiles base.TplName = "repo/find/files" +) + +// render the page to find repository files +func FindFiles(ctx *context.Context) { + ctx.Data["Title"] = ctx.Tr("repo.find") + ctx.Data["PageIsFindFiles"] = true + ctx.Data["PageIsViewCode"] = true + + fmt.Printf("ctx.Repo.RepoLink: %v\n", ctx.Repo.RepoLink) + branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL() + treeLink := branchLink + + if len(ctx.Repo.TreePath) > 0 { + treeLink += "/" + ctx.Repo.TreePath + } + + // Get current entry user currently looking at. + entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath) + if err != nil { + ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err) + return + } + + if entry.IsDir() { + renderDirectory(ctx, treeLink) + } else { + ctx.Data["Files"] = make([]interface{}, 0) + } + + if ctx.Written() { + return + } + + fmt.Printf("ctx.files: %v\n", ctx.Data["Files"]) + + ctx.Data["RepoLink"] = ctx.Repo.RepoLink + ctx.Data["RepoName"] = ctx.Repo.Repository.Name + ctx.Data["TreeLink"] = treeLink + + ctx.HTML(200, tplFindFiles) +} \ No newline at end of file diff --git a/routers/routes/web.go b/routers/routes/web.go index 0130009059f4..79b4e6b40e31 100644 --- a/routers/routes/web.go +++ b/routers/routes/web.go @@ -762,6 +762,7 @@ func RegisterRoutes(m *web.Route) { m.Combo("/compare/*", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists). Get(ignSignIn, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff). Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, bindIgnErr(auth.CreateIssueForm{}), repo.SetWhitespaceBehavior, repo.CompareAndPullRequestPost) + m.Get("/find/{branch}", context.RepoRefByType(context.RepoRefBranch), repo.FindFiles) }, context.RepoAssignment(), context.UnitTypes()) // Grouping for those endpoints that do require authentication diff --git a/templates/repo/find/files.tmpl b/templates/repo/find/files.tmpl new file mode 100644 index 000000000000..050ef6b38cc8 --- /dev/null +++ b/templates/repo/find/files.tmpl @@ -0,0 +1,52 @@ +{{template "base/head" .}} +
+ {{template "repo/header" .}} +
+
+ {{.RepoName}} + / + +
+ + + {{range $item := .Files}} + {{$entry := $item.Entry}} + {{$commit := $item.Commit}} + {{$subModuleFile := $item.SubModuleFile}} + + + + {{end}} + +
+ + {{if $entry.IsSubModule}} + {{svg "octicon-file-submodule"}} + {{$refURL := $subModuleFile.RefURL AppUrl $.Repository.FullName $.SSHDomain}} + {{if $refURL}} + {{$entry.Name}}@{{ShortSha $subModuleFile.RefID}} + {{else}} + {{$entry.Name}}@{{ShortSha $subModuleFile.RefID}} + {{end}} + {{else}} + {{if $entry.IsDir}} + {{$subJumpablePathName := $entry.GetSubJumpablePathName}} + {{$subJumpablePath := SubJumpablePath $subJumpablePathName}} + {{svg "octicon-file-directory"}} + + {{if eq (len $subJumpablePath) 2}} + {{index $subJumpablePath 0}}{{index $subJumpablePath 1}} + {{else}} + {{index $subJumpablePath 0}} + {{end}} + + {{else}} + {{svg (printf "octicon-%s" (EntryIcon $entry))}} + {{$entry.Name}} + {{end}} + {{end}} + +
+
+
+{{template "base/footer" .}} diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl index 4d8d28921172..c08e57c40e78 100644 --- a/templates/repo/home.tmpl +++ b/templates/repo/home.tmpl @@ -69,6 +69,11 @@ {{end}} +
+ + + +
{{else}}
{{EllipsisString .Repository.Name 30}}{{range $i, $v := .TreeNames}}/{{if eq $i $l}}{{EllipsisString $v 30}}{{else}}{{ $p := index $.Paths $i}}{{EllipsisString $v 30}}{{end}}{{end}}
{{end}} From efe69d49e96ab73e69c3247c9206dba573498f13 Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Tue, 16 Mar 2021 23:39:23 +0800 Subject: [PATCH 02/28] Modify router --- routers/routes/web.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/routes/web.go b/routers/routes/web.go index 79b4e6b40e31..34db426cbd03 100644 --- a/routers/routes/web.go +++ b/routers/routes/web.go @@ -762,7 +762,7 @@ func RegisterRoutes(m *web.Route) { m.Combo("/compare/*", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists). Get(ignSignIn, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff). Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, bindIgnErr(auth.CreateIssueForm{}), repo.SetWhitespaceBehavior, repo.CompareAndPullRequestPost) - m.Get("/find/{branch}", context.RepoRefByType(context.RepoRefBranch), repo.FindFiles) + m.Get("/find/*", context.RepoRefByType(context.RepoRefBranch), repo.FindFiles) }, context.RepoAssignment(), context.UnitTypes()) // Grouping for those endpoints that do require authentication From c4e3195f5ae6a14f8582560595f475937143ddb4 Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Wed, 17 Mar 2021 22:34:58 +0800 Subject: [PATCH 03/28] Get tree entries for find repo files. --- routers/repo/find.go | 42 +++++++++++++++++++--------------- templates/repo/find/files.tmpl | 32 +++----------------------- 2 files changed, 27 insertions(+), 47 deletions(-) diff --git a/routers/repo/find.go b/routers/repo/find.go index f6f153c0eff3..2eab4876b11f 100644 --- a/routers/repo/find.go +++ b/routers/repo/find.go @@ -5,8 +5,6 @@ package repo import ( - "fmt" - _ "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/git" @@ -19,11 +17,9 @@ const ( // render the page to find repository files func FindFiles(ctx *context.Context) { - ctx.Data["Title"] = ctx.Tr("repo.find") ctx.Data["PageIsFindFiles"] = true ctx.Data["PageIsViewCode"] = true - fmt.Printf("ctx.Repo.RepoLink: %v\n", ctx.Repo.RepoLink) branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL() treeLink := branchLink @@ -31,28 +27,38 @@ func FindFiles(ctx *context.Context) { treeLink += "/" + ctx.Repo.TreePath } - // Get current entry user currently looking at. - entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath) - if err != nil { - ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err) - return - } - - if entry.IsDir() { - renderDirectory(ctx, treeLink) - } else { - ctx.Data["Files"] = make([]interface{}, 0) - } + renderFiles(ctx, treeLink) if ctx.Written() { return } - fmt.Printf("ctx.files: %v\n", ctx.Data["Files"]) - ctx.Data["RepoLink"] = ctx.Repo.RepoLink ctx.Data["RepoName"] = ctx.Repo.Repository.Name ctx.Data["TreeLink"] = treeLink ctx.HTML(200, tplFindFiles) +} + +func renderFiles(ctx *context.Context, treeLink string) { + tree, err := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath) + if err != nil { + ctx.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err) + return + } + + entries, err := tree.ListEntriesRecursive() + if err != nil { + ctx.ServerError("ListEntries", err) + return + } + entries.CustomSort(base.NaturalSortLess) + + var fileEntries []*git.TreeEntry + for _, entry := range entries { + if !entry.IsDir() { + fileEntries = append(fileEntries, entry) + } + } + ctx.Data["Files"] = fileEntries } \ No newline at end of file diff --git a/templates/repo/find/files.tmpl b/templates/repo/find/files.tmpl index 050ef6b38cc8..91981a46b1f3 100644 --- a/templates/repo/find/files.tmpl +++ b/templates/repo/find/files.tmpl @@ -9,38 +9,12 @@ - {{range $item := .Files}} - {{$entry := $item.Entry}} - {{$commit := $item.Commit}} - {{$subModuleFile := $item.SubModuleFile}} + {{range $entry := .Files}} From ea4110f6ceec5d8cf220e4c6569b7fbbea75ff4c Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Thu, 18 Mar 2021 22:25:19 +0800 Subject: [PATCH 04/28] Conplete find files in repo. --- options/locale/locale_en-US.ini | 1 + routers/repo/find.go | 2 +- templates/repo/find/files.tmpl | 9 ++-- web_src/js/index.js | 75 +++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 4 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 9df50a341ebc..8277ee60c373 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -735,6 +735,7 @@ delete_preexisting_label = Delete delete_preexisting = Delete pre-existing files delete_preexisting_content = Delete files in %s delete_preexisting_success = Deleted unadopted files in %s +filter_repo_files_no_matching = No matching files found transfer.accept = Accept Transfer transfer.accept_desc = Transfer to "%s" diff --git a/routers/repo/find.go b/routers/repo/find.go index 2eab4876b11f..8954ef7651c0 100644 --- a/routers/repo/find.go +++ b/routers/repo/find.go @@ -56,7 +56,7 @@ func renderFiles(ctx *context.Context, treeLink string) { var fileEntries []*git.TreeEntry for _, entry := range entries { - if !entry.IsDir() { + if !entry.IsDir() && !entry.IsSubModule() { fileEntries = append(fileEntries, entry) } } diff --git a/templates/repo/find/files.tmpl b/templates/repo/find/files.tmpl index 91981a46b1f3..a480dc5efe47 100644 --- a/templates/repo/find/files.tmpl +++ b/templates/repo/find/files.tmpl @@ -5,22 +5,25 @@ -
- {{if $entry.IsSubModule}} - {{svg "octicon-file-submodule"}} - {{$refURL := $subModuleFile.RefURL AppUrl $.Repository.FullName $.SSHDomain}} - {{if $refURL}} - {{$entry.Name}}@{{ShortSha $subModuleFile.RefID}} - {{else}} - {{$entry.Name}}@{{ShortSha $subModuleFile.RefID}} - {{end}} - {{else}} - {{if $entry.IsDir}} - {{$subJumpablePathName := $entry.GetSubJumpablePathName}} - {{$subJumpablePath := SubJumpablePath $subJumpablePathName}} - {{svg "octicon-file-directory"}} - - {{if eq (len $subJumpablePath) 2}} - {{index $subJumpablePath 0}}{{index $subJumpablePath 1}} - {{else}} - {{index $subJumpablePath 0}} - {{end}} - - {{else}} - {{svg (printf "octicon-%s" (EntryIcon $entry))}} - {{$entry.Name}} - {{end}} - {{end}} + {{svg (printf "octicon-%s" (EntryIcon $entry))}} + {{$entry.Name}}
+
{{range $entry := .Files}} {{end}}
{{svg (printf "octicon-%s" (EntryIcon $entry))}} - {{$entry.Name}} + {{$entry.Name}}
+ {{template "base/footer" .}} diff --git a/web_src/js/index.js b/web_src/js/index.js index cda08870a5f7..b0379a86650b 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -2167,6 +2167,80 @@ function searchRepositories() { }); } +function hitAllKeys(keys, entry) { + let i = 0; + let j = 0; + const hitIndexes = []; + while (i < keys.length) { + for (; j < entry.length; j++) { + if (keys[i] === entry[j]) { + hitIndexes.push(j); + j++; + break; + } + } + + i++; + } + + return hitIndexes; +} + +function addHighLightToHit($a, entry, indexes) { + let highLightText = ''; + for (let i = 0; i < entry.length; i++) { + if (indexes.includes(i)) { + highLightText += `${entry[i]}`; + } else { + highLightText += entry[i]; + } + } + $a.html(highLightText); +} + +function removeHighLight($a, entry) { + $a.text(entry.replace(/(<([^>]+)>)/ig, '')); +} + +function filterRepoFiles(keys) { + if (keys.length > 0) { + let hit = false; + $('#repo-find-files-table tr').each(function() { + const entry = $(this).find('td:first').text(); + const $a = $(this).find('td:first').find('a:first'); + const hitIndexes = hitAllKeys(keys.trim(), entry.trim()); + if (hitIndexes.length > 0 && keys.trim().length === hitIndexes.length) { + addHighLightToHit($a, entry.trim(), hitIndexes); + $(this).show(); + hit = true; + } else { + removeHighLight($a, entry.trim()); + $(this).hide(); + } + }); + if (hit) { + $('#no-hit-prompt').hide(); + } else { + $('#no-hit-prompt').show(); + } + } else { + // remove all high light + $('#repo-find-files-table tr').each(function() { + const entry = $(this).find('td:first').text(); + const $a = $(this).find('td:first').find('a:first'); + removeHighLight($a, entry.trim()); + }); + $('#no-hit-prompt').hide(); + $('#repo-find-files-table tr').show(); + } +} + +function initFindFileInRepo() { + $('#repo-file-find-input').on('change paste keyup', () => { + filterRepoFiles($('#repo-file-find-input').val()); + }); +} + function initCodeView() { if ($('.code-view .lines-num').length > 0) { $(document).on('click', '.lines-num span', function (e) { @@ -2689,6 +2763,7 @@ $(document).ready(async () => { initNotificationsTable(); initPullRequestMergeInstruction(); initReleaseEditor(); + initFindFileInRepo(); const routes = { 'div.user.settings': initUserSettings, From 2d6a36b1edd6ababd8b186b860591ba4a42f7029 Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Thu, 18 Mar 2021 22:28:40 +0800 Subject: [PATCH 05/28] Make fmt --- routers/repo/find.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/routers/repo/find.go b/routers/repo/find.go index 8954ef7651c0..66912a5dcfd8 100644 --- a/routers/repo/find.go +++ b/routers/repo/find.go @@ -7,8 +7,8 @@ package repo import ( _ "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" - "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/git" ) const ( @@ -53,12 +53,12 @@ func renderFiles(ctx *context.Context, treeLink string) { return } entries.CustomSort(base.NaturalSortLess) - + var fileEntries []*git.TreeEntry - for _, entry := range entries { + for _, entry := range entries { if !entry.IsDir() && !entry.IsSubModule() { fileEntries = append(fileEntries, entry) } } ctx.Data["Files"] = fileEntries -} \ No newline at end of file +} From 5ce4a6d8b6bf0084f0aeef50c83dc371a68476e5 Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Thu, 18 Mar 2021 22:49:23 +0800 Subject: [PATCH 06/28] Update comment. --- web_src/js/index.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/web_src/js/index.js b/web_src/js/index.js index afcce2e19dc4..304f32a61a82 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -2207,13 +2207,15 @@ function filterRepoFiles(keys) { $('#repo-find-files-table tr').each(function() { const entry = $(this).find('td:first').text(); const $a = $(this).find('td:first').find('a:first'); - const hitIndexes = hitAllKeys(keys.trim(), entry.trim()); - if (hitIndexes.length > 0 && keys.trim().length === hitIndexes.length) { - addHighLightToHit($a, entry.trim(), hitIndexes); + const keysTrim = keys.trim(); + const entryTrim = entry.trim(); + const hitIndexes = hitAllKeys(keysTrim, entryTrim); + if (hitIndexes.length > 0 && keysTrim.length === hitIndexes.length) { + addHighLightToHit($a, entryTrim, hitIndexes); $(this).show(); hit = true; } else { - removeHighLight($a, entry.trim()); + removeHighLight($a, entryTrim); $(this).hide(); } }); @@ -2223,7 +2225,7 @@ function filterRepoFiles(keys) { $('#no-hit-prompt').show(); } } else { - // remove all high light + // Remove all highlight $('#repo-find-files-table tr').each(function() { const entry = $(this).find('td:first').text(); const $a = $(this).find('td:first').find('a:first'); From 5239d409e7691af490723b09f8f2970cae1b4fcc Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Fri, 19 Mar 2021 11:35:31 +0800 Subject: [PATCH 07/28] Move find files JS to individual file. --- routers/repo/find.go | 1 - web_src/js/features/find.js | 74 ++++++++++++++++++++++++++++++++++++ web_src/js/index.js | 76 +------------------------------------ 3 files changed, 75 insertions(+), 76 deletions(-) create mode 100644 web_src/js/features/find.js diff --git a/routers/repo/find.go b/routers/repo/find.go index 66912a5dcfd8..4590c102e922 100644 --- a/routers/repo/find.go +++ b/routers/repo/find.go @@ -5,7 +5,6 @@ package repo import ( - _ "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" diff --git a/web_src/js/features/find.js b/web_src/js/features/find.js new file mode 100644 index 000000000000..dc7e8ab7dd60 --- /dev/null +++ b/web_src/js/features/find.js @@ -0,0 +1,74 @@ +function hitAllKeys(keys, entry) { + let i = 0; + let j = 0; + const hitIndexes = []; + while (i < keys.length) { + for (; j < entry.length; j++) { + if (keys[i] === entry[j]) { + hitIndexes.push(j); + j++; + break; + } + } + i++; + } + + return hitIndexes; +} + +function addHighLightToHit($a, entry, indexes) { + let highLightText = ''; + for (let i = 0; i < entry.length; i++) { + if (indexes.includes(i)) { + highLightText += `${entry[i]}`; + } else { + highLightText += entry[i]; + } + } + $a.html(highLightText); +} + +function removeHighLight($a, entry) { + $a.text(entry.replace(/(<([^>]+)>)/ig, '')); +} + +function filterRepoFiles(keys) { + if (keys.length > 0) { + let hit = false; + $('#repo-find-files-table tr').each(function() { + const entry = $(this).find('td:first').text(); + const $a = $(this).find('td:first').find('a:first'); + const keysTrim = keys.trim(); + const entryTrim = entry.trim(); + const hitIndexes = hitAllKeys(keysTrim, entryTrim); + if (hitIndexes.length > 0 && keysTrim.length === hitIndexes.length) { + addHighLightToHit($a, entryTrim, hitIndexes); + $(this).show(); + hit = true; + } else { + removeHighLight($a, entryTrim); + $(this).hide(); + } + }); + if (hit) { + $('#no-hit-prompt').hide(); + } else { + $('#no-hit-prompt').show(); + } + } else { + // Remove all highlight + $('#repo-find-files-table tr').each(function() { + const entry = $(this).find('td:first').text(); + const $a = $(this).find('td:first').find('a:first'); + removeHighLight($a, entry.trim()); + }); + $('#no-hit-prompt').hide(); + $('#repo-find-files-table tr').show(); + } +} + +export default function initFindFileInRepo() { + $('#repo-file-find-input').on('change paste keyup', () => { + filterRepoFiles($('#repo-file-find-input').val()); + }); +} diff --git a/web_src/js/index.js b/web_src/js/index.js index 304f32a61a82..fb910c509761 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -21,6 +21,7 @@ import createColorPicker from './features/colorpicker.js'; import createDropzone from './features/dropzone.js'; import initTableSort from './features/tablesort.js'; import initImageDiff from './features/imagediff.js'; +import initFindFileInRepo from './features/find.js'; import ActivityTopAuthors from './components/ActivityTopAuthors.vue'; import {initNotificationsTable, initNotificationCount} from './features/notification.js'; import {initStopwatch} from './features/stopwatch.js'; @@ -2167,81 +2168,6 @@ function searchRepositories() { }); } -function hitAllKeys(keys, entry) { - let i = 0; - let j = 0; - const hitIndexes = []; - while (i < keys.length) { - for (; j < entry.length; j++) { - if (keys[i] === entry[j]) { - hitIndexes.push(j); - j++; - break; - } - } - i++; - } - - return hitIndexes; -} - -function addHighLightToHit($a, entry, indexes) { - let highLightText = ''; - for (let i = 0; i < entry.length; i++) { - if (indexes.includes(i)) { - highLightText += `${entry[i]}`; - } else { - highLightText += entry[i]; - } - } - $a.html(highLightText); -} - -function removeHighLight($a, entry) { - $a.text(entry.replace(/(<([^>]+)>)/ig, '')); -} - -function filterRepoFiles(keys) { - if (keys.length > 0) { - let hit = false; - $('#repo-find-files-table tr').each(function() { - const entry = $(this).find('td:first').text(); - const $a = $(this).find('td:first').find('a:first'); - const keysTrim = keys.trim(); - const entryTrim = entry.trim(); - const hitIndexes = hitAllKeys(keysTrim, entryTrim); - if (hitIndexes.length > 0 && keysTrim.length === hitIndexes.length) { - addHighLightToHit($a, entryTrim, hitIndexes); - $(this).show(); - hit = true; - } else { - removeHighLight($a, entryTrim); - $(this).hide(); - } - }); - if (hit) { - $('#no-hit-prompt').hide(); - } else { - $('#no-hit-prompt').show(); - } - } else { - // Remove all highlight - $('#repo-find-files-table tr').each(function() { - const entry = $(this).find('td:first').text(); - const $a = $(this).find('td:first').find('a:first'); - removeHighLight($a, entry.trim()); - }); - $('#no-hit-prompt').hide(); - $('#repo-find-files-table tr').show(); - } -} - -function initFindFileInRepo() { - $('#repo-file-find-input').on('change paste keyup', () => { - filterRepoFiles($('#repo-file-find-input').val()); - }); -} - function showCodeViewMenu() { // Get clicked tr const $code_tr = $('.code-view td.lines-code.active').parent(); From 09fe0f4398446fca7e7a2552ea2e6fac0494a7c2 Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Fri, 19 Mar 2021 13:50:16 +0800 Subject: [PATCH 08/28] Fix go lint --- routers/repo/find.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/repo/find.go b/routers/repo/find.go index 4590c102e922..0389f7a574db 100644 --- a/routers/repo/find.go +++ b/routers/repo/find.go @@ -14,7 +14,7 @@ const ( tplFindFiles base.TplName = "repo/find/files" ) -// render the page to find repository files +// FindFiles render the page to find repository files func FindFiles(ctx *context.Context) { ctx.Data["PageIsFindFiles"] = true ctx.Data["PageIsViewCode"] = true From c7f622d757bc0d9274b86084bd0fa707e2b81586 Mon Sep 17 00:00:00 2001 From: rogerluo410 Date: Tue, 23 Mar 2021 22:56:29 +0800 Subject: [PATCH 09/28] Update find repo files. --- routers/api/v1/api.go | 1 + routers/api/v1/repo/find_files.go | 105 ++++++++++++++++++++++++ routers/repo/find.go | 35 +------- templates/repo/find/files.tmpl | 10 ++- web_src/js/features/find.js | 128 ++++++++++++++++++++++++------ web_src/js/svg.js | 2 + 6 files changed, 223 insertions(+), 58 deletions(-) create mode 100644 routers/api/v1/repo/find_files.go diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 60c4f6d790d5..8faa198b9fcc 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -752,6 +752,7 @@ func Routes() *web.Route { Delete(reqAdmin(), repo.DeleteTeam) }, reqToken()) m.Get("/raw/*", context.RepoRefForAPI, reqRepoReader(models.UnitTypeCode), repo.GetRawFile) + m.Get("/find/*", context.RepoRefForAPI, repo.GetRepoFiles) m.Get("/archive/*", reqRepoReader(models.UnitTypeCode), repo.GetArchive) m.Combo("/forks").Get(repo.ListForks). Post(reqToken(), reqRepoReader(models.UnitTypeCode), bind(api.CreateForkOption{}), repo.CreateFork) diff --git a/routers/api/v1/repo/find_files.go b/routers/api/v1/repo/find_files.go new file mode 100644 index 000000000000..7725fb65e9a7 --- /dev/null +++ b/routers/api/v1/repo/find_files.go @@ -0,0 +1,105 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package repo + +import ( + "fmt" + "net/http" + "regexp" + "strings" + + "code.gitea.io/gitea/modules/base" + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/git" +) + +var excludedDirs = []string{ + "vendor", + "build", + "log", + "tmp", +} + +// GetRepoFiles get all files' entries of a repository +func GetRepoFiles(ctx *context.APIContext) { + // swagger:operation GET /repos/{owner}/{repo}/find/{branchname} repository repoFindFiles + // --- + // summary: Get all files' entries of a repository + // produces: + // - application/json + // parameters: + // - name: owner + // in: path + // description: owner of the repo + // type: string + // required: true + // - name: repo + // in: path + // description: name of the repo + // type: string + // required: true + // - name: branchname + // in: path + // description: branch name of the repo + // type: string + // required: true + // responses: + // 200: + // description: success + + tree, err := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath) + if err != nil { + ctx.Error(http.StatusInternalServerError, "Repo.Commit.SubTree", err) + return + } + + entries, err := tree.ListEntriesRecursive() + if err != nil { + ctx.Error(http.StatusInternalServerError, "ListEntriesRecursive", err) + return + } + entries.CustomSort(base.NaturalSortLess) + + rx := generateMatcher() + var files []string + for _, entry := range entries { + if !isExcludedEntry(entry, rx) { + files = append(files, entry.Name()) + } + } + ctx.JSON(http.StatusOK, files) +} + +func isExcludedEntry(entry *git.TreeEntry, rx *regexp.Regexp) bool { + if entry.IsDir() { + return true + } + if entry.IsSubModule() { + return true + } + if rx.MatchString(entry.Name()) { + return true + } + return false +} + +func generateMatcher() *regexp.Regexp { + dirRegex := "" + + for i, dir := range excludedDirs { + // Matched vendor or Vendor or VENDOR directories. + dirRegex += fmt.Sprintf("(^%s\\/.*$)|(^%s\\/.*$)|(^%s\\/.*$)", + dir, + strings.Title(strings.ToLower(dir)), + strings.ToUpper(dir), + ) + if i < len(excludedDirs)-1 { + dirRegex += "|" + } + } + + rx, _ := regexp.Compile(dirRegex) + return rx +} diff --git a/routers/repo/find.go b/routers/repo/find.go index 0389f7a574db..c4d9f942de76 100644 --- a/routers/repo/find.go +++ b/routers/repo/find.go @@ -1,4 +1,4 @@ -// Copyright 2017 The Gitea Authors. All rights reserved. +// Copyright 2021 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -7,7 +7,6 @@ package repo import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/git" ) const ( @@ -26,38 +25,12 @@ func FindFiles(ctx *context.Context) { treeLink += "/" + ctx.Repo.TreePath } - renderFiles(ctx, treeLink) - - if ctx.Written() { - return - } + ctx.Data["BranchName"] = ctx.Repo.BranchName + ctx.Data["OwnerName"] = ctx.Repo.Owner.Name + ctx.Data["RepoName"] = ctx.Repo.Repository.Name ctx.Data["RepoLink"] = ctx.Repo.RepoLink - ctx.Data["RepoName"] = ctx.Repo.Repository.Name ctx.Data["TreeLink"] = treeLink ctx.HTML(200, tplFindFiles) } - -func renderFiles(ctx *context.Context, treeLink string) { - tree, err := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath) - if err != nil { - ctx.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err) - return - } - - entries, err := tree.ListEntriesRecursive() - if err != nil { - ctx.ServerError("ListEntries", err) - return - } - entries.CustomSort(base.NaturalSortLess) - - var fileEntries []*git.TreeEntry - for _, entry := range entries { - if !entry.IsDir() && !entry.IsSubModule() { - fileEntries = append(fileEntries, entry) - } - } - ctx.Data["Files"] = fileEntries -} diff --git a/templates/repo/find/files.tmpl b/templates/repo/find/files.tmpl index a480dc5efe47..4546112ef497 100644 --- a/templates/repo/find/files.tmpl +++ b/templates/repo/find/files.tmpl @@ -1,7 +1,11 @@ {{template "base/head" .}}
{{template "repo/header" .}} -
+
+ + + +
{{.RepoName}} / @@ -9,7 +13,7 @@
- {{range $entry := .Files}} + {{/* {{range $entry := .Files}} - {{end}} + {{end}} */}}
@@ -18,7 +22,7 @@