You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.0 KiB
43 lines
1.0 KiB
6 years ago
|
// 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.
|
||
|
|
||
3 years ago
|
package files
|
||
6 years ago
|
|
||
|
import (
|
||
|
"strings"
|
||
|
|
||
|
"code.gitea.io/gitea/models"
|
||
5 years ago
|
"code.gitea.io/gitea/services/gitdiff"
|
||
6 years ago
|
)
|
||
|
|
||
|
// GetDiffPreview produces and returns diff result of a file which is not yet committed.
|
||
5 years ago
|
func GetDiffPreview(repo *models.Repository, branch, treePath, content string) (*gitdiff.Diff, error) {
|
||
6 years ago
|
if branch == "" {
|
||
|
branch = repo.DefaultBranch
|
||
|
}
|
||
6 years ago
|
t, err := NewTemporaryUploadRepository(repo)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
6 years ago
|
defer t.Close()
|
||
6 years ago
|
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()
|
||
|
}
|