repo_editor.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. log "gopkg.in/clog.v1"
  18. git "github.com/gogs/git-module"
  19. "github.com/gogs/gogs/models/errors"
  20. "github.com/gogs/gogs/pkg/process"
  21. "github.com/gogs/gogs/pkg/setting"
  22. )
  23. // ___________ .___.__ __ ___________.__.__
  24. // \_ _____/ __| _/|__|/ |_ \_ _____/|__| | ____
  25. // | __)_ / __ | | \ __\ | __) | | | _/ __ \
  26. // | \/ /_/ | | || | | \ | | |_\ ___/
  27. // /_______ /\____ | |__||__| \___ / |__|____/\___ >
  28. // \/ \/ \/ \/
  29. // discardLocalRepoBranchChanges discards local commits/changes of
  30. // given branch to make sure it is even to remote branch.
  31. func discardLocalRepoBranchChanges(localPath, branch string) error {
  32. if !com.IsExist(localPath) {
  33. return nil
  34. }
  35. // No need to check if nothing in the repository.
  36. if !git.IsBranchExist(localPath, branch) {
  37. return nil
  38. }
  39. refName := "origin/" + branch
  40. if err := git.ResetHEAD(localPath, true, refName); err != nil {
  41. return fmt.Errorf("git reset --hard %s: %v", refName, err)
  42. }
  43. return nil
  44. }
  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. func (repo *Repository) CheckoutNewBranch(oldBranch, newBranch string) error {
  60. return checkoutNewBranch(repo.RepoPath(), repo.LocalCopyPath(), oldBranch, newBranch)
  61. }
  62. type UpdateRepoFileOptions struct {
  63. LastCommitID string
  64. OldBranch string
  65. NewBranch string
  66. OldTreeName string
  67. NewTreeName string
  68. Message string
  69. Content string
  70. IsNewFile bool
  71. }
  72. // UpdateRepoFile adds or updates a file in repository.
  73. func (repo *Repository) UpdateRepoFile(doer *User, opts UpdateRepoFileOptions) (err error) {
  74. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  75. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  76. if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
  77. return fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", opts.OldBranch, err)
  78. } else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
  79. return fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", opts.OldBranch, err)
  80. }
  81. repoPath := repo.RepoPath()
  82. localPath := repo.LocalCopyPath()
  83. if opts.OldBranch != opts.NewBranch {
  84. // Directly return error if new branch already exists in the server
  85. if git.IsBranchExist(repoPath, opts.NewBranch) {
  86. return errors.BranchAlreadyExists{opts.NewBranch}
  87. }
  88. // Otherwise, delete branch from local copy in case out of sync
  89. if git.IsBranchExist(localPath, opts.NewBranch) {
  90. if err = git.DeleteBranch(localPath, opts.NewBranch, git.DeleteBranchOptions{
  91. Force: true,
  92. }); err != nil {
  93. return fmt.Errorf("DeleteBranch [name: %s]: %v", opts.NewBranch, err)
  94. }
  95. }
  96. if err := repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
  97. return fmt.Errorf("CheckoutNewBranch [old_branch: %s, new_branch: %s]: %v", opts.OldBranch, opts.NewBranch, err)
  98. }
  99. }
  100. oldFilePath := path.Join(localPath, opts.OldTreeName)
  101. filePath := path.Join(localPath, opts.NewTreeName)
  102. os.MkdirAll(path.Dir(filePath), os.ModePerm)
  103. // If it's meant to be a new file, make sure it doesn't exist.
  104. if opts.IsNewFile {
  105. if com.IsExist(filePath) {
  106. return ErrRepoFileAlreadyExist{filePath}
  107. }
  108. }
  109. // Ignore move step if it's a new file under a directory.
  110. // Otherwise, move the file when name changed.
  111. if com.IsFile(oldFilePath) && opts.OldTreeName != opts.NewTreeName {
  112. if err = git.MoveFile(localPath, opts.OldTreeName, opts.NewTreeName); err != nil {
  113. return fmt.Errorf("git mv %s %s: %v", opts.OldTreeName, opts.NewTreeName, err)
  114. }
  115. }
  116. if err = ioutil.WriteFile(filePath, []byte(opts.Content), 0666); err != nil {
  117. return fmt.Errorf("WriteFile: %v", err)
  118. }
  119. if err = git.AddChanges(localPath, true); err != nil {
  120. return fmt.Errorf("git add --all: %v", err)
  121. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  122. Committer: doer.NewGitSig(),
  123. Message: opts.Message,
  124. }); err != nil {
  125. return fmt.Errorf("CommitChanges: %v", err)
  126. } else if err = git.Push(localPath, "origin", opts.NewBranch); err != nil {
  127. return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
  128. }
  129. gitRepo, err := git.OpenRepository(repo.RepoPath())
  130. if err != nil {
  131. log.Error(2, "OpenRepository: %v", err)
  132. return nil
  133. }
  134. commit, err := gitRepo.GetBranchCommit(opts.NewBranch)
  135. if err != nil {
  136. log.Error(2, "GetBranchCommit [branch: %s]: %v", opts.NewBranch, err)
  137. return nil
  138. }
  139. // Simulate push event.
  140. pushCommits := &PushCommits{
  141. Len: 1,
  142. Commits: []*PushCommit{CommitToPushCommit(commit)},
  143. }
  144. oldCommitID := opts.LastCommitID
  145. if opts.NewBranch != opts.OldBranch {
  146. oldCommitID = git.EMPTY_SHA
  147. }
  148. if err := CommitRepoAction(CommitRepoActionOptions{
  149. PusherName: doer.Name,
  150. RepoOwnerID: repo.MustOwner().ID,
  151. RepoName: repo.Name,
  152. RefFullName: git.BRANCH_PREFIX + opts.NewBranch,
  153. OldCommitID: oldCommitID,
  154. NewCommitID: commit.ID.String(),
  155. Commits: pushCommits,
  156. }); err != nil {
  157. log.Error(2, "CommitRepoAction: %v", err)
  158. return nil
  159. }
  160. go AddTestPullRequestTask(doer, repo.ID, opts.NewBranch, true)
  161. return nil
  162. }
  163. // GetDiffPreview produces and returns diff result of a file which is not yet committed.
  164. func (repo *Repository) GetDiffPreview(branch, treePath, content string) (diff *Diff, err error) {
  165. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  166. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  167. if err = repo.DiscardLocalRepoBranchChanges(branch); err != nil {
  168. return nil, fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", branch, err)
  169. } else if err = repo.UpdateLocalCopyBranch(branch); err != nil {
  170. return nil, fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", branch, err)
  171. }
  172. localPath := repo.LocalCopyPath()
  173. filePath := path.Join(localPath, treePath)
  174. os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
  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.Add(fmt.Sprintf("GetDiffPreview [repo_path: %s]", repo.RepoPath()), cmd)
  189. defer process.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. type DeleteRepoFileOptions struct {
  207. LastCommitID string
  208. OldBranch string
  209. NewBranch string
  210. TreePath string
  211. Message string
  212. }
  213. func (repo *Repository) DeleteRepoFile(doer *User, opts DeleteRepoFileOptions) (err error) {
  214. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  215. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  216. if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
  217. return fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", opts.OldBranch, err)
  218. } else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
  219. return fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", opts.OldBranch, err)
  220. }
  221. if opts.OldBranch != opts.NewBranch {
  222. if err := repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
  223. return fmt.Errorf("CheckoutNewBranch [old_branch: %s, new_branch: %s]: %v", opts.OldBranch, opts.NewBranch, err)
  224. }
  225. }
  226. localPath := repo.LocalCopyPath()
  227. if err = os.Remove(path.Join(localPath, opts.TreePath)); err != nil {
  228. return fmt.Errorf("Remove: %v", err)
  229. }
  230. if err = git.AddChanges(localPath, true); err != nil {
  231. return fmt.Errorf("git add --all: %v", err)
  232. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  233. Committer: doer.NewGitSig(),
  234. Message: opts.Message,
  235. }); err != nil {
  236. return fmt.Errorf("CommitChanges: %v", err)
  237. } else if err = git.Push(localPath, "origin", opts.NewBranch); err != nil {
  238. return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
  239. }
  240. gitRepo, err := git.OpenRepository(repo.RepoPath())
  241. if err != nil {
  242. log.Error(2, "OpenRepository: %v", err)
  243. return nil
  244. }
  245. commit, err := gitRepo.GetBranchCommit(opts.NewBranch)
  246. if err != nil {
  247. log.Error(2, "GetBranchCommit [branch: %s]: %v", opts.NewBranch, err)
  248. return nil
  249. }
  250. // Simulate push event.
  251. pushCommits := &PushCommits{
  252. Len: 1,
  253. Commits: []*PushCommit{CommitToPushCommit(commit)},
  254. }
  255. if err := CommitRepoAction(CommitRepoActionOptions{
  256. PusherName: doer.Name,
  257. RepoOwnerID: repo.MustOwner().ID,
  258. RepoName: repo.Name,
  259. RefFullName: git.BRANCH_PREFIX + opts.NewBranch,
  260. OldCommitID: opts.LastCommitID,
  261. NewCommitID: commit.ID.String(),
  262. Commits: pushCommits,
  263. }); err != nil {
  264. log.Error(2, "CommitRepoAction: %v", err)
  265. return nil
  266. }
  267. go AddTestPullRequestTask(doer, repo.ID, opts.NewBranch, true)
  268. return nil
  269. }
  270. // ____ ___ .__ .___ ___________.___.__
  271. // | | \______ | | _________ __| _/ \_ _____/| | | ____ ______
  272. // | | /\____ \| | / _ \__ \ / __ | | __) | | | _/ __ \ / ___/
  273. // | | / | |_> > |_( <_> ) __ \_/ /_/ | | \ | | |_\ ___/ \___ \
  274. // |______/ | __/|____/\____(____ /\____ | \___ / |___|____/\___ >____ >
  275. // |__| \/ \/ \/ \/ \/
  276. //
  277. // Upload represent a uploaded file to a repo to be deleted when moved
  278. type Upload struct {
  279. ID int64
  280. UUID string `xorm:"uuid UNIQUE"`
  281. Name string
  282. }
  283. // UploadLocalPath returns where uploads is stored in local file system based on given UUID.
  284. func UploadLocalPath(uuid string) string {
  285. return path.Join(setting.Repository.Upload.TempPath, uuid[0:1], uuid[1:2], uuid)
  286. }
  287. // LocalPath returns where uploads are temporarily stored in local file system.
  288. func (upload *Upload) LocalPath() string {
  289. return UploadLocalPath(upload.UUID)
  290. }
  291. // NewUpload creates a new upload object.
  292. func NewUpload(name string, buf []byte, file multipart.File) (_ *Upload, err error) {
  293. upload := &Upload{
  294. UUID: gouuid.NewV4().String(),
  295. Name: name,
  296. }
  297. localPath := upload.LocalPath()
  298. if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil {
  299. return nil, fmt.Errorf("MkdirAll: %v", err)
  300. }
  301. fw, err := os.Create(localPath)
  302. if err != nil {
  303. return nil, fmt.Errorf("Create: %v", err)
  304. }
  305. defer fw.Close()
  306. if _, err = fw.Write(buf); err != nil {
  307. return nil, fmt.Errorf("Write: %v", err)
  308. } else if _, err = io.Copy(fw, file); err != nil {
  309. return nil, fmt.Errorf("Copy: %v", err)
  310. }
  311. if _, err := x.Insert(upload); err != nil {
  312. return nil, err
  313. }
  314. return upload, nil
  315. }
  316. func GetUploadByUUID(uuid string) (*Upload, error) {
  317. upload := &Upload{UUID: uuid}
  318. has, err := x.Get(upload)
  319. if err != nil {
  320. return nil, err
  321. } else if !has {
  322. return nil, ErrUploadNotExist{0, uuid}
  323. }
  324. return upload, nil
  325. }
  326. func GetUploadsByUUIDs(uuids []string) ([]*Upload, error) {
  327. if len(uuids) == 0 {
  328. return []*Upload{}, nil
  329. }
  330. // Silently drop invalid uuids.
  331. uploads := make([]*Upload, 0, len(uuids))
  332. return uploads, x.In("uuid", uuids).Find(&uploads)
  333. }
  334. func DeleteUploads(uploads ...*Upload) (err error) {
  335. if len(uploads) == 0 {
  336. return nil
  337. }
  338. sess := x.NewSession()
  339. defer sess.Close()
  340. if err = sess.Begin(); err != nil {
  341. return err
  342. }
  343. ids := make([]int64, len(uploads))
  344. for i := 0; i < len(uploads); i++ {
  345. ids[i] = uploads[i].ID
  346. }
  347. if _, err = sess.In("id", ids).Delete(new(Upload)); err != nil {
  348. return fmt.Errorf("delete uploads: %v", err)
  349. }
  350. for _, upload := range uploads {
  351. localPath := upload.LocalPath()
  352. if !com.IsFile(localPath) {
  353. continue
  354. }
  355. if err := os.Remove(localPath); err != nil {
  356. return fmt.Errorf("remove upload: %v", err)
  357. }
  358. }
  359. return sess.Commit()
  360. }
  361. func DeleteUpload(u *Upload) error {
  362. return DeleteUploads(u)
  363. }
  364. func DeleteUploadByUUID(uuid string) error {
  365. upload, err := GetUploadByUUID(uuid)
  366. if err != nil {
  367. if IsErrUploadNotExist(err) {
  368. return nil
  369. }
  370. return fmt.Errorf("GetUploadByUUID: %v", err)
  371. }
  372. if err := DeleteUpload(upload); err != nil {
  373. return fmt.Errorf("DeleteUpload: %v", err)
  374. }
  375. return nil
  376. }
  377. type UploadRepoFileOptions struct {
  378. LastCommitID string
  379. OldBranch string
  380. NewBranch string
  381. TreePath string
  382. Message string
  383. Files []string // In UUID format.
  384. }
  385. func (repo *Repository) UploadRepoFiles(doer *User, opts UploadRepoFileOptions) (err error) {
  386. if len(opts.Files) == 0 {
  387. return nil
  388. }
  389. uploads, err := GetUploadsByUUIDs(opts.Files)
  390. if err != nil {
  391. return fmt.Errorf("GetUploadsByUUIDs [uuids: %v]: %v", opts.Files, err)
  392. }
  393. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  394. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  395. if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
  396. return fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", opts.OldBranch, err)
  397. } else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
  398. return fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", opts.OldBranch, err)
  399. }
  400. if opts.OldBranch != opts.NewBranch {
  401. if err = repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
  402. return fmt.Errorf("CheckoutNewBranch [old_branch: %s, new_branch: %s]: %v", opts.OldBranch, opts.NewBranch, err)
  403. }
  404. }
  405. localPath := repo.LocalCopyPath()
  406. dirPath := path.Join(localPath, opts.TreePath)
  407. os.MkdirAll(dirPath, os.ModePerm)
  408. // Copy uploaded files into repository.
  409. for _, upload := range uploads {
  410. tmpPath := upload.LocalPath()
  411. targetPath := path.Join(dirPath, upload.Name)
  412. if !com.IsFile(tmpPath) {
  413. continue
  414. }
  415. if err = com.Copy(tmpPath, targetPath); err != nil {
  416. return fmt.Errorf("Copy: %v", err)
  417. }
  418. }
  419. if err = git.AddChanges(localPath, true); err != nil {
  420. return fmt.Errorf("git add --all: %v", err)
  421. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  422. Committer: doer.NewGitSig(),
  423. Message: opts.Message,
  424. }); err != nil {
  425. return fmt.Errorf("CommitChanges: %v", err)
  426. } else if err = git.Push(localPath, "origin", opts.NewBranch); err != nil {
  427. return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
  428. }
  429. gitRepo, err := git.OpenRepository(repo.RepoPath())
  430. if err != nil {
  431. log.Error(2, "OpenRepository: %v", err)
  432. return nil
  433. }
  434. commit, err := gitRepo.GetBranchCommit(opts.NewBranch)
  435. if err != nil {
  436. log.Error(2, "GetBranchCommit [branch: %s]: %v", opts.NewBranch, err)
  437. return nil
  438. }
  439. // Simulate push event.
  440. pushCommits := &PushCommits{
  441. Len: 1,
  442. Commits: []*PushCommit{CommitToPushCommit(commit)},
  443. }
  444. if err := CommitRepoAction(CommitRepoActionOptions{
  445. PusherName: doer.Name,
  446. RepoOwnerID: repo.MustOwner().ID,
  447. RepoName: repo.Name,
  448. RefFullName: git.BRANCH_PREFIX + opts.NewBranch,
  449. OldCommitID: opts.LastCommitID,
  450. NewCommitID: commit.ID.String(),
  451. Commits: pushCommits,
  452. }); err != nil {
  453. log.Error(2, "CommitRepoAction: %v", err)
  454. return nil
  455. }
  456. go AddTestPullRequestTask(doer, repo.ID, opts.NewBranch, true)
  457. return DeleteUploads(uploads...)
  458. }