mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	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>
		
			
				
	
	
		
			44 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			44 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Copyright 2019 The Gitea Authors. All rights reserved.
 | 
						|
// Use of this source code is governed by a MIT-style
 | 
						|
// license that can be found in the LICENSE file.
 | 
						|
 | 
						|
package files
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"strings"
 | 
						|
 | 
						|
	repo_model "code.gitea.io/gitea/models/repo"
 | 
						|
	"code.gitea.io/gitea/services/gitdiff"
 | 
						|
)
 | 
						|
 | 
						|
// GetDiffPreview produces and returns diff result of a file which is not yet committed.
 | 
						|
func GetDiffPreview(ctx context.Context, repo *repo_model.Repository, branch, treePath, content string) (*gitdiff.Diff, error) {
 | 
						|
	if branch == "" {
 | 
						|
		branch = repo.DefaultBranch
 | 
						|
	}
 | 
						|
	t, err := NewTemporaryUploadRepository(ctx, repo)
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	defer t.Close()
 | 
						|
	if err := t.Clone(branch); err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	if err := t.SetDefaultIndex(); err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
 | 
						|
	// Add the object to the database
 | 
						|
	objectHash, err := t.HashObject(strings.NewReader(content))
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
 | 
						|
	// Add the object to the index
 | 
						|
	if err := t.AddObjectToIndex("100644", objectHash, treePath); err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	return t.DiffIndex()
 | 
						|
}
 |