mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-03 21:08:25 +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:
		@@ -36,11 +36,11 @@ var commonWikiURLSuffixes = []string{".wiki.git", ".git/wiki"}
 | 
			
		||||
 | 
			
		||||
// WikiRemoteURL returns accessible repository URL for wiki if exists.
 | 
			
		||||
// Otherwise, it returns an empty string.
 | 
			
		||||
func WikiRemoteURL(remote string) string {
 | 
			
		||||
func WikiRemoteURL(ctx context.Context, remote string) string {
 | 
			
		||||
	remote = strings.TrimSuffix(remote, ".git")
 | 
			
		||||
	for _, suffix := range commonWikiURLSuffixes {
 | 
			
		||||
		wikiURL := remote + suffix
 | 
			
		||||
		if git.IsRepoURLAccessible(wikiURL) {
 | 
			
		||||
		if git.IsRepoURLAccessible(ctx, wikiURL) {
 | 
			
		||||
			return wikiURL
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
@@ -71,7 +71,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
 | 
			
		||||
		return repo, fmt.Errorf("Failed to remove %s: %v", repoPath, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err = git.CloneWithContext(ctx, opts.CloneAddr, repoPath, git.CloneRepoOptions{
 | 
			
		||||
	if err = git.Clone(ctx, opts.CloneAddr, repoPath, git.CloneRepoOptions{
 | 
			
		||||
		Mirror:  true,
 | 
			
		||||
		Quiet:   true,
 | 
			
		||||
		Timeout: migrateTimeout,
 | 
			
		||||
@@ -81,13 +81,13 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
 | 
			
		||||
 | 
			
		||||
	if opts.Wiki {
 | 
			
		||||
		wikiPath := repo_model.WikiPath(u.Name, opts.RepoName)
 | 
			
		||||
		wikiRemotePath := WikiRemoteURL(opts.CloneAddr)
 | 
			
		||||
		wikiRemotePath := WikiRemoteURL(ctx, opts.CloneAddr)
 | 
			
		||||
		if len(wikiRemotePath) > 0 {
 | 
			
		||||
			if err := util.RemoveAll(wikiPath); err != nil {
 | 
			
		||||
				return repo, fmt.Errorf("Failed to remove %s: %v", wikiPath, err)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if err = git.CloneWithContext(ctx, wikiRemotePath, wikiPath, git.CloneRepoOptions{
 | 
			
		||||
			if err = git.Clone(ctx, wikiRemotePath, wikiPath, git.CloneRepoOptions{
 | 
			
		||||
				Mirror:  true,
 | 
			
		||||
				Quiet:   true,
 | 
			
		||||
				Timeout: migrateTimeout,
 | 
			
		||||
@@ -116,7 +116,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
 | 
			
		||||
		return repo, fmt.Errorf("error in MigrateRepositoryGitData(git update-server-info): %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	gitRepo, err := git.OpenRepository(repoPath)
 | 
			
		||||
	gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return repo, fmt.Errorf("OpenRepository: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
@@ -196,7 +196,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
 | 
			
		||||
		repo.IsMirror = true
 | 
			
		||||
		err = models.UpdateRepository(repo, false)
 | 
			
		||||
	} else {
 | 
			
		||||
		repo, err = CleanUpMigrateInfo(repo)
 | 
			
		||||
		repo, err = CleanUpMigrateInfo(ctx, repo)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return repo, err
 | 
			
		||||
@@ -217,7 +217,7 @@ func cleanUpMigrateGitConfig(configPath string) error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CleanUpMigrateInfo finishes migrating repository and/or wiki with things that don't need to be done for mirrors.
 | 
			
		||||
func CleanUpMigrateInfo(repo *repo_model.Repository) (*repo_model.Repository, error) {
 | 
			
		||||
func CleanUpMigrateInfo(ctx context.Context, repo *repo_model.Repository) (*repo_model.Repository, error) {
 | 
			
		||||
	repoPath := repo.RepoPath()
 | 
			
		||||
	if err := createDelegateHooks(repoPath); err != nil {
 | 
			
		||||
		return repo, fmt.Errorf("createDelegateHooks: %v", err)
 | 
			
		||||
@@ -228,7 +228,7 @@ func CleanUpMigrateInfo(repo *repo_model.Repository) (*repo_model.Repository, er
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err := git.NewCommand("remote", "rm", "origin").RunInDir(repoPath)
 | 
			
		||||
	_, err := git.NewCommandContext(ctx, "remote", "rm", "origin").RunInDir(repoPath)
 | 
			
		||||
	if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
 | 
			
		||||
		return repo, fmt.Errorf("CleanUpMigrateInfo: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user