gitea源码

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "fmt"
  6. "strconv"
  7. )
  8. // EntryMode the type of the object in the git tree
  9. type EntryMode int
  10. // There are only a few file modes in Git. They look like unix file modes, but they can only be
  11. // one of these.
  12. const (
  13. // EntryModeNoEntry is possible if the file was added or removed in a commit. In the case of
  14. // when adding the base commit doesn't have the file in its tree, a mode of 0o000000 is used.
  15. EntryModeNoEntry EntryMode = 0o000000
  16. EntryModeBlob EntryMode = 0o100644
  17. EntryModeExec EntryMode = 0o100755
  18. EntryModeSymlink EntryMode = 0o120000
  19. EntryModeCommit EntryMode = 0o160000
  20. EntryModeTree EntryMode = 0o040000
  21. )
  22. // String converts an EntryMode to a string
  23. func (e EntryMode) String() string {
  24. return strconv.FormatInt(int64(e), 8)
  25. }
  26. // IsSubModule if the entry is a submodule
  27. func (e EntryMode) IsSubModule() bool {
  28. return e == EntryModeCommit
  29. }
  30. // IsDir if the entry is a sub dir
  31. func (e EntryMode) IsDir() bool {
  32. return e == EntryModeTree
  33. }
  34. // IsLink if the entry is a symlink
  35. func (e EntryMode) IsLink() bool {
  36. return e == EntryModeSymlink
  37. }
  38. // IsRegular if the entry is a regular file
  39. func (e EntryMode) IsRegular() bool {
  40. return e == EntryModeBlob
  41. }
  42. // IsExecutable if the entry is an executable file (not necessarily binary)
  43. func (e EntryMode) IsExecutable() bool {
  44. return e == EntryModeExec
  45. }
  46. func ParseEntryMode(mode string) (EntryMode, error) {
  47. switch mode {
  48. case "000000":
  49. return EntryModeNoEntry, nil
  50. case "100644":
  51. return EntryModeBlob, nil
  52. case "100755":
  53. return EntryModeExec, nil
  54. case "120000":
  55. return EntryModeSymlink, nil
  56. case "160000":
  57. return EntryModeCommit, nil
  58. case "040000", "040755": // git uses 040000 for tree object, but some users may get 040755 for unknown reasons
  59. return EntryModeTree, nil
  60. default:
  61. return 0, fmt.Errorf("unparsable entry mode: %s", mode)
  62. }
  63. }