mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-29 10:57:44 +09:00 
			
		
		
		
	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>
This commit is contained in:
		| @@ -303,6 +303,7 @@ func APIContexter() func(http.Handler) http.Handler { | ||||
| 			ctx.Resp.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions) | ||||
|  | ||||
| 			ctx.Data["CsrfToken"] = html.EscapeString(ctx.csrf.GetToken()) | ||||
| 			ctx.Data["Context"] = &ctx | ||||
|  | ||||
| 			next.ServeHTTP(ctx.Resp, ctx.Req) | ||||
|  | ||||
| @@ -321,35 +322,32 @@ func APIContexter() func(http.Handler) http.Handler { | ||||
| } | ||||
|  | ||||
| // ReferencesGitRepo injects the GitRepo into the Context | ||||
| func ReferencesGitRepo(allowEmpty bool) func(http.Handler) http.Handler { | ||||
| 	return func(next http.Handler) http.Handler { | ||||
| 		return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||||
| 			ctx := GetAPIContext(req) | ||||
| 			// Empty repository does not have reference information. | ||||
| 			if !allowEmpty && ctx.Repo.Repository.IsEmpty { | ||||
| func ReferencesGitRepo(allowEmpty bool) func(ctx *APIContext) (cancel context.CancelFunc) { | ||||
| 	return func(ctx *APIContext) (cancel context.CancelFunc) { | ||||
| 		// Empty repository does not have reference information. | ||||
| 		if !allowEmpty && ctx.Repo.Repository.IsEmpty { | ||||
| 			return | ||||
| 		} | ||||
|  | ||||
| 		// For API calls. | ||||
| 		if ctx.Repo.GitRepo == nil { | ||||
| 			repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) | ||||
| 			gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath) | ||||
| 			if err != nil { | ||||
| 				ctx.Error(http.StatusInternalServerError, "RepoRef Invalid repo "+repoPath, err) | ||||
| 				return | ||||
| 			} | ||||
|  | ||||
| 			// For API calls. | ||||
| 			if ctx.Repo.GitRepo == nil { | ||||
| 				repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) | ||||
| 				gitRepo, err := git.OpenRepository(repoPath) | ||||
| 				if err != nil { | ||||
| 					ctx.Error(http.StatusInternalServerError, "RepoRef Invalid repo "+repoPath, err) | ||||
| 					return | ||||
| 			ctx.Repo.GitRepo = gitRepo | ||||
| 			// We opened it, we should close it | ||||
| 			return func() { | ||||
| 				// If it's been set to nil then assume someone else has closed it. | ||||
| 				if ctx.Repo.GitRepo != nil { | ||||
| 					ctx.Repo.GitRepo.Close() | ||||
| 				} | ||||
| 				ctx.Repo.GitRepo = gitRepo | ||||
| 				// We opened it, we should close it | ||||
| 				defer func() { | ||||
| 					// If it's been set to nil then assume someone else has closed it. | ||||
| 					if ctx.Repo.GitRepo != nil { | ||||
| 						ctx.Repo.GitRepo.Close() | ||||
| 					} | ||||
| 				}() | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 			next.ServeHTTP(w, req) | ||||
| 		}) | ||||
| 		return | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -391,7 +389,7 @@ func RepoRefForAPI(next http.Handler) http.Handler { | ||||
|  | ||||
| 		if ctx.Repo.GitRepo == nil { | ||||
| 			repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) | ||||
| 			ctx.Repo.GitRepo, err = git.OpenRepository(repoPath) | ||||
| 			ctx.Repo.GitRepo, err = git.OpenRepositoryCtx(ctx, repoPath) | ||||
| 			if err != nil { | ||||
| 				ctx.InternalServerError(err) | ||||
| 				return | ||||
|   | ||||
		Reference in New Issue
	
	Block a user