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