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

1820 lines
50 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
  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. "bytes"
  7. "errors"
  8. "fmt"
  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/go-xorm/xorm"
  23. "github.com/gogits/gogs/modules/base"
  24. "github.com/gogits/gogs/modules/bindata"
  25. "github.com/gogits/gogs/modules/git"
  26. "github.com/gogits/gogs/modules/log"
  27. "github.com/gogits/gogs/modules/process"
  28. "github.com/gogits/gogs/modules/setting"
  29. )
  30. const (
  31. _TPL_UPDATE_HOOK = "#!/usr/bin/env %s\n%s update $1 $2 $3 --config='%s'\n"
  32. )
  33. var (
  34. ErrRepoFileNotExist = errors.New("Repository file does not exist")
  35. ErrRepoFileNotLoaded = errors.New("Repository file not loaded")
  36. ErrMirrorNotExist = errors.New("Mirror does not exist")
  37. ErrInvalidReference = errors.New("Invalid reference specified")
  38. ErrNameEmpty = errors.New("Name is empty")
  39. )
  40. var (
  41. Gitignores, Licenses, Readmes []string
  42. // Maximum items per page in forks, watchers and stars of a repo
  43. ItemsPerPage = 54
  44. )
  45. func LoadRepoConfig() {
  46. // Load .gitignore and license files and readme templates.
  47. types := []string{"gitignore", "license", "readme"}
  48. typeFiles := make([][]string, 3)
  49. for i, t := range types {
  50. files, err := bindata.AssetDir("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. Readmes = typeFiles[2]
  71. sort.Strings(Gitignores)
  72. sort.Strings(Licenses)
  73. sort.Strings(Readmes)
  74. }
  75. func NewRepoContext() {
  76. zip.Verbose = false
  77. // Check Git installation.
  78. if _, err := exec.LookPath("git"); err != nil {
  79. log.Fatal(4, "Fail to test 'git' command: %v (forgotten install?)", err)
  80. }
  81. // Check Git version.
  82. ver, err := git.GetVersion()
  83. if err != nil {
  84. log.Fatal(4, "Fail to get Git version: %v", err)
  85. }
  86. reqVer, err := git.ParseVersion("1.7.1")
  87. if err != nil {
  88. log.Fatal(4, "Fail to parse required Git version: %v", err)
  89. }
  90. if ver.LessThan(reqVer) {
  91. log.Fatal(4, "Gogs requires Git version greater or equal to 1.7.1")
  92. }
  93. log.Info("Git version: %s", ver.String())
  94. // Git requires setting user.name and user.email in order to commit changes.
  95. for configKey, defaultValue := range map[string]string{"user.name": "Gogs", "user.email": "gogs@fake.local"} {
  96. if stdout, stderr, err := process.Exec("NewRepoContext(get setting)", "git", "config", "--get", configKey); err != nil || strings.TrimSpace(stdout) == "" {
  97. // ExitError indicates this config is not set
  98. if _, ok := err.(*exec.ExitError); ok || strings.TrimSpace(stdout) == "" {
  99. if _, stderr, gerr := process.Exec("NewRepoContext(set "+configKey+")", "git", "config", "--global", configKey, defaultValue); gerr != nil {
  100. log.Fatal(4, "Fail to set git %s(%s): %s", configKey, gerr, stderr)
  101. }
  102. log.Info("Git config %s set to %s", configKey, defaultValue)
  103. } else {
  104. log.Fatal(4, "Fail to get git %s(%s): %s", configKey, err, stderr)
  105. }
  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 `xorm:"pk autoincr"`
  117. OwnerID int64 `xorm:"UNIQUE(s)"`
  118. Owner *User `xorm:"-"`
  119. LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
  120. Name string `xorm:"INDEX NOT NULL"`
  121. Description string
  122. Website string
  123. DefaultBranch 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. IsBare bool
  139. IsMirror bool
  140. *Mirror `xorm:"-"`
  141. IsFork bool `xorm:"NOT NULL DEFAULT false"`
  142. ForkID int64
  143. BaseRepo *Repository `xorm:"-"`
  144. Created time.Time `xorm:"CREATED"`
  145. Updated time.Time `xorm:"UPDATED"`
  146. }
  147. func (repo *Repository) AfterSet(colName string, _ xorm.Cell) {
  148. switch colName {
  149. case "num_closed_issues":
  150. repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
  151. case "num_closed_pulls":
  152. repo.NumOpenPulls = repo.NumPulls - repo.NumClosedPulls
  153. case "num_closed_milestones":
  154. repo.NumOpenMilestones = repo.NumMilestones - repo.NumClosedMilestones
  155. case "updated":
  156. repo.Updated = regulateTimeZone(repo.Updated)
  157. }
  158. }
  159. func (repo *Repository) getOwner(e Engine) (err error) {
  160. if repo.Owner == nil {
  161. repo.Owner, err = getUserByID(e, repo.OwnerID)
  162. }
  163. return err
  164. }
  165. func (repo *Repository) GetOwner() error {
  166. return repo.getOwner(x)
  167. }
  168. // GetAssignees returns all users that have write access of repository.
  169. func (repo *Repository) GetAssignees() (_ []*User, err error) {
  170. if err = repo.GetOwner(); err != nil {
  171. return nil, err
  172. }
  173. accesses := make([]*Access, 0, 10)
  174. if err = x.Where("repo_id=? AND mode>=?", repo.ID, ACCESS_MODE_WRITE).Find(&accesses); err != nil {
  175. return nil, err
  176. }
  177. users := make([]*User, 0, len(accesses)+1) // Just waste 1 unit does not matter.
  178. if !repo.Owner.IsOrganization() {
  179. users = append(users, repo.Owner)
  180. }
  181. var u *User
  182. for i := range accesses {
  183. u, err = GetUserByID(accesses[i].UserID)
  184. if err != nil {
  185. return nil, err
  186. }
  187. users = append(users, u)
  188. }
  189. return users, nil
  190. }
  191. // GetAssigneeByID returns the user that has write access of repository by given ID.
  192. func (repo *Repository) GetAssigneeByID(userID int64) (*User, error) {
  193. return GetAssigneeByID(repo, userID)
  194. }
  195. // GetMilestoneByID returns the milestone belongs to repository by given ID.
  196. func (repo *Repository) GetMilestoneByID(milestoneID int64) (*Milestone, error) {
  197. return GetRepoMilestoneByID(repo.ID, milestoneID)
  198. }
  199. // IssueStats returns number of open and closed repository issues by given filter mode.
  200. func (repo *Repository) IssueStats(uid int64, filterMode int, isPull bool) (int64, int64) {
  201. return GetRepoIssueStats(repo.ID, uid, filterMode, isPull)
  202. }
  203. func (repo *Repository) GetMirror() (err error) {
  204. repo.Mirror, err = GetMirror(repo.ID)
  205. return err
  206. }
  207. func (repo *Repository) GetBaseRepo() (err error) {
  208. if !repo.IsFork {
  209. return nil
  210. }
  211. repo.BaseRepo, err = GetRepositoryByID(repo.ForkID)
  212. return err
  213. }
  214. func (repo *Repository) repoPath(e Engine) (string, error) {
  215. if err := repo.getOwner(e); err != nil {
  216. return "", err
  217. }
  218. return RepoPath(repo.Owner.Name, repo.Name), nil
  219. }
  220. func (repo *Repository) RepoPath() (string, error) {
  221. return repo.repoPath(x)
  222. }
  223. func (repo *Repository) RepoLink() (string, error) {
  224. if err := repo.GetOwner(); err != nil {
  225. return "", err
  226. }
  227. return setting.AppSubUrl + "/" + repo.Owner.Name + "/" + repo.Name, nil
  228. }
  229. func (repo *Repository) HasAccess(u *User) bool {
  230. has, _ := HasAccess(u, repo, ACCESS_MODE_READ)
  231. return has
  232. }
  233. func (repo *Repository) IsOwnedBy(userID int64) bool {
  234. return repo.OwnerID == userID
  235. }
  236. // CanBeForked returns true if repository meets the requirements of being forked.
  237. func (repo *Repository) CanBeForked() bool {
  238. return !repo.IsBare && !repo.IsMirror
  239. }
  240. func (repo *Repository) NextIssueIndex() int64 {
  241. return int64(repo.NumIssues+repo.NumPulls) + 1
  242. }
  243. var (
  244. DescPattern = regexp.MustCompile(`https?://\S+`)
  245. )
  246. // DescriptionHtml does special handles to description and return HTML string.
  247. func (repo *Repository) DescriptionHtml() template.HTML {
  248. sanitize := func(s string) string {
  249. return fmt.Sprintf(`<a href="%[1]s" target="_blank">%[1]s</a>`, s)
  250. }
  251. return template.HTML(DescPattern.ReplaceAllStringFunc(base.Sanitizer.Sanitize(repo.Description), sanitize))
  252. }
  253. func (repo *Repository) LocalCopyPath() string {
  254. return path.Join(setting.RepoRootPath, "local", com.ToStr(repo.ID))
  255. }
  256. // UpdateLocalCopy makes sure the local copy of repository is up-to-date.
  257. func (repo *Repository) UpdateLocalCopy() error {
  258. repoPath, err := repo.RepoPath()
  259. if err != nil {
  260. return err
  261. }
  262. localPath := repo.LocalCopyPath()
  263. if !com.IsExist(localPath) {
  264. _, stderr, err := process.Exec(
  265. fmt.Sprintf("UpdateLocalCopy(git clone): %s", repoPath), "git", "clone", repoPath, localPath)
  266. if err != nil {
  267. return fmt.Errorf("git clone: %v - %s", err, stderr)
  268. }
  269. } else {
  270. _, stderr, err := process.ExecDir(-1, localPath,
  271. fmt.Sprintf("UpdateLocalCopy(git pull): %s", repoPath), "git", "pull")
  272. if err != nil {
  273. return fmt.Errorf("git pull: %v - %s", err, stderr)
  274. }
  275. }
  276. return nil
  277. }
  278. func isRepositoryExist(e Engine, u *User, repoName string) (bool, error) {
  279. has, err := e.Get(&Repository{
  280. OwnerID: u.Id,
  281. LowerName: strings.ToLower(repoName),
  282. })
  283. return has && com.IsDir(RepoPath(u.Name, repoName)), err
  284. }
  285. // IsRepositoryExist returns true if the repository with given name under user has already existed.
  286. func IsRepositoryExist(u *User, repoName string) (bool, error) {
  287. return isRepositoryExist(x, u, repoName)
  288. }
  289. // CloneLink represents different types of clone URLs of repository.
  290. type CloneLink struct {
  291. SSH string
  292. HTTPS string
  293. Git string
  294. }
  295. // CloneLink returns clone URLs of repository.
  296. func (repo *Repository) CloneLink() (cl CloneLink, err error) {
  297. if err = repo.GetOwner(); err != nil {
  298. return cl, err
  299. }
  300. if setting.SSHPort != 22 {
  301. cl.SSH = fmt.Sprintf("ssh://%s@%s:%d/%s/%s.git", setting.RunUser, setting.SSHDomain, setting.SSHPort, repo.Owner.LowerName, repo.LowerName)
  302. } else {
  303. cl.SSH = fmt.Sprintf("%s@%s:%s/%s.git", setting.RunUser, setting.SSHDomain, repo.Owner.LowerName, repo.LowerName)
  304. }
  305. cl.HTTPS = fmt.Sprintf("%s%s/%s.git", setting.AppUrl, repo.Owner.LowerName, repo.LowerName)
  306. return cl, nil
  307. }
  308. var (
  309. reservedNames = []string{"debug", "raw", "install", "api", "avatar", "user", "org", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin", "new"}
  310. reservedPatterns = []string{"*.git", "*.keys"}
  311. )
  312. // IsUsableName checks if name is reserved or pattern of name is not allowed.
  313. func IsUsableName(name string) error {
  314. name = strings.TrimSpace(strings.ToLower(name))
  315. if utf8.RuneCountInString(name) == 0 {
  316. return ErrNameEmpty
  317. }
  318. for i := range reservedNames {
  319. if name == reservedNames[i] {
  320. return ErrNameReserved{name}
  321. }
  322. }
  323. for _, pat := range reservedPatterns {
  324. if pat[0] == '*' && strings.HasSuffix(name, pat[1:]) ||
  325. (pat[len(pat)-1] == '*' && strings.HasPrefix(name, pat[:len(pat)-1])) {
  326. return ErrNamePatternNotAllowed{pat}
  327. }
  328. }
  329. return nil
  330. }
  331. // Mirror represents a mirror information of repository.
  332. type Mirror struct {
  333. ID int64 `xorm:"pk autoincr"`
  334. RepoID int64
  335. Repo *Repository `xorm:"-"`
  336. Interval int // Hour.
  337. Updated time.Time `xorm:"UPDATED"`
  338. NextUpdate time.Time
  339. }
  340. func (m *Mirror) AfterSet(colName string, _ xorm.Cell) {
  341. var err error
  342. switch colName {
  343. case "repo_id":
  344. m.Repo, err = GetRepositoryByID(m.RepoID)
  345. if err != nil {
  346. log.Error(3, "GetRepositoryByID[%d]: %v", m.ID, err)
  347. }
  348. }
  349. }
  350. func getMirror(e Engine, repoId int64) (*Mirror, error) {
  351. m := &Mirror{RepoID: repoId}
  352. has, err := e.Get(m)
  353. if err != nil {
  354. return nil, err
  355. } else if !has {
  356. return nil, ErrMirrorNotExist
  357. }
  358. return m, nil
  359. }
  360. // GetMirror returns mirror object by given repository ID.
  361. func GetMirror(repoId int64) (*Mirror, error) {
  362. return getMirror(x, repoId)
  363. }
  364. func updateMirror(e Engine, m *Mirror) error {
  365. _, err := e.Id(m.ID).Update(m)
  366. return err
  367. }
  368. func UpdateMirror(m *Mirror) error {
  369. return updateMirror(x, m)
  370. }
  371. // MirrorRepository creates a mirror repository from source.
  372. func MirrorRepository(repoId int64, userName, repoName, repoPath, url string) error {
  373. _, stderr, err := process.ExecTimeout(10*time.Minute,
  374. fmt.Sprintf("MirrorRepository: %s/%s", userName, repoName),
  375. "git", "clone", "--mirror", url, repoPath)
  376. if err != nil {
  377. return errors.New("git clone --mirror: " + stderr)
  378. }
  379. if _, err = x.InsertOne(&Mirror{
  380. RepoID: repoId,
  381. Interval: 24,
  382. NextUpdate: time.Now().Add(24 * time.Hour),
  383. }); err != nil {
  384. return err
  385. }
  386. return nil
  387. }
  388. // MigrateRepository migrates a existing repository from other project hosting.
  389. func MigrateRepository(u *User, name, desc string, private, mirror bool, url string) (*Repository, error) {
  390. repo, err := CreateRepository(u, CreateRepoOptions{
  391. Name: name,
  392. Description: desc,
  393. IsPrivate: private,
  394. IsMirror: mirror,
  395. })
  396. if err != nil {
  397. return nil, err
  398. }
  399. // Clone to temprory path and do the init commit.
  400. tmpDir := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()))
  401. os.MkdirAll(tmpDir, os.ModePerm)
  402. repoPath := RepoPath(u.Name, name)
  403. if u.IsOrganization() {
  404. t, err := u.GetOwnerTeam()
  405. if err != nil {
  406. return nil, err
  407. }
  408. repo.NumWatches = t.NumMembers
  409. } else {
  410. repo.NumWatches = 1
  411. }
  412. repo.IsBare = false
  413. if mirror {
  414. if err = MirrorRepository(repo.ID, u.Name, repo.Name, repoPath, url); err != nil {
  415. return repo, err
  416. }
  417. repo.IsMirror = true
  418. return repo, UpdateRepository(repo, false)
  419. } else {
  420. os.RemoveAll(repoPath)
  421. }
  422. // FIXME: this command could for both migrate and mirror
  423. _, stderr, err := process.ExecTimeout(10*time.Minute,
  424. fmt.Sprintf("MigrateRepository: %s", repoPath),
  425. "git", "clone", "--mirror", "--bare", "--quiet", url, repoPath)
  426. if err != nil {
  427. return repo, fmt.Errorf("git clone --mirror --bare --quiet: %v", stderr)
  428. } else if err = createUpdateHook(repoPath); err != nil {
  429. return repo, fmt.Errorf("create update hook: %v", err)
  430. }
  431. // Check if repository is empty.
  432. _, stderr, err = com.ExecCmdDir(repoPath, "git", "log", "-1")
  433. if err != nil {
  434. if strings.Contains(stderr, "fatal: bad default revision 'HEAD'") {
  435. repo.IsBare = true
  436. } else {
  437. return repo, fmt.Errorf("check bare: %v - %s", err, stderr)
  438. }
  439. }
  440. // Check if repository has master branch, if so set it to default branch.
  441. gitRepo, err := git.OpenRepository(repoPath)
  442. if err != nil {
  443. return repo, fmt.Errorf("open git repository: %v", err)
  444. }
  445. if gitRepo.IsBranchExist("master") {
  446. repo.DefaultBranch = "master"
  447. }
  448. return repo, UpdateRepository(repo, false)
  449. }
  450. // initRepoCommit temporarily changes with work directory.
  451. func initRepoCommit(tmpPath string, sig *git.Signature) (err error) {
  452. var stderr string
  453. if _, stderr, err = process.ExecDir(-1,
  454. tmpPath, fmt.Sprintf("initRepoCommit(git add): %s", tmpPath),
  455. "git", "add", "--all"); err != nil {
  456. return fmt.Errorf("git add: %s", stderr)
  457. }
  458. if _, stderr, err = process.ExecDir(-1,
  459. tmpPath, fmt.Sprintf("initRepoCommit(git commit): %s", tmpPath),
  460. "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email),
  461. "-m", "initial commit"); err != nil {
  462. return fmt.Errorf("git commit: %s", stderr)
  463. }
  464. if _, stderr, err = process.ExecDir(-1,
  465. tmpPath, fmt.Sprintf("initRepoCommit(git push): %s", tmpPath),
  466. "git", "push", "origin", "master"); err != nil {
  467. return fmt.Errorf("git push: %s", stderr)
  468. }
  469. return nil
  470. }
  471. func createUpdateHook(repoPath string) error {
  472. hookPath := path.Join(repoPath, "hooks/update")
  473. os.MkdirAll(path.Dir(hookPath), os.ModePerm)
  474. return ioutil.WriteFile(hookPath,
  475. []byte(fmt.Sprintf(_TPL_UPDATE_HOOK, setting.ScriptType, "\""+appPath+"\"", setting.CustomConf)), 0777)
  476. }
  477. type CreateRepoOptions struct {
  478. Name string
  479. Description string
  480. Gitignores string
  481. License string
  482. Readme string
  483. IsPrivate bool
  484. IsMirror bool
  485. AutoInit bool
  486. }
  487. func getRepoInitFile(tp, name string) ([]byte, error) {
  488. relPath := path.Join("conf", tp, name)
  489. // Use custom file when available.
  490. customPath := path.Join(setting.CustomPath, relPath)
  491. if com.IsFile(customPath) {
  492. return ioutil.ReadFile(customPath)
  493. }
  494. return bindata.Asset(relPath)
  495. }
  496. func prepareRepoCommit(repo *Repository, tmpDir, repoPath string, opts CreateRepoOptions) error {
  497. // Clone to temprory path and do the init commit.
  498. _, stderr, err := process.Exec(
  499. fmt.Sprintf("initRepository(git clone): %s", repoPath), "git", "clone", repoPath, tmpDir)
  500. if err != nil {
  501. return fmt.Errorf("git clone: %v - %s", err, stderr)
  502. }
  503. // README
  504. data, err := getRepoInitFile("readme", opts.Readme)
  505. if err != nil {
  506. return fmt.Errorf("getRepoInitFile[%s]: %v", opts.Readme, err)
  507. }
  508. cloneLink, err := repo.CloneLink()
  509. if err != nil {
  510. return fmt.Errorf("CloneLink: %v", err)
  511. }
  512. match := map[string]string{
  513. "Name": repo.Name,
  514. "Description": repo.Description,
  515. "CloneURL.SSH": cloneLink.SSH,
  516. "CloneURL.HTTPS": cloneLink.HTTPS,
  517. }
  518. if err = ioutil.WriteFile(filepath.Join(tmpDir, "README.md"),
  519. []byte(com.Expand(string(data), match)), 0644); err != nil {
  520. return fmt.Errorf("write README.md: %v", err)
  521. }
  522. // .gitignore
  523. if len(opts.Gitignores) > 0 {
  524. var buf bytes.Buffer
  525. names := strings.Split(opts.Gitignores, ",")
  526. for _, name := range names {
  527. data, err = getRepoInitFile("gitignore", name)
  528. if err != nil {
  529. return fmt.Errorf("getRepoInitFile[%s]: %v", name, err)
  530. }
  531. buf.WriteString("# ---> " + name + "\n")
  532. buf.Write(data)
  533. buf.WriteString("\n")
  534. }
  535. if buf.Len() > 0 {
  536. if err = ioutil.WriteFile(filepath.Join(tmpDir, ".gitignore"), buf.Bytes(), 0644); err != nil {
  537. return fmt.Errorf("write .gitignore: %v", err)
  538. }
  539. }
  540. }
  541. // LICENSE
  542. if len(opts.License) > 0 {
  543. data, err = getRepoInitFile("license", opts.License)
  544. if err != nil {
  545. return fmt.Errorf("getRepoInitFile[%s]: %v", opts.License, err)
  546. }
  547. if err = ioutil.WriteFile(filepath.Join(tmpDir, "LICENSE"), data, 0644); err != nil {
  548. return fmt.Errorf("write LICENSE: %v", err)
  549. }
  550. }
  551. return nil
  552. }
  553. // InitRepository initializes README and .gitignore if needed.
  554. func initRepository(e Engine, repoPath string, u *User, repo *Repository, opts CreateRepoOptions) error {
  555. // Somehow the directory could exist.
  556. if com.IsExist(repoPath) {
  557. return fmt.Errorf("initRepository: path already exists: %s", repoPath)
  558. }
  559. // Init bare new repository.
  560. os.MkdirAll(repoPath, os.ModePerm)
  561. _, stderr, err := process.ExecDir(-1, repoPath,
  562. fmt.Sprintf("initRepository(git init --bare): %s", repoPath), "git", "init", "--bare")
  563. if err != nil {
  564. return fmt.Errorf("git init --bare: %v - %s", err, stderr)
  565. }
  566. if err := createUpdateHook(repoPath); err != nil {
  567. return err
  568. }
  569. tmpDir := filepath.Join(os.TempDir(), "gogs-"+repo.Name+"-"+com.ToStr(time.Now().Nanosecond()))
  570. // Initialize repository according to user's choice.
  571. if opts.AutoInit {
  572. os.MkdirAll(tmpDir, os.ModePerm)
  573. defer os.RemoveAll(tmpDir)
  574. if err = prepareRepoCommit(repo, tmpDir, repoPath, opts); err != nil {
  575. return fmt.Errorf("prepareRepoCommit: %v", err)
  576. }
  577. // Apply changes and commit.
  578. if err = initRepoCommit(tmpDir, u.NewGitSig()); err != nil {
  579. return fmt.Errorf("initRepoCommit: %v", err)
  580. }
  581. }
  582. // Re-fetch the repository from database before updating it (else it would
  583. // override changes that were done earlier with sql)
  584. if repo, err = getRepositoryByID(e, repo.ID); err != nil {
  585. return fmt.Errorf("getRepositoryByID: %v", err)
  586. }
  587. if !opts.AutoInit {
  588. repo.IsBare = true
  589. }
  590. repo.DefaultBranch = "master"
  591. if err = updateRepository(e, repo, false); err != nil {
  592. return fmt.Errorf("updateRepository: %v", err)
  593. }
  594. return nil
  595. }
  596. func createRepository(e *xorm.Session, u *User, repo *Repository) (err error) {
  597. if err = IsUsableName(repo.Name); err != nil {
  598. return err
  599. }
  600. has, err := isRepositoryExist(e, u, repo.Name)
  601. if err != nil {
  602. return fmt.Errorf("IsRepositoryExist: %v", err)
  603. } else if has {
  604. return ErrRepoAlreadyExist{u.Name, repo.Name}
  605. }
  606. if _, err = e.Insert(repo); err != nil {
  607. return err
  608. }
  609. u.NumRepos++
  610. // Remember visibility preference.
  611. u.LastRepoVisibility = repo.IsPrivate
  612. if err = updateUser(e, u); err != nil {
  613. return fmt.Errorf("updateUser: %v", err)
  614. }
  615. // Give access to all members in owner team.
  616. if u.IsOrganization() {
  617. t, err := u.getOwnerTeam(e)
  618. if err != nil {
  619. return fmt.Errorf("getOwnerTeam: %v", err)
  620. } else if err = t.addRepository(e, repo); err != nil {
  621. return fmt.Errorf("addRepository: %v", err)
  622. }
  623. } else {
  624. // Organization automatically called this in addRepository method.
  625. if err = repo.recalculateAccesses(e); err != nil {
  626. return fmt.Errorf("recalculateAccesses: %v", err)
  627. }
  628. }
  629. if err = watchRepo(e, u.Id, repo.ID, true); err != nil {
  630. return fmt.Errorf("watchRepo: %v", err)
  631. } else if err = newRepoAction(e, u, repo); err != nil {
  632. return fmt.Errorf("newRepoAction: %v", err)
  633. }
  634. return nil
  635. }
  636. // CreateRepository creates a repository for given user or organization.
  637. func CreateRepository(u *User, opts CreateRepoOptions) (_ *Repository, err error) {
  638. repo := &Repository{
  639. OwnerID: u.Id,
  640. Owner: u,
  641. Name: opts.Name,
  642. LowerName: strings.ToLower(opts.Name),
  643. Description: opts.Description,
  644. IsPrivate: opts.IsPrivate,
  645. }
  646. sess := x.NewSession()
  647. defer sessionRelease(sess)
  648. if err = sess.Begin(); err != nil {
  649. return nil, err
  650. }
  651. if err = createRepository(sess, u, repo); err != nil {
  652. return nil, err
  653. }
  654. // No need for init mirror.
  655. if !opts.IsMirror {
  656. repoPath := RepoPath(u.Name, repo.Name)
  657. if err = initRepository(sess, repoPath, u, repo, opts); err != nil {
  658. if err2 := os.RemoveAll(repoPath); err2 != nil {
  659. log.Error(4, "initRepository: %v", err)
  660. return nil, fmt.Errorf(
  661. "delete repo directory %s/%s failed(2): %v", u.Name, repo.Name, err2)
  662. }
  663. return nil, fmt.Errorf("initRepository: %v", err)
  664. }
  665. _, stderr, err := process.ExecDir(-1,
  666. repoPath, fmt.Sprintf("CreateRepository(git update-server-info): %s", repoPath),
  667. "git", "update-server-info")
  668. if err != nil {
  669. return nil, errors.New("CreateRepository(git update-server-info): " + stderr)
  670. }
  671. }
  672. return repo, sess.Commit()
  673. }
  674. func countRepositories(showPrivate bool) int64 {
  675. sess := x.NewSession()
  676. if !showPrivate {
  677. sess.Where("is_private=", false)
  678. }
  679. count, _ := sess.Count(new(Repository))
  680. return count
  681. }
  682. // CountRepositories returns number of repositories.
  683. func CountRepositories() int64 {
  684. return countRepositories(true)
  685. }
  686. // CountPublicRepositories returns number of public repositories.
  687. func CountPublicRepositories() int64 {
  688. return countRepositories(false)
  689. }
  690. // RepositoriesWithUsers returns number of repos in given page.
  691. func RepositoriesWithUsers(page, pageSize int) (_ []*Repository, err error) {
  692. repos := make([]*Repository, 0, pageSize)
  693. if err = x.Limit(pageSize, (page-1)*pageSize).Asc("id").Find(&repos); err != nil {
  694. return nil, err
  695. }
  696. for i := range repos {
  697. if err = repos[i].GetOwner(); err != nil {
  698. return nil, err
  699. }
  700. }
  701. return repos, nil
  702. }
  703. // RepoPath returns repository path by given user and repository name.
  704. func RepoPath(userName, repoName string) string {
  705. return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".git")
  706. }
  707. // TransferOwnership transfers all corresponding setting from old user to new one.
  708. func TransferOwnership(u *User, newOwnerName string, repo *Repository) error {
  709. newOwner, err := GetUserByName(newOwnerName)
  710. if err != nil {
  711. return fmt.Errorf("get new owner '%s': %v", newOwnerName, err)
  712. }
  713. // Check if new owner has repository with same name.
  714. has, err := IsRepositoryExist(newOwner, repo.Name)
  715. if err != nil {
  716. return fmt.Errorf("IsRepositoryExist: %v", err)
  717. } else if has {
  718. return ErrRepoAlreadyExist{newOwnerName, repo.Name}
  719. }
  720. sess := x.NewSession()
  721. defer sessionRelease(sess)
  722. if err = sess.Begin(); err != nil {
  723. return fmt.Errorf("sess.Begin: %v", err)
  724. }
  725. owner := repo.Owner
  726. // Note: we have to set value here to make sure recalculate accesses is based on
  727. // new owner.
  728. repo.OwnerID = newOwner.Id
  729. repo.Owner = newOwner
  730. // Update repository.
  731. if _, err := sess.Id(repo.ID).Update(repo); err != nil {
  732. return fmt.Errorf("update owner: %v", err)
  733. }
  734. // Remove redundant collaborators.
  735. collaborators, err := repo.GetCollaborators()
  736. if err != nil {
  737. return fmt.Errorf("GetCollaborators: %v", err)
  738. }
  739. // Dummy object.
  740. collaboration := &Collaboration{RepoID: repo.ID}
  741. for _, c := range collaborators {
  742. collaboration.UserID = c.Id
  743. if c.Id == newOwner.Id || newOwner.IsOrgMember(c.Id) {
  744. if _, err = sess.Delete(collaboration); err != nil {
  745. return fmt.Errorf("remove collaborator '%d': %v", c.Id, err)
  746. }
  747. }
  748. }
  749. // Remove old team-repository relations.
  750. if owner.IsOrganization() {
  751. if err = owner.getTeams(sess); err != nil {
  752. return fmt.Errorf("getTeams: %v", err)
  753. }
  754. for _, t := range owner.Teams {
  755. if !t.hasRepository(sess, repo.ID) {
  756. continue
  757. }
  758. t.NumRepos--
  759. if _, err := sess.Id(t.ID).AllCols().Update(t); err != nil {
  760. return fmt.Errorf("decrease team repository count '%d': %v", t.ID, err)
  761. }
  762. }
  763. if err = owner.removeOrgRepo(sess, repo.ID); err != nil {
  764. return fmt.Errorf("removeOrgRepo: %v", err)
  765. }
  766. }
  767. if newOwner.IsOrganization() {
  768. t, err := newOwner.GetOwnerTeam()
  769. if err != nil {
  770. return fmt.Errorf("GetOwnerTeam: %v", err)
  771. } else if err = t.addRepository(sess, repo); err != nil {
  772. return fmt.Errorf("add to owner team: %v", err)
  773. }
  774. } else {
  775. // Organization called this in addRepository method.
  776. if err = repo.recalculateAccesses(sess); err != nil {
  777. return fmt.Errorf("recalculateAccesses: %v", err)
  778. }
  779. }
  780. // Update repository count.
  781. if _, err = sess.Exec("UPDATE `user` SET num_repos=num_repos+1 WHERE id=?", newOwner.Id); err != nil {
  782. return fmt.Errorf("increase new owner repository count: %v", err)
  783. } else if _, err = sess.Exec("UPDATE `user` SET num_repos=num_repos-1 WHERE id=?", owner.Id); err != nil {
  784. return fmt.Errorf("decrease old owner repository count: %v", err)
  785. }
  786. if err = watchRepo(sess, newOwner.Id, repo.ID, true); err != nil {
  787. return fmt.Errorf("watchRepo: %v", err)
  788. } else if err = transferRepoAction(sess, u, owner, newOwner, repo); err != nil {
  789. return fmt.Errorf("transferRepoAction: %v", err)
  790. }
  791. // Change repository directory name.
  792. if err = os.Rename(RepoPath(owner.Name, repo.Name), RepoPath(newOwner.Name, repo.Name)); err != nil {
  793. return fmt.Errorf("rename directory: %v", err)
  794. }
  795. return sess.Commit()
  796. }
  797. // ChangeRepositoryName changes all corresponding setting from old repository name to new one.
  798. func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error) {
  799. oldRepoName = strings.ToLower(oldRepoName)
  800. newRepoName = strings.ToLower(newRepoName)
  801. if err = IsUsableName(newRepoName); err != nil {
  802. return err
  803. }
  804. has, err := IsRepositoryExist(u, newRepoName)
  805. if err != nil {
  806. return fmt.Errorf("IsRepositoryExist: %v", err)
  807. } else if has {
  808. return ErrRepoAlreadyExist{u.Name, newRepoName}
  809. }
  810. // Change repository directory name.
  811. return os.Rename(RepoPath(u.LowerName, oldRepoName), RepoPath(u.LowerName, newRepoName))
  812. }
  813. func getRepositoriesByForkID(e Engine, forkID int64) ([]*Repository, error) {
  814. repos := make([]*Repository, 0, 10)
  815. return repos, e.Where("fork_id=?", forkID).Find(&repos)
  816. }
  817. // GetRepositoriesByForkID returns all repositories with given fork ID.
  818. func GetRepositoriesByForkID(forkID int64) ([]*Repository, error) {
  819. return getRepositoriesByForkID(x, forkID)
  820. }
  821. func updateRepository(e Engine, repo *Repository, visibilityChanged bool) (err error) {
  822. repo.LowerName = strings.ToLower(repo.Name)
  823. if len(repo.Description) > 255 {
  824. repo.Description = repo.Description[:255]
  825. }
  826. if len(repo.Website) > 255 {
  827. repo.Website = repo.Website[:255]
  828. }
  829. if _, err = e.Id(repo.ID).AllCols().Update(repo); err != nil {
  830. return fmt.Errorf("update: %v", err)
  831. }
  832. if visibilityChanged {
  833. if err = repo.getOwner(e); err != nil {
  834. return fmt.Errorf("getOwner: %v", err)
  835. }
  836. if repo.Owner.IsOrganization() {
  837. // Organization repository need to recalculate access table when visivility is changed.
  838. if err = repo.recalculateTeamAccesses(e, 0); err != nil {
  839. return fmt.Errorf("recalculateTeamAccesses: %v", err)
  840. }
  841. }
  842. forkRepos, err := getRepositoriesByForkID(e, repo.ID)
  843. if err != nil {
  844. return fmt.Errorf("getRepositoriesByForkID: %v", err)
  845. }
  846. for i := range forkRepos {
  847. forkRepos[i].IsPrivate = repo.IsPrivate
  848. if err = updateRepository(e, forkRepos[i], true); err != nil {
  849. return fmt.Errorf("updateRepository[%d]: %v", forkRepos[i].ID, err)
  850. }
  851. }
  852. }
  853. return nil
  854. }
  855. func UpdateRepository(repo *Repository, visibilityChanged bool) (err error) {
  856. sess := x.NewSession()
  857. defer sessionRelease(sess)
  858. if err = sess.Begin(); err != nil {
  859. return err
  860. }
  861. if err = updateRepository(x, repo, visibilityChanged); err != nil {
  862. return fmt.Errorf("updateRepository: %v", err)
  863. }
  864. return sess.Commit()
  865. }
  866. // DeleteRepository deletes a repository for a user or organization.
  867. func DeleteRepository(uid, repoID int64) error {
  868. repo := &Repository{ID: repoID, OwnerID: uid}
  869. has, err := x.Get(repo)
  870. if err != nil {
  871. return err
  872. } else if !has {
  873. return ErrRepoNotExist{repoID, uid, ""}
  874. }
  875. // In case is a organization.
  876. org, err := GetUserByID(uid)
  877. if err != nil {
  878. return err
  879. }
  880. if org.IsOrganization() {
  881. if err = org.GetTeams(); err != nil {
  882. return err
  883. }
  884. }
  885. sess := x.NewSession()
  886. defer sessionRelease(sess)
  887. if err = sess.Begin(); err != nil {
  888. return err
  889. }
  890. if org.IsOrganization() {
  891. for _, t := range org.Teams {
  892. if !t.hasRepository(sess, repoID) {
  893. continue
  894. } else if err = t.removeRepository(sess, repo, false); err != nil {
  895. return err
  896. }
  897. }
  898. }
  899. if _, err = sess.Delete(&Repository{ID: repoID}); err != nil {
  900. return err
  901. } else if _, err = sess.Delete(&Access{RepoID: repo.ID}); err != nil {
  902. return err
  903. } else if _, err = sess.Delete(&Action{RepoID: repo.ID}); err != nil {
  904. return err
  905. } else if _, err = sess.Delete(&Watch{RepoID: repoID}); err != nil {
  906. return err
  907. } else if _, err = sess.Delete(&Mirror{RepoID: repoID}); err != nil {
  908. return err
  909. } else if _, err = sess.Delete(&IssueUser{RepoID: repoID}); err != nil {
  910. return err
  911. } else if _, err = sess.Delete(&Milestone{RepoID: repoID}); err != nil {
  912. return err
  913. } else if _, err = sess.Delete(&Release{RepoId: repoID}); err != nil {
  914. return err
  915. } else if _, err = sess.Delete(&Collaboration{RepoID: repoID}); err != nil {
  916. return err
  917. } else if _, err = sess.Delete(&PullRequest{BaseRepoID: repoID}); err != nil {
  918. return err
  919. }
  920. // Delete comments and attachments.
  921. issues := make([]*Issue, 0, 25)
  922. attachmentPaths := make([]string, 0, len(issues))
  923. if err = sess.Where("repo_id=?", repoID).Find(&issues); err != nil {
  924. return err
  925. }
  926. for i := range issues {
  927. if _, err = sess.Delete(&Comment{IssueID: issues[i].ID}); err != nil {
  928. return err
  929. }
  930. attachments := make([]*Attachment, 0, 5)
  931. if err = sess.Where("issue_id=?", issues[i].ID).Find(&attachments); err != nil {
  932. return err
  933. }
  934. for j := range attachments {
  935. attachmentPaths = append(attachmentPaths, attachments[j].LocalPath())
  936. }
  937. if _, err = sess.Delete(&Attachment{IssueID: issues[i].ID}); err != nil {
  938. return err
  939. }
  940. }
  941. if _, err = sess.Delete(&Issue{RepoID: repoID}); err != nil {
  942. return err
  943. }
  944. if repo.IsFork {
  945. if _, err = sess.Exec("UPDATE `repository` SET num_forks=num_forks-1 WHERE id=?", repo.ForkID); err != nil {
  946. return fmt.Errorf("decrease fork count: %v", err)
  947. }
  948. }
  949. if _, err = sess.Exec("UPDATE `user` SET num_repos=num_repos-1 WHERE id=?", uid); err != nil {
  950. return err
  951. }
  952. // Remove repository files.
  953. repoPath, err := repo.repoPath(sess)
  954. if err != nil {
  955. return fmt.Errorf("RepoPath: %v", err)
  956. }
  957. if err = os.RemoveAll(repoPath); err != nil {
  958. desc := fmt.Sprintf("delete repository files[%s]: %v", repoPath, err)
  959. log.Warn(desc)
  960. if err = CreateRepositoryNotice(desc); err != nil {
  961. log.Error(4, "add notice: %v", err)
  962. }
  963. }
  964. // Remove attachment files.
  965. for i := range attachmentPaths {
  966. if err = os.Remove(attachmentPaths[i]); err != nil {
  967. log.Warn("delete attachment: %v", err)
  968. }
  969. }
  970. if err = sess.Commit(); err != nil {
  971. return fmt.Errorf("Commit: %v", err)
  972. }
  973. if repo.NumForks > 0 {
  974. if repo.IsPrivate {
  975. forkRepos, err := GetRepositoriesByForkID(repo.ID)
  976. if err != nil {
  977. return fmt.Errorf("getRepositoriesByForkID: %v", err)
  978. }
  979. for i := range forkRepos {
  980. if err = DeleteRepository(forkRepos[i].OwnerID, forkRepos[i].ID); err != nil {
  981. log.Error(4, "updateRepository[%d]: %v", forkRepos[i].ID, err)
  982. }
  983. }
  984. } else {
  985. if _, err = x.Exec("UPDATE `repository` SET fork_id=0,is_fork=? WHERE fork_id=?", false, repo.ID); err != nil {
  986. log.Error(4, "reset 'fork_id' and 'is_fork': %v", err)
  987. }
  988. }
  989. }
  990. return nil
  991. }
  992. // GetRepositoryByRef returns a Repository specified by a GFM reference.
  993. // See https://help.github.com/articles/writing-on-github#references for more information on the syntax.
  994. func GetRepositoryByRef(ref string) (*Repository, error) {
  995. n := strings.IndexByte(ref, byte('/'))
  996. if n < 2 {
  997. return nil, ErrInvalidReference
  998. }
  999. userName, repoName := ref[:n], ref[n+1:]
  1000. user, err := GetUserByName(userName)
  1001. if err != nil {
  1002. return nil, err
  1003. }
  1004. return GetRepositoryByName(user.Id, repoName)
  1005. }
  1006. // GetRepositoryByName returns the repository by given name under user if exists.
  1007. func GetRepositoryByName(uid int64, repoName string) (*Repository, error) {
  1008. repo := &Repository{
  1009. OwnerID: uid,
  1010. LowerName: strings.ToLower(repoName),
  1011. }
  1012. has, err := x.Get(repo)
  1013. if err != nil {
  1014. return nil, err
  1015. } else if !has {
  1016. return nil, ErrRepoNotExist{0, uid, repoName}
  1017. }
  1018. return repo, err
  1019. }
  1020. func getRepositoryByID(e Engine, id int64) (*Repository, error) {
  1021. repo := new(Repository)
  1022. has, err := e.Id(id).Get(repo)
  1023. if err != nil {
  1024. return nil, err
  1025. } else if !has {
  1026. return nil, ErrRepoNotExist{id, 0, ""}
  1027. }
  1028. return repo, nil
  1029. }
  1030. // GetRepositoryByID returns the repository by given id if exists.
  1031. func GetRepositoryByID(id int64) (*Repository, error) {
  1032. return getRepositoryByID(x, id)
  1033. }
  1034. // GetRepositories returns a list of repositories of given user.
  1035. func GetRepositories(uid int64, private bool) ([]*Repository, error) {
  1036. repos := make([]*Repository, 0, 10)
  1037. sess := x.Desc("updated")
  1038. if !private {
  1039. sess.Where("is_private=?", false)
  1040. }
  1041. return repos, sess.Find(&repos, &Repository{OwnerID: uid})
  1042. }
  1043. // GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
  1044. func GetRecentUpdatedRepositories(page int) (repos []*Repository, err error) {
  1045. return repos, x.Limit(setting.ExplorePagingNum, (page-1)*setting.ExplorePagingNum).
  1046. Where("is_private=?", false).Limit(setting.ExplorePagingNum).Desc("updated").Find(&repos)
  1047. }
  1048. func getRepositoryCount(e Engine, u *User) (int64, error) {
  1049. return x.Count(&Repository{OwnerID: u.Id})
  1050. }
  1051. // GetRepositoryCount returns the total number of repositories of user.
  1052. func GetRepositoryCount(u *User) (int64, error) {
  1053. return getRepositoryCount(x, u)
  1054. }
  1055. type SearchOption struct {
  1056. Keyword string
  1057. Uid int64
  1058. Limit int
  1059. Private bool
  1060. }
  1061. // SearchRepositoryByName returns given number of repositories whose name contains keyword.
  1062. func SearchRepositoryByName(opt SearchOption) (repos []*Repository, err error) {
  1063. if len(opt.Keyword) == 0 {
  1064. return repos, nil
  1065. }
  1066. opt.Keyword = strings.ToLower(opt.Keyword)
  1067. repos = make([]*Repository, 0, opt.Limit)
  1068. // Append conditions.
  1069. sess := x.Limit(opt.Limit)
  1070. if opt.Uid > 0 {
  1071. sess.Where("owner_id=?", opt.Uid)
  1072. }
  1073. if !opt.Private {
  1074. sess.And("is_private=?", false)
  1075. }
  1076. sess.And("lower_name like ?", "%"+opt.Keyword+"%").Find(&repos)
  1077. return repos, err
  1078. }
  1079. // DeleteRepositoryArchives deletes all repositories' archives.
  1080. func DeleteRepositoryArchives() error {
  1081. return x.Where("id > 0").Iterate(new(Repository),
  1082. func(idx int, bean interface{}) error {
  1083. repo := bean.(*Repository)
  1084. if err := repo.GetOwner(); err != nil {
  1085. return err
  1086. }
  1087. return os.RemoveAll(filepath.Join(RepoPath(repo.Owner.Name, repo.Name), "archives"))
  1088. })
  1089. }
  1090. // RewriteRepositoryUpdateHook rewrites all repositories' update hook.
  1091. func RewriteRepositoryUpdateHook() error {
  1092. return x.Where("id > 0").Iterate(new(Repository),
  1093. func(idx int, bean interface{}) error {
  1094. repo := bean.(*Repository)
  1095. if err := repo.GetOwner(); err != nil {
  1096. return err
  1097. }
  1098. return createUpdateHook(RepoPath(repo.Owner.Name, repo.Name))
  1099. })
  1100. }
  1101. var (
  1102. // Prevent duplicate running tasks.
  1103. isMirrorUpdating = false
  1104. isGitFscking = false
  1105. isCheckingRepos = false
  1106. )
  1107. // MirrorUpdate checks and updates mirror repositories.
  1108. func MirrorUpdate() {
  1109. if isMirrorUpdating {
  1110. return
  1111. }
  1112. isMirrorUpdating = true
  1113. defer func() { isMirrorUpdating = false }()
  1114. log.Trace("Doing: MirrorUpdate")
  1115. mirrors := make([]*Mirror, 0, 10)
  1116. if err := x.Iterate(new(Mirror), func(idx int, bean interface{}) error {
  1117. m := bean.(*Mirror)
  1118. if m.NextUpdate.After(time.Now()) {
  1119. return nil
  1120. }
  1121. if m.Repo == nil {
  1122. log.Error(4, "Disconnected mirror repository found: %d", m.ID)
  1123. return nil
  1124. }
  1125. repoPath, err := m.Repo.RepoPath()
  1126. if err != nil {
  1127. return fmt.Errorf("Repo.RepoPath: %v", err)
  1128. }
  1129. if _, stderr, err := process.ExecDir(10*time.Minute,
  1130. repoPath, fmt.Sprintf("MirrorUpdate: %s", repoPath),
  1131. "git", "remote", "update", "--prune"); err != nil {
  1132. desc := fmt.Sprintf("Fail to update mirror repository(%s): %s", repoPath, stderr)
  1133. log.Error(4, desc)
  1134. if err = CreateRepositoryNotice(desc); err != nil {
  1135. log.Error(4, "CreateRepositoryNotice: %v", err)
  1136. }
  1137. return nil
  1138. }
  1139. m.NextUpdate = time.Now().Add(time.Duration(m.Interval) * time.Hour)
  1140. mirrors = append(mirrors, m)
  1141. return nil
  1142. }); err != nil {
  1143. log.Error(4, "MirrorUpdate: %v", err)
  1144. }
  1145. for i := range mirrors {
  1146. if err := UpdateMirror(mirrors[i]); err != nil {
  1147. log.Error(4, "UpdateMirror[%d]: %v", mirrors[i].ID, err)
  1148. }
  1149. }
  1150. }
  1151. // GitFsck calls 'git fsck' to check repository health.
  1152. func GitFsck() {
  1153. if isGitFscking {
  1154. return
  1155. }
  1156. isGitFscking = true
  1157. defer func() { isGitFscking = false }()
  1158. log.Trace("Doing: GitFsck")
  1159. args := append([]string{"fsck"}, setting.Cron.RepoHealthCheck.Args...)
  1160. if err := x.Where("id>0").Iterate(new(Repository),
  1161. func(idx int, bean interface{}) error {
  1162. repo := bean.(*Repository)
  1163. repoPath, err := repo.RepoPath()
  1164. if err != nil {
  1165. return fmt.Errorf("RepoPath: %v", err)
  1166. }
  1167. _, _, err = process.ExecDir(-1, repoPath, "Repository health check", "git", args...)
  1168. if err != nil {
  1169. desc := fmt.Sprintf("Fail to health check repository(%s)", repoPath)
  1170. log.Warn(desc)
  1171. if err = CreateRepositoryNotice(desc); err != nil {
  1172. log.Error(4, "CreateRepositoryNotice: %v", err)
  1173. }
  1174. }
  1175. return nil
  1176. }); err != nil {
  1177. log.Error(4, "GitFsck: %v", err)
  1178. }
  1179. }
  1180. func GitGcRepos() error {
  1181. args := append([]string{"gc"}, setting.Git.GcArgs...)
  1182. return x.Where("id > 0").Iterate(new(Repository),
  1183. func(idx int, bean interface{}) error {
  1184. repo := bean.(*Repository)
  1185. if err := repo.GetOwner(); err != nil {
  1186. return err
  1187. }
  1188. _, stderr, err := process.ExecDir(-1, RepoPath(repo.Owner.Name, repo.Name), "Repository garbage collection", "git", args...)
  1189. if err != nil {
  1190. return fmt.Errorf("%v: %v", err, stderr)
  1191. }
  1192. return nil
  1193. })
  1194. }
  1195. type repoChecker struct {
  1196. querySQL, correctSQL string
  1197. desc string
  1198. }
  1199. func repoStatsCheck(checker *repoChecker) {
  1200. results, err := x.Query(checker.querySQL)
  1201. if err != nil {
  1202. log.Error(4, "Select %s: %v", checker.desc, err)
  1203. return
  1204. }
  1205. for _, result := range results {
  1206. id := com.StrTo(result["id"]).MustInt64()
  1207. log.Trace("Updating %s: %d", checker.desc, id)
  1208. _, err = x.Exec(checker.correctSQL, id, id)
  1209. if err != nil {
  1210. log.Error(4, "Update %s[%d]: %v", checker.desc, id, err)
  1211. }
  1212. }
  1213. }
  1214. func CheckRepoStats() {
  1215. if isCheckingRepos {
  1216. return
  1217. }
  1218. isCheckingRepos = true
  1219. defer func() { isCheckingRepos = false }()
  1220. log.Trace("Doing: CheckRepoStats")
  1221. checkers := []*repoChecker{
  1222. // Repository.NumWatches
  1223. {
  1224. "SELECT repo.id FROM `repository` repo WHERE repo.num_watches!=(SELECT COUNT(*) FROM `watch` WHERE repo_id=repo.id)",
  1225. "UPDATE `repository` SET num_watches=(SELECT COUNT(*) FROM `watch` WHERE repo_id=?) WHERE id=?",
  1226. "repository count 'num_watches'",
  1227. },
  1228. // Repository.NumStars
  1229. {
  1230. "SELECT repo.id FROM `repository` repo WHERE repo.num_stars!=(SELECT COUNT(*) FROM `star` WHERE repo_id=repo.id)",
  1231. "UPDATE `repository` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE repo_id=?) WHERE id=?",
  1232. "repository count 'num_stars'",
  1233. },
  1234. // Label.NumIssues
  1235. {
  1236. "SELECT label.id FROM `label` WHERE label.num_issues!=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=label.id)",
  1237. "UPDATE `label` SET num_issues=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=?) WHERE id=?",
  1238. "label count 'num_issues'",
  1239. },
  1240. // User.NumRepos
  1241. {
  1242. "SELECT `user`.id FROM `user` WHERE `user`.num_repos!=(SELECT COUNT(*) FROM `repository` WHERE owner_id=`user`.id)",
  1243. "UPDATE `user` SET num_repos=(SELECT COUNT(*) FROM `repository` WHERE owner_id=?) WHERE id=?",
  1244. "user count 'num_repos'",
  1245. },
  1246. }
  1247. for i := range checkers {
  1248. repoStatsCheck(checkers[i])
  1249. }
  1250. // FIXME: use checker when v0.8, stop supporting old fork repo format.
  1251. // ***** START: Repository.NumForks *****
  1252. results, err := x.Query("SELECT repo.id FROM `repository` repo WHERE repo.num_forks!=(SELECT COUNT(*) FROM `repository` WHERE fork_id=repo.id)")
  1253. if err != nil {
  1254. log.Error(4, "Select repository count 'num_forks': %v", err)
  1255. } else {
  1256. for _, result := range results {
  1257. id := com.StrTo(result["id"]).MustInt64()
  1258. log.Trace("Updating repository count 'num_forks': %d", id)
  1259. repo, err := GetRepositoryByID(id)
  1260. if err != nil {
  1261. log.Error(4, "GetRepositoryByID[%d]: %v", id, err)
  1262. continue
  1263. }
  1264. rawResult, err := x.Query("SELECT COUNT(*) FROM `repository` WHERE fork_id=?", repo.ID)
  1265. if err != nil {
  1266. log.Error(4, "Select count of forks[%d]: %v", repo.ID, err)
  1267. continue
  1268. }
  1269. repo.NumForks = int(parseCountResult(rawResult))
  1270. if err = UpdateRepository(repo, false); err != nil {
  1271. log.Error(4, "UpdateRepository[%d]: %v", id, err)
  1272. continue
  1273. }
  1274. }
  1275. }
  1276. // ***** END: Repository.NumForks *****
  1277. }
  1278. // _________ .__ .__ ___. __ .__
  1279. // \_ ___ \ ____ | | | | _____ \_ |__ ________________ _/ |_|__| ____ ____
  1280. // / \ \/ / _ \| | | | \__ \ | __ \ / _ \_ __ \__ \\ __\ |/ _ \ / \
  1281. // \ \___( <_> ) |_| |__/ __ \| \_\ ( <_> ) | \// __ \| | | ( <_> ) | \
  1282. // \______ /\____/|____/____(____ /___ /\____/|__| (____ /__| |__|\____/|___| /
  1283. // \/ \/ \/ \/ \/
  1284. // A Collaboration is a relation between an individual and a repository
  1285. type Collaboration struct {
  1286. ID int64 `xorm:"pk autoincr"`
  1287. RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  1288. UserID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  1289. Created time.Time `xorm:"CREATED"`
  1290. }
  1291. // Add collaborator and accompanying access
  1292. func (repo *Repository) AddCollaborator(u *User) error {
  1293. collaboration := &Collaboration{
  1294. RepoID: repo.ID,
  1295. UserID: u.Id,
  1296. }
  1297. has, err := x.Get(collaboration)
  1298. if err != nil {
  1299. return err
  1300. } else if has {
  1301. return nil
  1302. }
  1303. if err = repo.GetOwner(); err != nil {
  1304. return fmt.Errorf("GetOwner: %v", err)
  1305. }
  1306. sess := x.NewSession()
  1307. defer sessionRelease(sess)
  1308. if err = sess.Begin(); err != nil {
  1309. return err
  1310. }
  1311. if _, err = sess.InsertOne(collaboration); err != nil {
  1312. return err
  1313. }
  1314. if repo.Owner.IsOrganization() {
  1315. err = repo.recalculateTeamAccesses(sess, 0)
  1316. } else {
  1317. err = repo.recalculateAccesses(sess)
  1318. }
  1319. if err != nil {
  1320. return fmt.Errorf("recalculateAccesses 'team=%v': %v", repo.Owner.IsOrganization(), err)
  1321. }
  1322. return sess.Commit()
  1323. }
  1324. func (repo *Repository) getCollaborators(e Engine) ([]*User, error) {
  1325. collaborations := make([]*Collaboration, 0)
  1326. if err := e.Find(&collaborations, &Collaboration{RepoID: repo.ID}); err != nil {
  1327. return nil, err
  1328. }
  1329. users := make([]*User, len(collaborations))
  1330. for i, c := range collaborations {
  1331. user, err := getUserByID(e, c.UserID)
  1332. if err != nil {
  1333. return nil, err
  1334. }
  1335. users[i] = user
  1336. }
  1337. return users, nil
  1338. }
  1339. // GetCollaborators returns the collaborators for a repository
  1340. func (repo *Repository) GetCollaborators() ([]*User, error) {
  1341. return repo.getCollaborators(x)
  1342. }
  1343. // Delete collaborator and accompanying access
  1344. func (repo *Repository) DeleteCollaborator(u *User) (err error) {
  1345. collaboration := &Collaboration{
  1346. RepoID: repo.ID,
  1347. UserID: u.Id,
  1348. }
  1349. sess := x.NewSession()
  1350. defer sessionRelease(sess)
  1351. if err = sess.Begin(); err != nil {
  1352. return err
  1353. }
  1354. if has, err := sess.Delete(collaboration); err != nil || has == 0 {
  1355. return err
  1356. } else if err = repo.recalculateAccesses(sess); err != nil {
  1357. return err
  1358. }
  1359. return sess.Commit()
  1360. }
  1361. // __ __ __ .__
  1362. // / \ / \_____ _/ |_ ____ | |__
  1363. // \ \/\/ /\__ \\ __\/ ___\| | \
  1364. // \ / / __ \| | \ \___| Y \
  1365. // \__/\ / (____ /__| \___ >___| /
  1366. // \/ \/ \/ \/
  1367. // Watch is connection request for receiving repository notification.
  1368. type Watch struct {
  1369. ID int64 `xorm:"pk autoincr"`
  1370. UserID int64 `xorm:"UNIQUE(watch)"`
  1371. RepoID int64 `xorm:"UNIQUE(watch)"`
  1372. }
  1373. func isWatching(e Engine, uid, repoId int64) bool {
  1374. has, _ := e.Get(&Watch{0, uid, repoId})
  1375. return has
  1376. }
  1377. // IsWatching checks if user has watched given repository.
  1378. func IsWatching(uid, repoId int64) bool {
  1379. return isWatching(x, uid, repoId)
  1380. }
  1381. func watchRepo(e Engine, uid, repoId int64, watch bool) (err error) {
  1382. if watch {
  1383. if isWatching(e, uid, repoId) {
  1384. return nil
  1385. }
  1386. if _, err = e.Insert(&Watch{RepoID: repoId, UserID: uid}); err != nil {
  1387. return err
  1388. }
  1389. _, err = e.Exec("UPDATE `repository` SET num_watches = num_watches + 1 WHERE id = ?", repoId)
  1390. } else {
  1391. if !isWatching(e, uid, repoId) {
  1392. return nil
  1393. }
  1394. if _, err = e.Delete(&Watch{0, uid, repoId}); err != nil {
  1395. return err
  1396. }
  1397. _, err = e.Exec("UPDATE `repository` SET num_watches=num_watches-1 WHERE id=?", repoId)
  1398. }
  1399. return err
  1400. }
  1401. // Watch or unwatch repository.
  1402. func WatchRepo(uid, repoId int64, watch bool) (err error) {
  1403. return watchRepo(x, uid, repoId, watch)
  1404. }
  1405. func getWatchers(e Engine, rid int64) ([]*Watch, error) {
  1406. watches := make([]*Watch, 0, 10)
  1407. err := e.Find(&watches, &Watch{RepoID: rid})
  1408. return watches, err
  1409. }
  1410. // GetWatchers returns all watchers of given repository.
  1411. func GetWatchers(rid int64) ([]*Watch, error) {
  1412. return getWatchers(x, rid)
  1413. }
  1414. // Repository.GetWatchers returns all users watching given repository.
  1415. func (repo *Repository) GetWatchers(offset int) ([]*User, error) {
  1416. users := make([]*User, 0, 10)
  1417. offset = (offset - 1) * ItemsPerPage
  1418. err := x.Limit(ItemsPerPage, offset).Where("repo_id=?", repo.ID).Join("LEFT", "watch", "user.id=watch.user_id").Find(&users)
  1419. return users, err
  1420. }
  1421. func notifyWatchers(e Engine, act *Action) error {
  1422. // Add feeds for user self and all watchers.
  1423. watches, err := getWatchers(e, act.RepoID)
  1424. if err != nil {
  1425. return fmt.Errorf("get watchers: %v", err)
  1426. }
  1427. // Add feed for actioner.
  1428. act.UserID = act.ActUserID
  1429. if _, err = e.InsertOne(act); err != nil {
  1430. return fmt.Errorf("insert new actioner: %v", err)
  1431. }
  1432. for i := range watches {
  1433. if act.ActUserID == watches[i].UserID {
  1434. continue
  1435. }
  1436. act.ID = 0
  1437. act.UserID = watches[i].UserID
  1438. if _, err = e.InsertOne(act); err != nil {
  1439. return fmt.Errorf("insert new action: %v", err)
  1440. }
  1441. }
  1442. return nil
  1443. }
  1444. // NotifyWatchers creates batch of actions for every watcher.
  1445. func NotifyWatchers(act *Action) error {
  1446. return notifyWatchers(x, act)
  1447. }
  1448. // _________ __
  1449. // / _____// |______ _______
  1450. // \_____ \\ __\__ \\_ __ \
  1451. // / \| | / __ \| | \/
  1452. // /_______ /|__| (____ /__|
  1453. // \/ \/
  1454. type Star struct {
  1455. ID int64 `xorm:"pk autoincr"`
  1456. UID int64 `xorm:"UNIQUE(s)"`
  1457. RepoID int64 `xorm:"UNIQUE(s)"`
  1458. }
  1459. // Star or unstar repository.
  1460. func StarRepo(uid, repoId int64, star bool) (err error) {
  1461. if star {
  1462. if IsStaring(uid, repoId) {
  1463. return nil
  1464. }
  1465. if _, err = x.Insert(&Star{UID: uid, RepoID: repoId}); err != nil {
  1466. return err
  1467. } else if _, err = x.Exec("UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoId); err != nil {
  1468. return err
  1469. }
  1470. _, err = x.Exec("UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", uid)
  1471. } else {
  1472. if !IsStaring(uid, repoId) {
  1473. return nil
  1474. }
  1475. if _, err = x.Delete(&Star{0, uid, repoId}); err != nil {
  1476. return err
  1477. } else if _, err = x.Exec("UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoId); err != nil {
  1478. return err
  1479. }
  1480. _, err = x.Exec("UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", uid)
  1481. }
  1482. return err
  1483. }
  1484. // IsStaring checks if user has starred given repository.
  1485. func IsStaring(uid, repoId int64) bool {
  1486. has, _ := x.Get(&Star{0, uid, repoId})
  1487. return has
  1488. }
  1489. func (repo *Repository) GetStars(offset int) ([]*User, error) {
  1490. users := make([]*User, 0, 10)
  1491. offset = (offset - 1) * ItemsPerPage
  1492. err := x.Limit(ItemsPerPage, offset).Where("repo_id=?", repo.ID).Join("LEFT", "star", "user.id=star.uid").Find(&users)
  1493. return users, err
  1494. }
  1495. // ___________ __
  1496. // \_ _____/__________| | __
  1497. // | __)/ _ \_ __ \ |/ /
  1498. // | \( <_> ) | \/ <
  1499. // \___ / \____/|__| |__|_ \
  1500. // \/ \/
  1501. // HasForkedRepo checks if given user has already forked a repository with given ID.
  1502. func HasForkedRepo(ownerID, repoID int64) (*Repository, bool) {
  1503. repo := new(Repository)
  1504. has, _ := x.Where("owner_id=? AND fork_id=?", ownerID, repoID).Get(repo)
  1505. return repo, has
  1506. }
  1507. func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Repository, err error) {
  1508. repo := &Repository{
  1509. OwnerID: u.Id,
  1510. Owner: u,
  1511. Name: name,
  1512. LowerName: strings.ToLower(name),
  1513. Description: desc,
  1514. DefaultBranch: oldRepo.DefaultBranch,
  1515. IsPrivate: oldRepo.IsPrivate,
  1516. IsFork: true,
  1517. ForkID: oldRepo.ID,
  1518. }
  1519. sess := x.NewSession()
  1520. defer sessionRelease(sess)
  1521. if err = sess.Begin(); err != nil {
  1522. return nil, err
  1523. }
  1524. if err = createRepository(sess, u, repo); err != nil {
  1525. return nil, err
  1526. }
  1527. if _, err = sess.Exec("UPDATE `repository` SET num_forks=num_forks+1 WHERE id=?", oldRepo.ID); err != nil {
  1528. return nil, err
  1529. }
  1530. oldRepoPath, err := oldRepo.RepoPath()
  1531. if err != nil {
  1532. return nil, fmt.Errorf("get old repository path: %v", err)
  1533. }
  1534. repoPath := RepoPath(u.Name, repo.Name)
  1535. _, stderr, err := process.ExecTimeout(10*time.Minute,
  1536. fmt.Sprintf("ForkRepository(git clone): %s/%s", u.Name, repo.Name),
  1537. "git", "clone", "--bare", oldRepoPath, repoPath)
  1538. if err != nil {
  1539. return nil, fmt.Errorf("git clone: %v", stderr)
  1540. }
  1541. _, stderr, err = process.ExecDir(-1,
  1542. repoPath, fmt.Sprintf("ForkRepository(git update-server-info): %s", repoPath),
  1543. "git", "update-server-info")
  1544. if err != nil {
  1545. return nil, fmt.Errorf("git update-server-info: %v", err)
  1546. }
  1547. if err = createUpdateHook(repoPath); err != nil {
  1548. return nil, fmt.Errorf("createUpdateHook: %v", err)
  1549. }
  1550. return repo, sess.Commit()
  1551. }
  1552. func (repo *Repository) GetForks() ([]*Repository, error) {
  1553. forks := make([]*Repository, 0, 10)
  1554. err := x.Find(&forks, &Repository{ForkID: repo.ID})
  1555. return forks, err
  1556. }