本站源代码
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

80 Zeilen
2.0KB

  1. package git
  2. import (
  3. "bytes"
  4. "fmt"
  5. "path/filepath"
  6. )
  7. // Status represents the current status of a Worktree.
  8. // The key of the map is the path of the file.
  9. type Status map[string]*FileStatus
  10. // File returns the FileStatus for a given path, if the FileStatus doesn't
  11. // exists a new FileStatus is added to the map using the path as key.
  12. func (s Status) File(path string) *FileStatus {
  13. if _, ok := (s)[path]; !ok {
  14. s[path] = &FileStatus{Worktree: Untracked, Staging: Untracked}
  15. }
  16. return s[path]
  17. }
  18. // IsUntracked checks if file for given path is 'Untracked'
  19. func (s Status) IsUntracked(path string) bool {
  20. stat, ok := (s)[filepath.ToSlash(path)]
  21. return ok && stat.Worktree == Untracked
  22. }
  23. // IsClean returns true if all the files are in Unmodified status.
  24. func (s Status) IsClean() bool {
  25. for _, status := range s {
  26. if status.Worktree != Unmodified || status.Staging != Unmodified {
  27. return false
  28. }
  29. }
  30. return true
  31. }
  32. func (s Status) String() string {
  33. buf := bytes.NewBuffer(nil)
  34. for path, status := range s {
  35. if status.Staging == Unmodified && status.Worktree == Unmodified {
  36. continue
  37. }
  38. if status.Staging == Renamed {
  39. path = fmt.Sprintf("%s -> %s", path, status.Extra)
  40. }
  41. fmt.Fprintf(buf, "%c%c %s\n", status.Staging, status.Worktree, path)
  42. }
  43. return buf.String()
  44. }
  45. // FileStatus contains the status of a file in the worktree
  46. type FileStatus struct {
  47. // Staging is the status of a file in the staging area
  48. Staging StatusCode
  49. // Worktree is the status of a file in the worktree
  50. Worktree StatusCode
  51. // Extra contains extra information, such as the previous name in a rename
  52. Extra string
  53. }
  54. // StatusCode status code of a file in the Worktree
  55. type StatusCode byte
  56. const (
  57. Unmodified StatusCode = ' '
  58. Untracked StatusCode = '?'
  59. Modified StatusCode = 'M'
  60. Added StatusCode = 'A'
  61. Deleted StatusCode = 'D'
  62. Renamed StatusCode = 'R'
  63. Copied StatusCode = 'C'
  64. UpdatedButUnmerged StatusCode = 'U'
  65. )
上海开阖软件有限公司 沪ICP备12045867号-1