repo.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. // Copyright 2014 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. "errors"
  7. "fmt"
  8. "html"
  9. "html/template"
  10. "io/ioutil"
  11. "os"
  12. "path"
  13. "path/filepath"
  14. "regexp"
  15. "runtime"
  16. "sort"
  17. "strings"
  18. "time"
  19. "unicode/utf8"
  20. "github.com/Unknwon/cae/zip"
  21. "github.com/Unknwon/com"
  22. "github.com/gogits/git"
  23. "github.com/gogits/gogs/modules/base"
  24. "github.com/gogits/gogs/modules/bin"
  25. "github.com/gogits/gogs/modules/log"
  26. "github.com/gogits/gogs/modules/process"
  27. "github.com/gogits/gogs/modules/setting"
  28. )
  29. const (
  30. TPL_UPDATE_HOOK = "#!/usr/bin/env %s\n%s update $1 $2 $3\n"
  31. )
  32. var (
  33. ErrRepoAlreadyExist = errors.New("Repository already exist")
  34. ErrRepoNotExist = errors.New("Repository does not exist")
  35. ErrRepoFileNotExist = errors.New("Repository file does not exist")
  36. ErrRepoNameIllegal = errors.New("Repository name contains illegal characters")
  37. ErrRepoFileNotLoaded = errors.New("Repository file not loaded")
  38. ErrMirrorNotExist = errors.New("Mirror does not exist")
  39. ErrInvalidReference = errors.New("Invalid reference specified")
  40. )
  41. var (
  42. LanguageIgns, Licenses []string
  43. )
  44. var (
  45. DescriptionPattern = regexp.MustCompile(`https?://\S+`)
  46. )
  47. // getAssetList returns corresponding asset list in 'conf'.
  48. func getAssetList(prefix string) []string {
  49. assets := make([]string, 0, 15)
  50. for _, name := range bin.AssetNames() {
  51. if strings.HasPrefix(name, prefix) {
  52. assets = append(assets, strings.TrimPrefix(name, prefix+"/"))
  53. }
  54. }
  55. return assets
  56. }
  57. func LoadRepoConfig() {
  58. // Load .gitignore and license files.
  59. types := []string{"gitignore", "license"}
  60. typeFiles := make([][]string, 2)
  61. for i, t := range types {
  62. files := getAssetList(path.Join("conf", t))
  63. customPath := path.Join(setting.CustomPath, "conf", t)
  64. if com.IsDir(customPath) {
  65. customFiles, err := com.StatDir(customPath)
  66. if err != nil {
  67. log.Fatal("Fail to get custom %s files: %v", t, err)
  68. }
  69. for _, f := range customFiles {
  70. if !com.IsSliceContainsStr(files, f) {
  71. files = append(files, f)
  72. }
  73. }
  74. }
  75. typeFiles[i] = files
  76. }
  77. LanguageIgns = typeFiles[0]
  78. Licenses = typeFiles[1]
  79. sort.Strings(LanguageIgns)
  80. sort.Strings(Licenses)
  81. }
  82. func NewRepoContext() {
  83. zip.Verbose = false
  84. // Check if server has basic git setting.
  85. stdout, stderr, err := process.Exec("NewRepoContext(get setting)", "git", "config", "--get", "user.name")
  86. if err != nil {
  87. log.Fatal("repo.NewRepoContext(fail to get git user.name): %s", stderr)
  88. } else if err != nil || len(strings.TrimSpace(stdout)) == 0 {
  89. if _, stderr, err = process.Exec("NewRepoContext(set email)", "git", "config", "--global", "user.email", "[email protected]"); err != nil {
  90. log.Fatal("repo.NewRepoContext(fail to set git user.email): %s", stderr)
  91. } else if _, stderr, err = process.Exec("NewRepoContext(set name)", "git", "config", "--global", "user.name", "Gogs"); err != nil {
  92. log.Fatal("repo.NewRepoContext(fail to set git user.name): %s", stderr)
  93. }
  94. }
  95. barePath := path.Join(setting.RepoRootPath, "git-bare.zip")
  96. if !com.IsExist(barePath) {
  97. data, err := bin.Asset("conf/content/git-bare.zip")
  98. if err != nil {
  99. log.Fatal("Fail to get asset 'git-bare.zip': %v", err)
  100. } else if err := ioutil.WriteFile(barePath, data, os.ModePerm); err != nil {
  101. log.Fatal("Fail to write asset 'git-bare.zip': %v", err)
  102. }
  103. }
  104. }
  105. // Repository represents a git repository.
  106. type Repository struct {
  107. Id int64
  108. OwnerId int64 `xorm:"UNIQUE(s)"`
  109. Owner *User `xorm:"-"`
  110. ForkId int64
  111. LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
  112. Name string `xorm:"INDEX NOT NULL"`
  113. Description string
  114. Website string
  115. NumWatches int
  116. NumStars int
  117. NumForks int
  118. NumIssues int
  119. NumClosedIssues int
  120. NumOpenIssues int `xorm:"-"`
  121. NumMilestones int `xorm:"NOT NULL DEFAULT 0"`
  122. NumClosedMilestones int `xorm:"NOT NULL DEFAULT 0"`
  123. NumOpenMilestones int `xorm:"-"`
  124. NumTags int `xorm:"-"`
  125. IsPrivate bool
  126. IsMirror bool
  127. IsBare bool
  128. IsGoget bool
  129. DefaultBranch string
  130. Created time.Time `xorm:"CREATED"`
  131. Updated time.Time `xorm:"UPDATED"`
  132. }
  133. func (repo *Repository) GetOwner() (err error) {
  134. repo.Owner, err = GetUserById(repo.OwnerId)
  135. return err
  136. }
  137. func (repo *Repository) DescriptionHtml() template.HTML {
  138. sanitize := func(s string) string {
  139. // TODO(nuss-justin): Improve sanitization. Strip all tags?
  140. ss := html.EscapeString(s)
  141. return fmt.Sprintf(`<a href="%s" target="_blank">%s</a>`, ss, ss)
  142. }
  143. return template.HTML(DescriptionPattern.ReplaceAllStringFunc(repo.Description, sanitize))
  144. }
  145. // IsRepositoryExist returns true if the repository with given name under user has already existed.
  146. func IsRepositoryExist(u *User, repoName string) (bool, error) {
  147. repo := Repository{OwnerId: u.Id}
  148. has, err := x.Where("lower_name = ?", strings.ToLower(repoName)).Get(&repo)
  149. if err != nil {
  150. return has, err
  151. } else if !has {
  152. return false, nil
  153. }
  154. return com.IsDir(RepoPath(u.Name, repoName)), nil
  155. }
  156. var (
  157. illegalEquals = []string{"debug", "raw", "install", "api", "avatar", "user", "org", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin", "new"}
  158. illegalSuffixs = []string{".git"}
  159. )
  160. // IsLegalName returns false if name contains illegal characters.
  161. func IsLegalName(repoName string) bool {
  162. repoName = strings.ToLower(repoName)
  163. for _, char := range illegalEquals {
  164. if repoName == char {
  165. return false
  166. }
  167. }
  168. for _, char := range illegalSuffixs {
  169. if strings.HasSuffix(repoName, char) {
  170. return false
  171. }
  172. }
  173. return true
  174. }
  175. // Mirror represents a mirror information of repository.
  176. type Mirror struct {
  177. Id int64
  178. RepoId int64
  179. RepoName string // <user name>/<repo name>
  180. Interval int // Hour.
  181. Updated time.Time `xorm:"UPDATED"`
  182. NextUpdate time.Time
  183. }
  184. // MirrorRepository creates a mirror repository from source.
  185. func MirrorRepository(repoId int64, userName, repoName, repoPath, url string) error {
  186. _, stderr, err := process.ExecTimeout(10*time.Minute,
  187. fmt.Sprintf("MirrorRepository: %s/%s", userName, repoName),
  188. "git", "clone", "--mirror", url, repoPath)
  189. if err != nil {
  190. return errors.New("git clone --mirror: " + stderr)
  191. }
  192. if _, err = x.InsertOne(&Mirror{
  193. RepoId: repoId,
  194. RepoName: strings.ToLower(userName + "/" + repoName),
  195. Interval: 24,
  196. NextUpdate: time.Now().Add(24 * time.Hour),
  197. }); err != nil {
  198. return err
  199. }
  200. return git.UnpackRefs(repoPath)
  201. }
  202. func GetMirror(repoId int64) (*Mirror, error) {
  203. m := &Mirror{RepoId: repoId}
  204. has, err := x.Get(m)
  205. if err != nil {
  206. return nil, err
  207. } else if !has {
  208. return nil, ErrMirrorNotExist
  209. }
  210. return m, nil
  211. }
  212. func UpdateMirror(m *Mirror) error {
  213. _, err := x.Id(m.Id).Update(m)
  214. return err
  215. }
  216. // MirrorUpdate checks and updates mirror repositories.
  217. func MirrorUpdate() {
  218. if err := x.Iterate(new(Mirror), func(idx int, bean interface{}) error {
  219. m := bean.(*Mirror)
  220. if m.NextUpdate.After(time.Now()) {
  221. return nil
  222. }
  223. repoPath := filepath.Join(setting.RepoRootPath, m.RepoName+".git")
  224. if _, stderr, err := process.ExecDir(10*time.Minute,
  225. repoPath, fmt.Sprintf("MirrorUpdate: %s", repoPath),
  226. "git", "remote", "update"); err != nil {
  227. return errors.New("git remote update: " + stderr)
  228. } else if err = git.UnpackRefs(repoPath); err != nil {
  229. return errors.New("UnpackRefs: " + err.Error())
  230. }
  231. m.NextUpdate = time.Now().Add(time.Duration(m.Interval) * time.Hour)
  232. return UpdateMirror(m)
  233. }); err != nil {
  234. log.Error("repo.MirrorUpdate: %v", err)
  235. }
  236. }
  237. // MigrateRepository migrates a existing repository from other project hosting.
  238. func MigrateRepository(u *User, name, desc string, private, mirror bool, url string) (*Repository, error) {
  239. repo, err := CreateRepository(u, name, desc, "", "", private, mirror, false)
  240. if err != nil {
  241. return nil, err
  242. }
  243. // Clone to temprory path and do the init commit.
  244. tmpDir := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()))
  245. os.MkdirAll(tmpDir, os.ModePerm)
  246. repoPath := RepoPath(u.Name, name)
  247. repo.IsBare = false
  248. if mirror {
  249. if err = MirrorRepository(repo.Id, u.Name, repo.Name, repoPath, url); err != nil {
  250. return repo, err
  251. }
  252. repo.IsMirror = true
  253. return repo, UpdateRepository(repo)
  254. }
  255. // Clone from local repository.
  256. _, stderr, err := process.ExecTimeout(10*time.Minute,
  257. fmt.Sprintf("MigrateRepository(git clone): %s", repoPath),
  258. "git", "clone", repoPath, tmpDir)
  259. if err != nil {
  260. return repo, errors.New("git clone: " + stderr)
  261. }
  262. // Pull data from source.
  263. if _, stderr, err = process.ExecDir(3*time.Minute,
  264. tmpDir, fmt.Sprintf("MigrateRepository(git pull): %s", repoPath),
  265. "git", "pull", url); err != nil {
  266. return repo, errors.New("git pull: " + stderr)
  267. }
  268. // Push data to local repository.
  269. if _, stderr, err = process.ExecDir(3*time.Minute,
  270. tmpDir, fmt.Sprintf("MigrateRepository(git push): %s", repoPath),
  271. "git", "push", "origin", "master"); err != nil {
  272. return repo, errors.New("git push: " + stderr)
  273. }
  274. return repo, UpdateRepository(repo)
  275. }
  276. // extractGitBareZip extracts git-bare.zip to repository path.
  277. func extractGitBareZip(repoPath string) error {
  278. z, err := zip.Open(filepath.Join(setting.RepoRootPath, "git-bare.zip"))
  279. if err != nil {
  280. return err
  281. }
  282. defer z.Close()
  283. return z.ExtractTo(repoPath)
  284. }
  285. // initRepoCommit temporarily changes with work directory.
  286. func initRepoCommit(tmpPath string, sig *git.Signature) (err error) {
  287. var stderr string
  288. if _, stderr, err = process.ExecDir(-1,
  289. tmpPath, fmt.Sprintf("initRepoCommit(git add): %s", tmpPath),
  290. "git", "add", "--all"); err != nil {
  291. return errors.New("git add: " + stderr)
  292. }
  293. if _, stderr, err = process.ExecDir(-1,
  294. tmpPath, fmt.Sprintf("initRepoCommit(git commit): %s", tmpPath),
  295. "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email),
  296. "-m", "Init commit"); err != nil {
  297. return errors.New("git commit: " + stderr)
  298. }
  299. if _, stderr, err = process.ExecDir(-1,
  300. tmpPath, fmt.Sprintf("initRepoCommit(git push): %s", tmpPath),
  301. "git", "push", "origin", "master"); err != nil {
  302. return errors.New("git push: " + stderr)
  303. }
  304. return nil
  305. }
  306. func createHookUpdate(hookPath, content string) error {
  307. pu, err := os.OpenFile(hookPath, os.O_CREATE|os.O_WRONLY, 0777)
  308. if err != nil {
  309. return err
  310. }
  311. defer pu.Close()
  312. _, err = pu.WriteString(content)
  313. return err
  314. }
  315. // SetRepoEnvs sets environment variables for command update.
  316. func SetRepoEnvs(userId int64, userName, repoName, repoUserName string) {
  317. os.Setenv("userId", base.ToStr(userId))
  318. os.Setenv("userName", userName)
  319. os.Setenv("repoName", repoName)
  320. os.Setenv("repoUserName", repoUserName)
  321. }
  322. // InitRepository initializes README and .gitignore if needed.
  323. func initRepository(f string, user *User, repo *Repository, initReadme bool, repoLang, license string) error {
  324. repoPath := RepoPath(user.Name, repo.Name)
  325. // Create bare new repository.
  326. if err := extractGitBareZip(repoPath); err != nil {
  327. return err
  328. }
  329. if runtime.GOOS == "windows" {
  330. rp := strings.NewReplacer("\\", "/")
  331. appPath = "\"" + rp.Replace(appPath) + "\""
  332. } else {
  333. rp := strings.NewReplacer("\\", "/", " ", "\\ ")
  334. appPath = rp.Replace(appPath)
  335. }
  336. // hook/post-update
  337. if err := createHookUpdate(filepath.Join(repoPath, "hooks", "update"),
  338. fmt.Sprintf(TPL_UPDATE_HOOK, setting.ScriptType, appPath)); err != nil {
  339. return err
  340. }
  341. // Initialize repository according to user's choice.
  342. fileName := map[string]string{}
  343. if initReadme {
  344. fileName["readme"] = "README.md"
  345. }
  346. if repoLang != "" {
  347. fileName["gitign"] = ".gitignore"
  348. }
  349. if license != "" {
  350. fileName["license"] = "LICENSE"
  351. }
  352. // Clone to temprory path and do the init commit.
  353. tmpDir := filepath.Join(os.TempDir(), base.ToStr(time.Now().Nanosecond()))
  354. os.MkdirAll(tmpDir, os.ModePerm)
  355. _, stderr, err := process.Exec(
  356. fmt.Sprintf("initRepository(git clone): %s", repoPath),
  357. "git", "clone", repoPath, tmpDir)
  358. if err != nil {
  359. return errors.New("initRepository(git clone): " + stderr)
  360. }
  361. // README
  362. if initReadme {
  363. defaultReadme := repo.Name + "\n" + strings.Repeat("=",
  364. utf8.RuneCountInString(repo.Name)) + "\n\n" + repo.Description
  365. if err := ioutil.WriteFile(filepath.Join(tmpDir, fileName["readme"]),
  366. []byte(defaultReadme), 0644); err != nil {
  367. return err
  368. }
  369. }
  370. // .gitignore
  371. if repoLang != "" {
  372. filePath := "conf/gitignore/" + repoLang
  373. targetPath := path.Join(tmpDir, fileName["gitign"])
  374. data, err := bin.Asset(filePath)
  375. if err == nil {
  376. if err = ioutil.WriteFile(targetPath, data, os.ModePerm); err != nil {
  377. return err
  378. }
  379. } else {
  380. // Check custom files.
  381. filePath = path.Join(setting.CustomPath, "conf/gitignore", repoLang)
  382. if com.IsFile(filePath) {
  383. if err := com.Copy(filePath, targetPath); err != nil {
  384. return err
  385. }
  386. }
  387. }
  388. }
  389. // LICENSE
  390. if license != "" {
  391. filePath := "conf/license/" + license
  392. targetPath := path.Join(tmpDir, fileName["license"])
  393. data, err := bin.Asset(filePath)
  394. if err == nil {
  395. if err = ioutil.WriteFile(targetPath, data, os.ModePerm); err != nil {
  396. return err
  397. }
  398. } else {
  399. // Check custom files.
  400. filePath = path.Join(setting.CustomPath, "conf/license", license)
  401. if com.IsFile(filePath) {
  402. if err := com.Copy(filePath, targetPath); err != nil {
  403. return err
  404. }
  405. }
  406. }
  407. }
  408. if len(fileName) == 0 {
  409. return nil
  410. }
  411. SetRepoEnvs(user.Id, user.Name, repo.Name, user.Name)
  412. // Apply changes and commit.
  413. return initRepoCommit(tmpDir, user.NewGitSig())
  414. }
  415. // CreateRepository creates a repository for given user or organization.
  416. func CreateRepository(u *User, name, desc, lang, license string, private, mirror, initReadme bool) (*Repository, error) {
  417. if !IsLegalName(name) {
  418. return nil, ErrRepoNameIllegal
  419. }
  420. isExist, err := IsRepositoryExist(u, name)
  421. if err != nil {
  422. return nil, err
  423. } else if isExist {
  424. return nil, ErrRepoAlreadyExist
  425. }
  426. sess := x.NewSession()
  427. defer sess.Close()
  428. if err = sess.Begin(); err != nil {
  429. return nil, err
  430. }
  431. repo := &Repository{
  432. OwnerId: u.Id,
  433. Owner: u,
  434. Name: name,
  435. LowerName: strings.ToLower(name),
  436. Description: desc,
  437. IsPrivate: private,
  438. IsBare: lang == "" && license == "" && !initReadme,
  439. }
  440. if !repo.IsBare {
  441. repo.DefaultBranch = "master"
  442. }
  443. if _, err = sess.Insert(repo); err != nil {
  444. sess.Rollback()
  445. return nil, err
  446. }
  447. var t *Team // Owner team.
  448. mode := WRITABLE
  449. if mirror {
  450. mode = READABLE
  451. }
  452. access := &Access{
  453. UserName: u.LowerName,
  454. RepoName: strings.ToLower(path.Join(u.Name, repo.Name)),
  455. Mode: mode,
  456. }
  457. // Give access to all members in owner team.
  458. if u.IsOrganization() {
  459. t, err = u.GetOwnerTeam()
  460. if err != nil {
  461. sess.Rollback()
  462. return nil, err
  463. }
  464. us, err := GetTeamMembers(u.Id, t.Id)
  465. if err != nil {
  466. sess.Rollback()
  467. return nil, err
  468. }
  469. for _, u := range us {
  470. access.UserName = u.LowerName
  471. if _, err = sess.Insert(access); err != nil {
  472. sess.Rollback()
  473. return nil, err
  474. }
  475. }
  476. } else {
  477. if _, err = sess.Insert(access); err != nil {
  478. sess.Rollback()
  479. return nil, err
  480. }
  481. }
  482. rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?"
  483. if _, err = sess.Exec(rawSql, u.Id); err != nil {
  484. sess.Rollback()
  485. return nil, err
  486. }
  487. // Update owner team info and count.
  488. if u.IsOrganization() {
  489. t.RepoIds += "$" + base.ToStr(repo.Id) + "|"
  490. t.NumRepos++
  491. if _, err = sess.Id(t.Id).AllCols().Update(t); err != nil {
  492. sess.Rollback()
  493. return nil, err
  494. }
  495. }
  496. if err = sess.Commit(); err != nil {
  497. return nil, err
  498. }
  499. if u.IsOrganization() {
  500. ous, err := GetOrgUsersByOrgId(u.Id)
  501. if err != nil {
  502. log.Error("repo.CreateRepository(GetOrgUsersByOrgId): %v", err)
  503. } else {
  504. for _, ou := range ous {
  505. if err = WatchRepo(ou.Uid, repo.Id, true); err != nil {
  506. log.Error("repo.CreateRepository(WatchRepo): %v", err)
  507. }
  508. }
  509. }
  510. }
  511. if err = WatchRepo(u.Id, repo.Id, true); err != nil {
  512. log.Error("repo.CreateRepository(WatchRepo2): %v", err)
  513. }
  514. if err = NewRepoAction(u, repo); err != nil {
  515. log.Error("repo.CreateRepository(NewRepoAction): %v", err)
  516. }
  517. // No need for init for mirror.
  518. if mirror {
  519. return repo, nil
  520. }
  521. repoPath := RepoPath(u.Name, repo.Name)
  522. if err = initRepository(repoPath, u, repo, initReadme, lang, license); err != nil {
  523. if err2 := os.RemoveAll(repoPath); err2 != nil {
  524. log.Error("repo.CreateRepository(initRepository): %v", err)
  525. return nil, errors.New(fmt.Sprintf(
  526. "delete repo directory %s/%s failed(2): %v", u.Name, repo.Name, err2))
  527. }
  528. return nil, err
  529. }
  530. _, stderr, err := process.ExecDir(-1,
  531. repoPath, fmt.Sprintf("CreateRepository(git update-server-info): %s", repoPath),
  532. "git", "update-server-info")
  533. if err != nil {
  534. return nil, errors.New("CreateRepository(git update-server-info): " + stderr)
  535. }
  536. return repo, nil
  537. }
  538. // CountRepositories returns number of repositories.
  539. func CountRepositories() int64 {
  540. count, _ := x.Count(new(Repository))
  541. return count
  542. }
  543. // GetRepositoriesWithUsers returns given number of repository objects with offset.
  544. // It also auto-gets corresponding users.
  545. func GetRepositoriesWithUsers(num, offset int) ([]*Repository, error) {
  546. repos := make([]*Repository, 0, num)
  547. if err := x.Limit(num, offset).Asc("id").Find(&repos); err != nil {
  548. return nil, err
  549. }
  550. for _, repo := range repos {
  551. repo.Owner = &User{Id: repo.OwnerId}
  552. has, err := x.Get(repo.Owner)
  553. if err != nil {
  554. return nil, err
  555. } else if !has {
  556. return nil, ErrUserNotExist
  557. }
  558. }
  559. return repos, nil
  560. }
  561. // RepoPath returns repository path by given user and repository name.
  562. func RepoPath(userName, repoName string) string {
  563. return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".git")
  564. }
  565. // TransferOwnership transfers all corresponding setting from old user to new one.
  566. func TransferOwnership(u *User, newOwner string, repo *Repository) (err error) {
  567. newUser, err := GetUserByName(newOwner)
  568. if err != nil {
  569. return err
  570. }
  571. sess := x.NewSession()
  572. defer sess.Close()
  573. if err = sess.Begin(); err != nil {
  574. return err
  575. }
  576. if _, err = sess.Where("repo_name = ?", u.LowerName+"/"+repo.LowerName).
  577. And("user_name = ?", u.LowerName).Update(&Access{UserName: newUser.LowerName}); err != nil {
  578. sess.Rollback()
  579. return err
  580. }
  581. if _, err = sess.Where("repo_name = ?", u.LowerName+"/"+repo.LowerName).Update(&Access{
  582. RepoName: newUser.LowerName + "/" + repo.LowerName,
  583. }); err != nil {
  584. sess.Rollback()
  585. return err
  586. }
  587. // Update repository.
  588. repo.OwnerId = newUser.Id
  589. if _, err := sess.Id(repo.Id).Update(repo); err != nil {
  590. sess.Rollback()
  591. return err
  592. }
  593. // Update user repository number.
  594. rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?"
  595. if _, err = sess.Exec(rawSql, newUser.Id); err != nil {
  596. sess.Rollback()
  597. return err
  598. }
  599. rawSql = "UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?"
  600. if _, err = sess.Exec(rawSql, u.Id); err != nil {
  601. sess.Rollback()
  602. return err
  603. }
  604. // Change repository directory name.
  605. if err = os.Rename(RepoPath(u.Name, repo.Name), RepoPath(newUser.Name, repo.Name)); err != nil {
  606. sess.Rollback()
  607. return err
  608. }
  609. if err = sess.Commit(); err != nil {
  610. return err
  611. }
  612. // Add watch of new owner to repository.
  613. if !IsWatching(newUser.Id, repo.Id) {
  614. if err = WatchRepo(newUser.Id, repo.Id, true); err != nil {
  615. return err
  616. }
  617. }
  618. if err = TransferRepoAction(u, newUser, repo); err != nil {
  619. return err
  620. }
  621. return nil
  622. }
  623. // ChangeRepositoryName changes all corresponding setting from old repository name to new one.
  624. func ChangeRepositoryName(userName, oldRepoName, newRepoName string) (err error) {
  625. // Update accesses.
  626. accesses := make([]Access, 0, 10)
  627. if err = x.Find(&accesses, &Access{RepoName: strings.ToLower(userName + "/" + oldRepoName)}); err != nil {
  628. return err
  629. }
  630. sess := x.NewSession()
  631. defer sess.Close()
  632. if err = sess.Begin(); err != nil {
  633. return err
  634. }
  635. for i := range accesses {
  636. accesses[i].RepoName = userName + "/" + newRepoName
  637. if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil {
  638. return err
  639. }
  640. }
  641. // Change repository directory name.
  642. if err = os.Rename(RepoPath(userName, oldRepoName), RepoPath(userName, newRepoName)); err != nil {
  643. sess.Rollback()
  644. return err
  645. }
  646. return sess.Commit()
  647. }
  648. func UpdateRepository(repo *Repository) error {
  649. repo.LowerName = strings.ToLower(repo.Name)
  650. if len(repo.Description) > 255 {
  651. repo.Description = repo.Description[:255]
  652. }
  653. if len(repo.Website) > 255 {
  654. repo.Website = repo.Website[:255]
  655. }
  656. _, err := x.Id(repo.Id).AllCols().Update(repo)
  657. return err
  658. }
  659. // DeleteRepository deletes a repository for a user or orgnaztion.
  660. func DeleteRepository(userId, repoId int64, userName string) error {
  661. repo := &Repository{Id: repoId, OwnerId: userId}
  662. has, err := x.Get(repo)
  663. if err != nil {
  664. return err
  665. } else if !has {
  666. return ErrRepoNotExist
  667. }
  668. sess := x.NewSession()
  669. defer sess.Close()
  670. if err = sess.Begin(); err != nil {
  671. return err
  672. }
  673. if _, err = sess.Delete(&Repository{Id: repoId}); err != nil {
  674. sess.Rollback()
  675. return err
  676. }
  677. if _, err := sess.Delete(&Access{RepoName: strings.ToLower(path.Join(userName, repo.Name))}); err != nil {
  678. sess.Rollback()
  679. return err
  680. }
  681. if _, err := sess.Delete(&Action{RepoId: repo.Id}); err != nil {
  682. sess.Rollback()
  683. return err
  684. }
  685. if _, err = sess.Delete(&Watch{RepoId: repoId}); err != nil {
  686. sess.Rollback()
  687. return err
  688. }
  689. if _, err = sess.Delete(&Mirror{RepoId: repoId}); err != nil {
  690. sess.Rollback()
  691. return err
  692. }
  693. if _, err = sess.Delete(&IssueUser{RepoId: repoId}); err != nil {
  694. sess.Rollback()
  695. return err
  696. }
  697. if _, err = sess.Delete(&Milestone{RepoId: repoId}); err != nil {
  698. sess.Rollback()
  699. return err
  700. }
  701. if _, err = sess.Delete(&Release{RepoId: repoId}); err != nil {
  702. sess.Rollback()
  703. return err
  704. }
  705. // Delete comments.
  706. if err = x.Iterate(&Issue{RepoId: repoId}, func(idx int, bean interface{}) error {
  707. issue := bean.(*Issue)
  708. if _, err = sess.Delete(&Comment{IssueId: issue.Id}); err != nil {
  709. sess.Rollback()
  710. return err
  711. }
  712. return nil
  713. }); err != nil {
  714. sess.Rollback()
  715. return err
  716. }
  717. if _, err = sess.Delete(&Issue{RepoId: repoId}); err != nil {
  718. sess.Rollback()
  719. return err
  720. }
  721. rawSql := "UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?"
  722. if _, err = sess.Exec(rawSql, userId); err != nil {
  723. sess.Rollback()
  724. return err
  725. }
  726. if err = os.RemoveAll(RepoPath(userName, repo.Name)); err != nil {
  727. sess.Rollback()
  728. return err
  729. }
  730. return sess.Commit()
  731. }
  732. // GetRepositoryByRef returns a Repository specified by a GFM reference.
  733. // See https://help.github.com/articles/writing-on-github#references for more information on the syntax.
  734. func GetRepositoryByRef(ref string) (*Repository, error) {
  735. n := strings.IndexByte(ref, byte('/'))
  736. if n < 2 {
  737. return nil, ErrInvalidReference
  738. }
  739. userName, repoName := ref[:n], ref[n+1:]
  740. user, err := GetUserByName(userName)
  741. if err != nil {
  742. return nil, err
  743. }
  744. return GetRepositoryByName(user.Id, repoName)
  745. }
  746. // GetRepositoryByName returns the repository by given name under user if exists.
  747. func GetRepositoryByName(userId int64, repoName string) (*Repository, error) {
  748. repo := &Repository{
  749. OwnerId: userId,
  750. LowerName: strings.ToLower(repoName),
  751. }
  752. has, err := x.Get(repo)
  753. if err != nil {
  754. return nil, err
  755. } else if !has {
  756. return nil, ErrRepoNotExist
  757. }
  758. return repo, err
  759. }
  760. // GetRepositoryById returns the repository by given id if exists.
  761. func GetRepositoryById(id int64) (*Repository, error) {
  762. repo := &Repository{}
  763. has, err := x.Id(id).Get(repo)
  764. if err != nil {
  765. return nil, err
  766. } else if !has {
  767. return nil, ErrRepoNotExist
  768. }
  769. return repo, nil
  770. }
  771. // GetRepositories returns a list of repositories of given user.
  772. func GetRepositories(uid int64, private bool) ([]*Repository, error) {
  773. repos := make([]*Repository, 0, 10)
  774. sess := x.Desc("updated")
  775. if !private {
  776. sess.Where("is_private=?", false)
  777. }
  778. err := sess.Find(&repos, &Repository{OwnerId: uid})
  779. return repos, err
  780. }
  781. // GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
  782. func GetRecentUpdatedRepositories() (repos []*Repository, err error) {
  783. err = x.Where("is_private=?", false).Limit(5).Desc("updated").Find(&repos)
  784. return repos, err
  785. }
  786. // GetRepositoryCount returns the total number of repositories of user.
  787. func GetRepositoryCount(user *User) (int64, error) {
  788. return x.Count(&Repository{OwnerId: user.Id})
  789. }
  790. // GetCollaboratorNames returns a list of user name of repository's collaborators.
  791. func GetCollaboratorNames(repoName string) ([]string, error) {
  792. accesses := make([]*Access, 0, 10)
  793. if err := x.Find(&accesses, &Access{RepoName: strings.ToLower(repoName)}); err != nil {
  794. return nil, err
  795. }
  796. names := make([]string, len(accesses))
  797. for i := range accesses {
  798. names[i] = accesses[i].UserName
  799. }
  800. return names, nil
  801. }
  802. // GetCollaborativeRepos returns a list of repositories that user is collaborator.
  803. func GetCollaborativeRepos(uname string) ([]*Repository, error) {
  804. uname = strings.ToLower(uname)
  805. accesses := make([]*Access, 0, 10)
  806. if err := x.Find(&accesses, &Access{UserName: uname}); err != nil {
  807. return nil, err
  808. }
  809. repos := make([]*Repository, 0, 10)
  810. for _, access := range accesses {
  811. infos := strings.Split(access.RepoName, "/")
  812. if infos[0] == uname {
  813. continue
  814. }
  815. u, err := GetUserByName(infos[0])
  816. if err != nil {
  817. return nil, err
  818. }
  819. repo, err := GetRepositoryByName(u.Id, infos[1])
  820. if err != nil {
  821. return nil, err
  822. }
  823. repo.Owner = u
  824. repos = append(repos, repo)
  825. }
  826. return repos, nil
  827. }
  828. // GetCollaborators returns a list of users of repository's collaborators.
  829. func GetCollaborators(repoName string) (us []*User, err error) {
  830. accesses := make([]*Access, 0, 10)
  831. if err = x.Find(&accesses, &Access{RepoName: strings.ToLower(repoName)}); err != nil {
  832. return nil, err
  833. }
  834. us = make([]*User, len(accesses))
  835. for i := range accesses {
  836. us[i], err = GetUserByName(accesses[i].UserName)
  837. if err != nil {
  838. return nil, err
  839. }
  840. }
  841. return us, nil
  842. }
  843. // Watch is connection request for receiving repository notifycation.
  844. type Watch struct {
  845. Id int64
  846. UserId int64 `xorm:"UNIQUE(watch)"`
  847. RepoId int64 `xorm:"UNIQUE(watch)"`
  848. }
  849. // Watch or unwatch repository.
  850. func WatchRepo(uid, rid int64, watch bool) (err error) {
  851. if watch {
  852. if _, err = x.Insert(&Watch{RepoId: rid, UserId: uid}); err != nil {
  853. return err
  854. }
  855. rawSql := "UPDATE `repository` SET num_watches = num_watches + 1 WHERE id = ?"
  856. _, err = x.Exec(rawSql, rid)
  857. } else {
  858. if _, err = x.Delete(&Watch{0, uid, rid}); err != nil {
  859. return err
  860. }
  861. rawSql := "UPDATE `repository` SET num_watches = num_watches - 1 WHERE id = ?"
  862. _, err = x.Exec(rawSql, rid)
  863. }
  864. return err
  865. }
  866. // GetWatchers returns all watchers of given repository.
  867. func GetWatchers(rid int64) ([]*Watch, error) {
  868. watches := make([]*Watch, 0, 10)
  869. err := x.Find(&watches, &Watch{RepoId: rid})
  870. return watches, err
  871. }
  872. // NotifyWatchers creates batch of actions for every watcher.
  873. func NotifyWatchers(act *Action) error {
  874. // Add feeds for user self and all watchers.
  875. watches, err := GetWatchers(act.RepoId)
  876. if err != nil {
  877. return errors.New("repo.NotifyWatchers(get watches): " + err.Error())
  878. }
  879. // Add feed for actioner.
  880. act.UserId = act.ActUserId
  881. if _, err = x.InsertOne(act); err != nil {
  882. return errors.New("repo.NotifyWatchers(create action): " + err.Error())
  883. }
  884. for i := range watches {
  885. if act.ActUserId == watches[i].UserId {
  886. continue
  887. }
  888. act.Id = 0
  889. act.UserId = watches[i].UserId
  890. if _, err = x.InsertOne(act); err != nil {
  891. return errors.New("repo.NotifyWatchers(create action): " + err.Error())
  892. }
  893. }
  894. return nil
  895. }
  896. // IsWatching checks if user has watched given repository.
  897. func IsWatching(uid, rid int64) bool {
  898. has, _ := x.Get(&Watch{0, uid, rid})
  899. return has
  900. }
  901. func ForkRepository(repoName string, uid int64) {
  902. }