repo_editor.go 18 KB

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