本站源代码
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

99 lines
2.7KB

  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 git
  5. import (
  6. "bytes"
  7. "strings"
  8. )
  9. // ReadTreeToIndex reads a treeish to the index
  10. func (repo *Repository) ReadTreeToIndex(treeish string) error {
  11. if len(treeish) != 40 {
  12. res, err := NewCommand("rev-parse", "--verify", treeish).RunInDir(repo.Path)
  13. if err != nil {
  14. return err
  15. }
  16. if len(res) > 0 {
  17. treeish = res[:len(res)-1]
  18. }
  19. }
  20. id, err := NewIDFromString(treeish)
  21. if err != nil {
  22. return err
  23. }
  24. return repo.readTreeToIndex(id)
  25. }
  26. func (repo *Repository) readTreeToIndex(id SHA1) error {
  27. _, err := NewCommand("read-tree", id.String()).RunInDir(repo.Path)
  28. if err != nil {
  29. return err
  30. }
  31. return nil
  32. }
  33. // EmptyIndex empties the index
  34. func (repo *Repository) EmptyIndex() error {
  35. _, err := NewCommand("read-tree", "--empty").RunInDir(repo.Path)
  36. return err
  37. }
  38. // LsFiles checks if the given filenames are in the index
  39. func (repo *Repository) LsFiles(filenames ...string) ([]string, error) {
  40. cmd := NewCommand("ls-files", "-z", "--")
  41. for _, arg := range filenames {
  42. if arg != "" {
  43. cmd.AddArguments(arg)
  44. }
  45. }
  46. res, err := cmd.RunInDirBytes(repo.Path)
  47. if err != nil {
  48. return nil, err
  49. }
  50. filelist := make([]string, 0, len(filenames))
  51. for _, line := range bytes.Split(res, []byte{'\000'}) {
  52. filelist = append(filelist, string(line))
  53. }
  54. return filelist, err
  55. }
  56. // RemoveFilesFromIndex removes given filenames from the index - it does not check whether they are present.
  57. func (repo *Repository) RemoveFilesFromIndex(filenames ...string) error {
  58. cmd := NewCommand("update-index", "--remove", "-z", "--index-info")
  59. stdout := new(bytes.Buffer)
  60. stderr := new(bytes.Buffer)
  61. buffer := new(bytes.Buffer)
  62. for _, file := range filenames {
  63. if file != "" {
  64. buffer.WriteString("0 0000000000000000000000000000000000000000\t")
  65. buffer.WriteString(file)
  66. buffer.WriteByte('\000')
  67. }
  68. }
  69. return cmd.RunInDirFullPipeline(repo.Path, stdout, stderr, bytes.NewReader(buffer.Bytes()))
  70. }
  71. // AddObjectToIndex adds the provided object hash to the index at the provided filename
  72. func (repo *Repository) AddObjectToIndex(mode string, object SHA1, filename string) error {
  73. cmd := NewCommand("update-index", "--add", "--replace", "--cacheinfo", mode, object.String(), filename)
  74. _, err := cmd.RunInDir(repo.Path)
  75. return err
  76. }
  77. // WriteTree writes the current index as a tree to the object db and returns its hash
  78. func (repo *Repository) WriteTree() (*Tree, error) {
  79. res, err := NewCommand("write-tree").RunInDir(repo.Path)
  80. if err != nil {
  81. return nil, err
  82. }
  83. id, err := NewIDFromString(strings.TrimSpace(res))
  84. if err != nil {
  85. return nil, err
  86. }
  87. return NewTree(repo, id), nil
  88. }
上海开阖软件有限公司 沪ICP备12045867号-1