You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo.go 27KB

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