You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

update.go 13 kB

Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. "fmt"
  8. "path"
  9. "strings"
  10. "golang.org/x/net/html/charset"
  11. "golang.org/x/text/transform"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/base"
  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. )
  20. // IdentityOptions for a person's identity like an author or committer
  21. type IdentityOptions struct {
  22. Name string
  23. Email string
  24. }
  25. // UpdateRepoFileOptions holds the repository file update options
  26. type UpdateRepoFileOptions struct {
  27. LastCommitID string
  28. OldBranch string
  29. NewBranch string
  30. TreePath string
  31. FromTreePath string
  32. Message string
  33. Content string
  34. SHA string
  35. IsNewFile bool
  36. Author *IdentityOptions
  37. Committer *IdentityOptions
  38. }
  39. func detectEncodingAndBOM(entry *git.TreeEntry, repo *models.Repository) (string, bool) {
  40. reader, err := entry.Blob().DataAsync()
  41. if err != nil {
  42. // return default
  43. return "UTF-8", false
  44. }
  45. defer reader.Close()
  46. buf := make([]byte, 1024)
  47. n, err := reader.Read(buf)
  48. if err != nil {
  49. // return default
  50. return "UTF-8", false
  51. }
  52. buf = buf[:n]
  53. if setting.LFS.StartServer {
  54. meta := lfs.IsPointerFile(&buf)
  55. if meta != nil {
  56. meta, err = repo.GetLFSMetaObjectByOid(meta.Oid)
  57. if err != nil && err != models.ErrLFSObjectNotExist {
  58. // return default
  59. return "UTF-8", false
  60. }
  61. }
  62. if meta != nil {
  63. dataRc, err := lfs.ReadMetaObject(meta)
  64. if err != nil {
  65. // return default
  66. return "UTF-8", false
  67. }
  68. defer dataRc.Close()
  69. buf = make([]byte, 1024)
  70. n, err = dataRc.Read(buf)
  71. if err != nil {
  72. // return default
  73. return "UTF-8", false
  74. }
  75. buf = buf[:n]
  76. }
  77. }
  78. encoding, err := base.DetectEncoding(buf)
  79. if err != nil {
  80. // just default to utf-8 and no bom
  81. return "UTF-8", false
  82. }
  83. if encoding == "UTF-8" {
  84. return encoding, bytes.Equal(buf[0:3], base.UTF8BOM)
  85. }
  86. charsetEncoding, _ := charset.Lookup(encoding)
  87. if charsetEncoding == nil {
  88. return "UTF-8", false
  89. }
  90. result, n, err := transform.String(charsetEncoding.NewDecoder(), string(buf))
  91. if err != nil {
  92. // return default
  93. return "UTF-8", false
  94. }
  95. if n > 2 {
  96. return encoding, bytes.Equal([]byte(result)[0:3], base.UTF8BOM)
  97. }
  98. return encoding, false
  99. }
  100. // CreateOrUpdateRepoFile adds or updates a file in the given repository
  101. func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *UpdateRepoFileOptions) (*structs.FileResponse, error) {
  102. // If no branch name is set, assume master
  103. if opts.OldBranch == "" {
  104. opts.OldBranch = repo.DefaultBranch
  105. }
  106. if opts.NewBranch == "" {
  107. opts.NewBranch = opts.OldBranch
  108. }
  109. // oldBranch must exist for this operation
  110. if _, err := repo.GetBranch(opts.OldBranch); err != nil {
  111. return nil, err
  112. }
  113. // A NewBranch can be specified for the file to be created/updated in a new branch.
  114. // Check to make sure the branch does not already exist, otherwise we can't proceed.
  115. // If we aren't branching to a new branch, make sure user can commit to the given branch
  116. if opts.NewBranch != opts.OldBranch {
  117. existingBranch, err := repo.GetBranch(opts.NewBranch)
  118. if existingBranch != nil {
  119. return nil, models.ErrBranchAlreadyExists{
  120. BranchName: opts.NewBranch,
  121. }
  122. }
  123. if err != nil && !git.IsErrBranchNotExist(err) {
  124. return nil, err
  125. }
  126. } else if protected, _ := repo.IsProtectedBranchForPush(opts.OldBranch, doer); protected {
  127. return nil, models.ErrUserCannotCommit{UserName: doer.LowerName}
  128. }
  129. // If FromTreePath is not set, set it to the opts.TreePath
  130. if opts.TreePath != "" && opts.FromTreePath == "" {
  131. opts.FromTreePath = opts.TreePath
  132. }
  133. // Check that the path given in opts.treePath is valid (not a git path)
  134. treePath := CleanUploadFileName(opts.TreePath)
  135. if treePath == "" {
  136. return nil, models.ErrFilenameInvalid{
  137. Path: opts.TreePath,
  138. }
  139. }
  140. // If there is a fromTreePath (we are copying it), also clean it up
  141. fromTreePath := CleanUploadFileName(opts.FromTreePath)
  142. if fromTreePath == "" && opts.FromTreePath != "" {
  143. return nil, models.ErrFilenameInvalid{
  144. Path: opts.FromTreePath,
  145. }
  146. }
  147. message := strings.TrimSpace(opts.Message)
  148. author, committer := GetAuthorAndCommitterUsers(opts.Committer, opts.Author, doer)
  149. t, err := NewTemporaryUploadRepository(repo)
  150. if err != nil {
  151. log.Error("%v", err)
  152. }
  153. defer t.Close()
  154. if err := t.Clone(opts.OldBranch); err != nil {
  155. return nil, err
  156. }
  157. if err := t.SetDefaultIndex(); err != nil {
  158. return nil, err
  159. }
  160. // Get the commit of the original branch
  161. commit, err := t.GetBranchCommit(opts.OldBranch)
  162. if err != nil {
  163. return nil, err // Couldn't get a commit for the branch
  164. }
  165. // Assigned LastCommitID in opts if it hasn't been set
  166. if opts.LastCommitID == "" {
  167. opts.LastCommitID = commit.ID.String()
  168. }
  169. encoding := "UTF-8"
  170. bom := false
  171. if !opts.IsNewFile {
  172. fromEntry, err := commit.GetTreeEntryByPath(fromTreePath)
  173. if err != nil {
  174. return nil, err
  175. }
  176. if opts.SHA != "" {
  177. // If a SHA was given and the SHA given doesn't match the SHA of the fromTreePath, throw error
  178. if opts.SHA != fromEntry.ID.String() {
  179. return nil, models.ErrSHADoesNotMatch{
  180. Path: treePath,
  181. GivenSHA: opts.SHA,
  182. CurrentSHA: fromEntry.ID.String(),
  183. }
  184. }
  185. } else if opts.LastCommitID != "" {
  186. // If a lastCommitID was given and it doesn't match the commitID of the head of the branch throw
  187. // an error, but only if we aren't creating a new branch.
  188. if commit.ID.String() != opts.LastCommitID && opts.OldBranch == opts.NewBranch {
  189. if changed, err := commit.FileChangedSinceCommit(treePath, opts.LastCommitID); err != nil {
  190. return nil, err
  191. } else if changed {
  192. return nil, models.ErrCommitIDDoesNotMatch{
  193. GivenCommitID: opts.LastCommitID,
  194. CurrentCommitID: opts.LastCommitID,
  195. }
  196. }
  197. // The file wasn't modified, so we are good to delete it
  198. }
  199. } else {
  200. // When updating a file, a lastCommitID or SHA needs to be given to make sure other commits
  201. // haven't been made. We throw an error if one wasn't provided.
  202. return nil, models.ErrSHAOrCommitIDNotProvided{}
  203. }
  204. encoding, bom = detectEncodingAndBOM(fromEntry, repo)
  205. }
  206. // For the path where this file will be created/updated, we need to make
  207. // sure no parts of the path are existing files or links except for the last
  208. // item in the path which is the file name, and that shouldn't exist IF it is
  209. // a new file OR is being moved to a new path.
  210. treePathParts := strings.Split(treePath, "/")
  211. subTreePath := ""
  212. for index, part := range treePathParts {
  213. subTreePath = path.Join(subTreePath, part)
  214. entry, err := commit.GetTreeEntryByPath(subTreePath)
  215. if err != nil {
  216. if git.IsErrNotExist(err) {
  217. // Means there is no item with that name, so we're good
  218. break
  219. }
  220. return nil, err
  221. }
  222. if index < len(treePathParts)-1 {
  223. if !entry.IsDir() {
  224. return nil, models.ErrFilePathInvalid{
  225. Message: fmt.Sprintf("a file exists where you’re trying to create a subdirectory [path: %s]", subTreePath),
  226. Path: subTreePath,
  227. Name: part,
  228. Type: git.EntryModeBlob,
  229. }
  230. }
  231. } else if entry.IsLink() {
  232. return nil, models.ErrFilePathInvalid{
  233. Message: fmt.Sprintf("a symbolic link exists where you’re trying to create a subdirectory [path: %s]", subTreePath),
  234. Path: subTreePath,
  235. Name: part,
  236. Type: git.EntryModeSymlink,
  237. }
  238. } else if entry.IsDir() {
  239. return nil, models.ErrFilePathInvalid{
  240. Message: fmt.Sprintf("a directory exists where you’re trying to create a file [path: %s]", subTreePath),
  241. Path: subTreePath,
  242. Name: part,
  243. Type: git.EntryModeTree,
  244. }
  245. } else if fromTreePath != treePath || opts.IsNewFile {
  246. // The entry shouldn't exist if we are creating new file or moving to a new path
  247. return nil, models.ErrRepoFileAlreadyExists{
  248. Path: treePath,
  249. }
  250. }
  251. }
  252. // Get the two paths (might be the same if not moving) from the index if they exist
  253. filesInIndex, err := t.LsFiles(opts.TreePath, opts.FromTreePath)
  254. if err != nil {
  255. return nil, fmt.Errorf("UpdateRepoFile: %v", err)
  256. }
  257. // If is a new file (not updating) then the given path shouldn't exist
  258. if opts.IsNewFile {
  259. for _, file := range filesInIndex {
  260. if file == opts.TreePath {
  261. return nil, models.ErrRepoFileAlreadyExists{
  262. Path: opts.TreePath,
  263. }
  264. }
  265. }
  266. }
  267. // Remove the old path from the tree
  268. if fromTreePath != treePath && len(filesInIndex) > 0 {
  269. for _, file := range filesInIndex {
  270. if file == fromTreePath {
  271. if err := t.RemoveFilesFromIndex(opts.FromTreePath); err != nil {
  272. return nil, err
  273. }
  274. }
  275. }
  276. }
  277. // Check there is no way this can return multiple infos
  278. filename2attribute2info, err := t.CheckAttribute("filter", treePath)
  279. if err != nil {
  280. return nil, err
  281. }
  282. content := opts.Content
  283. if bom {
  284. content = string(base.UTF8BOM) + content
  285. }
  286. if encoding != "UTF-8" {
  287. charsetEncoding, _ := charset.Lookup(encoding)
  288. if charsetEncoding != nil {
  289. result, _, err := transform.String(charsetEncoding.NewEncoder(), string(content))
  290. if err != nil {
  291. // Look if we can't encode back in to the original we should just stick with utf-8
  292. log.Error("Error re-encoding %s (%s) as %s - will stay as UTF-8: %v", opts.TreePath, opts.FromTreePath, encoding, err)
  293. result = content
  294. }
  295. content = result
  296. } else {
  297. log.Error("Unknown encoding: %s", encoding)
  298. }
  299. }
  300. // Reset the opts.Content to our adjusted content to ensure that LFS gets the correct content
  301. opts.Content = content
  302. var lfsMetaObject *models.LFSMetaObject
  303. if setting.LFS.StartServer && filename2attribute2info[treePath] != nil && filename2attribute2info[treePath]["filter"] == "lfs" {
  304. // OK so we are supposed to LFS this data!
  305. oid, err := models.GenerateLFSOid(strings.NewReader(opts.Content))
  306. if err != nil {
  307. return nil, err
  308. }
  309. lfsMetaObject = &models.LFSMetaObject{Oid: oid, Size: int64(len(opts.Content)), RepositoryID: repo.ID}
  310. content = lfsMetaObject.Pointer()
  311. }
  312. // Add the object to the database
  313. objectHash, err := t.HashObject(strings.NewReader(content))
  314. if err != nil {
  315. return nil, err
  316. }
  317. // Add the object to the index
  318. if err := t.AddObjectToIndex("100644", objectHash, treePath); err != nil {
  319. return nil, err
  320. }
  321. // Now write the tree
  322. treeHash, err := t.WriteTree()
  323. if err != nil {
  324. return nil, err
  325. }
  326. // Now commit the tree
  327. commitHash, err := t.CommitTree(author, committer, treeHash, message)
  328. if err != nil {
  329. return nil, err
  330. }
  331. if lfsMetaObject != nil {
  332. // We have an LFS object - create it
  333. lfsMetaObject, err = models.NewLFSMetaObject(lfsMetaObject)
  334. if err != nil {
  335. return nil, err
  336. }
  337. contentStore := &lfs.ContentStore{BasePath: setting.LFS.ContentPath}
  338. if !contentStore.Exists(lfsMetaObject) {
  339. if err := contentStore.Put(lfsMetaObject, strings.NewReader(opts.Content)); err != nil {
  340. if err2 := repo.RemoveLFSMetaObjectByOid(lfsMetaObject.Oid); err2 != nil {
  341. return nil, fmt.Errorf("Error whilst removing failed inserted LFS object %s: %v (Prev Error: %v)", lfsMetaObject.Oid, err2, err)
  342. }
  343. return nil, err
  344. }
  345. }
  346. }
  347. // Then push this tree to NewBranch
  348. if err := t.Push(doer, commitHash, opts.NewBranch); err != nil {
  349. return nil, err
  350. }
  351. // Simulate push event.
  352. oldCommitID := opts.LastCommitID
  353. if opts.NewBranch != opts.OldBranch || oldCommitID == "" {
  354. oldCommitID = git.EmptySHA
  355. }
  356. if err = repo.GetOwner(); err != nil {
  357. return nil, fmt.Errorf("GetOwner: %v", err)
  358. }
  359. err = PushUpdate(
  360. repo,
  361. opts.NewBranch,
  362. models.PushUpdateOptions{
  363. PusherID: doer.ID,
  364. PusherName: doer.Name,
  365. RepoUserName: repo.Owner.Name,
  366. RepoName: repo.Name,
  367. RefFullName: git.BranchPrefix + opts.NewBranch,
  368. OldCommitID: oldCommitID,
  369. NewCommitID: commitHash,
  370. },
  371. )
  372. if err != nil {
  373. return nil, fmt.Errorf("PushUpdate: %v", err)
  374. }
  375. commit, err = t.GetCommit(commitHash)
  376. if err != nil {
  377. return nil, err
  378. }
  379. file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, treePath)
  380. if err != nil {
  381. return nil, err
  382. }
  383. return file, nil
  384. }
  385. // PushUpdate must be called for any push actions in order to
  386. // generates necessary push action history feeds and other operations
  387. func PushUpdate(repo *models.Repository, branch string, opts models.PushUpdateOptions) error {
  388. err := models.PushUpdate(branch, opts)
  389. if err != nil {
  390. return fmt.Errorf("PushUpdate: %v", err)
  391. }
  392. if opts.RefFullName == git.BranchPrefix+repo.DefaultBranch {
  393. models.UpdateRepoIndexer(repo)
  394. }
  395. return nil
  396. }