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.

diff.go 998B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repofiles
  5. import (
  6. "strings"
  7. "code.gitea.io/gitea/models"
  8. )
  9. // GetDiffPreview produces and returns diff result of a file which is not yet committed.
  10. func GetDiffPreview(repo *models.Repository, branch, treePath, content string) (*models.Diff, error) {
  11. if branch == "" {
  12. branch = repo.DefaultBranch
  13. }
  14. t, err := NewTemporaryUploadRepository(repo)
  15. if err != nil {
  16. return nil, err
  17. }
  18. defer t.Close()
  19. if err := t.Clone(branch); err != nil {
  20. return nil, err
  21. }
  22. if err := t.SetDefaultIndex(); err != nil {
  23. return nil, err
  24. }
  25. // Add the object to the database
  26. objectHash, err := t.HashObject(strings.NewReader(content))
  27. if err != nil {
  28. return nil, err
  29. }
  30. // Add the object to the index
  31. if err := t.AddObjectToIndex("100644", objectHash, treePath); err != nil {
  32. return nil, err
  33. }
  34. return t.DiffIndex()
  35. }