gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package structs
  5. import "time"
  6. // FileOptions options for all file APIs
  7. type FileOptions struct {
  8. // message (optional) for the commit of this file. if not supplied, a default message will be used
  9. Message string `json:"message"`
  10. // branch (optional) to base this file from. if not given, the default branch is used
  11. BranchName string `json:"branch" binding:"GitRefName;MaxSize(100)"`
  12. // new_branch (optional) will make a new branch from `branch` before creating the file
  13. NewBranchName string `json:"new_branch" binding:"GitRefName;MaxSize(100)"`
  14. // `author` and `committer` are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used)
  15. Author Identity `json:"author"`
  16. Committer Identity `json:"committer"`
  17. Dates CommitDateOptions `json:"dates"`
  18. // Add a Signed-off-by trailer by the committer at the end of the commit log message.
  19. Signoff bool `json:"signoff"`
  20. }
  21. type FileOptionsWithSHA struct {
  22. FileOptions
  23. // the blob ID (SHA) for the file that already exists, it is required for changing existing files
  24. // required: true
  25. SHA string `json:"sha" binding:"Required"`
  26. }
  27. func (f *FileOptions) GetFileOptions() *FileOptions {
  28. return f
  29. }
  30. type FileOptionsInterface interface {
  31. GetFileOptions() *FileOptions
  32. }
  33. var _ FileOptionsInterface = (*FileOptions)(nil)
  34. // CreateFileOptions options for creating files
  35. // Note: `author` and `committer` are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used)
  36. type CreateFileOptions struct {
  37. FileOptions
  38. // content must be base64 encoded
  39. // required: true
  40. ContentBase64 string `json:"content"`
  41. }
  42. // DeleteFileOptions options for deleting files (used for other File structs below)
  43. // Note: `author` and `committer` are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used)
  44. type DeleteFileOptions struct {
  45. FileOptionsWithSHA
  46. }
  47. // UpdateFileOptions options for updating files
  48. // Note: `author` and `committer` are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used)
  49. type UpdateFileOptions struct {
  50. FileOptionsWithSHA
  51. // content must be base64 encoded
  52. // required: true
  53. ContentBase64 string `json:"content"`
  54. // from_path (optional) is the path of the original file which will be moved/renamed to the path in the URL
  55. FromPath string `json:"from_path" binding:"MaxSize(500)"`
  56. }
  57. // FIXME: there is no LastCommitID in FileOptions, actually it should be an alternative to the SHA in ChangeFileOperation
  58. // ChangeFileOperation for creating, updating or deleting a file
  59. type ChangeFileOperation struct {
  60. // indicates what to do with the file: "create" for creating a new file, "update" for updating an existing file,
  61. // "upload" for creating or updating a file, "rename" for renaming a file, and "delete" for deleting an existing file.
  62. // required: true
  63. // enum: create,update,upload,rename,delete
  64. Operation string `json:"operation" binding:"Required"`
  65. // path to the existing or new file
  66. // required: true
  67. Path string `json:"path" binding:"Required;MaxSize(500)"`
  68. // new or updated file content, it must be base64 encoded
  69. ContentBase64 string `json:"content"`
  70. // the blob ID (SHA) for the file that already exists, required for changing existing files
  71. SHA string `json:"sha"`
  72. // old path of the file to move
  73. FromPath string `json:"from_path"`
  74. }
  75. // ChangeFilesOptions options for creating, updating or deleting multiple files
  76. // Note: `author` and `committer` are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used)
  77. type ChangeFilesOptions struct {
  78. FileOptions
  79. // list of file operations
  80. // required: true
  81. Files []*ChangeFileOperation `json:"files" binding:"Required"`
  82. }
  83. // ApplyDiffPatchFileOptions options for applying a diff patch
  84. // Note: `author` and `committer` are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used)
  85. type ApplyDiffPatchFileOptions struct {
  86. FileOptions
  87. // required: true
  88. Content string `json:"content"`
  89. }
  90. // FileLinksResponse contains the links for a repo's file
  91. type FileLinksResponse struct {
  92. // Self is the API URL for this file
  93. Self *string `json:"self"`
  94. // GitURL is the Git API URL for this file
  95. GitURL *string `json:"git"`
  96. // HTMLURL is the web URL for this file
  97. HTMLURL *string `json:"html"`
  98. }
  99. type ContentsExtResponse struct {
  100. // FileContents contains file information when the path represents a file
  101. FileContents *ContentsResponse `json:"file_contents,omitempty"`
  102. // DirContents contains directory listing when the path represents a directory
  103. DirContents []*ContentsResponse `json:"dir_contents,omitempty"`
  104. }
  105. // ContentsResponse contains information about a repo's entry's (dir, file, symlink, submodule) metadata and content
  106. type ContentsResponse struct {
  107. // Name is the file or directory name
  108. Name string `json:"name"`
  109. // Path is the full path to the file or directory
  110. Path string `json:"path"`
  111. // SHA is the Git blob or tree SHA
  112. SHA string `json:"sha"`
  113. // LastCommitSHA is the SHA of the last commit that affected this file
  114. LastCommitSHA *string `json:"last_commit_sha,omitempty"`
  115. // swagger:strfmt date-time
  116. LastCommitterDate *time.Time `json:"last_committer_date,omitempty"`
  117. // swagger:strfmt date-time
  118. LastAuthorDate *time.Time `json:"last_author_date,omitempty"`
  119. // LastCommitMessage is the message of the last commit that affected this file
  120. LastCommitMessage *string `json:"last_commit_message,omitempty"`
  121. // `type` will be `file`, `dir`, `symlink`, or `submodule`
  122. Type string `json:"type"`
  123. // Size is the file size in bytes
  124. Size int64 `json:"size"`
  125. // `encoding` is populated when `type` is `file`, otherwise null
  126. Encoding *string `json:"encoding"`
  127. // `content` is populated when `type` is `file`, otherwise null
  128. Content *string `json:"content"`
  129. // `target` is populated when `type` is `symlink`, otherwise null
  130. Target *string `json:"target"`
  131. // URL is the API URL for this file or directory
  132. URL *string `json:"url"`
  133. // HTMLURL is the web URL for this file or directory
  134. HTMLURL *string `json:"html_url"`
  135. // GitURL is the Git API URL for this blob or tree
  136. GitURL *string `json:"git_url"`
  137. // DownloadURL is the direct download URL for this file
  138. DownloadURL *string `json:"download_url"`
  139. // `submodule_git_url` is populated when `type` is `submodule`, otherwise null
  140. SubmoduleGitURL *string `json:"submodule_git_url"`
  141. // Links contains related URLs for this file or directory
  142. Links *FileLinksResponse `json:"_links"`
  143. // LfsOid is the Git LFS object ID if this file is stored in LFS
  144. LfsOid *string `json:"lfs_oid,omitempty"`
  145. // LfsSize is the file size if this file is stored in LFS
  146. LfsSize *int64 `json:"lfs_size,omitempty"`
  147. }
  148. // FileCommitResponse contains information generated from a Git commit for a repo's file.
  149. type FileCommitResponse struct {
  150. CommitMeta
  151. // HTMLURL is the web URL for viewing this commit
  152. HTMLURL string `json:"html_url"`
  153. // Author is the commit author information
  154. Author *CommitUser `json:"author"`
  155. // Committer is the commit committer information
  156. Committer *CommitUser `json:"committer"`
  157. // Parents contains parent commit metadata
  158. Parents []*CommitMeta `json:"parents"`
  159. // Message is the commit message
  160. Message string `json:"message"`
  161. // Tree contains the tree metadata for this commit
  162. Tree *CommitMeta `json:"tree"`
  163. }
  164. // FileResponse contains information about a repo's file
  165. type FileResponse struct {
  166. // Content contains the file content and metadata
  167. Content *ContentsResponse `json:"content"`
  168. // Commit contains the commit information for this file operation
  169. Commit *FileCommitResponse `json:"commit"`
  170. // Verification contains the commit signature verification information
  171. Verification *PayloadCommitVerification `json:"verification"`
  172. }
  173. // FilesResponse contains information about multiple files from a repo
  174. type FilesResponse struct {
  175. // Files contains the list of file contents and metadata
  176. Files []*ContentsResponse `json:"files"`
  177. // Commit contains the commit information for this file operation
  178. Commit *FileCommitResponse `json:"commit"`
  179. // Verification contains the commit signature verification information
  180. Verification *PayloadCommitVerification `json:"verification"`
  181. }
  182. // FileDeleteResponse contains information about a repo's file that was deleted
  183. type FileDeleteResponse struct {
  184. // Content is always null for delete operations
  185. Content any `json:"content"` // to be set to nil
  186. // Commit contains the commit information for this delete operation
  187. Commit *FileCommitResponse `json:"commit"`
  188. // Verification contains the commit signature verification information
  189. Verification *PayloadCommitVerification `json:"verification"`
  190. }
  191. // GetFilesOptions options for retrieving metadate and content of multiple files
  192. type GetFilesOptions struct {
  193. // Files is the list of file paths to retrieve
  194. Files []string `json:"files" binding:"Required"`
  195. }