mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 19:38:23 +00: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:
		| @@ -685,8 +685,8 @@ type Diff struct { | ||||
| } | ||||
|  | ||||
| // LoadComments loads comments into each line | ||||
| func (diff *Diff) LoadComments(issue *models.Issue, currentUser *user_model.User) error { | ||||
| 	allComments, err := models.FetchCodeComments(issue, currentUser) | ||||
| func (diff *Diff) LoadComments(ctx context.Context, issue *models.Issue, currentUser *user_model.User) error { | ||||
| 	allComments, err := models.FetchCodeComments(ctx, issue, currentUser) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| @@ -1407,7 +1407,7 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff | ||||
| 				IndexFile:  indexFilename, | ||||
| 				WorkTree:   worktree, | ||||
| 			} | ||||
| 			ctx, cancel := context.WithCancel(git.DefaultContext) | ||||
| 			ctx, cancel := context.WithCancel(ctx) | ||||
| 			if err := checker.Init(ctx); err != nil { | ||||
| 				log.Error("Unable to open checker for %s. Error: %v", opts.AfterCommitID, err) | ||||
| 			} else { | ||||
| @@ -1484,12 +1484,12 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff | ||||
| 	if len(opts.BeforeCommitID) == 0 || opts.BeforeCommitID == git.EmptySHA { | ||||
| 		shortstatArgs = []string{git.EmptyTreeSHA, opts.AfterCommitID} | ||||
| 	} | ||||
| 	diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(repoPath, shortstatArgs...) | ||||
| 	diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(ctx, repoPath, shortstatArgs...) | ||||
| 	if err != nil && strings.Contains(err.Error(), "no merge base") { | ||||
| 		// git >= 2.28 now returns an error if base and head have become unrelated. | ||||
| 		// previously it would return the results of git diff --shortstat base head so let's try that... | ||||
| 		shortstatArgs = []string{opts.BeforeCommitID, opts.AfterCommitID} | ||||
| 		diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(repoPath, shortstatArgs...) | ||||
| 		diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(ctx, repoPath, shortstatArgs...) | ||||
| 	} | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
|   | ||||
| @@ -13,6 +13,7 @@ import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"code.gitea.io/gitea/models" | ||||
| 	"code.gitea.io/gitea/models/db" | ||||
| 	"code.gitea.io/gitea/models/unittest" | ||||
| 	user_model "code.gitea.io/gitea/models/user" | ||||
| 	"code.gitea.io/gitea/modules/git" | ||||
| @@ -670,7 +671,7 @@ func TestDiff_LoadComments(t *testing.T) { | ||||
| 	issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) | ||||
| 	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) | ||||
| 	diff := setupDefaultDiff() | ||||
| 	assert.NoError(t, diff.LoadComments(issue, user)) | ||||
| 	assert.NoError(t, diff.LoadComments(db.DefaultContext, issue, user)) | ||||
| 	assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2) | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user