2019-02-12 18:37:31 +05:30
|
|
|
// 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.
|
|
|
|
|
2021-11-24 13:26:24 +05:30
|
|
|
package files
|
2019-02-12 18:37:31 +05:30
|
|
|
|
|
|
|
import (
|
2022-01-20 04:56:57 +05:30
|
|
|
"context"
|
2019-02-12 18:37:31 +05:30
|
|
|
"strings"
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2019-09-06 07:50:09 +05:30
|
|
|
"code.gitea.io/gitea/services/gitdiff"
|
2019-02-12 18:37:31 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// GetDiffPreview produces and returns diff result of a file which is not yet committed.
|
2022-01-20 04:56:57 +05:30
|
|
|
func GetDiffPreview(ctx context.Context, repo *repo_model.Repository, branch, treePath, content string) (*gitdiff.Diff, error) {
|
2019-04-17 21:36:35 +05:30
|
|
|
if branch == "" {
|
|
|
|
branch = repo.DefaultBranch
|
|
|
|
}
|
2022-01-20 04:56:57 +05:30
|
|
|
t, err := NewTemporaryUploadRepository(ctx, repo)
|
2019-02-12 18:37:31 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-17 21:36:35 +05:30
|
|
|
defer t.Close()
|
2019-02-12 18:37:31 +05:30
|
|
|
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()
|
|
|
|
}
|