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.

514 lines
15 KiB

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
5 years ago
  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. defer gitRepo.Close()
  390. if err = repo.UpdateSize(); err != nil {
  391. log.Error("Failed to update size for repository: %v", err)
  392. }
  393. var commits = &models.PushCommits{}
  394. if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
  395. // If is tag reference
  396. tagName := opts.RefFullName[len(git.TagPrefix):]
  397. if isDelRef {
  398. err = models.PushUpdateDeleteTag(repo, tagName)
  399. if err != nil {
  400. return fmt.Errorf("PushUpdateDeleteTag: %v", err)
  401. }
  402. } else {
  403. // Clear cache for tag commit count
  404. cache.Remove(repo.GetCommitsCountCacheKey(tagName, true))
  405. err = models.PushUpdateAddTag(repo, gitRepo, tagName)
  406. if err != nil {
  407. return fmt.Errorf("PushUpdateAddTag: %v", err)
  408. }
  409. }
  410. } else if !isDelRef {
  411. // If is branch reference
  412. // Clear cache for branch commit count
  413. cache.Remove(repo.GetCommitsCountCacheKey(opts.RefFullName[len(git.BranchPrefix):], true))
  414. newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
  415. if err != nil {
  416. return fmt.Errorf("gitRepo.GetCommit: %v", err)
  417. }
  418. // Push new branch.
  419. var l *list.List
  420. if isNewRef {
  421. l, err = newCommit.CommitsBeforeLimit(10)
  422. if err != nil {
  423. return fmt.Errorf("newCommit.CommitsBeforeLimit: %v", err)
  424. }
  425. } else {
  426. l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
  427. if err != nil {
  428. return fmt.Errorf("newCommit.CommitsBeforeUntil: %v", err)
  429. }
  430. }
  431. commits = models.ListToPushCommits(l)
  432. }
  433. if err := CommitRepoAction(CommitRepoActionOptions{
  434. PusherName: opts.PusherName,
  435. RepoOwnerID: repo.OwnerID,
  436. RepoName: repo.Name,
  437. RefFullName: opts.RefFullName,
  438. OldCommitID: opts.OldCommitID,
  439. NewCommitID: opts.NewCommitID,
  440. Commits: commits,
  441. }); err != nil {
  442. return fmt.Errorf("CommitRepoAction: %v", err)
  443. }
  444. pusher, err := models.GetUserByID(opts.PusherID)
  445. if err != nil {
  446. return err
  447. }
  448. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  449. go pull_service.AddTestPullRequestTask(pusher, repo.ID, branch, true)
  450. if opts.RefFullName == git.BranchPrefix+repo.DefaultBranch {
  451. models.UpdateRepoIndexer(repo)
  452. }
  453. if err = models.WatchIfAuto(opts.PusherID, repo.ID, true); err != nil {
  454. log.Warn("Fail to perform auto watch on user %v for repo %v: %v", opts.PusherID, repo.ID, err)
  455. }
  456. return nil
  457. }