repo_editor.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. "strings"
  15. "time"
  16. "github.com/Unknwon/com"
  17. gouuid "github.com/satori/go.uuid"
  18. log "gopkg.in/clog.v1"
  19. git "github.com/G-Node/git-module"
  20. "bytes"
  21. "encoding/json"
  22. "net/http"
  23. gannex "github.com/G-Node/go-annex"
  24. "github.com/G-Node/gogs/models/errors"
  25. "github.com/G-Node/gogs/pkg/process"
  26. "github.com/G-Node/gogs/pkg/setting"
  27. "github.com/G-Node/gogs/pkg/tool"
  28. )
  29. const (
  30. ENV_AUTH_USER_ID = "GOGS_AUTH_USER_ID"
  31. ENV_AUTH_USER_NAME = "GOGS_AUTH_USER_NAME"
  32. ENV_AUTH_USER_EMAIL = "GOGS_AUTH_USER_EMAIL"
  33. ENV_REPO_OWNER_NAME = "GOGS_REPO_OWNER_NAME"
  34. ENV_REPO_OWNER_SALT_MD5 = "GOGS_REPO_OWNER_SALT_MD5"
  35. ENV_REPO_ID = "GOGS_REPO_ID"
  36. ENV_REPO_NAME = "GOGS_REPO_NAME"
  37. ENV_REPO_CUSTOM_HOOKS_PATH = "GOGS_REPO_CUSTOM_HOOKS_PATH"
  38. )
  39. type ComposeHookEnvsOptions struct {
  40. AuthUser *User
  41. OwnerName string
  42. OwnerSalt string
  43. RepoID int64
  44. RepoName string
  45. RepoPath string
  46. }
  47. func ComposeHookEnvs(opts ComposeHookEnvsOptions) []string {
  48. envs := []string{
  49. "SSH_ORIGINAL_COMMAND=1",
  50. ENV_AUTH_USER_ID + "=" + com.ToStr(opts.AuthUser.ID),
  51. ENV_AUTH_USER_NAME + "=" + opts.AuthUser.Name,
  52. ENV_AUTH_USER_EMAIL + "=" + opts.AuthUser.Email,
  53. ENV_REPO_OWNER_NAME + "=" + opts.OwnerName,
  54. ENV_REPO_OWNER_SALT_MD5 + "=" + tool.MD5(opts.OwnerSalt),
  55. ENV_REPO_ID + "=" + com.ToStr(opts.RepoID),
  56. ENV_REPO_NAME + "=" + opts.RepoName,
  57. ENV_REPO_CUSTOM_HOOKS_PATH + "=" + path.Join(opts.RepoPath, "custom_hooks"),
  58. }
  59. return envs
  60. }
  61. // ___________ .___.__ __ ___________.__.__
  62. // \_ _____/ __| _/|__|/ |_ \_ _____/|__| | ____
  63. // | __)_ / __ | | \ __\ | __) | | | _/ __ \
  64. // | \/ /_/ | | || | | \ | | |_\ ___/
  65. // /_______ /\____ | |__||__| \___ / |__|____/\___ >
  66. // \/ \/ \/ \/
  67. // discardLocalRepoBranchChanges discards local commits/changes of
  68. // given branch to make sure it is even to remote branch.
  69. func discardLocalRepoBranchChanges(localPath, branch string) error {
  70. if !com.IsExist(localPath) {
  71. return nil
  72. }
  73. // No need to check if nothing in the repository.
  74. if !git.IsBranchExist(localPath, branch) {
  75. return nil
  76. }
  77. refName := "origin/" + branch
  78. if err := git.ResetHEAD(localPath, true, refName); err != nil {
  79. return fmt.Errorf("git reset --hard %s: %v", refName, err)
  80. }
  81. return nil
  82. }
  83. func (repo *Repository) DiscardLocalRepoBranchChanges(branch string) error {
  84. return discardLocalRepoBranchChanges(repo.LocalCopyPath(), branch)
  85. }
  86. // checkoutNewBranch checks out to a new branch from the a branch name.
  87. func checkoutNewBranch(repoPath, localPath, oldBranch, newBranch string) error {
  88. if err := git.Checkout(localPath, git.CheckoutOptions{
  89. Timeout: time.Duration(setting.Git.Timeout.Pull) * time.Second,
  90. Branch: newBranch,
  91. OldBranch: oldBranch,
  92. }); err != nil {
  93. return fmt.Errorf("git checkout -b %s %s: %v", newBranch, oldBranch, err)
  94. }
  95. return nil
  96. }
  97. func (repo *Repository) CheckoutNewBranch(oldBranch, newBranch string) error {
  98. return checkoutNewBranch(repo.RepoPath(), repo.LocalCopyPath(), oldBranch, newBranch)
  99. }
  100. type UpdateRepoFileOptions struct {
  101. LastCommitID string
  102. OldBranch string
  103. NewBranch string
  104. OldTreeName string
  105. NewTreeName string
  106. Message string
  107. Content string
  108. IsNewFile bool
  109. }
  110. // UpdateRepoFile adds or updates a file in repository.
  111. func (repo *Repository) UpdateRepoFile(doer *User, opts UpdateRepoFileOptions) (err error) {
  112. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  113. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  114. if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
  115. return fmt.Errorf("discard local repo branch[%s] changes: %v", opts.OldBranch, err)
  116. } else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
  117. return fmt.Errorf("update local copy branch[%s]: %v", opts.OldBranch, err)
  118. }
  119. repoPath := repo.RepoPath()
  120. localPath := repo.LocalCopyPath()
  121. if opts.OldBranch != opts.NewBranch {
  122. // Directly return error if new branch already exists in the server
  123. if git.IsBranchExist(repoPath, opts.NewBranch) {
  124. return errors.BranchAlreadyExists{opts.NewBranch}
  125. }
  126. // Otherwise, delete branch from local copy in case out of sync
  127. if git.IsBranchExist(localPath, opts.NewBranch) {
  128. if err = git.DeleteBranch(localPath, opts.NewBranch, git.DeleteBranchOptions{
  129. Force: true,
  130. }); err != nil {
  131. return fmt.Errorf("delete branch[%s]: %v", opts.NewBranch, err)
  132. }
  133. }
  134. if err := repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
  135. return fmt.Errorf("checkout new branch[%s] from old branch[%s]: %v", opts.NewBranch, opts.OldBranch, err)
  136. }
  137. }
  138. oldFilePath := path.Join(localPath, opts.OldTreeName)
  139. filePath := path.Join(localPath, opts.NewTreeName)
  140. os.MkdirAll(path.Dir(filePath), os.ModePerm)
  141. // If it's meant to be a new file, make sure it doesn't exist.
  142. if opts.IsNewFile {
  143. if com.IsExist(filePath) {
  144. return ErrRepoFileAlreadyExist{filePath}
  145. }
  146. }
  147. // Ignore move step if it's a new file under a directory.
  148. // Otherwise, move the file when name changed.
  149. if com.IsFile(oldFilePath) && opts.OldTreeName != opts.NewTreeName {
  150. if err = git.MoveFile(localPath, opts.OldTreeName, opts.NewTreeName); err != nil {
  151. return fmt.Errorf("git mv %q %q: %v", opts.OldTreeName, opts.NewTreeName, err)
  152. }
  153. }
  154. if err = ioutil.WriteFile(filePath, []byte(opts.Content), 0666); err != nil {
  155. return fmt.Errorf("write file: %v", err)
  156. }
  157. if err = git.AddChanges(localPath, true); err != nil {
  158. return fmt.Errorf("git add --all: %v", err)
  159. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  160. Committer: doer.NewGitSig(),
  161. Message: opts.Message,
  162. }); err != nil {
  163. return fmt.Errorf("commit changes on %q: %v", localPath, err)
  164. } else if err = git.PushWithEnvs(localPath, "origin", opts.NewBranch,
  165. ComposeHookEnvs(ComposeHookEnvsOptions{
  166. AuthUser: doer,
  167. OwnerName: repo.MustOwner().Name,
  168. OwnerSalt: repo.MustOwner().Salt,
  169. RepoID: repo.ID,
  170. RepoName: repo.Name,
  171. RepoPath: repo.RepoPath(),
  172. })); err != nil {
  173. return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
  174. }
  175. if setting.Search.Do {
  176. StartIndexing(doer, repo.MustOwner(), repo)
  177. }
  178. return nil
  179. }
  180. // GetDiffPreview produces and returns diff result of a file which is not yet committed.
  181. func (repo *Repository) GetDiffPreview(branch, treePath, content string) (diff *Diff, err error) {
  182. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  183. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  184. if err = repo.DiscardLocalRepoBranchChanges(branch); err != nil {
  185. return nil, fmt.Errorf("discard local repo branch[%s] changes: %v", branch, err)
  186. } else if err = repo.UpdateLocalCopyBranch(branch); err != nil {
  187. return nil, fmt.Errorf("update local copy branch[%s]: %v", branch, err)
  188. }
  189. localPath := repo.LocalCopyPath()
  190. filePath := path.Join(localPath, treePath)
  191. os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
  192. if err = ioutil.WriteFile(filePath, []byte(content), 0666); err != nil {
  193. return nil, fmt.Errorf("write file: %v", err)
  194. }
  195. cmd := exec.Command("git", "diff", treePath)
  196. cmd.Dir = localPath
  197. cmd.Stderr = os.Stderr
  198. stdout, err := cmd.StdoutPipe()
  199. if err != nil {
  200. return nil, fmt.Errorf("get stdout pipe: %v", err)
  201. }
  202. if err = cmd.Start(); err != nil {
  203. return nil, fmt.Errorf("start: %v", err)
  204. }
  205. pid := process.Add(fmt.Sprintf("GetDiffPreview [repo_path: %s]", repo.RepoPath()), cmd)
  206. defer process.Remove(pid)
  207. diff, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdout)
  208. if err != nil {
  209. return nil, fmt.Errorf("parse path: %v", err)
  210. }
  211. if err = cmd.Wait(); err != nil {
  212. return nil, fmt.Errorf("wait: %v", err)
  213. }
  214. return diff, nil
  215. }
  216. // ________ .__ __ ___________.__.__
  217. // \______ \ ____ | | _____/ |_ ____ \_ _____/|__| | ____
  218. // | | \_/ __ \| | _/ __ \ __\/ __ \ | __) | | | _/ __ \
  219. // | ` \ ___/| |_\ ___/| | \ ___/ | \ | | |_\ ___/
  220. // /_______ /\___ >____/\___ >__| \___ > \___ / |__|____/\___ >
  221. // \/ \/ \/ \/ \/ \/
  222. //
  223. type DeleteRepoFileOptions struct {
  224. LastCommitID string
  225. OldBranch string
  226. NewBranch string
  227. TreePath string
  228. Message string
  229. }
  230. func (repo *Repository) DeleteRepoFile(doer *User, opts DeleteRepoFileOptions) (err error) {
  231. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  232. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  233. if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
  234. return fmt.Errorf("discard local repo branch[%s] changes: %v", opts.OldBranch, err)
  235. } else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
  236. return fmt.Errorf("update local copy branch[%s]: %v", opts.OldBranch, err)
  237. }
  238. if opts.OldBranch != opts.NewBranch {
  239. if err := repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
  240. return fmt.Errorf("checkout new branch[%s] from old branch[%s]: %v", opts.NewBranch, opts.OldBranch, err)
  241. }
  242. }
  243. localPath := repo.LocalCopyPath()
  244. if err = os.Remove(path.Join(localPath, opts.TreePath)); err != nil {
  245. return fmt.Errorf("remove file %q: %v", opts.TreePath, err)
  246. }
  247. if err = git.AddChanges(localPath, true); err != nil {
  248. return fmt.Errorf("git add --all: %v", err)
  249. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  250. Committer: doer.NewGitSig(),
  251. Message: opts.Message,
  252. }); err != nil {
  253. return fmt.Errorf("commit changes to %q: %v", localPath, err)
  254. } else if err = git.PushWithEnvs(localPath, "origin", opts.NewBranch,
  255. ComposeHookEnvs(ComposeHookEnvsOptions{
  256. AuthUser: doer,
  257. OwnerName: repo.MustOwner().Name,
  258. OwnerSalt: repo.MustOwner().Salt,
  259. RepoID: repo.ID,
  260. RepoName: repo.Name,
  261. RepoPath: repo.RepoPath(),
  262. })); err != nil {
  263. return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
  264. }
  265. return nil
  266. }
  267. // ____ ___ .__ .___ ___________.___.__
  268. // | | \______ | | _________ __| _/ \_ _____/| | | ____ ______
  269. // | | /\____ \| | / _ \__ \ / __ | | __) | | | _/ __ \ / ___/
  270. // | | / | |_> > |_( <_> ) __ \_/ /_/ | | \ | | |_\ ___/ \___ \
  271. // |______/ | __/|____/\____(____ /\____ | \___ / |___|____/\___ >____ >
  272. // |__| \/ \/ \/ \/ \/
  273. //
  274. // Upload represent a uploaded file to a repo to be deleted when moved
  275. type Upload struct {
  276. ID int64
  277. UUID string `xorm:"uuid UNIQUE"`
  278. Name string
  279. }
  280. // UploadLocalPath returns where uploads is stored in local file system based on given UUID.
  281. func UploadLocalPath(uuid string) string {
  282. return path.Join(setting.Repository.Upload.TempPath, uuid[0:1], uuid[1:2], uuid)
  283. }
  284. // LocalPath returns where uploads are temporarily stored in local file system.
  285. func (upload *Upload) LocalPath() string {
  286. return UploadLocalPath(upload.UUID)
  287. }
  288. // NewUpload creates a new upload object.
  289. func NewUpload(name string, buf []byte, file multipart.File) (_ *Upload, err error) {
  290. if tool.IsMaliciousPath(name) {
  291. return nil, fmt.Errorf("malicious path detected: %s", name)
  292. }
  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("mkdir all: %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("get upload by UUID[%s]: %v", uuid, err)
  371. }
  372. if err := DeleteUpload(upload); err != nil {
  373. return fmt.Errorf("delete upload: %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. // isRepositoryGitPath returns true if given path is or resides inside ".git" path of the repository.
  386. func isRepositoryGitPath(path string) bool {
  387. return strings.HasSuffix(path, ".git") || strings.Contains(path, ".git"+string(os.PathSeparator))
  388. }
  389. func (repo *Repository) UploadRepoFiles(doer *User, opts UploadRepoFileOptions) (err error) {
  390. if len(opts.Files) == 0 {
  391. return nil
  392. }
  393. uploads, err := GetUploadsByUUIDs(opts.Files)
  394. if err != nil {
  395. return fmt.Errorf("get uploads by UUIDs[%v]: %v", opts.Files, err)
  396. }
  397. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  398. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  399. if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
  400. return fmt.Errorf("discard local repo branch[%s] changes: %v", opts.OldBranch, err)
  401. } else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
  402. return fmt.Errorf("update local copy branch[%s]: %v", opts.OldBranch, err)
  403. }
  404. if opts.OldBranch != opts.NewBranch {
  405. if err = repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
  406. return fmt.Errorf("checkout new branch[%s] from old branch[%s]: %v", opts.NewBranch, opts.OldBranch, err)
  407. }
  408. }
  409. localPath := repo.LocalCopyPath()
  410. dirPath := path.Join(localPath, opts.TreePath)
  411. os.MkdirAll(dirPath, os.ModePerm)
  412. // Copy uploaded files into repository
  413. for _, upload := range uploads {
  414. tmpPath := upload.LocalPath()
  415. if !com.IsFile(tmpPath) {
  416. continue
  417. }
  418. // Prevent copying files into .git directory, see https://github.com/gogs/gogs/issues/5558.
  419. if isRepositoryGitPath(upload.Name) {
  420. continue
  421. }
  422. targetPath := path.Join(dirPath, upload.Name)
  423. if err = com.Copy(tmpPath, targetPath); err != nil {
  424. return fmt.Errorf("copy: %v", err)
  425. }
  426. repoFileName := path.Join(opts.TreePath, upload.Name)
  427. if err = com.Copy(tmpPath, targetPath); err != nil {
  428. return fmt.Errorf("copy: %v", err)
  429. }
  430. // needed for annex, due to symlinks
  431. os.Remove(targetPath)
  432. log.Trace("Check for annexing: %s", upload.Name)
  433. if finfo, err := os.Stat(targetPath); err == nil {
  434. log.Trace("Filesize is:%d", finfo.Size())
  435. // Should we annex
  436. if finfo.Size() > setting.Repository.Upload.AnexFileMinSize*gannex.MEGABYTE {
  437. log.Trace("This file should be annexed: %s", upload.Name)
  438. // annex init in case it isnt yet
  439. if msg, err := gannex.AInit(localPath, "annex.backend"); err != nil {
  440. log.Error(1, "Annex init failed with error: %v,%s,%s", err, msg, repoFileName)
  441. }
  442. // worm for compatibility
  443. gannex.Md5(localPath)
  444. if msg, err := gannex.Add(repoFileName, localPath); err != nil {
  445. log.Error(1, "Annex add failed with error: %v,%s,%s", err, msg, repoFileName)
  446. }
  447. }
  448. } else {
  449. log.Error(1, "could not stat: %s", targetPath)
  450. }
  451. }
  452. if err = git.AddChanges(localPath, true); err != nil {
  453. return fmt.Errorf("git add --all: %v", err)
  454. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  455. Committer: doer.NewGitSig(),
  456. Message: opts.Message,
  457. }); err != nil {
  458. return fmt.Errorf("commit changes on %q: %v", localPath, err)
  459. } else if err = git.PushWithEnvs(localPath, "origin", opts.NewBranch,
  460. ComposeHookEnvs(ComposeHookEnvsOptions{
  461. AuthUser: doer,
  462. OwnerName: repo.MustOwner().Name,
  463. OwnerSalt: repo.MustOwner().Salt,
  464. RepoID: repo.ID,
  465. RepoName: repo.Name,
  466. RepoPath: repo.RepoPath(),
  467. })); err != nil {
  468. return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
  469. }
  470. // Sometimes you need this twice
  471. if msg, err := gannex.ASync(localPath, "--content", "--no-pull"); err != nil {
  472. log.Error(1, "Annex sync failed with error: %v,%s", err, msg)
  473. } else {
  474. log.Trace("Annex sync:%s", msg)
  475. }
  476. if msg, err := gannex.ASync(localPath, "--content", "--no-pull"); err != nil {
  477. log.Error(1, "Annex sync failed with error: %v,%s", err, msg)
  478. } else {
  479. log.Trace("Annex sync:%s", msg)
  480. }
  481. // We better start out cleaning now. No use keeping files around with annex
  482. if msg, err := gannex.AUInit(localPath); err != nil {
  483. log.Error(1, "Annex uninit failed with error: %v,%s, at: %s/ This repository might fail at "+
  484. "subsequent uploads!", err, msg, localPath)
  485. }
  486. // Indexing support
  487. if setting.Search.Do {
  488. StartIndexing(doer, repo.MustOwner(), repo)
  489. }
  490. RemoveAllWithNotice("Cleaning out after upload", localPath)
  491. return DeleteUploads(uploads...)
  492. }
  493. func StartIndexing(user, owner *User, repo *Repository) {
  494. var ireq struct{ RepoID, RepoPath string }
  495. ireq.RepoID = fmt.Sprintf("%d", repo.ID)
  496. ireq.RepoPath = repo.FullName()
  497. data, err := json.Marshal(ireq)
  498. if err != nil {
  499. log.Trace("could not marshal index request :%+v", err)
  500. return
  501. }
  502. req, _ := http.NewRequest(http.MethodPost, setting.Search.IndexUrl, bytes.NewReader(data))
  503. client := http.Client{}
  504. resp, err := client.Do(req)
  505. if err != nil || resp.StatusCode != http.StatusOK {
  506. log.Trace("Error doing index request:%+v", err)
  507. return
  508. }
  509. }