repo_editor.go 16 KB

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