aboutsummaryrefslogtreecommitdiffstats
path: root/modules/repofiles/diff.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/repofiles/diff.go')
-rw-r--r--modules/repofiles/diff.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/modules/repofiles/diff.go b/modules/repofiles/diff.go
new file mode 100644
index 0000000000..3b5de5fa6f
--- /dev/null
+++ b/modules/repofiles/diff.go
@@ -0,0 +1,41 @@
+// 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"
+)
+
+// GetDiffPreview produces and returns diff result of a file which is not yet committed.
+func GetDiffPreview(repo *models.Repository, branch, treePath, content string) (*models.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()
+}