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.

update.go 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 repofiles
  5. import (
  6. "fmt"
  7. "path"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/git"
  11. "code.gitea.io/gitea/modules/lfs"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/sdk/gitea"
  14. )
  15. // IdentityOptions for a person's identity like an author or committer
  16. type IdentityOptions struct {
  17. Name string
  18. Email string
  19. }
  20. // UpdateRepoFileOptions holds the repository file update options
  21. type UpdateRepoFileOptions struct {
  22. LastCommitID string
  23. OldBranch string
  24. NewBranch string
  25. TreePath string
  26. FromTreePath string
  27. Message string
  28. Content string
  29. SHA string
  30. IsNewFile bool
  31. Author *IdentityOptions
  32. Committer *IdentityOptions
  33. }
  34. // CreateOrUpdateRepoFile adds or updates a file in the given repository
  35. func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *UpdateRepoFileOptions) (*gitea.FileResponse, error) {
  36. // If no branch name is set, assume master
  37. if opts.OldBranch == "" {
  38. opts.OldBranch = repo.DefaultBranch
  39. }
  40. if opts.NewBranch == "" {
  41. opts.NewBranch = opts.OldBranch
  42. }
  43. // oldBranch must exist for this operation
  44. if _, err := repo.GetBranch(opts.OldBranch); err != nil {
  45. return nil, err
  46. }
  47. // A NewBranch can be specified for the file to be created/updated in a new branch.
  48. // Check to make sure the branch does not already exist, otherwise we can't proceed.
  49. // If we aren't branching to a new branch, make sure user can commit to the given branch
  50. if opts.NewBranch != opts.OldBranch {
  51. existingBranch, err := repo.GetBranch(opts.NewBranch)
  52. if existingBranch != nil {
  53. return nil, models.ErrBranchAlreadyExists{
  54. BranchName: opts.NewBranch,
  55. }
  56. }
  57. if err != nil && !git.IsErrBranchNotExist(err) {
  58. return nil, err
  59. }
  60. } else {
  61. if protected, _ := repo.IsProtectedBranchForPush(opts.OldBranch, doer); protected {
  62. return nil, models.ErrUserCannotCommit{UserName: doer.LowerName}
  63. }
  64. }
  65. // If FromTreePath is not set, set it to the opts.TreePath
  66. if opts.TreePath != "" && opts.FromTreePath == "" {
  67. opts.FromTreePath = opts.TreePath
  68. }
  69. // Check that the path given in opts.treePath is valid (not a git path)
  70. treePath := CleanUploadFileName(opts.TreePath)
  71. if treePath == "" {
  72. return nil, models.ErrFilenameInvalid{
  73. Path: opts.TreePath,
  74. }
  75. }
  76. // If there is a fromTreePath (we are copying it), also clean it up
  77. fromTreePath := CleanUploadFileName(opts.FromTreePath)
  78. if fromTreePath == "" && opts.FromTreePath != "" {
  79. return nil, models.ErrFilenameInvalid{
  80. Path: opts.FromTreePath,
  81. }
  82. }
  83. message := strings.TrimSpace(opts.Message)
  84. author, committer := GetAuthorAndCommitterUsers(opts.Committer, opts.Author, doer)
  85. t, err := NewTemporaryUploadRepository(repo)
  86. defer t.Close()
  87. if err != nil {
  88. return nil, err
  89. }
  90. if err := t.Clone(opts.OldBranch); err != nil {
  91. return nil, err
  92. }
  93. if err := t.SetDefaultIndex(); err != nil {
  94. return nil, err
  95. }
  96. // Get the commit of the original branch
  97. commit, err := t.GetBranchCommit(opts.OldBranch)
  98. if err != nil {
  99. return nil, err // Couldn't get a commit for the branch
  100. }
  101. // Assigned LastCommitID in opts if it hasn't been set
  102. if opts.LastCommitID == "" {
  103. opts.LastCommitID = commit.ID.String()
  104. }
  105. if !opts.IsNewFile {
  106. fromEntry, err := commit.GetTreeEntryByPath(fromTreePath)
  107. if err != nil {
  108. return nil, err
  109. }
  110. if opts.SHA != "" {
  111. // If a SHA was given and the SHA given doesn't match the SHA of the fromTreePath, throw error
  112. if opts.SHA != fromEntry.ID.String() {
  113. return nil, models.ErrSHADoesNotMatch{
  114. Path: treePath,
  115. GivenSHA: opts.SHA,
  116. CurrentSHA: fromEntry.ID.String(),
  117. }
  118. }
  119. } else if opts.LastCommitID != "" {
  120. // If a lastCommitID was given and it doesn't match the commitID of the head of the branch throw
  121. // an error, but only if we aren't creating a new branch.
  122. if commit.ID.String() != opts.LastCommitID && opts.OldBranch == opts.NewBranch {
  123. if changed, err := commit.FileChangedSinceCommit(treePath, opts.LastCommitID); err != nil {
  124. return nil, err
  125. } else if changed {
  126. return nil, models.ErrCommitIDDoesNotMatch{
  127. GivenCommitID: opts.LastCommitID,
  128. CurrentCommitID: opts.LastCommitID,
  129. }
  130. }
  131. // The file wasn't modified, so we are good to delete it
  132. }
  133. } else {
  134. // When updating a file, a lastCommitID or SHA needs to be given to make sure other commits
  135. // haven't been made. We throw an error if one wasn't provided.
  136. return nil, models.ErrSHAOrCommitIDNotProvided{}
  137. }
  138. }
  139. // For the path where this file will be created/updated, we need to make
  140. // sure no parts of the path are existing files or links except for the last
  141. // item in the path which is the file name, and that shouldn't exist IF it is
  142. // a new file OR is being moved to a new path.
  143. treePathParts := strings.Split(treePath, "/")
  144. subTreePath := ""
  145. for index, part := range treePathParts {
  146. subTreePath = path.Join(subTreePath, part)
  147. entry, err := commit.GetTreeEntryByPath(subTreePath)
  148. if err != nil {
  149. if git.IsErrNotExist(err) {
  150. // Means there is no item with that name, so we're good
  151. break
  152. }
  153. return nil, err
  154. }
  155. if index < len(treePathParts)-1 {
  156. if !entry.IsDir() {
  157. return nil, models.ErrFilePathInvalid{
  158. Message: fmt.Sprintf("a file exists where you’re trying to create a subdirectory [path: %s]", subTreePath),
  159. Path: subTreePath,
  160. Name: part,
  161. Type: git.EntryModeBlob,
  162. }
  163. }
  164. } else if entry.IsLink() {
  165. return nil, models.ErrFilePathInvalid{
  166. Message: fmt.Sprintf("a symbolic link exists where you’re trying to create a subdirectory [path: %s]", subTreePath),
  167. Path: subTreePath,
  168. Name: part,
  169. Type: git.EntryModeSymlink,
  170. }
  171. } else if entry.IsDir() {
  172. return nil, models.ErrFilePathInvalid{
  173. Message: fmt.Sprintf("a directory exists where you’re trying to create a file [path: %s]", subTreePath),
  174. Path: subTreePath,
  175. Name: part,
  176. Type: git.EntryModeTree,
  177. }
  178. } else if fromTreePath != treePath || opts.IsNewFile {
  179. // The entry shouldn't exist if we are creating new file or moving to a new path
  180. return nil, models.ErrRepoFileAlreadyExists{
  181. Path: treePath,
  182. }
  183. }
  184. }
  185. // Get the two paths (might be the same if not moving) from the index if they exist
  186. filesInIndex, err := t.LsFiles(opts.TreePath, opts.FromTreePath)
  187. if err != nil {
  188. return nil, fmt.Errorf("UpdateRepoFile: %v", err)
  189. }
  190. // If is a new file (not updating) then the given path shouldn't exist
  191. if opts.IsNewFile {
  192. for _, file := range filesInIndex {
  193. if file == opts.TreePath {
  194. return nil, models.ErrRepoFileAlreadyExists{
  195. Path: opts.TreePath,
  196. }
  197. }
  198. }
  199. }
  200. // Remove the old path from the tree
  201. if fromTreePath != treePath && len(filesInIndex) > 0 {
  202. for _, file := range filesInIndex {
  203. if file == fromTreePath {
  204. if err := t.RemoveFilesFromIndex(opts.FromTreePath); err != nil {
  205. return nil, err
  206. }
  207. }
  208. }
  209. }
  210. // Check there is no way this can return multiple infos
  211. filename2attribute2info, err := t.CheckAttribute("filter", treePath)
  212. if err != nil {
  213. return nil, err
  214. }
  215. content := opts.Content
  216. var lfsMetaObject *models.LFSMetaObject
  217. if filename2attribute2info[treePath] != nil && filename2attribute2info[treePath]["filter"] == "lfs" {
  218. // OK so we are supposed to LFS this data!
  219. oid, err := models.GenerateLFSOid(strings.NewReader(opts.Content))
  220. if err != nil {
  221. return nil, err
  222. }
  223. lfsMetaObject = &models.LFSMetaObject{Oid: oid, Size: int64(len(opts.Content)), RepositoryID: repo.ID}
  224. content = lfsMetaObject.Pointer()
  225. }
  226. // Add the object to the database
  227. objectHash, err := t.HashObject(strings.NewReader(content))
  228. if err != nil {
  229. return nil, err
  230. }
  231. // Add the object to the index
  232. if err := t.AddObjectToIndex("100644", objectHash, treePath); err != nil {
  233. return nil, err
  234. }
  235. // Now write the tree
  236. treeHash, err := t.WriteTree()
  237. if err != nil {
  238. return nil, err
  239. }
  240. // Now commit the tree
  241. commitHash, err := t.CommitTree(author, committer, treeHash, message)
  242. if err != nil {
  243. return nil, err
  244. }
  245. if lfsMetaObject != nil {
  246. // We have an LFS object - create it
  247. lfsMetaObject, err = models.NewLFSMetaObject(lfsMetaObject)
  248. if err != nil {
  249. return nil, err
  250. }
  251. contentStore := &lfs.ContentStore{BasePath: setting.LFS.ContentPath}
  252. if !contentStore.Exists(lfsMetaObject) {
  253. if err := contentStore.Put(lfsMetaObject, strings.NewReader(opts.Content)); err != nil {
  254. if err2 := repo.RemoveLFSMetaObjectByOid(lfsMetaObject.Oid); err2 != nil {
  255. return nil, fmt.Errorf("Error whilst removing failed inserted LFS object %s: %v (Prev Error: %v)", lfsMetaObject.Oid, err2, err)
  256. }
  257. return nil, err
  258. }
  259. }
  260. }
  261. // Then push this tree to NewBranch
  262. if err := t.Push(doer, commitHash, opts.NewBranch); err != nil {
  263. return nil, err
  264. }
  265. // Simulate push event.
  266. oldCommitID := opts.LastCommitID
  267. if opts.NewBranch != opts.OldBranch || oldCommitID == "" {
  268. oldCommitID = git.EmptySHA
  269. }
  270. if err = repo.GetOwner(); err != nil {
  271. return nil, fmt.Errorf("GetOwner: %v", err)
  272. }
  273. err = models.PushUpdate(
  274. opts.NewBranch,
  275. models.PushUpdateOptions{
  276. PusherID: doer.ID,
  277. PusherName: doer.Name,
  278. RepoUserName: repo.Owner.Name,
  279. RepoName: repo.Name,
  280. RefFullName: git.BranchPrefix + opts.NewBranch,
  281. OldCommitID: oldCommitID,
  282. NewCommitID: commitHash,
  283. },
  284. )
  285. if err != nil {
  286. return nil, fmt.Errorf("PushUpdate: %v", err)
  287. }
  288. models.UpdateRepoIndexer(repo)
  289. commit, err = t.GetCommit(commitHash)
  290. if err != nil {
  291. return nil, err
  292. }
  293. file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, treePath)
  294. if err != nil {
  295. return nil, err
  296. }
  297. return file, nil
  298. }