summaryrefslogtreecommitdiffstats
path: root/routers/api/v1/repo/commits.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2022-01-19 23:26:57 +0000
committerGitHub <noreply@github.com>2022-01-19 23:26:57 +0000
commit5cb0c9aa0d7eed087055b1efca79628957207d36 (patch)
treed117a514e1f17e5f6bfcda1be273f6a971112663 /routers/api/v1/repo/commits.go
parent4563148a61ba892e8f2bb66342f00a950bcd5315 (diff)
downloadgitea-5cb0c9aa0d7eed087055b1efca79628957207d36.tar.gz
gitea-5cb0c9aa0d7eed087055b1efca79628957207d36.zip
Propagate context and ensure git commands run in request context (#17868)
This PR continues the work in #17125 by progressively ensuring that git commands run within the request context. This now means that the if there is a git repo already open in the context it will be used instead of reopening it. Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'routers/api/v1/repo/commits.go')
-rw-r--r--routers/api/v1/repo/commits.go31
1 files changed, 10 insertions, 21 deletions
diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go
index a2b4298602..b6c47e0685 100644
--- a/routers/api/v1/repo/commits.go
+++ b/routers/api/v1/repo/commits.go
@@ -62,13 +62,7 @@ func GetSingleCommit(ctx *context.APIContext) {
}
func getCommit(ctx *context.APIContext, identifier string) {
- gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
- if err != nil {
- ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
- return
- }
- defer gitRepo.Close()
- commit, err := gitRepo.GetCommit(identifier)
+ commit, err := ctx.Repo.GitRepo.GetCommit(identifier)
if err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound(identifier)
@@ -78,7 +72,7 @@ func getCommit(ctx *context.APIContext, identifier string) {
return
}
- json, err := convert.ToCommit(ctx.Repo.Repository, commit, nil)
+ json, err := convert.ToCommit(ctx.Repo.Repository, ctx.Repo.GitRepo, commit, nil)
if err != nil {
ctx.Error(http.StatusInternalServerError, "toCommit", err)
return
@@ -136,13 +130,6 @@ func GetAllCommits(ctx *context.APIContext) {
return
}
- gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
- if err != nil {
- ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
- return
- }
- defer gitRepo.Close()
-
listOptions := utils.GetListOptions(ctx)
if listOptions.Page <= 0 {
listOptions.Page = 1
@@ -158,26 +145,27 @@ func GetAllCommits(ctx *context.APIContext) {
var (
commitsCountTotal int64
commits []*git.Commit
+ err error
)
if len(path) == 0 {
var baseCommit *git.Commit
if len(sha) == 0 {
// no sha supplied - use default branch
- head, err := gitRepo.GetHEADBranch()
+ head, err := ctx.Repo.GitRepo.GetHEADBranch()
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetHEADBranch", err)
return
}
- baseCommit, err = gitRepo.GetBranchCommit(head.Name)
+ baseCommit, err = ctx.Repo.GitRepo.GetBranchCommit(head.Name)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return
}
} else {
// get commit specified by sha
- baseCommit, err = gitRepo.GetCommit(sha)
+ baseCommit, err = ctx.Repo.GitRepo.GetCommit(sha)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return
@@ -202,7 +190,7 @@ func GetAllCommits(ctx *context.APIContext) {
sha = ctx.Repo.Repository.DefaultBranch
}
- commitsCountTotal, err = gitRepo.FileCommitsCount(sha, path)
+ commitsCountTotal, err = ctx.Repo.GitRepo.FileCommitsCount(sha, path)
if err != nil {
ctx.Error(http.StatusInternalServerError, "FileCommitsCount", err)
return
@@ -211,7 +199,7 @@ func GetAllCommits(ctx *context.APIContext) {
return
}
- commits, err = gitRepo.CommitsByFileAndRange(sha, path, listOptions.Page)
+ commits, err = ctx.Repo.GitRepo.CommitsByFileAndRange(sha, path, listOptions.Page)
if err != nil {
ctx.Error(http.StatusInternalServerError, "CommitsByFileAndRange", err)
return
@@ -225,7 +213,7 @@ func GetAllCommits(ctx *context.APIContext) {
apiCommits := make([]*api.Commit, len(commits))
for i, commit := range commits {
// Create json struct
- apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, commit, userCache)
+ apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, ctx.Repo.GitRepo, commit, userCache)
if err != nil {
ctx.Error(http.StatusInternalServerError, "toCommit", err)
return
@@ -282,6 +270,7 @@ func DownloadCommitDiffOrPatch(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
if err := git.GetRawDiff(
+ ctx,
repoPath,
ctx.Params(":sha"),
git.RawDiffType(ctx.Params(":diffType")),