mirror of
https://github.com/go-gitea/gitea.git
synced 2024-12-15 05:06:48 +01:00
c03d75fbd5
* move git diff codes from models to services/gitdiff * fix template * fix test * fix template
43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
// 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.
|
|
|
|
package repofiles
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"code.gitea.io/gitea/models"
|
|
"code.gitea.io/gitea/services/gitdiff"
|
|
)
|
|
|
|
// GetDiffPreview produces and returns diff result of a file which is not yet committed.
|
|
func GetDiffPreview(repo *models.Repository, branch, treePath, content string) (*gitdiff.Diff, error) {
|
|
if branch == "" {
|
|
branch = repo.DefaultBranch
|
|
}
|
|
t, err := NewTemporaryUploadRepository(repo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer t.Close()
|
|
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()
|
|
}
|