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.

repo_index.go 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }