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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. "bytes"
  7. "container/list"
  8. "fmt"
  9. "path"
  10. "strings"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/cache"
  13. "code.gitea.io/gitea/modules/charset"
  14. "code.gitea.io/gitea/modules/git"
  15. "code.gitea.io/gitea/modules/lfs"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/setting"
  18. "code.gitea.io/gitea/modules/structs"
  19. pull_service "code.gitea.io/gitea/services/pull"
  20. stdcharset "golang.org/x/net/html/charset"
  21. "golang.org/x/text/transform"
  22. )
  23. // IdentityOptions for a person's identity like an author or committer
  24. type IdentityOptions struct {
  25. Name string
  26. Email string
  27. }
  28. // UpdateRepoFileOptions holds the repository file update options
  29. type UpdateRepoFileOptions struct {
  30. LastCommitID string
  31. OldBranch string
  32. NewBranch string
  33. TreePath string
  34. FromTreePath string
  35. Message string
  36. Content string
  37. SHA string
  38. IsNewFile bool
  39. Author *IdentityOptions
  40. Committer *IdentityOptions
  41. }
  42. func detectEncodingAndBOM(entry *git.TreeEntry, repo *models.Repository) (string, bool) {
  43. reader, err := entry.Blob().DataAsync()
  44. if err != nil {
  45. // return default
  46. return "UTF-8", false
  47. }
  48. defer reader.Close()
  49. buf := make([]byte, 1024)
  50. n, err := reader.Read(buf)
  51. if err != nil {
  52. // return default
  53. return "UTF-8", false
  54. }
  55. buf = buf[:n]
  56. if setting.LFS.StartServer {
  57. meta := lfs.IsPointerFile(&buf)
  58. if meta != nil {
  59. meta, err = repo.GetLFSMetaObjectByOid(meta.Oid)
  60. if err != nil && err != models.ErrLFSObjectNotExist {
  61. // return default
  62. return "UTF-8", false
  63. }
  64. }
  65. if meta != nil {
  66. dataRc, err := lfs.ReadMetaObject(meta)
  67. if err != nil {
  68. // return default
  69. return "UTF-8", false
  70. }
  71. defer dataRc.Close()
  72. buf = make([]byte, 1024)
  73. n, err = dataRc.Read(buf)
  74. if err != nil {
  75. // return default
  76. return "UTF-8", false
  77. }
  78. buf = buf[:n]
  79. }
  80. }
  81. encoding, err := charset.DetectEncoding(buf)
  82. if err != nil {
  83. // just default to utf-8 and no bom
  84. return "UTF-8", false
  85. }
  86. if encoding == "UTF-8" {
  87. return encoding, bytes.Equal(buf[0:3], charset.UTF8BOM)
  88. }
  89. charsetEncoding, _ := stdcharset.Lookup(encoding)
  90. if charsetEncoding == nil {
  91. return "UTF-8", false
  92. }
  93. result, n, err := transform.String(charsetEncoding.NewDecoder(), string(buf))
  94. if err != nil {
  95. // return default
  96. return "UTF-8", false
  97. }
  98. if n > 2 {
  99. return encoding, bytes.Equal([]byte(result)[0:3], charset.UTF8BOM)
  100. }
  101. return encoding, false
  102. }
  103. // CreateOrUpdateRepoFile adds or updates a file in the given repository
  104. func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *UpdateRepoFileOptions) (*structs.FileResponse, error) {
  105. // If no branch name is set, assume master
  106. if opts.OldBranch == "" {
  107. opts.OldBranch = repo.DefaultBranch
  108. }
  109. if opts.NewBranch == "" {
  110. opts.NewBranch = opts.OldBranch
  111. }
  112. // oldBranch must exist for this operation
  113. if _, err := repo.GetBranch(opts.OldBranch); err != nil {
  114. return nil, err
  115. }
  116. // A NewBranch can be specified for the file to be created/updated in a new branch.
  117. // Check to make sure the branch does not already exist, otherwise we can't proceed.
  118. // If we aren't branching to a new branch, make sure user can commit to the given branch
  119. if opts.NewBranch != opts.OldBranch {
  120. existingBranch, err := repo.GetBranch(opts.NewBranch)
  121. if existingBranch != nil {
  122. return nil, models.ErrBranchAlreadyExists{
  123. BranchName: opts.NewBranch,
  124. }
  125. }
  126. if err != nil && !git.IsErrBranchNotExist(err) {
  127. return nil, err
  128. }
  129. } else if protected, _ := repo.IsProtectedBranchForPush(opts.OldBranch, doer); protected {
  130. return nil, models.ErrUserCannotCommit{UserName: doer.LowerName}
  131. }
  132. // If FromTreePath is not set, set it to the opts.TreePath
  133. if opts.TreePath != "" && opts.FromTreePath == "" {
  134. opts.FromTreePath = opts.TreePath
  135. }
  136. // Check that the path given in opts.treePath is valid (not a git path)
  137. treePath := CleanUploadFileName(opts.TreePath)
  138. if treePath == "" {
  139. return nil, models.ErrFilenameInvalid{
  140. Path: opts.TreePath,
  141. }
  142. }
  143. // If there is a fromTreePath (we are copying it), also clean it up
  144. fromTreePath := CleanUploadFileName(opts.FromTreePath)
  145. if fromTreePath == "" && opts.FromTreePath != "" {
  146. return nil, models.ErrFilenameInvalid{
  147. Path: opts.FromTreePath,
  148. }
  149. }
  150. message := strings.TrimSpace(opts.Message)
  151. author, committer := GetAuthorAndCommitterUsers(opts.Committer, opts.Author, doer)
  152. t, err := NewTemporaryUploadRepository(repo)
  153. if err != nil {
  154. log.Error("%v", err)
  155. }
  156. defer t.Close()
  157. if err := t.Clone(opts.OldBranch); err != nil {
  158. return nil, err
  159. }
  160. if err := t.SetDefaultIndex(); err != nil {
  161. return nil, err
  162. }
  163. // Get the commit of the original branch
  164. commit, err := t.GetBranchCommit(opts.OldBranch)
  165. if err != nil {
  166. return nil, err // Couldn't get a commit for the branch
  167. }
  168. // Assigned LastCommitID in opts if it hasn't been set
  169. if opts.LastCommitID == "" {
  170. opts.LastCommitID = commit.ID.String()
  171. } else {
  172. lastCommitID, err := t.gitRepo.ConvertToSHA1(opts.LastCommitID)
  173. if err != nil {
  174. return nil, fmt.Errorf("DeleteRepoFile: Invalid last commit ID: %v", err)
  175. }
  176. opts.LastCommitID = lastCommitID.String()
  177. }
  178. encoding := "UTF-8"
  179. bom := false
  180. if !opts.IsNewFile {
  181. fromEntry, err := commit.GetTreeEntryByPath(fromTreePath)
  182. if err != nil {
  183. return nil, err
  184. }
  185. if opts.SHA != "" {
  186. // If a SHA was given and the SHA given doesn't match the SHA of the fromTreePath, throw error
  187. if opts.SHA != fromEntry.ID.String() {
  188. return nil, models.ErrSHADoesNotMatch{
  189. Path: treePath,
  190. GivenSHA: opts.SHA,
  191. CurrentSHA: fromEntry.ID.String(),
  192. }
  193. }
  194. } else if opts.LastCommitID != "" {
  195. // If a lastCommitID was given and it doesn't match the commitID of the head of the branch throw
  196. // an error, but only if we aren't creating a new branch.
  197. if commit.ID.String() != opts.LastCommitID && opts.OldBranch == opts.NewBranch {
  198. if changed, err := commit.FileChangedSinceCommit(treePath, opts.LastCommitID); err != nil {
  199. return nil, err
  200. } else if changed {
  201. return nil, models.ErrCommitIDDoesNotMatch{
  202. GivenCommitID: opts.LastCommitID,
  203. CurrentCommitID: opts.LastCommitID,
  204. }
  205. }
  206. // The file wasn't modified, so we are good to delete it
  207. }
  208. } else {
  209. // When updating a file, a lastCommitID or SHA needs to be given to make sure other commits
  210. // haven't been made. We throw an error if one wasn't provided.
  211. return nil, models.ErrSHAOrCommitIDNotProvided{}
  212. }
  213. encoding, bom = detectEncodingAndBOM(fromEntry, repo)
  214. }
  215. // For the path where this file will be created/updated, we need to make
  216. // sure no parts of the path are existing files or links except for the last
  217. // item in the path which is the file name, and that shouldn't exist IF it is
  218. // a new file OR is being moved to a new path.
  219. treePathParts := strings.Split(treePath, "/")
  220. subTreePath := ""
  221. for index, part := range treePathParts {
  222. subTreePath = path.Join(subTreePath, part)
  223. entry, err := commit.GetTreeEntryByPath(subTreePath)
  224. if err != nil {
  225. if git.IsErrNotExist(err) {
  226. // Means there is no item with that name, so we're good
  227. break
  228. }
  229. return nil, err
  230. }
  231. if index < len(treePathParts)-1 {
  232. if !entry.IsDir() {
  233. return nil, models.ErrFilePathInvalid{
  234. Message: fmt.Sprintf("a file exists where you’re trying to create a subdirectory [path: %s]", subTreePath),
  235. Path: subTreePath,
  236. Name: part,
  237. Type: git.EntryModeBlob,
  238. }
  239. }
  240. } else if entry.IsLink() {
  241. return nil, models.ErrFilePathInvalid{
  242. Message: fmt.Sprintf("a symbolic link exists where you’re trying to create a subdirectory [path: %s]", subTreePath),
  243. Path: subTreePath,
  244. Name: part,
  245. Type: git.EntryModeSymlink,
  246. }
  247. } else if entry.IsDir() {
  248. return nil, models.ErrFilePathInvalid{
  249. Message: fmt.Sprintf("a directory exists where you’re trying to create a file [path: %s]", subTreePath),
  250. Path: subTreePath,
  251. Name: part,
  252. Type: git.EntryModeTree,
  253. }
  254. } else if fromTreePath != treePath || opts.IsNewFile {
  255. // The entry shouldn't exist if we are creating new file or moving to a new path
  256. return nil, models.ErrRepoFileAlreadyExists{
  257. Path: treePath,
  258. }
  259. }
  260. }
  261. // Get the two paths (might be the same if not moving) from the index if they exist
  262. filesInIndex, err := t.LsFiles(opts.TreePath, opts.FromTreePath)
  263. if err != nil {
  264. return nil, fmt.Errorf("UpdateRepoFile: %v", err)
  265. }
  266. // If is a new file (not updating) then the given path shouldn't exist
  267. if opts.IsNewFile {
  268. for _, file := range filesInIndex {
  269. if file == opts.TreePath {
  270. return nil, models.ErrRepoFileAlreadyExists{
  271. Path: opts.TreePath,
  272. }
  273. }
  274. }
  275. }
  276. // Remove the old path from the tree
  277. if fromTreePath != treePath && len(filesInIndex) > 0 {
  278. for _, file := range filesInIndex {
  279. if file == fromTreePath {
  280. if err := t.RemoveFilesFromIndex(opts.FromTreePath); err != nil {
  281. return nil, err
  282. }
  283. }
  284. }
  285. }
  286. content := opts.Content
  287. if bom {
  288. content = string(charset.UTF8BOM) + content
  289. }
  290. if encoding != "UTF-8" {
  291. charsetEncoding, _ := stdcharset.Lookup(encoding)
  292. if charsetEncoding != nil {
  293. result, _, err := transform.String(charsetEncoding.NewEncoder(), content)
  294. if err != nil {
  295. // Look if we can't encode back in to the original we should just stick with utf-8
  296. log.Error("Error re-encoding %s (%s) as %s - will stay as UTF-8: %v", opts.TreePath, opts.FromTreePath, encoding, err)
  297. result = content
  298. }
  299. content = result
  300. } else {
  301. log.Error("Unknown encoding: %s", encoding)
  302. }
  303. }
  304. // Reset the opts.Content to our adjusted content to ensure that LFS gets the correct content
  305. opts.Content = content
  306. var lfsMetaObject *models.LFSMetaObject
  307. if setting.LFS.StartServer {
  308. // Check there is no way this can return multiple infos
  309. filename2attribute2info, err := t.CheckAttribute("filter", treePath)
  310. if err != nil {
  311. return nil, err
  312. }
  313. if filename2attribute2info[treePath] != nil && filename2attribute2info[treePath]["filter"] == "lfs" {
  314. // OK so we are supposed to LFS this data!
  315. oid, err := models.GenerateLFSOid(strings.NewReader(opts.Content))
  316. if err != nil {
  317. return nil, err
  318. }
  319. lfsMetaObject = &models.LFSMetaObject{Oid: oid, Size: int64(len(opts.Content)), RepositoryID: repo.ID}
  320. content = lfsMetaObject.Pointer()
  321. }
  322. }
  323. // Add the object to the database
  324. objectHash, err := t.HashObject(strings.NewReader(content))
  325. if err != nil {
  326. return nil, err
  327. }
  328. // Add the object to the index
  329. if err := t.AddObjectToIndex("100644", objectHash, treePath); err != nil {
  330. return nil, err
  331. }
  332. // Now write the tree
  333. treeHash, err := t.WriteTree()
  334. if err != nil {
  335. return nil, err
  336. }
  337. // Now commit the tree
  338. commitHash, err := t.CommitTree(author, committer, treeHash, message)
  339. if err != nil {
  340. return nil, err
  341. }
  342. if lfsMetaObject != nil {
  343. // We have an LFS object - create it
  344. lfsMetaObject, err = models.NewLFSMetaObject(lfsMetaObject)
  345. if err != nil {
  346. return nil, err
  347. }
  348. contentStore := &lfs.ContentStore{BasePath: setting.LFS.ContentPath}
  349. if !contentStore.Exists(lfsMetaObject) {
  350. if err := contentStore.Put(lfsMetaObject, strings.NewReader(opts.Content)); err != nil {
  351. if _, err2 := repo.RemoveLFSMetaObjectByOid(lfsMetaObject.Oid); err2 != nil {
  352. return nil, fmt.Errorf("Error whilst removing failed inserted LFS object %s: %v (Prev Error: %v)", lfsMetaObject.Oid, err2, err)
  353. }
  354. return nil, err
  355. }
  356. }
  357. }
  358. // Then push this tree to NewBranch
  359. if err := t.Push(doer, commitHash, opts.NewBranch); err != nil {
  360. return nil, err
  361. }
  362. commit, err = t.GetCommit(commitHash)
  363. if err != nil {
  364. return nil, err
  365. }
  366. file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, treePath)
  367. if err != nil {
  368. return nil, err
  369. }
  370. return file, nil
  371. }
  372. // PushUpdate must be called for any push actions in order to
  373. // generates necessary push action history feeds and other operations
  374. func PushUpdate(repo *models.Repository, branch string, opts models.PushUpdateOptions) error {
  375. isNewRef := opts.OldCommitID == git.EmptySHA
  376. isDelRef := opts.NewCommitID == git.EmptySHA
  377. if isNewRef && isDelRef {
  378. return fmt.Errorf("Old and new revisions are both %s", git.EmptySHA)
  379. }
  380. repoPath := models.RepoPath(opts.RepoUserName, opts.RepoName)
  381. _, err := git.NewCommand("update-server-info").RunInDir(repoPath)
  382. if err != nil {
  383. return fmt.Errorf("Failed to call 'git update-server-info': %v", err)
  384. }
  385. gitRepo, err := git.OpenRepository(repoPath)
  386. if err != nil {
  387. return fmt.Errorf("OpenRepository: %v", err)
  388. }
  389. if err = repo.UpdateSize(); err != nil {
  390. log.Error("Failed to update size for repository: %v", err)
  391. }
  392. var commits = &models.PushCommits{}
  393. if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
  394. // If is tag reference
  395. tagName := opts.RefFullName[len(git.TagPrefix):]
  396. if isDelRef {
  397. err = models.PushUpdateDeleteTag(repo, tagName)
  398. if err != nil {
  399. return fmt.Errorf("PushUpdateDeleteTag: %v", err)
  400. }
  401. } else {
  402. // Clear cache for tag commit count
  403. cache.Remove(repo.GetCommitsCountCacheKey(tagName, true))
  404. err = models.PushUpdateAddTag(repo, gitRepo, tagName)
  405. if err != nil {
  406. return fmt.Errorf("PushUpdateAddTag: %v", err)
  407. }
  408. }
  409. } else if !isDelRef {
  410. // If is branch reference
  411. // Clear cache for branch commit count
  412. cache.Remove(repo.GetCommitsCountCacheKey(opts.RefFullName[len(git.BranchPrefix):], true))
  413. newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
  414. if err != nil {
  415. return fmt.Errorf("gitRepo.GetCommit: %v", err)
  416. }
  417. // Push new branch.
  418. var l *list.List
  419. if isNewRef {
  420. l, err = newCommit.CommitsBeforeLimit(10)
  421. if err != nil {
  422. return fmt.Errorf("newCommit.CommitsBeforeLimit: %v", err)
  423. }
  424. } else {
  425. l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
  426. if err != nil {
  427. return fmt.Errorf("newCommit.CommitsBeforeUntil: %v", err)
  428. }
  429. }
  430. commits = models.ListToPushCommits(l)
  431. }
  432. if err := CommitRepoAction(CommitRepoActionOptions{
  433. PusherName: opts.PusherName,
  434. RepoOwnerID: repo.OwnerID,
  435. RepoName: repo.Name,
  436. RefFullName: opts.RefFullName,
  437. OldCommitID: opts.OldCommitID,
  438. NewCommitID: opts.NewCommitID,
  439. Commits: commits,
  440. }); err != nil {
  441. return fmt.Errorf("CommitRepoAction: %v", err)
  442. }
  443. pusher, err := models.GetUserByID(opts.PusherID)
  444. if err != nil {
  445. return err
  446. }
  447. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  448. go pull_service.AddTestPullRequestTask(pusher, repo.ID, branch, true)
  449. if opts.RefFullName == git.BranchPrefix+repo.DefaultBranch {
  450. models.UpdateRepoIndexer(repo)
  451. }
  452. if err = models.WatchIfAuto(opts.PusherID, repo.ID, true); err != nil {
  453. log.Warn("Fail to perform auto watch on user %v for repo %v: %v", opts.PusherID, repo.ID, err)
  454. }
  455. return nil
  456. }