2019-02-12 14:07:31 +01:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-02-12 14:07:31 +01:00
|
|
|
|
2021-11-24 08:56:24 +01:00
|
|
|
package files
|
2019-02-12 14:07:31 +01:00
|
|
|
|
|
|
|
import (
|
2022-01-20 00:26:57 +01:00
|
|
|
"context"
|
2019-02-12 14:07:31 +01:00
|
|
|
"strings"
|
|
|
|
|
2021-12-10 02:27:50 +01:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2019-09-06 04:20:09 +02:00
|
|
|
"code.gitea.io/gitea/services/gitdiff"
|
2019-02-12 14:07:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetDiffPreview produces and returns diff result of a file which is not yet committed.
|
2022-01-20 00:26:57 +01:00
|
|
|
func GetDiffPreview(ctx context.Context, repo *repo_model.Repository, branch, treePath, content string) (*gitdiff.Diff, error) {
|
2019-04-17 18:06:35 +02:00
|
|
|
if branch == "" {
|
|
|
|
branch = repo.DefaultBranch
|
|
|
|
}
|
2022-01-20 00:26:57 +01:00
|
|
|
t, err := NewTemporaryUploadRepository(ctx, repo)
|
2019-02-12 14:07:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-17 18:06:35 +02:00
|
|
|
defer t.Close()
|
2024-01-21 15:18:37 +01:00
|
|
|
if err := t.Clone(branch, true); err != nil {
|
2019-02-12 14:07:31 +01:00
|
|
|
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()
|
|
|
|
}
|