2019-02-12 13:07:31 +00:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-02-12 13:07:31 +00:00
|
|
|
|
2021-11-24 15:56:24 +08:00
|
|
|
package files
|
2019-02-12 13:07:31 +00:00
|
|
|
|
|
|
|
|
import (
|
2022-01-19 23:26:57 +00:00
|
|
|
"context"
|
2019-02-12 13:07:31 +00:00
|
|
|
"strings"
|
|
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2019-09-06 10:20:09 +08:00
|
|
|
"code.gitea.io/gitea/services/gitdiff"
|
2019-02-12 13:07:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// GetDiffPreview produces and returns diff result of a file which is not yet committed.
|
2026-02-10 11:29:28 +08:00
|
|
|
func GetDiffPreview(ctx context.Context, repo *repo_model.Repository, branch, treePath, oldContent, newContent string) (*gitdiff.Diff, error) {
|
2019-04-17 10:06:35 -06:00
|
|
|
if branch == "" {
|
|
|
|
|
branch = repo.DefaultBranch
|
|
|
|
|
}
|
2025-03-08 22:12:46 +01:00
|
|
|
t, err := NewTemporaryUploadRepository(repo)
|
2019-02-12 13:07:31 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2019-04-17 10:06:35 -06:00
|
|
|
defer t.Close()
|
2025-03-08 22:12:46 +01:00
|
|
|
if err := t.Clone(ctx, branch, true); err != nil {
|
2019-02-12 13:07:31 +00:00
|
|
|
return nil, err
|
|
|
|
|
}
|
2025-03-08 22:12:46 +01:00
|
|
|
if err := t.SetDefaultIndex(ctx); err != nil {
|
2019-02-12 13:07:31 +00:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the object to the database
|
2026-02-10 11:29:28 +08:00
|
|
|
objectHash, err := t.HashObjectAndWrite(ctx, strings.NewReader(newContent))
|
2019-02-12 13:07:31 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the object to the index
|
2025-03-08 22:12:46 +01:00
|
|
|
if err := t.AddObjectToIndex(ctx, "100644", objectHash, treePath); err != nil {
|
2019-02-12 13:07:31 +00:00
|
|
|
return nil, err
|
|
|
|
|
}
|
2026-02-10 11:29:28 +08:00
|
|
|
return t.DiffIndex(ctx, oldContent, newContent)
|
2019-02-12 13:07:31 +00:00
|
|
|
}
|