From a741466ad14ae9bbfc9e00d6a975e233ef86b99c Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 17 Jul 2021 15:33:20 +0100 Subject: [PATCH 1/4] Defer Last Commit Info One of the biggest reasons for slow repository browsing is that we wait until last commit information has been generated for all files in the repository. This PR proposes deferring this generation to a new POST endpoint that does the look up outside of the main page request. Signed-off-by: Andrew Thornton --- modules/git/commit_info_gogit.go | 61 ++++---- modules/git/commit_info_nogogit.go | 47 +++--- modules/git/last_commit_cache.go | 3 + modules/git/last_commit_cache_gogit.go | 8 +- modules/git/last_commit_cache_nogogit.go | 16 +- modules/git/log_name_status.go | 13 +- modules/git/notes_gogit.go | 3 +- modules/git/notes_nogogit.go | 3 +- routers/web/repo/view.go | 189 ++++++++++++++++------- routers/web/web.go | 3 + templates/repo/view_list.tmpl | 68 ++++---- web_src/js/features/lastcommitloader.js | 31 ++++ web_src/js/index.js | 2 + 13 files changed, 296 insertions(+), 151 deletions(-) create mode 100644 web_src/js/features/lastcommitloader.js diff --git a/modules/git/commit_info_gogit.go b/modules/git/commit_info_gogit.go index a8006dcef2e6..44a1c3a7c358 100644 --- a/modules/git/commit_info_gogit.go +++ b/modules/git/commit_info_gogit.go @@ -43,20 +43,17 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath return nil, nil, err } if len(unHitPaths) > 0 { - revs2, err := GetLastCommitForPaths(ctx, c, treePath, unHitPaths) + revs2, err := GetLastCommitForPaths(ctx, cache, c, treePath, unHitPaths) if err != nil { return nil, nil, err } for k, v := range revs2 { - if err := cache.Put(commit.ID.String(), path.Join(treePath, k), v.ID().String()); err != nil { - return nil, nil, err - } revs[k] = v } } } else { - revs, err = GetLastCommitForPaths(ctx, c, treePath, entryPaths) + revs, err = GetLastCommitForPaths(ctx, nil, c, treePath, entryPaths) } if err != nil { return nil, nil, err @@ -69,25 +66,29 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath commitsInfo[i] = CommitInfo{ Entry: entry, } + + // Check if we have found a commit for this entry in time if rev, ok := revs[entry.Name()]; ok { entryCommit := convertCommit(rev) commitsInfo[i].Commit = entryCommit - if entry.IsSubModule() { - subModuleURL := "" - var fullPath string - if len(treePath) > 0 { - fullPath = treePath + "/" + entry.Name() - } else { - fullPath = entry.Name() - } - if subModule, err := commit.GetSubModule(fullPath); err != nil { - return nil, nil, err - } else if subModule != nil { - subModuleURL = subModule.URL - } - subModuleFile := NewSubModuleFile(entryCommit, subModuleURL, entry.ID.String()) - commitsInfo[i].SubModuleFile = subModuleFile + } + + // If the entry if a submodule add a submodule file for this + if entry.IsSubModule() { + subModuleURL := "" + var fullPath string + if len(treePath) > 0 { + fullPath = treePath + "/" + entry.Name() + } else { + fullPath = entry.Name() } + if subModule, err := commit.GetSubModule(fullPath); err != nil { + return nil, nil, err + } else if subModule != nil { + subModuleURL = subModule.URL + } + subModuleFile := NewSubModuleFile(commitsInfo[i].Commit, subModuleURL, entry.ID.String()) + commitsInfo[i].SubModuleFile = subModuleFile } } @@ -174,7 +175,9 @@ func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cac } // GetLastCommitForPaths returns last commit information -func GetLastCommitForPaths(ctx context.Context, c cgobject.CommitNode, treePath string, paths []string) (map[string]*object.Commit, error) { +func GetLastCommitForPaths(ctx context.Context, cache *LastCommitCache, c cgobject.CommitNode, treePath string, paths []string) (map[string]*object.Commit, error) { + refSha := c.ID().String() + // We do a tree traversal with nodes sorted by commit time heap := binaryheap.NewWith(func(a, b interface{}) int { if a.(*commitAndPaths).commit.CommitTime().Before(b.(*commitAndPaths).commit.CommitTime()) { @@ -191,10 +194,13 @@ func GetLastCommitForPaths(ctx context.Context, c cgobject.CommitNode, treePath // Start search from the root commit and with full set of paths heap.Push(&commitAndPaths{c, paths, initialHashes}) - +heaploop: for { select { case <-ctx.Done(): + if ctx.Err() == context.DeadlineExceeded { + break heaploop + } return nil, ctx.Err() default: } @@ -232,14 +238,14 @@ func GetLastCommitForPaths(ctx context.Context, c cgobject.CommitNode, treePath } var remainingPaths []string - for i, path := range current.paths { + for i, pth := range current.paths { // The results could already contain some newer change for the same path, // so don't override that and bail out on the file early. - if resultNodes[path] == nil { + if resultNodes[pth] == nil { if pathUnchanged[i] { // The path existed with the same hash in at least one parent so it could // not have been changed in this commit directly. - remainingPaths = append(remainingPaths, path) + remainingPaths = append(remainingPaths, pth) } else { // There are few possible cases how can we get here: // - The path didn't exist in any parent, so it must have been created by @@ -249,7 +255,10 @@ func GetLastCommitForPaths(ctx context.Context, c cgobject.CommitNode, treePath // - We are looking at a merge commit and the hash of the file doesn't // match any of the hashes being merged. This is more common for directories, // but it can also happen if a file is changed through conflict resolution. - resultNodes[path] = current.commit + resultNodes[pth] = current.commit + if err := cache.Put(refSha, path.Join(treePath, pth), current.commit.ID().String()); err != nil { + return nil, err + } } } } diff --git a/modules/git/commit_info_nogogit.go b/modules/git/commit_info_nogogit.go index 060ecba26190..31aa98c3d615 100644 --- a/modules/git/commit_info_nogogit.go +++ b/modules/git/commit_info_nogogit.go @@ -36,21 +36,18 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath } if len(unHitPaths) > 0 { sort.Strings(unHitPaths) - commits, err := GetLastCommitForPaths(ctx, commit, treePath, unHitPaths) + commits, err := GetLastCommitForPaths(ctx, cache, commit, treePath, unHitPaths) if err != nil { return nil, nil, err } for pth, found := range commits { - if err := cache.Put(commit.ID.String(), path.Join(treePath, pth), found.ID.String()); err != nil { - return nil, nil, err - } revs[pth] = found } } } else { sort.Strings(entryPaths) - revs, err = GetLastCommitForPaths(ctx, commit, treePath, entryPaths) + revs, err = GetLastCommitForPaths(ctx, nil, commit, treePath, entryPaths) } if err != nil { return nil, nil, err @@ -61,27 +58,31 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath commitsInfo[i] = CommitInfo{ Entry: entry, } + + // Check if we have found a commit for this entry in time if entryCommit, ok := revs[entry.Name()]; ok { commitsInfo[i].Commit = entryCommit - if entry.IsSubModule() { - subModuleURL := "" - var fullPath string - if len(treePath) > 0 { - fullPath = treePath + "/" + entry.Name() - } else { - fullPath = entry.Name() - } - if subModule, err := commit.GetSubModule(fullPath); err != nil { - return nil, nil, err - } else if subModule != nil { - subModuleURL = subModule.URL - } - subModuleFile := NewSubModuleFile(entryCommit, subModuleURL, entry.ID.String()) - commitsInfo[i].SubModuleFile = subModuleFile - } } else { log.Debug("missing commit for %s", entry.Name()) } + + // If the entry if a submodule add a submodule file for this + if entry.IsSubModule() { + subModuleURL := "" + var fullPath string + if len(treePath) > 0 { + fullPath = treePath + "/" + entry.Name() + } else { + fullPath = entry.Name() + } + if subModule, err := commit.GetSubModule(fullPath); err != nil { + return nil, nil, err + } else if subModule != nil { + subModuleURL = subModule.URL + } + subModuleFile := NewSubModuleFile(commitsInfo[i].Commit, subModuleURL, entry.ID.String()) + commitsInfo[i].SubModuleFile = subModuleFile + } } // Retrieve the commit for the treePath itself (see above). We basically @@ -120,9 +121,9 @@ func getLastCommitForPathsByCache(ctx context.Context, commitID, treePath string } // GetLastCommitForPaths returns last commit information -func GetLastCommitForPaths(ctx context.Context, commit *Commit, treePath string, paths []string) (map[string]*Commit, error) { +func GetLastCommitForPaths(ctx context.Context, cache *LastCommitCache, commit *Commit, treePath string, paths []string) (map[string]*Commit, error) { // We read backwards from the commit to obtain all of the commits - revs, err := WalkGitLog(ctx, commit.repo, commit, treePath, paths...) + revs, err := WalkGitLog(ctx, cache, commit.repo, commit, treePath, paths...) if err != nil { return nil, err } diff --git a/modules/git/last_commit_cache.go b/modules/git/last_commit_cache.go index e2d296641f3b..d4ec517b5102 100644 --- a/modules/git/last_commit_cache.go +++ b/modules/git/last_commit_cache.go @@ -26,6 +26,9 @@ func (c *LastCommitCache) getCacheKey(repoPath, ref, entryPath string) string { // Put put the last commit id with commit and entry path func (c *LastCommitCache) Put(ref, entryPath, commitID string) error { + if c == nil || c.cache == nil { + return nil + } log.Debug("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID) return c.cache.Put(c.getCacheKey(c.repoPath, ref, entryPath), commitID, c.ttl()) } diff --git a/modules/git/last_commit_cache_gogit.go b/modules/git/last_commit_cache_gogit.go index b8e0db46a926..87dfa922c8dc 100644 --- a/modules/git/last_commit_cache_gogit.go +++ b/modules/git/last_commit_cache_gogit.go @@ -8,7 +8,6 @@ package git import ( "context" - "path" "code.gitea.io/gitea/modules/log" @@ -92,15 +91,12 @@ func (c *LastCommitCache) recursiveCache(ctx context.Context, index cgobject.Com entryMap[entry.Name()] = entry } - commits, err := GetLastCommitForPaths(ctx, index, treePath, entryPaths) + commits, err := GetLastCommitForPaths(ctx, c, index, treePath, entryPaths) if err != nil { return err } - for entry, cm := range commits { - if err := c.Put(index.ID().String(), path.Join(treePath, entry), cm.ID().String()); err != nil { - return err - } + for entry := range commits { if entryMap[entry].IsDir() { subTree, err := tree.SubTree(entry) if err != nil { diff --git a/modules/git/last_commit_cache_nogogit.go b/modules/git/last_commit_cache_nogogit.go index faf6e23fa816..d4a98727555c 100644 --- a/modules/git/last_commit_cache_nogogit.go +++ b/modules/git/last_commit_cache_nogogit.go @@ -9,7 +9,6 @@ package git import ( "bufio" "context" - "path" "code.gitea.io/gitea/modules/log" ) @@ -79,28 +78,23 @@ func (c *LastCommitCache) recursiveCache(ctx context.Context, commit *Commit, tr } entryPaths := make([]string, len(entries)) - entryMap := make(map[string]*TreeEntry) for i, entry := range entries { entryPaths[i] = entry.Name() - entryMap[entry.Name()] = entry } - commits, err := GetLastCommitForPaths(ctx, commit, treePath, entryPaths) + _, err = WalkGitLog(ctx, c, commit.repo, commit, treePath, entryPaths...) if err != nil { return err } - for entry, entryCommit := range commits { - if err := c.Put(commit.ID.String(), path.Join(treePath, entry), entryCommit.ID.String()); err != nil { - return err - } + for _, treeEntry := range entries { // entryMap won't contain "" therefore skip this. - if treeEntry := entryMap[entry]; treeEntry != nil && treeEntry.IsDir() { - subTree, err := tree.SubTree(entry) + if treeEntry.IsDir() { + subTree, err := tree.SubTree(treeEntry.Name()) if err != nil { return err } - if err := c.recursiveCache(ctx, commit, subTree, entry, level-1); err != nil { + if err := c.recursiveCache(ctx, commit, subTree, treeEntry.Name(), level-1); err != nil { return err } } diff --git a/modules/git/log_name_status.go b/modules/git/log_name_status.go index 803d614d611a..eac8bd254985 100644 --- a/modules/git/log_name_status.go +++ b/modules/git/log_name_status.go @@ -267,7 +267,9 @@ func (g *LogNameStatusRepoParser) Close() { } // WalkGitLog walks the git log --name-status for the head commit in the provided treepath and files -func WalkGitLog(ctx context.Context, repo *Repository, head *Commit, treepath string, paths ...string) (map[string]string, error) { +func WalkGitLog(ctx context.Context, cache *LastCommitCache, repo *Repository, head *Commit, treepath string, paths ...string) (map[string]string, error) { + headRef := head.ID.String() + tree, err := head.SubTree(treepath) if err != nil { return nil, err @@ -328,6 +330,9 @@ heaploop: for { select { case <-ctx.Done(): + if ctx.Err() == context.DeadlineExceeded { + break heaploop + } return nil, ctx.Err() default: } @@ -348,10 +353,16 @@ heaploop: changed[i] = false if results[i] == "" { results[i] = current.CommitID + if err := cache.Put(headRef, path.Join(treepath, paths[i]), current.CommitID); err != nil { + return nil, err + } delete(path2idx, paths[i]) remaining-- if results[0] == "" { results[0] = current.CommitID + if err := cache.Put(headRef, treepath, current.CommitID); err != nil { + return nil, err + } delete(path2idx, "") remaining-- } diff --git a/modules/git/notes_gogit.go b/modules/git/notes_gogit.go index 534a5d517153..ce40e04248b0 100644 --- a/modules/git/notes_gogit.go +++ b/modules/git/notes_gogit.go @@ -14,6 +14,7 @@ import ( ) // GetNote retrieves the git-notes data for a given commit. +// FIXME: Add LastCommitCache support func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) error { notes, err := repo.GetCommit(NotesRef) if err != nil { @@ -63,7 +64,7 @@ func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) return err } - lastCommits, err := GetLastCommitForPaths(ctx, commitNode, "", []string{path}) + lastCommits, err := GetLastCommitForPaths(ctx, nil, commitNode, "", []string{path}) if err != nil { return err } diff --git a/modules/git/notes_nogogit.go b/modules/git/notes_nogogit.go index 267087a86faf..7deb72b37c87 100644 --- a/modules/git/notes_nogogit.go +++ b/modules/git/notes_nogogit.go @@ -13,6 +13,7 @@ import ( ) // GetNote retrieves the git-notes data for a given commit. +// FIXME: Add LastCommitCache support func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) error { notes, err := repo.GetCommit(NotesRef) if err != nil { @@ -64,7 +65,7 @@ func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) path = path[idx+1:] } - lastCommits, err := GetLastCommitForPaths(ctx, notes, treePath, []string{path}) + lastCommits, err := GetLastCommitForPaths(ctx, nil, notes, treePath, []string{path}) if err != nil { return err } diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 21bd80c40667..5625a3453d9f 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -7,6 +7,7 @@ package repo import ( "bytes" + gocontext "context" "encoding/base64" "fmt" gotemplate "html/template" @@ -17,6 +18,7 @@ import ( "path" "strconv" "strings" + "time" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" @@ -34,11 +36,12 @@ import ( ) const ( - tplRepoEMPTY base.TplName = "repo/empty" - tplRepoHome base.TplName = "repo/home" - tplWatchers base.TplName = "repo/watchers" - tplForks base.TplName = "repo/forks" - tplMigrating base.TplName = "repo/migrate/migrating" + tplRepoEMPTY base.TplName = "repo/empty" + tplRepoHome base.TplName = "repo/home" + tplRepoViewList base.TplName = "repo/view_list" + tplWatchers base.TplName = "repo/watchers" + tplForks base.TplName = "repo/forks" + tplMigrating base.TplName = "repo/migrate/migrating" ) type namedBlob struct { @@ -128,28 +131,8 @@ func getReadmeFileFromPath(commit *git.Commit, treePath string) (*namedBlob, err } func renderDirectory(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.ListEntries() - if err != nil { - ctx.ServerError("ListEntries", err) - return - } - entries.CustomSort(base.NaturalSortLess) - - var c *git.LastCommitCache - if setting.CacheService.LastCommit.Enabled && ctx.Repo.CommitsCount >= setting.CacheService.LastCommit.CommitsCount { - c = git.NewLastCommitCache(ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, setting.LastCommitCacheTTLSeconds, cache.GetCache()) - } - - var latestCommit *git.Commit - ctx.Data["Files"], latestCommit, err = entries.GetCommitsInfo(ctx, ctx.Repo.Commit, ctx.Repo.TreePath, c) - if err != nil { - ctx.ServerError("GetCommitsInfo", err) + entries := renderDirectoryFiles(ctx, 1*time.Second) + if ctx.Written() { return } @@ -186,6 +169,7 @@ func renderDirectory(ctx *context.Context, treeLink string) { isSymlink := entry.IsLink() target := entry if isSymlink { + var err error target, err = entry.FollowLinks() if err != nil && !git.IsErrBadLink(err) { ctx.ServerError("FollowLinks", err) @@ -207,6 +191,7 @@ func renderDirectory(ctx *context.Context, treeLink string) { name := entry.Name() isSymlink := entry.IsLink() if isSymlink { + var err error entry, err = entry.FollowLinks() if err != nil && !git.IsErrBadLink(err) { ctx.ServerError("FollowLinks", err) @@ -237,6 +222,7 @@ func renderDirectory(ctx *context.Context, treeLink string) { if entry == nil { continue } + var err error readmeFile, err = getReadmeFileFromPath(ctx.Repo.Commit, entry.GetSubJumpablePathName()) if err != nil { ctx.ServerError("getReadmeFileFromPath", err) @@ -361,34 +347,12 @@ func renderDirectory(ctx *context.Context, treeLink string) { } } - // Show latest commit info of repository in table header, - // or of directory if not in root directory. - ctx.Data["LatestCommit"] = latestCommit - verification := models.ParseCommitWithSignature(latestCommit) - - if err := models.CalculateTrustStatus(verification, ctx.Repo.Repository, nil); err != nil { - ctx.ServerError("CalculateTrustStatus", err) - return - } - ctx.Data["LatestCommitVerification"] = verification - - ctx.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit) - - statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, ctx.Repo.Commit.ID.String(), models.ListOptions{}) - if err != nil { - log.Error("GetLatestCommitStatus: %v", err) - } - - ctx.Data["LatestCommitStatus"] = models.CalcCommitStatus(statuses) - ctx.Data["LatestCommitStatuses"] = statuses - // Check permission to add or upload new file. if ctx.Repo.CanWrite(models.UnitTypeCode) && ctx.Repo.IsViewBranch { ctx.Data["CanAddFile"] = !ctx.Repo.Repository.IsArchived ctx.Data["CanUploadFile"] = setting.Repository.Upload.Enabled && !ctx.Repo.Repository.IsArchived } - ctx.Data["SSHDomain"] = setting.SSH.Domain } func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink string) { @@ -610,8 +574,7 @@ func safeURL(address string) string { return u.String() } -// Home render repository home page -func Home(ctx *context.Context) { +func checkHomeCodeViewable(ctx *context.Context) { if len(ctx.Repo.Units) > 0 { if ctx.Repo.Repository.IsBeingCreated() { task, err := models.GetMigratingTask(ctx.Repo.Repository.ID) @@ -644,7 +607,6 @@ func Home(ctx *context.Context) { var firstUnit *models.Unit for _, repoUnit := range ctx.Repo.Units { if repoUnit.Type == models.UnitTypeCode { - renderCode(ctx) return } @@ -663,6 +625,129 @@ func Home(ctx *context.Context) { ctx.NotFound("Home", fmt.Errorf(ctx.Tr("units.error.no_unit_allowed_repo"))) } +// Home render repository home page +func Home(ctx *context.Context) { + checkHomeCodeViewable(ctx) + if ctx.Written() { + return + } + + renderCode(ctx) +} + +// LastCommit returns lastCommit data for the provided branch/tag/commit and directory (in url) and filenames in body +func LastCommit(ctx *context.Context) { + checkHomeCodeViewable(ctx) + if ctx.Written() { + return + } + + renderDirectoryFiles(ctx, 0) + if ctx.Written() { + return + } + + ctx.HTML(http.StatusOK, tplRepoViewList) +} + +func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entries { + tree, err := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath) + if err != nil { + ctx.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err) + return nil + } + + ctx.Data["LastCommitLoaderURL"] = ctx.Repo.RepoLink + "/lastcommit/" + ctx.Repo.CommitID + "/" + 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 nil + } + + if !entry.IsDir() { + ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err) + return nil + } + + allEntries, err := tree.ListEntries() + if err != nil { + ctx.ServerError("ListEntries", err) + return nil + } + allEntries.CustomSort(base.NaturalSortLess) + + commitInfoCtx := gocontext.Context(ctx) + if timeout > 0 { + var cancel gocontext.CancelFunc + commitInfoCtx, cancel = gocontext.WithTimeout(ctx, timeout) + defer cancel() + } + + var c *git.LastCommitCache + if setting.CacheService.LastCommit.Enabled && ctx.Repo.CommitsCount >= setting.CacheService.LastCommit.CommitsCount { + c = git.NewLastCommitCache(ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, setting.LastCommitCacheTTLSeconds, cache.GetCache()) + } + + selected := map[string]bool{} + for _, pth := range ctx.QueryStrings("f[]") { + selected[pth] = true + } + + entries := allEntries + if len(selected) > 0 { + entries = make(git.Entries, 0, len(selected)) + for _, entry := range allEntries { + if selected[entry.Name()] { + entries = append(entries, entry) + } + } + } + + var latestCommit *git.Commit + ctx.Data["Files"], latestCommit, err = entries.GetCommitsInfo(commitInfoCtx, ctx.Repo.Commit, ctx.Repo.TreePath, c) + if err != nil { + ctx.ServerError("GetCommitsInfo", err) + return nil + } + + // Show latest commit info of repository in table header, + // or of directory if not in root directory. + ctx.Data["LatestCommit"] = latestCommit + if latestCommit != nil { + + verification := models.ParseCommitWithSignature(latestCommit) + + if err := models.CalculateTrustStatus(verification, ctx.Repo.Repository, nil); err != nil { + ctx.ServerError("CalculateTrustStatus", err) + return nil + } + ctx.Data["LatestCommitVerification"] = verification + ctx.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit) + } + + statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, ctx.Repo.Commit.ID.String(), models.ListOptions{}) + if err != nil { + log.Error("GetLatestCommitStatus: %v", err) + } + + ctx.Data["LatestCommitStatus"] = models.CalcCommitStatus(statuses) + ctx.Data["LatestCommitStatuses"] = statuses + + branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL() + treeLink := branchLink + + if len(ctx.Repo.TreePath) > 0 { + treeLink += "/" + ctx.Repo.TreePath + } + + ctx.Data["TreeLink"] = treeLink + ctx.Data["SSHDomain"] = setting.SSH.Domain + + return allEntries +} + func renderLanguageStats(ctx *context.Context) { langs, err := ctx.Repo.Repository.GetTopLanguageStats(5) if err != nil { diff --git a/routers/web/web.go b/routers/web/web.go index 7a47f479c0da..a28be422924e 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -976,6 +976,9 @@ func RegisterRoutes(m *web.Route) { m.Get("/commit/{sha:([a-f0-9]{7,40})}.{ext:patch|diff}", repo.MustBeNotEmpty, reqRepoCodeReader, repo.RawDiff) }, ignSignIn, context.RepoAssignment, context.UnitTypes()) + + m.Post("/{username}/{reponame}/lastcommit/*", ignSignInAndCsrf, context.RepoAssignment, context.UnitTypes(), context.RepoRefByType(context.RepoRefCommit), reqRepoCodeReader, repo.LastCommit) + m.Group("/{username}/{reponame}", func() { m.Get("/stars", repo.Stars) m.Get("/watchers", repo.Watchers) diff --git a/templates/repo/view_list.tmpl b/templates/repo/view_list.tmpl index fc66fb6b2d5b..7f99b0ace18e 100644 --- a/templates/repo/view_list.tmpl +++ b/templates/repo/view_list.tmpl @@ -1,36 +1,40 @@ - +
- - + @@ -43,7 +47,7 @@ {{$entry := $item.Entry}} {{$commit := $item.Commit}} {{$subModuleFile := $item.SubModuleFile}} - + - + {{end}} diff --git a/web_src/js/features/lastcommitloader.js b/web_src/js/features/lastcommitloader.js new file mode 100644 index 000000000000..0ecbc94b6aa5 --- /dev/null +++ b/web_src/js/features/lastcommitloader.js @@ -0,0 +1,31 @@ +const {csrf} = window.config; + +export async function initLastCommitLoader() { + const entryMap = {}; + + const entries = $('table#repo-files-table tr.notready') + .map((_, v) => { + entryMap[$(v).data('entryname')] = $(v); + return $(v).data('entryname'); + }) + .get(); + + if (entries.length === 0) { + return; + } + + const lastCommitLoaderURL = $('table#repo-files-table').data('lastCommitLoaderUrl'); + + $.post(lastCommitLoaderURL, { + _csrf: csrf, + 'f': entries, + }, (data) => { + $(data).find('tr').each((_, row) => { + if (row.className === 'commit-list') { + $('table#repo-files-table .commit-list').replaceWith(row); + return; + } + entryMap[$(row).data('entryname')].replaceWith(row); + }); + }); +} diff --git a/web_src/js/index.js b/web_src/js/index.js index 6156429deb6c..1a343090da74 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -20,6 +20,7 @@ import initTableSort from './features/tablesort.js'; import {createCodeEditor, createMonaco} from './features/codeeditor.js'; import {initMarkupAnchors} from './markup/anchors.js'; import {initNotificationsTable, initNotificationCount} from './features/notification.js'; +import {initLastCommitLoader} from './features/lastcommitloader.js'; import {initStopwatch} from './features/stopwatch.js'; import {showLineButton} from './code/linebutton.js'; import {initMarkupContent, initCommentContent} from './markup/content.js'; @@ -2842,6 +2843,7 @@ $(document).ready(async () => { initContextPopups(); initTableSort(); initNotificationsTable(); + initLastCommitLoader(); initPullRequestMergeInstruction(); initFileViewToggle(); initReleaseEditor(); From dc70d906bed04ebd4c9d41282f639f16552b2bdc Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 26 Jul 2021 18:25:04 +0100 Subject: [PATCH 2/4] handle large directories a bit better Signed-off-by: Andrew Thornton --- routers/web/repo/view.go | 16 ++++++++++++++++ templates/repo/view_list.tmpl | 2 +- web_src/js/features/lastcommitloader.js | 15 ++++++++++++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 5625a3453d9f..02fcee067a9a 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -647,6 +647,22 @@ func LastCommit(ctx *context.Context) { return } + 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)-2] + } + } + branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL() + ctx.Data["BranchLink"] = branchLink + ctx.HTML(http.StatusOK, tplRepoViewList) } diff --git a/templates/repo/view_list.tmpl b/templates/repo/view_list.tmpl index 7f99b0ace18e..3e7bcbe505a5 100644 --- a/templates/repo/view_list.tmpl +++ b/templates/repo/view_list.tmpl @@ -47,7 +47,7 @@ {{$entry := $item.Entry}} {{$commit := $item.Commit}} {{$subModuleFile := $item.SubModuleFile}} - +
- {{if .LatestCommitUser}} - {{avatar .LatestCommitUser 24}} - {{if .LatestCommitUser.FullName}} - {{.LatestCommitUser.FullName}} - {{else}} - {{if .LatestCommit.Author}}{{.LatestCommit.Author.Name}}{{else}}{{.LatestCommitUser.Name}}{{end}} - {{end}} + + {{if not .LatestCommit}} +
{{else}} - {{if .LatestCommit.Author}} - {{avatarByEmail .LatestCommit.Author.Email .LatestCommit.Author.Name 24}} - {{.LatestCommit.Author.Name}} - {{end}} - {{end}} - - {{ShortSha .LatestCommit.ID.String}} - {{if .LatestCommit.Signature}} - {{template "repo/shabox_badge" dict "root" $ "verification" .LatestCommitVerification}} + {{if .LatestCommitUser}} + {{avatar .LatestCommitUser 24}} + {{if .LatestCommitUser.FullName}} + {{.LatestCommitUser.FullName}} + {{else}} + {{if .LatestCommit.Author}}{{.LatestCommit.Author.Name}}{{else}}{{.LatestCommitUser.Name}}{{end}} + {{end}} + {{else}} + {{if .LatestCommit.Author}} + {{avatarByEmail .LatestCommit.Author.Email .LatestCommit.Author.Name 24}} + {{.LatestCommit.Author.Name}} + {{end}} {{end}} - - {{template "repo/commit_statuses" dict "Status" .LatestCommitStatus "Statuses" .LatestCommitStatuses "root" $}} - {{ $commitLink:= printf "%s/commit/%s" .RepoLink .LatestCommit.ID }} - {{RenderCommitMessageLinkSubject .LatestCommit.Message $.RepoLink $commitLink $.Repository.ComposeMetas}} - {{if IsMultilineCommitMessage .LatestCommit.Message}} - - + + {{ShortSha .LatestCommit.ID.String}} + {{if .LatestCommit.Signature}} + {{template "repo/shabox_badge" dict "root" $ "verification" .LatestCommitVerification}} + {{end}} + + {{template "repo/commit_statuses" dict "Status" .LatestCommitStatus "Statuses" .LatestCommitStatuses "root" $}} + {{ $commitLink:= printf "%s/commit/%s" .RepoLink .LatestCommit.ID }} + {{RenderCommitMessageLinkSubject .LatestCommit.Message $.RepoLink $commitLink $.Repository.ComposeMetas}} + {{if IsMultilineCommitMessage .LatestCommit.Message}} + + + {{end}} + {{end}} -
{{if .LatestCommit.Author}}{{TimeSince .LatestCommit.Author.When $.Lang}}{{end}}{{if .LatestCommit}}{{if .LatestCommit.Author}}{{TimeSince .LatestCommit.Author.When $.Lang}}{{end}}{{end}}
{{if $entry.IsSubModule}} @@ -75,10 +79,14 @@ - {{$commit.Summary | RenderEmoji}} + {{if $commit}} + {{$commit.Summary | RenderEmoji}} + {{else}} +
+ {{end}}
{{TimeSince $commit.Committer.When $.Lang}}{{if $commit}}{{TimeSince $commit.Committer.When $.Lang}}{{end}}
{{if $entry.IsSubModule}} diff --git a/web_src/js/features/lastcommitloader.js b/web_src/js/features/lastcommitloader.js index 0ecbc94b6aa5..964255f22925 100644 --- a/web_src/js/features/lastcommitloader.js +++ b/web_src/js/features/lastcommitloader.js @@ -5,8 +5,8 @@ export async function initLastCommitLoader() { const entries = $('table#repo-files-table tr.notready') .map((_, v) => { - entryMap[$(v).data('entryname')] = $(v); - return $(v).data('entryname'); + entryMap[$(v).attr('data-entryname')] = $(v); + return $(v).attr('data-entryname'); }) .get(); @@ -16,6 +16,15 @@ export async function initLastCommitLoader() { const lastCommitLoaderURL = $('table#repo-files-table').data('lastCommitLoaderUrl'); + if (entries.length > 200) { + $.post(lastCommitLoaderURL, { + _csrf: csrf, + }, (data) => { + $('table#repo-files-table').replaceWith(data); + }); + return; + } + $.post(lastCommitLoaderURL, { _csrf: csrf, 'f': entries, @@ -25,7 +34,7 @@ export async function initLastCommitLoader() { $('table#repo-files-table .commit-list').replaceWith(row); return; } - entryMap[$(row).data('entryname')].replaceWith(row); + entryMap[$(row).attr('data-entryname')].replaceWith(row); }); }); } From 953cb863c3c15160fecb5a9f2d5d011abd64533c Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 12 Sep 2021 17:34:59 +0100 Subject: [PATCH 3/4] Placate lint Signed-off-by: Andrew Thornton --- modules/git/log_name_status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/git/log_name_status.go b/modules/git/log_name_status.go index fbe4489cdc3a..e792b02a5dc5 100644 --- a/modules/git/log_name_status.go +++ b/modules/git/log_name_status.go @@ -344,7 +344,7 @@ heaploop: if ctx.Err() == context.DeadlineExceeded { break heaploop } - g.Close() + g.Close() return nil, ctx.Err() default: } From 67b37355c3d22051ddda37cf2655291599533432 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 25 Sep 2021 16:59:54 +0100 Subject: [PATCH 4/4] fix broken merge Signed-off-by: Andrew Thornton --- routers/web/repo/view.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 4a8c66f3e946..0777a10e7b9a 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -747,7 +747,7 @@ func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entri ctx.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit) } - statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, ctx.Repo.Commit.ID.String(), models.ListOptions{}) + statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, ctx.Repo.Commit.ID.String(), db.ListOptions{}) if err != nil { log.Error("GetLatestCommitStatus: %v", err) }