Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

repo_editor.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. // Copyright 2016 The Gogs 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 models
  5. import (
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "mime/multipart"
  10. "os"
  11. "os/exec"
  12. "path"
  13. "path/filepath"
  14. "time"
  15. "github.com/Unknwon/com"
  16. gouuid "github.com/satori/go.uuid"
  17. "code.gitea.io/git"
  18. "code.gitea.io/gitea/modules/log"
  19. "code.gitea.io/gitea/modules/process"
  20. "code.gitea.io/gitea/modules/setting"
  21. )
  22. // ___________ .___.__ __ ___________.__.__
  23. // \_ _____/ __| _/|__|/ |_ \_ _____/|__| | ____
  24. // | __)_ / __ | | \ __\ | __) | | | _/ __ \
  25. // | \/ /_/ | | || | | \ | | |_\ ___/
  26. // /_______ /\____ | |__||__| \___ / |__|____/\___ >
  27. // \/ \/ \/ \/
  28. // discardLocalRepoBranchChanges discards local commits/changes of
  29. // given branch to make sure it is even to remote branch.
  30. func discardLocalRepoBranchChanges(localPath, branch string) error {
  31. if !com.IsExist(localPath) {
  32. return nil
  33. }
  34. // No need to check if nothing in the repository.
  35. if !git.IsBranchExist(localPath, branch) {
  36. return nil
  37. }
  38. refName := "origin/" + branch
  39. if err := git.ResetHEAD(localPath, true, refName); err != nil {
  40. return fmt.Errorf("git reset --hard %s: %v", refName, err)
  41. }
  42. return nil
  43. }
  44. // DiscardLocalRepoBranchChanges discards the local repository branch changes
  45. func (repo *Repository) DiscardLocalRepoBranchChanges(branch string) error {
  46. return discardLocalRepoBranchChanges(repo.LocalCopyPath(), branch)
  47. }
  48. // checkoutNewBranch checks out to a new branch from the a branch name.
  49. func checkoutNewBranch(repoPath, localPath, oldBranch, newBranch string) error {
  50. if err := git.Checkout(localPath, git.CheckoutOptions{
  51. Timeout: time.Duration(setting.Git.Timeout.Pull) * time.Second,
  52. Branch: newBranch,
  53. OldBranch: oldBranch,
  54. }); err != nil {
  55. return fmt.Errorf("git checkout -b %s %s: %v", newBranch, oldBranch, err)
  56. }
  57. return nil
  58. }
  59. // CheckoutNewBranch checks out a new branch
  60. func (repo *Repository) CheckoutNewBranch(oldBranch, newBranch string) error {
  61. return checkoutNewBranch(repo.RepoPath(), repo.LocalCopyPath(), oldBranch, newBranch)
  62. }
  63. // UpdateRepoFileOptions holds the repository file update options
  64. type UpdateRepoFileOptions struct {
  65. LastCommitID string
  66. OldBranch string
  67. NewBranch string
  68. OldTreeName string
  69. NewTreeName string
  70. Message string
  71. Content string
  72. IsNewFile bool
  73. }
  74. // UpdateRepoFile adds or updates a file in repository.
  75. func (repo *Repository) UpdateRepoFile(doer *User, opts UpdateRepoFileOptions) (err error) {
  76. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  77. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  78. if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
  79. return fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", opts.OldBranch, err)
  80. } else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
  81. return fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", opts.OldBranch, err)
  82. }
  83. if opts.OldBranch != opts.NewBranch {
  84. if err := repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
  85. return fmt.Errorf("CheckoutNewBranch [old_branch: %s, new_branch: %s]: %v", opts.OldBranch, opts.NewBranch, err)
  86. }
  87. }
  88. localPath := repo.LocalCopyPath()
  89. oldFilePath := path.Join(localPath, opts.OldTreeName)
  90. filePath := path.Join(localPath, opts.NewTreeName)
  91. dir := path.Dir(filePath)
  92. if err := os.MkdirAll(dir, os.ModePerm); err != nil {
  93. return fmt.Errorf("Failed to create dir %s: %v", dir, err)
  94. }
  95. // If it's meant to be a new file, make sure it doesn't exist.
  96. if opts.IsNewFile {
  97. if com.IsExist(filePath) {
  98. return ErrRepoFileAlreadyExist{filePath}
  99. }
  100. }
  101. // Ignore move step if it's a new file under a directory.
  102. // Otherwise, move the file when name changed.
  103. if com.IsFile(oldFilePath) && opts.OldTreeName != opts.NewTreeName {
  104. if err = git.MoveFile(localPath, opts.OldTreeName, opts.NewTreeName); err != nil {
  105. return fmt.Errorf("git mv %s %s: %v", opts.OldTreeName, opts.NewTreeName, err)
  106. }
  107. }
  108. if err = ioutil.WriteFile(filePath, []byte(opts.Content), 0666); err != nil {
  109. return fmt.Errorf("WriteFile: %v", err)
  110. }
  111. if err = git.AddChanges(localPath, true); err != nil {
  112. return fmt.Errorf("git add --all: %v", err)
  113. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  114. Committer: doer.NewGitSig(),
  115. Message: opts.Message,
  116. }); err != nil {
  117. return fmt.Errorf("CommitChanges: %v", err)
  118. } else if err = git.Push(localPath, git.PushOptions{
  119. Remote: "origin",
  120. Branch: opts.NewBranch,
  121. }); err != nil {
  122. return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
  123. }
  124. gitRepo, err := git.OpenRepository(repo.RepoPath())
  125. if err != nil {
  126. log.Error(4, "OpenRepository: %v", err)
  127. return nil
  128. }
  129. commit, err := gitRepo.GetBranchCommit(opts.NewBranch)
  130. if err != nil {
  131. log.Error(4, "GetBranchCommit [branch: %s]: %v", opts.NewBranch, err)
  132. return nil
  133. }
  134. // Simulate push event.
  135. oldCommitID := opts.LastCommitID
  136. if opts.NewBranch != opts.OldBranch {
  137. oldCommitID = git.EmptySHA
  138. }
  139. if err = repo.GetOwner(); err != nil {
  140. return fmt.Errorf("GetOwner: %v", err)
  141. }
  142. err = PushUpdate(
  143. opts.NewBranch,
  144. PushUpdateOptions{
  145. PusherID: doer.ID,
  146. PusherName: doer.Name,
  147. RepoUserName: repo.Owner.Name,
  148. RepoName: repo.Name,
  149. RefFullName: git.BranchPrefix + opts.NewBranch,
  150. OldCommitID: oldCommitID,
  151. NewCommitID: commit.ID.String(),
  152. },
  153. )
  154. if err != nil {
  155. return fmt.Errorf("PushUpdate: %v", err)
  156. }
  157. UpdateRepoIndexer(repo)
  158. return nil
  159. }
  160. // GetDiffPreview produces and returns diff result of a file which is not yet committed.
  161. func (repo *Repository) GetDiffPreview(branch, treePath, content string) (diff *Diff, err error) {
  162. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  163. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  164. if err = repo.DiscardLocalRepoBranchChanges(branch); err != nil {
  165. return nil, fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", branch, err)
  166. } else if err = repo.UpdateLocalCopyBranch(branch); err != nil {
  167. return nil, fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", branch, err)
  168. }
  169. localPath := repo.LocalCopyPath()
  170. filePath := path.Join(localPath, treePath)
  171. dir := filepath.Dir(filePath)
  172. if err := os.MkdirAll(dir, os.ModePerm); err != nil {
  173. return nil, fmt.Errorf("Failed to create dir %s: %v", dir, err)
  174. }
  175. if err = ioutil.WriteFile(filePath, []byte(content), 0666); err != nil {
  176. return nil, fmt.Errorf("WriteFile: %v", err)
  177. }
  178. cmd := exec.Command("git", "diff", treePath)
  179. cmd.Dir = localPath
  180. cmd.Stderr = os.Stderr
  181. stdout, err := cmd.StdoutPipe()
  182. if err != nil {
  183. return nil, fmt.Errorf("StdoutPipe: %v", err)
  184. }
  185. if err = cmd.Start(); err != nil {
  186. return nil, fmt.Errorf("Start: %v", err)
  187. }
  188. pid := process.GetManager().Add(fmt.Sprintf("GetDiffPreview [repo_path: %s]", repo.RepoPath()), cmd)
  189. defer process.GetManager().Remove(pid)
  190. diff, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdout)
  191. if err != nil {
  192. return nil, fmt.Errorf("ParsePatch: %v", err)
  193. }
  194. if err = cmd.Wait(); err != nil {
  195. return nil, fmt.Errorf("Wait: %v", err)
  196. }
  197. return diff, nil
  198. }
  199. // ________ .__ __ ___________.__.__
  200. // \______ \ ____ | | _____/ |_ ____ \_ _____/|__| | ____
  201. // | | \_/ __ \| | _/ __ \ __\/ __ \ | __) | | | _/ __ \
  202. // | ` \ ___/| |_\ ___/| | \ ___/ | \ | | |_\ ___/
  203. // /_______ /\___ >____/\___ >__| \___ > \___ / |__|____/\___ >
  204. // \/ \/ \/ \/ \/ \/
  205. //
  206. // DeleteRepoFileOptions holds the repository delete file options
  207. type DeleteRepoFileOptions struct {
  208. LastCommitID string
  209. OldBranch string
  210. NewBranch string
  211. TreePath string
  212. Message string
  213. }
  214. // DeleteRepoFile deletes a repository file
  215. func (repo *Repository) DeleteRepoFile(doer *User, opts DeleteRepoFileOptions) (err error) {
  216. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  217. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  218. if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
  219. return fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", opts.OldBranch, err)
  220. } else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
  221. return fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", opts.OldBranch, err)
  222. }
  223. if opts.OldBranch != opts.NewBranch {
  224. if err := repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
  225. return fmt.Errorf("CheckoutNewBranch [old_branch: %s, new_branch: %s]: %v", opts.OldBranch, opts.NewBranch, err)
  226. }
  227. }
  228. localPath := repo.LocalCopyPath()
  229. if err = os.Remove(path.Join(localPath, opts.TreePath)); err != nil {
  230. return fmt.Errorf("Remove: %v", err)
  231. }
  232. if err = git.AddChanges(localPath, true); err != nil {
  233. return fmt.Errorf("git add --all: %v", err)
  234. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  235. Committer: doer.NewGitSig(),
  236. Message: opts.Message,
  237. }); err != nil {
  238. return fmt.Errorf("CommitChanges: %v", err)
  239. } else if err = git.Push(localPath, git.PushOptions{
  240. Remote: "origin",
  241. Branch: opts.NewBranch,
  242. }); err != nil {
  243. return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
  244. }
  245. gitRepo, err := git.OpenRepository(repo.RepoPath())
  246. if err != nil {
  247. log.Error(4, "OpenRepository: %v", err)
  248. return nil
  249. }
  250. commit, err := gitRepo.GetBranchCommit(opts.NewBranch)
  251. if err != nil {
  252. log.Error(4, "GetBranchCommit [branch: %s]: %v", opts.NewBranch, err)
  253. return nil
  254. }
  255. // Simulate push event.
  256. oldCommitID := opts.LastCommitID
  257. if opts.NewBranch != opts.OldBranch {
  258. oldCommitID = git.EmptySHA
  259. }
  260. if err = repo.GetOwner(); err != nil {
  261. return fmt.Errorf("GetOwner: %v", err)
  262. }
  263. err = PushUpdate(
  264. opts.NewBranch,
  265. PushUpdateOptions{
  266. PusherID: doer.ID,
  267. PusherName: doer.Name,
  268. RepoUserName: repo.Owner.Name,
  269. RepoName: repo.Name,
  270. RefFullName: git.BranchPrefix + opts.NewBranch,
  271. OldCommitID: oldCommitID,
  272. NewCommitID: commit.ID.String(),
  273. },
  274. )
  275. if err != nil {
  276. return fmt.Errorf("PushUpdate: %v", err)
  277. }
  278. return nil
  279. }
  280. // ____ ___ .__ .___ ___________.___.__
  281. // | | \______ | | _________ __| _/ \_ _____/| | | ____ ______
  282. // | | /\____ \| | / _ \__ \ / __ | | __) | | | _/ __ \ / ___/
  283. // | | / | |_> > |_( <_> ) __ \_/ /_/ | | \ | | |_\ ___/ \___ \
  284. // |______/ | __/|____/\____(____ /\____ | \___ / |___|____/\___ >____ >
  285. // |__| \/ \/ \/ \/ \/
  286. //
  287. // Upload represent a uploaded file to a repo to be deleted when moved
  288. type Upload struct {
  289. ID int64 `xorm:"pk autoincr"`
  290. UUID string `xorm:"uuid UNIQUE"`
  291. Name string
  292. }
  293. // UploadLocalPath returns where uploads is stored in local file system based on given UUID.
  294. func UploadLocalPath(uuid string) string {
  295. return path.Join(setting.Repository.Upload.TempPath, uuid[0:1], uuid[1:2], uuid)
  296. }
  297. // LocalPath returns where uploads are temporarily stored in local file system.
  298. func (upload *Upload) LocalPath() string {
  299. return UploadLocalPath(upload.UUID)
  300. }
  301. // NewUpload creates a new upload object.
  302. func NewUpload(name string, buf []byte, file multipart.File) (_ *Upload, err error) {
  303. upload := &Upload{
  304. UUID: gouuid.NewV4().String(),
  305. Name: name,
  306. }
  307. localPath := upload.LocalPath()
  308. if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
  309. return nil, fmt.Errorf("MkdirAll: %v", err)
  310. }
  311. fw, err := os.Create(localPath)
  312. if err != nil {
  313. return nil, fmt.Errorf("Create: %v", err)
  314. }
  315. defer fw.Close()
  316. if _, err = fw.Write(buf); err != nil {
  317. return nil, fmt.Errorf("Write: %v", err)
  318. } else if _, err = io.Copy(fw, file); err != nil {
  319. return nil, fmt.Errorf("Copy: %v", err)
  320. }
  321. if _, err := x.Insert(upload); err != nil {
  322. return nil, err
  323. }
  324. return upload, nil
  325. }
  326. // GetUploadByUUID returns the Upload by UUID
  327. func GetUploadByUUID(uuid string) (*Upload, error) {
  328. upload := &Upload{UUID: uuid}
  329. has, err := x.Get(upload)
  330. if err != nil {
  331. return nil, err
  332. } else if !has {
  333. return nil, ErrUploadNotExist{0, uuid}
  334. }
  335. return upload, nil
  336. }
  337. // GetUploadsByUUIDs returns multiple uploads by UUIDS
  338. func GetUploadsByUUIDs(uuids []string) ([]*Upload, error) {
  339. if len(uuids) == 0 {
  340. return []*Upload{}, nil
  341. }
  342. // Silently drop invalid uuids.
  343. uploads := make([]*Upload, 0, len(uuids))
  344. return uploads, x.In("uuid", uuids).Find(&uploads)
  345. }
  346. // DeleteUploads deletes multiple uploads
  347. func DeleteUploads(uploads ...*Upload) (err error) {
  348. if len(uploads) == 0 {
  349. return nil
  350. }
  351. sess := x.NewSession()
  352. defer sess.Close()
  353. if err = sess.Begin(); err != nil {
  354. return err
  355. }
  356. ids := make([]int64, len(uploads))
  357. for i := 0; i < len(uploads); i++ {
  358. ids[i] = uploads[i].ID
  359. }
  360. if _, err = sess.
  361. In("id", ids).
  362. Delete(new(Upload)); err != nil {
  363. return fmt.Errorf("delete uploads: %v", err)
  364. }
  365. for _, upload := range uploads {
  366. localPath := upload.LocalPath()
  367. if !com.IsFile(localPath) {
  368. continue
  369. }
  370. if err := os.Remove(localPath); err != nil {
  371. return fmt.Errorf("remove upload: %v", err)
  372. }
  373. }
  374. return sess.Commit()
  375. }
  376. // DeleteUpload delete a upload
  377. func DeleteUpload(u *Upload) error {
  378. return DeleteUploads(u)
  379. }
  380. // DeleteUploadByUUID deletes a upload by UUID
  381. func DeleteUploadByUUID(uuid string) error {
  382. upload, err := GetUploadByUUID(uuid)
  383. if err != nil {
  384. if IsErrUploadNotExist(err) {
  385. return nil
  386. }
  387. return fmt.Errorf("GetUploadByUUID: %v", err)
  388. }
  389. if err := DeleteUpload(upload); err != nil {
  390. return fmt.Errorf("DeleteUpload: %v", err)
  391. }
  392. return nil
  393. }
  394. // UploadRepoFileOptions contains the uploaded repository file options
  395. type UploadRepoFileOptions struct {
  396. LastCommitID string
  397. OldBranch string
  398. NewBranch string
  399. TreePath string
  400. Message string
  401. Files []string // In UUID format.
  402. }
  403. // UploadRepoFiles uploads files to a repository
  404. func (repo *Repository) UploadRepoFiles(doer *User, opts UploadRepoFileOptions) (err error) {
  405. if len(opts.Files) == 0 {
  406. return nil
  407. }
  408. uploads, err := GetUploadsByUUIDs(opts.Files)
  409. if err != nil {
  410. return fmt.Errorf("GetUploadsByUUIDs [uuids: %v]: %v", opts.Files, err)
  411. }
  412. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  413. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  414. if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
  415. return fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", opts.OldBranch, err)
  416. } else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
  417. return fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", opts.OldBranch, err)
  418. }
  419. if opts.OldBranch != opts.NewBranch {
  420. if err = repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
  421. return fmt.Errorf("CheckoutNewBranch [old_branch: %s, new_branch: %s]: %v", opts.OldBranch, opts.NewBranch, err)
  422. }
  423. }
  424. localPath := repo.LocalCopyPath()
  425. dirPath := path.Join(localPath, opts.TreePath)
  426. if err := os.MkdirAll(dirPath, os.ModePerm); err != nil {
  427. return fmt.Errorf("Failed to create dir %s: %v", dirPath, err)
  428. }
  429. // Copy uploaded files into repository.
  430. for _, upload := range uploads {
  431. tmpPath := upload.LocalPath()
  432. targetPath := path.Join(dirPath, upload.Name)
  433. if !com.IsFile(tmpPath) {
  434. continue
  435. }
  436. if err = com.Copy(tmpPath, targetPath); err != nil {
  437. return fmt.Errorf("Copy: %v", err)
  438. }
  439. }
  440. if err = git.AddChanges(localPath, true); err != nil {
  441. return fmt.Errorf("git add --all: %v", err)
  442. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  443. Committer: doer.NewGitSig(),
  444. Message: opts.Message,
  445. }); err != nil {
  446. return fmt.Errorf("CommitChanges: %v", err)
  447. } else if err = git.Push(localPath, git.PushOptions{
  448. Remote: "origin",
  449. Branch: opts.NewBranch,
  450. }); err != nil {
  451. return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
  452. }
  453. gitRepo, err := git.OpenRepository(repo.RepoPath())
  454. if err != nil {
  455. log.Error(4, "OpenRepository: %v", err)
  456. return nil
  457. }
  458. commit, err := gitRepo.GetBranchCommit(opts.NewBranch)
  459. if err != nil {
  460. log.Error(4, "GetBranchCommit [branch: %s]: %v", opts.NewBranch, err)
  461. return nil
  462. }
  463. // Simulate push event.
  464. oldCommitID := opts.LastCommitID
  465. if opts.NewBranch != opts.OldBranch {
  466. oldCommitID = git.EmptySHA
  467. }
  468. if err = repo.GetOwner(); err != nil {
  469. return fmt.Errorf("GetOwner: %v", err)
  470. }
  471. err = PushUpdate(
  472. opts.NewBranch,
  473. PushUpdateOptions{
  474. PusherID: doer.ID,
  475. PusherName: doer.Name,
  476. RepoUserName: repo.Owner.Name,
  477. RepoName: repo.Name,
  478. RefFullName: git.BranchPrefix + opts.NewBranch,
  479. OldCommitID: oldCommitID,
  480. NewCommitID: commit.ID.String(),
  481. },
  482. )
  483. if err != nil {
  484. return fmt.Errorf("PushUpdate: %v", err)
  485. }
  486. return DeleteUploads(uploads...)
  487. }