mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-10-31 06:21:11 +00:00 
			
		
		
		
	- Massive replacement of changing `code.gitea.io/gitea` to `forgejo.org`. - Resolves forgejo/discussions#258 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7337 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Reviewed-by: Beowulf <beowulf@beocode.eu> Reviewed-by: Panagiotis "Ivory" Vasilopoulos <git@n0toose.net> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
		
			
				
	
	
		
			42 lines
		
	
	
	
		
			1,010 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			42 lines
		
	
	
	
		
			1,010 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2019 The Gitea Authors. All rights reserved.
 | |
| // SPDX-License-Identifier: MIT
 | |
| 
 | |
| package files
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"strings"
 | |
| 
 | |
| 	repo_model "forgejo.org/models/repo"
 | |
| 	"forgejo.org/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, true); 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()
 | |
| }
 |