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