From 74f827c4301b478063dcae942d91b3e964a71bbf Mon Sep 17 00:00:00 2001 From: hang Date: Thu, 25 Nov 2021 16:06:21 +0800 Subject: [PATCH] add:/src/commit/* --- routers/api/v1/api.go | 1 + routers/api/v1/repo/file.go | 157 ++++++++++++++++++++++++++++++++++++ routers/api/v1/repo/repo.go | 7 +- services/gitdiff/gitdiff.go | 7 ++ 4 files changed, 171 insertions(+), 1 deletion(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index fd6e6210e..2eb57443b 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -760,6 +760,7 @@ func Routes() *web.Route { // m.Get("/compare/*", context.RepoAssignment(), repo.MustBeNotEmpty, reqRepoCodeReader, // repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.CompareDiff) // m.Get("/src/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.GetFileContents) + m.Get("/src/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.GetFileContents) //end by coder m.Group("/hooks", func() { diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index e7ed4349e..bc3289dea 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -6,12 +6,16 @@ package repo import ( + "code.gitea.io/gitea/modules/setting" "encoding/base64" "fmt" "net/http" + "net/url" + "strings" "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/repofiles" @@ -702,3 +706,156 @@ func GetReadmeContentsByPath(ctx *context.APIContext) { ctx.JSON(http.StatusOK, fileList) } } + +func safeURL(address string) string { + u, err := url.Parse(address) + if err != nil { + return address + } + u.User = nil + return u.String() +} + +const ( + tplMigrating base.TplName = "repo/migrating" + tplRepoEMPTY base.TplName = "repo/empty" +) + +func GetFileContents(ctx *context.APIContext) { + if len(ctx.Repo.Units) > 0 { + if ctx.Repo.Repository.IsBeingCreated() { + task, err := models.GetMigratingTask(ctx.Repo.Repository.ID) + if err != nil { + ctx.ServerError("models.GetMigratingTask", err) + return + } + cfg, err := task.MigrateConfig() + if err != nil { + ctx.ServerError("task.MigrateConfig", err) + return + } + + ctx.Data["Repo"] = ctx.Repo + ctx.Data["MigrateTask"] = task + ctx.Data["CloneAddr"] = safeURL(cfg.CloneAddr) + ctx.HTML(200, tplMigrating) + return + } + + var firstUnit *models.Unit + for _, repoUnit := range ctx.Repo.Units { + if repoUnit.Type == models.UnitTypeCode { + renderCode(ctx.Context) + fileContent := struct { + Content interface{} + }{ + Content: ctx.Data["FileContent"], + } + ctx.JSON(http.StatusOK, fileContent) + return + } + + unit, ok := models.Units[repoUnit.Type] + if ok && (firstUnit == nil || !firstUnit.IsLessThan(unit)) { + firstUnit = &unit + } + } + + if firstUnit != nil { + ctx.Redirect(fmt.Sprintf("%s/%s%s", setting.AppSubURL, ctx.Repo.Repository.FullName(), firstUnit.URI)) + return + } + } + + ctx.NotFound("Home", fmt.Errorf(ctx.Tr("units.error.no_unit_allowed_repo"))) +} + +func renderCode(ctx *context.Context) { + ctx.Data["PageIsViewCode"] = true + + if ctx.Repo.Repository.IsEmpty { + ctx.HTML(200, tplRepoEMPTY) + return + } + + title := ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name + if len(ctx.Repo.Repository.Description) > 0 { + title += ": " + ctx.Repo.Repository.Description + } + ctx.Data["Title"] = title + + branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL() + treeLink := branchLink + // rawLink := ctx.Repo.RepoLink + "/raw/" + ctx.Repo.BranchNameSubURL() + + if len(ctx.Repo.TreePath) > 0 { + treeLink += "/" + ctx.Repo.TreePath + } + + // Get Topics of this repo + renderRepoTopics(ctx) + if ctx.Written() { + return + } + + // Get current entry user currently looking at. + entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath) + if err != nil { + return + } + + renderLanguageStats(ctx) + if ctx.Written() { + return + } + + if entry.IsDir() { + // renderDirectory(ctx, treeLink) + } else { + // renderFile(ctx, entry, treeLink, rawLink) + } + if ctx.Written() { + 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] + } + } + + ctx.Data["Paths"] = paths + ctx.Data["TreeLink"] = treeLink + ctx.Data["TreeNames"] = treeNames + ctx.Data["BranchLink"] = branchLink + // ctx.HTML(200, tplRepoHome) +} + +func renderRepoTopics(ctx *context.Context) { + topics, err := models.FindTopics(&models.FindTopicOptions{ + RepoID: ctx.Repo.Repository.ID, + }) + if err != nil { + ctx.ServerError("models.FindTopics", err) + return + } + ctx.Data["Topics"] = topics +} + +func renderLanguageStats(ctx *context.Context) { + langs, err := ctx.Repo.Repository.GetTopLanguageStats(5) + if err != nil { + ctx.ServerError("Repo.GetTopLanguageStats", err) + return + } + + ctx.Data["LanguageStats"] = langs +} diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index b76f850eb..ad45b4325 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -1140,7 +1140,12 @@ func PrepareCompareDiff( return true } - diff, err := gitdiff.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name), + repoPath := models.RepoPath(headUser.Name, headRepo.Name) + + // gitRepo, _ := git.OpenRepository(repoPath) + gitRepo, _ := git.OpenRepository(repoPath) + + diff, err := gitdiff.GetDiffRange(gitRepo, compareInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles) if err != nil { diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 4be115f03..e4517fe77 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -1210,6 +1210,13 @@ func readFileName(rd *strings.Reader) (string, bool) { return name[2:], ambiguity } +// GetDiffRange builds a Diff between two commits of a repository. +// passing the empty string as beforeCommitID returns a diff from the +// parent commit. +func GetDiffRange(gitRepo *git.Repository, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) { + return GetDiffRangeWithWhitespaceBehavior(gitRepo, beforeCommitID, afterCommitID, maxLines, maxLineCharacters, maxFiles, "") +} + // GetDiffRangeWithWhitespaceBehavior builds a Diff between two commits of a repository. // Passing the empty string as beforeCommitID returns a diff from the parent commit. // The whitespaceBehavior is either an empty string or a git flag