2019-02-12 08:07:31 -05:00
|
|
|
// 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 02:56:24 -05:00
|
|
|
package files
|
2019-02-12 08:07:31 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2019-09-05 22:20:09 -04:00
|
|
|
"code.gitea.io/gitea/services/gitdiff"
|
2019-02-12 08:07:31 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetDiffPreview produces and returns diff result of a file which is not yet committed.
|
2019-09-05 22:20:09 -04:00
|
|
|
func GetDiffPreview(repo *models.Repository, branch, treePath, content string) (*gitdiff.Diff, error) {
|
2019-04-17 12:06:35 -04:00
|
|
|
if branch == "" {
|
|
|
|
branch = repo.DefaultBranch
|
|
|
|
}
|
2019-02-12 08:07:31 -05:00
|
|
|
t, err := NewTemporaryUploadRepository(repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-17 12:06:35 -04:00
|
|
|
defer t.Close()
|
2019-02-12 08:07:31 -05:00
|
|
|
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()
|
|
|
|
}
|