1
1
mirror of https://github.com/go-gitea/gitea synced 2025-03-10 20:54:30 +00:00
TheFox0x7 4c4c56c7cd
Decouple context from repository related structs (#33823)
Calls that required context implicitly are made to pass it as argument
2025-03-08 13:12:46 -08:00

43 lines
1.0 KiB
Go

// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
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(repo)
if err != nil {
return nil, err
}
defer t.Close()
if err := t.Clone(ctx, branch, true); err != nil {
return nil, err
}
if err := t.SetDefaultIndex(ctx); err != nil {
return nil, err
}
// Add the object to the database
objectHash, err := t.HashObject(ctx, strings.NewReader(content))
if err != nil {
return nil, err
}
// Add the object to the index
if err := t.AddObjectToIndex(ctx, "100644", objectHash, treePath); err != nil {
return nil, err
}
return t.DiffIndex(ctx)
}