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

repo.go 32 kB

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