本站源代码
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 line
1.0KB

  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. "code.gitea.io/gitea/services/gitdiff"
  9. )
  10. // GetDiffPreview produces and returns diff result of a file which is not yet committed.
  11. func GetDiffPreview(repo *models.Repository, branch, treePath, content string) (*gitdiff.Diff, error) {
  12. if branch == "" {
  13. branch = repo.DefaultBranch
  14. }
  15. t, err := NewTemporaryUploadRepository(repo)
  16. if err != nil {
  17. return nil, err
  18. }
  19. defer t.Close()
  20. if err := t.Clone(branch); err != nil {
  21. return nil, err
  22. }
  23. if err := t.SetDefaultIndex(); err != nil {
  24. return nil, err
  25. }
  26. // Add the object to the database
  27. objectHash, err := t.HashObject(strings.NewReader(content))
  28. if err != nil {
  29. return nil, err
  30. }
  31. // Add the object to the index
  32. if err := t.AddObjectToIndex("100644", objectHash, treePath); err != nil {
  33. return nil, err
  34. }
  35. return t.DiffIndex()
  36. }
上海开阖软件有限公司 沪ICP备12045867号-1