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

repo.go 42KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  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/template"
  9. "io/ioutil"
  10. "os"
  11. "os/exec"
  12. "path"
  13. "path/filepath"
  14. "regexp"
  15. "sort"
  16. "strings"
  17. "time"
  18. "unicode/utf8"
  19. "github.com/Unknwon/cae/zip"
  20. "github.com/Unknwon/com"
  21. "github.com/go-xorm/xorm"
  22. "github.com/gogits/gogs/modules/base"
  23. "github.com/gogits/gogs/modules/bindata"
  24. "github.com/gogits/gogs/modules/git"
  25. "github.com/gogits/gogs/modules/log"
  26. "github.com/gogits/gogs/modules/process"
  27. "github.com/gogits/gogs/modules/setting"
  28. )
  29. const (
  30. _TPL_UPDATE_HOOK = "#!/usr/bin/env %s\n%s update $1 $2 $3 --config='%s'\n"
  31. )
  32. var (
  33. ErrRepoFileNotExist = errors.New("Repository file does not exist")
  34. ErrRepoFileNotLoaded = errors.New("Repository file not loaded")
  35. ErrMirrorNotExist = errors.New("Mirror does not exist")
  36. ErrInvalidReference = errors.New("Invalid reference specified")
  37. ErrNameEmpty = errors.New("Name is empty")
  38. )
  39. var (
  40. Gitignores, Licenses []string
  41. )
  42. var (
  43. DescPattern = 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 := 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. 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. // Git requires setting user.name and user.email in order to commit changes.
  92. for configKey, defaultValue := range map[string]string{"user.name": "Gogs", "user.email": "gogs@fake.local"} {
  93. if stdout, stderr, err := process.Exec("NewRepoContext(get setting)", "git", "config", "--get", configKey); err != nil || strings.TrimSpace(stdout) == "" {
  94. // ExitError indicates this config is not set
  95. if _, ok := err.(*exec.ExitError); ok || strings.TrimSpace(stdout) == "" {
  96. if _, stderr, gerr := process.Exec("NewRepoContext(set "+configKey+")", "git", "config", "--global", configKey, defaultValue); gerr != nil {
  97. log.Fatal(4, "Fail to set git %s(%s): %s", configKey, gerr, stderr)
  98. }
  99. log.Info("Git config %s set to %s", configKey, defaultValue)
  100. } else {
  101. log.Fatal(4, "Fail to get git %s(%s): %s", configKey, err, stderr)
  102. }
  103. }
  104. }
  105. // Set git some configurations.
  106. if _, stderr, err := process.Exec("NewRepoContext(git config --global core.quotepath false)",
  107. "git", "config", "--global", "core.quotepath", "false"); err != nil {
  108. log.Fatal(4, "Fail to execute 'git config --global core.quotepath false': %s", stderr)
  109. }
  110. }
  111. // Repository represents a git repository.
  112. type Repository struct {
  113. ID int64 `xorm:"pk autoincr"`
  114. OwnerID int64 `xorm:"UNIQUE(s)"`
  115. Owner *User `xorm:"-"`
  116. LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
  117. Name string `xorm:"INDEX NOT NULL"`
  118. Description string
  119. Website string
  120. DefaultBranch string
  121. NumWatches int
  122. NumStars int
  123. NumForks int
  124. NumIssues int
  125. NumClosedIssues int
  126. NumOpenIssues int `xorm:"-"`
  127. NumPulls int
  128. NumClosedPulls int
  129. NumOpenPulls int `xorm:"-"`
  130. NumMilestones int `xorm:"NOT NULL DEFAULT 0"`
  131. NumClosedMilestones int `xorm:"NOT NULL DEFAULT 0"`
  132. NumOpenMilestones int `xorm:"-"`
  133. NumTags int `xorm:"-"`
  134. IsPrivate bool
  135. IsBare bool
  136. IsMirror bool
  137. *Mirror `xorm:"-"`
  138. IsFork bool `xorm:"NOT NULL DEFAULT false"`
  139. ForkID int64
  140. BaseRepo *Repository `xorm:"-"`
  141. Created time.Time `xorm:"CREATED"`
  142. Updated time.Time `xorm:"UPDATED"`
  143. }
  144. func (repo *Repository) getOwner(e Engine) (err error) {
  145. if repo.Owner == nil {
  146. repo.Owner, err = getUserByID(e, repo.OwnerID)
  147. }
  148. return err
  149. }
  150. func (repo *Repository) GetOwner() (err error) {
  151. return repo.getOwner(x)
  152. }
  153. // GetAssignees returns all users that have write access of repository.
  154. func (repo *Repository) GetAssignees() (_ []*User, err error) {
  155. if err = repo.GetOwner(); err != nil {
  156. return nil, err
  157. }
  158. accesses := make([]*Access, 0, 10)
  159. if err = x.Where("repo_id=? AND mode>=?", repo.ID, ACCESS_MODE_WRITE).Find(&accesses); err != nil {
  160. return nil, err
  161. }
  162. users := make([]*User, 0, len(accesses)+1) // Just waste 1 unit does not matter.
  163. if !repo.Owner.IsOrganization() {
  164. users = append(users, repo.Owner)
  165. }
  166. var u *User
  167. for i := range accesses {
  168. u, err = GetUserByID(accesses[i].UserID)
  169. if err != nil {
  170. return nil, err
  171. }
  172. users = append(users, u)
  173. }
  174. return users, nil
  175. }
  176. // GetAssigneeByID returns the user that has write access of repository by given ID.
  177. func (repo *Repository) GetAssigneeByID(userID int64) (*User, error) {
  178. return GetAssigneeByID(repo, userID)
  179. }
  180. // GetMilestoneByID returns the milestone belongs to repository by given ID.
  181. func (repo *Repository) GetMilestoneByID(milestoneID int64) (*Milestone, error) {
  182. return GetRepoMilestoneByID(repo.ID, milestoneID)
  183. }
  184. func (repo *Repository) GetMirror() (err error) {
  185. repo.Mirror, err = GetMirror(repo.ID)
  186. return err
  187. }
  188. func (repo *Repository) GetBaseRepo() (err error) {
  189. if !repo.IsFork {
  190. return nil
  191. }
  192. repo.BaseRepo, err = GetRepositoryByID(repo.ForkID)
  193. return err
  194. }
  195. func (repo *Repository) RepoPath() (string, error) {
  196. if err := repo.GetOwner(); err != nil {
  197. return "", err
  198. }
  199. return RepoPath(repo.Owner.Name, repo.Name), nil
  200. }
  201. func (repo *Repository) RepoLink() (string, error) {
  202. if err := repo.GetOwner(); err != nil {
  203. return "", err
  204. }
  205. return setting.AppSubUrl + "/" + repo.Owner.Name + "/" + repo.Name, nil
  206. }
  207. func (repo *Repository) HasAccess(u *User) bool {
  208. has, _ := HasAccess(u, repo, ACCESS_MODE_READ)
  209. return has
  210. }
  211. func (repo *Repository) IsOwnedBy(u *User) bool {
  212. return repo.OwnerID == u.Id
  213. }
  214. // DescriptionHtml does special handles to description and return HTML string.
  215. func (repo *Repository) DescriptionHtml() template.HTML {
  216. sanitize := func(s string) string {
  217. return fmt.Sprintf(`<a href="%[1]s" target="_blank">%[1]s</a>`, s)
  218. }
  219. return template.HTML(DescPattern.ReplaceAllStringFunc(base.Sanitizer.Sanitize(repo.Description), sanitize))
  220. }
  221. func isRepositoryExist(e Engine, u *User, repoName string) (bool, error) {
  222. has, err := e.Get(&Repository{
  223. OwnerID: u.Id,
  224. LowerName: strings.ToLower(repoName),
  225. })
  226. return has && com.IsDir(RepoPath(u.Name, repoName)), err
  227. }
  228. // IsRepositoryExist returns true if the repository with given name under user has already existed.
  229. func IsRepositoryExist(u *User, repoName string) (bool, error) {
  230. return isRepositoryExist(x, u, repoName)
  231. }
  232. // CloneLink represents different types of clone URLs of repository.
  233. type CloneLink struct {
  234. SSH string
  235. HTTPS string
  236. Git string
  237. }
  238. // CloneLink returns clone URLs of repository.
  239. func (repo *Repository) CloneLink() (cl CloneLink, err error) {
  240. if err = repo.GetOwner(); err != nil {
  241. return cl, err
  242. }
  243. if setting.SSHPort != 22 {
  244. cl.SSH = fmt.Sprintf("ssh://%s@%s:%d/%s/%s.git", setting.RunUser, setting.SSHDomain, setting.SSHPort, repo.Owner.LowerName, repo.LowerName)
  245. } else {
  246. cl.SSH = fmt.Sprintf("%s@%s:%s/%s.git", setting.RunUser, setting.SSHDomain, repo.Owner.LowerName, repo.LowerName)
  247. }
  248. cl.HTTPS = fmt.Sprintf("%s%s/%s.git", setting.AppUrl, repo.Owner.LowerName, repo.LowerName)
  249. return cl, nil
  250. }
  251. var (
  252. reservedNames = []string{"debug", "raw", "install", "api", "avatar", "user", "org", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin", "new"}
  253. reservedPatterns = []string{"*.git", "*.keys"}
  254. )
  255. // IsUsableName checks if name is reserved or pattern of name is not allowed.
  256. func IsUsableName(name string) error {
  257. name = strings.TrimSpace(strings.ToLower(name))
  258. if utf8.RuneCountInString(name) == 0 {
  259. return ErrNameEmpty
  260. }
  261. for i := range reservedNames {
  262. if name == reservedNames[i] {
  263. return ErrNameReserved{name}
  264. }
  265. }
  266. for _, pat := range reservedPatterns {
  267. if pat[0] == '*' && strings.HasSuffix(name, pat[1:]) ||
  268. (pat[len(pat)-1] == '*' && strings.HasPrefix(name, pat[:len(pat)-1])) {
  269. return ErrNamePatternNotAllowed{pat}
  270. }
  271. }
  272. return nil
  273. }
  274. // Mirror represents a mirror information of repository.
  275. type Mirror struct {
  276. ID int64 `xorm:"pk autoincr"`
  277. RepoID int64
  278. RepoName string // <user name>/<repo name>
  279. Interval int // Hour.
  280. Updated time.Time `xorm:"UPDATED"`
  281. NextUpdate time.Time
  282. }
  283. func getMirror(e Engine, repoId int64) (*Mirror, error) {
  284. m := &Mirror{RepoID: repoId}
  285. has, err := e.Get(m)
  286. if err != nil {
  287. return nil, err
  288. } else if !has {
  289. return nil, ErrMirrorNotExist
  290. }
  291. return m, nil
  292. }
  293. // GetMirror returns mirror object by given repository ID.
  294. func GetMirror(repoId int64) (*Mirror, error) {
  295. return getMirror(x, repoId)
  296. }
  297. func updateMirror(e Engine, m *Mirror) error {
  298. _, err := e.Id(m.ID).Update(m)
  299. return err
  300. }
  301. func UpdateMirror(m *Mirror) error {
  302. return updateMirror(x, m)
  303. }
  304. // MirrorRepository creates a mirror repository from source.
  305. func MirrorRepository(repoId int64, userName, repoName, repoPath, url string) error {
  306. _, stderr, err := process.ExecTimeout(10*time.Minute,
  307. fmt.Sprintf("MirrorRepository: %s/%s", userName, repoName),
  308. "git", "clone", "--mirror", url, repoPath)
  309. if err != nil {
  310. return errors.New("git clone --mirror: " + stderr)
  311. }
  312. if _, err = x.InsertOne(&Mirror{
  313. RepoID: repoId,
  314. RepoName: strings.ToLower(userName + "/" + repoName),
  315. Interval: 24,
  316. NextUpdate: time.Now().Add(24 * time.Hour),
  317. }); err != nil {
  318. return err
  319. }
  320. return nil
  321. }
  322. // MigrateRepository migrates a existing repository from other project hosting.
  323. func MigrateRepository(u *User, name, desc string, private, mirror bool, url string) (*Repository, error) {
  324. repo, err := CreateRepository(u, name, desc, "", "", private, mirror, false)
  325. if err != nil {
  326. return nil, err
  327. }
  328. // Clone to temprory path and do the init commit.
  329. tmpDir := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()))
  330. os.MkdirAll(tmpDir, os.ModePerm)
  331. repoPath := RepoPath(u.Name, name)
  332. if u.IsOrganization() {
  333. t, err := u.GetOwnerTeam()
  334. if err != nil {
  335. return nil, err
  336. }
  337. repo.NumWatches = t.NumMembers
  338. } else {
  339. repo.NumWatches = 1
  340. }
  341. repo.IsBare = false
  342. if mirror {
  343. if err = MirrorRepository(repo.ID, u.Name, repo.Name, repoPath, url); err != nil {
  344. return repo, err
  345. }
  346. repo.IsMirror = true
  347. return repo, UpdateRepository(repo, false)
  348. } else {
  349. os.RemoveAll(repoPath)
  350. }
  351. // FIXME: this command could for both migrate and mirror
  352. _, stderr, err := process.ExecTimeout(10*time.Minute,
  353. fmt.Sprintf("MigrateRepository: %s", repoPath),
  354. "git", "clone", "--mirror", "--bare", "--quiet", url, repoPath)
  355. if err != nil {
  356. return repo, fmt.Errorf("git clone --mirror --bare --quiet: %v", stderr)
  357. } else if err = createUpdateHook(repoPath); err != nil {
  358. return repo, fmt.Errorf("create update hook: %v", err)
  359. }
  360. // Check if repository has master branch, if so set it to default branch.
  361. gitRepo, err := git.OpenRepository(repoPath)
  362. if err != nil {
  363. return repo, fmt.Errorf("open git repository: %v", err)
  364. }
  365. if gitRepo.IsBranchExist("master") {
  366. repo.DefaultBranch = "master"
  367. }
  368. return repo, UpdateRepository(repo, false)
  369. }
  370. // initRepoCommit temporarily changes with work directory.
  371. func initRepoCommit(tmpPath string, sig *git.Signature) (err error) {
  372. var stderr string
  373. if _, stderr, err = process.ExecDir(-1,
  374. tmpPath, fmt.Sprintf("initRepoCommit(git add): %s", tmpPath),
  375. "git", "add", "--all"); err != nil {
  376. return errors.New("git add: " + stderr)
  377. }
  378. if _, stderr, err = process.ExecDir(-1,
  379. tmpPath, fmt.Sprintf("initRepoCommit(git commit): %s", tmpPath),
  380. "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email),
  381. "-m", "Init commit"); err != nil {
  382. return errors.New("git commit: " + stderr)
  383. }
  384. if _, stderr, err = process.ExecDir(-1,
  385. tmpPath, fmt.Sprintf("initRepoCommit(git push): %s", tmpPath),
  386. "git", "push", "origin", "master"); err != nil {
  387. return errors.New("git push: " + stderr)
  388. }
  389. return nil
  390. }
  391. func createUpdateHook(repoPath string) error {
  392. return ioutil.WriteFile(path.Join(repoPath, "hooks/update"),
  393. []byte(fmt.Sprintf(_TPL_UPDATE_HOOK, setting.ScriptType, "\""+appPath+"\"", setting.CustomConf)), 0777)
  394. }
  395. // InitRepository initializes README and .gitignore if needed.
  396. func initRepository(e Engine, repoPath string, u *User, repo *Repository, initReadme bool, repoLang, license string) error {
  397. // Somehow the directory could exist.
  398. if com.IsExist(repoPath) {
  399. return fmt.Errorf("initRepository: path already exists: %s", repoPath)
  400. }
  401. // Init bare new repository.
  402. os.MkdirAll(repoPath, os.ModePerm)
  403. _, stderr, err := process.ExecDir(-1, repoPath,
  404. fmt.Sprintf("initRepository(git init --bare): %s", repoPath),
  405. "git", "init", "--bare")
  406. if err != nil {
  407. return fmt.Errorf("git init --bare: %s", err)
  408. }
  409. if err := createUpdateHook(repoPath); err != nil {
  410. return err
  411. }
  412. // Initialize repository according to user's choice.
  413. fileName := map[string]string{}
  414. if initReadme {
  415. fileName["readme"] = "README.md"
  416. }
  417. if repoLang != "" {
  418. fileName["gitign"] = ".gitignore"
  419. }
  420. if license != "" {
  421. fileName["license"] = "LICENSE"
  422. }
  423. // Clone to temprory path and do the init commit.
  424. tmpDir := filepath.Join(os.TempDir(), com.ToStr(time.Now().Nanosecond()))
  425. os.MkdirAll(tmpDir, os.ModePerm)
  426. defer os.RemoveAll(tmpDir)
  427. _, stderr, err = process.Exec(
  428. fmt.Sprintf("initRepository(git clone): %s", repoPath),
  429. "git", "clone", repoPath, tmpDir)
  430. if err != nil {
  431. return errors.New("git clone: " + stderr)
  432. }
  433. // README
  434. if initReadme {
  435. defaultReadme := repo.Name + "\n" + strings.Repeat("=",
  436. utf8.RuneCountInString(repo.Name)) + "\n\n" + repo.Description
  437. if err := ioutil.WriteFile(filepath.Join(tmpDir, fileName["readme"]),
  438. []byte(defaultReadme), 0644); err != nil {
  439. return err
  440. }
  441. }
  442. // FIXME: following two can be merged.
  443. // .gitignore
  444. // Copy custom file when available.
  445. customPath := path.Join(setting.CustomPath, "conf/gitignore", repoLang)
  446. targetPath := path.Join(tmpDir, fileName["gitign"])
  447. if com.IsFile(customPath) {
  448. if err := com.Copy(customPath, targetPath); err != nil {
  449. return fmt.Errorf("copy gitignore: %v", err)
  450. }
  451. } else if com.IsSliceContainsStr(Gitignores, repoLang) {
  452. if err = ioutil.WriteFile(targetPath,
  453. bindata.MustAsset(path.Join("conf/gitignore", repoLang)), 0644); err != nil {
  454. return fmt.Errorf("generate gitignore: %v", err)
  455. }
  456. } else {
  457. delete(fileName, "gitign")
  458. }
  459. // LICENSE
  460. customPath = path.Join(setting.CustomPath, "conf/license", license)
  461. targetPath = path.Join(tmpDir, fileName["license"])
  462. if com.IsFile(customPath) {
  463. if err = com.Copy(customPath, targetPath); err != nil {
  464. return fmt.Errorf("copy license: %v", err)
  465. }
  466. } else if com.IsSliceContainsStr(Licenses, license) {
  467. if err = ioutil.WriteFile(targetPath,
  468. bindata.MustAsset(path.Join("conf/license", license)), 0644); err != nil {
  469. return fmt.Errorf("generate license: %v", err)
  470. }
  471. } else {
  472. delete(fileName, "license")
  473. }
  474. if len(fileName) == 0 {
  475. // Re-fetch the repository from database before updating it (else it would
  476. // override changes that were done earlier with sql)
  477. if repo, err = getRepositoryByID(e, repo.ID); err != nil {
  478. return err
  479. }
  480. repo.IsBare = true
  481. repo.DefaultBranch = "master"
  482. return updateRepository(e, repo, false)
  483. }
  484. // Apply changes and commit.
  485. return initRepoCommit(tmpDir, u.NewGitSig())
  486. }
  487. func createRepository(e *xorm.Session, u *User, repo *Repository) (err error) {
  488. if err = IsUsableName(repo.Name); err != nil {
  489. return err
  490. }
  491. has, err := isRepositoryExist(e, u, repo.Name)
  492. if err != nil {
  493. return fmt.Errorf("IsRepositoryExist: %v", err)
  494. } else if has {
  495. return ErrRepoAlreadyExist{u.Name, repo.Name}
  496. }
  497. if _, err = e.Insert(repo); err != nil {
  498. return err
  499. } else if _, err = e.Exec("UPDATE `user` SET num_repos=num_repos+1 WHERE id=?", u.Id); err != nil {
  500. return err
  501. }
  502. // Give access to all members in owner team.
  503. if u.IsOrganization() {
  504. t, err := u.getOwnerTeam(e)
  505. if err != nil {
  506. return fmt.Errorf("getOwnerTeam: %v", err)
  507. } else if err = t.addRepository(e, repo); err != nil {
  508. return fmt.Errorf("addRepository: %v", err)
  509. }
  510. } else {
  511. // Organization automatically called this in addRepository method.
  512. if err = repo.recalculateAccesses(e); err != nil {
  513. return fmt.Errorf("recalculateAccesses: %v", err)
  514. }
  515. }
  516. if err = watchRepo(e, u.Id, repo.ID, true); err != nil {
  517. return fmt.Errorf("watchRepo: %v", err)
  518. } else if err = newRepoAction(e, u, repo); err != nil {
  519. return fmt.Errorf("newRepoAction: %v", err)
  520. }
  521. return nil
  522. }
  523. // CreateRepository creates a repository for given user or organization.
  524. func CreateRepository(u *User, name, desc, lang, license string, isPrivate, isMirror, initReadme bool) (_ *Repository, err error) {
  525. repo := &Repository{
  526. OwnerID: u.Id,
  527. Owner: u,
  528. Name: name,
  529. LowerName: strings.ToLower(name),
  530. Description: desc,
  531. IsPrivate: isPrivate,
  532. }
  533. sess := x.NewSession()
  534. defer sessionRelease(sess)
  535. if err = sess.Begin(); err != nil {
  536. return nil, err
  537. }
  538. if err = createRepository(sess, u, repo); err != nil {
  539. return nil, err
  540. }
  541. // No need for init mirror.
  542. if !isMirror {
  543. repoPath := RepoPath(u.Name, repo.Name)
  544. if err = initRepository(sess, repoPath, u, repo, initReadme, lang, license); err != nil {
  545. if err2 := os.RemoveAll(repoPath); err2 != nil {
  546. log.Error(4, "initRepository: %v", err)
  547. return nil, fmt.Errorf(
  548. "delete repo directory %s/%s failed(2): %v", u.Name, repo.Name, err2)
  549. }
  550. return nil, fmt.Errorf("initRepository: %v", err)
  551. }
  552. _, stderr, err := process.ExecDir(-1,
  553. repoPath, fmt.Sprintf("CreateRepository(git update-server-info): %s", repoPath),
  554. "git", "update-server-info")
  555. if err != nil {
  556. return nil, errors.New("CreateRepository(git update-server-info): " + stderr)
  557. }
  558. }
  559. return repo, sess.Commit()
  560. }
  561. // CountRepositories returns number of repositories.
  562. func CountRepositories() int64 {
  563. count, _ := x.Count(new(Repository))
  564. return count
  565. }
  566. // GetRepositoriesWithUsers returns given number of repository objects with offset.
  567. // It also auto-gets corresponding users.
  568. func GetRepositoriesWithUsers(num, offset int) ([]*Repository, error) {
  569. repos := make([]*Repository, 0, num)
  570. if err := x.Limit(num, offset).Asc("id").Find(&repos); err != nil {
  571. return nil, err
  572. }
  573. for _, repo := range repos {
  574. repo.Owner = &User{Id: repo.OwnerID}
  575. has, err := x.Get(repo.Owner)
  576. if err != nil {
  577. return nil, err
  578. } else if !has {
  579. return nil, ErrUserNotExist{repo.OwnerID, ""}
  580. }
  581. }
  582. return repos, nil
  583. }
  584. // RepoPath returns repository path by given user and repository name.
  585. func RepoPath(userName, repoName string) string {
  586. return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".git")
  587. }
  588. // TransferOwnership transfers all corresponding setting from old user to new one.
  589. func TransferOwnership(u *User, newOwnerName string, repo *Repository) error {
  590. newOwner, err := GetUserByName(newOwnerName)
  591. if err != nil {
  592. return fmt.Errorf("get new owner '%s': %v", newOwnerName, err)
  593. }
  594. // Check if new owner has repository with same name.
  595. has, err := IsRepositoryExist(newOwner, repo.Name)
  596. if err != nil {
  597. return fmt.Errorf("IsRepositoryExist: %v", err)
  598. } else if has {
  599. return ErrRepoAlreadyExist{newOwnerName, repo.Name}
  600. }
  601. sess := x.NewSession()
  602. defer sessionRelease(sess)
  603. if err = sess.Begin(); err != nil {
  604. return fmt.Errorf("sess.Begin: %v", err)
  605. }
  606. owner := repo.Owner
  607. // Note: we have to set value here to make sure recalculate accesses is based on
  608. // new owner.
  609. repo.OwnerID = newOwner.Id
  610. repo.Owner = newOwner
  611. // Update repository.
  612. if _, err := sess.Id(repo.ID).Update(repo); err != nil {
  613. return fmt.Errorf("update owner: %v", err)
  614. }
  615. // Remove redundant collaborators.
  616. collaborators, err := repo.GetCollaborators()
  617. if err != nil {
  618. return fmt.Errorf("GetCollaborators: %v", err)
  619. }
  620. // Dummy object.
  621. collaboration := &Collaboration{RepoID: repo.ID}
  622. for _, c := range collaborators {
  623. collaboration.UserID = c.Id
  624. if c.Id == newOwner.Id || newOwner.IsOrgMember(c.Id) {
  625. if _, err = sess.Delete(collaboration); err != nil {
  626. return fmt.Errorf("remove collaborator '%d': %v", c.Id, err)
  627. }
  628. }
  629. }
  630. // Remove old team-repository relations.
  631. if owner.IsOrganization() {
  632. if err = owner.getTeams(sess); err != nil {
  633. return fmt.Errorf("getTeams: %v", err)
  634. }
  635. for _, t := range owner.Teams {
  636. if !t.hasRepository(sess, repo.ID) {
  637. continue
  638. }
  639. t.NumRepos--
  640. if _, err := sess.Id(t.ID).AllCols().Update(t); err != nil {
  641. return fmt.Errorf("decrease team repository count '%d': %v", t.ID, err)
  642. }
  643. }
  644. if err = owner.removeOrgRepo(sess, repo.ID); err != nil {
  645. return fmt.Errorf("removeOrgRepo: %v", err)
  646. }
  647. }
  648. if newOwner.IsOrganization() {
  649. t, err := newOwner.GetOwnerTeam()
  650. if err != nil {
  651. return fmt.Errorf("GetOwnerTeam: %v", err)
  652. } else if err = t.addRepository(sess, repo); err != nil {
  653. return fmt.Errorf("add to owner team: %v", err)
  654. }
  655. } else {
  656. // Organization called this in addRepository method.
  657. if err = repo.recalculateAccesses(sess); err != nil {
  658. return fmt.Errorf("recalculateAccesses: %v", err)
  659. }
  660. }
  661. // Update repository count.
  662. if _, err = sess.Exec("UPDATE `user` SET num_repos=num_repos+1 WHERE id=?", newOwner.Id); err != nil {
  663. return fmt.Errorf("increase new owner repository count: %v", err)
  664. } else if _, err = sess.Exec("UPDATE `user` SET num_repos=num_repos-1 WHERE id=?", owner.Id); err != nil {
  665. return fmt.Errorf("decrease old owner repository count: %v", err)
  666. }
  667. if err = watchRepo(sess, newOwner.Id, repo.ID, true); err != nil {
  668. return fmt.Errorf("watchRepo: %v", err)
  669. } else if err = transferRepoAction(sess, u, owner, newOwner, repo); err != nil {
  670. return fmt.Errorf("transferRepoAction: %v", err)
  671. }
  672. // Update mirror information.
  673. if repo.IsMirror {
  674. mirror, err := getMirror(sess, repo.ID)
  675. if err != nil {
  676. return fmt.Errorf("getMirror: %v", err)
  677. }
  678. mirror.RepoName = newOwner.LowerName + "/" + repo.LowerName
  679. if err = updateMirror(sess, mirror); err != nil {
  680. return fmt.Errorf("updateMirror: %v", err)
  681. }
  682. }
  683. // Change repository directory name.
  684. if err = os.Rename(RepoPath(owner.Name, repo.Name), RepoPath(newOwner.Name, repo.Name)); err != nil {
  685. return fmt.Errorf("rename directory: %v", err)
  686. }
  687. return sess.Commit()
  688. }
  689. // ChangeRepositoryName changes all corresponding setting from old repository name to new one.
  690. func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error) {
  691. oldRepoName = strings.ToLower(oldRepoName)
  692. newRepoName = strings.ToLower(newRepoName)
  693. if err = IsUsableName(newRepoName); err != nil {
  694. return err
  695. }
  696. has, err := IsRepositoryExist(u, newRepoName)
  697. if err != nil {
  698. return fmt.Errorf("IsRepositoryExist: %v", err)
  699. } else if has {
  700. return ErrRepoAlreadyExist{u.Name, newRepoName}
  701. }
  702. // Change repository directory name.
  703. return os.Rename(RepoPath(u.LowerName, oldRepoName), RepoPath(u.LowerName, newRepoName))
  704. }
  705. func updateRepository(e Engine, repo *Repository, visibilityChanged bool) (err error) {
  706. repo.LowerName = strings.ToLower(repo.Name)
  707. if len(repo.Description) > 255 {
  708. repo.Description = repo.Description[:255]
  709. }
  710. if len(repo.Website) > 255 {
  711. repo.Website = repo.Website[:255]
  712. }
  713. if _, err = e.Id(repo.ID).AllCols().Update(repo); err != nil {
  714. return fmt.Errorf("update: %v", err)
  715. }
  716. if visibilityChanged {
  717. if err = repo.getOwner(e); err != nil {
  718. return fmt.Errorf("getOwner: %v", err)
  719. }
  720. if !repo.Owner.IsOrganization() {
  721. return nil
  722. }
  723. // Organization repository need to recalculate access table when visivility is changed.
  724. if err = repo.recalculateTeamAccesses(e, 0); err != nil {
  725. return fmt.Errorf("recalculateTeamAccesses: %v", err)
  726. }
  727. }
  728. return nil
  729. }
  730. func UpdateRepository(repo *Repository, visibilityChanged bool) (err error) {
  731. sess := x.NewSession()
  732. defer sessionRelease(sess)
  733. if err = sess.Begin(); err != nil {
  734. return err
  735. }
  736. if err = updateRepository(x, repo, visibilityChanged); err != nil {
  737. return fmt.Errorf("updateRepository: %v", err)
  738. }
  739. return sess.Commit()
  740. }
  741. // DeleteRepository deletes a repository for a user or organization.
  742. func DeleteRepository(uid, repoID int64, userName string) error {
  743. repo := &Repository{ID: repoID, OwnerID: uid}
  744. has, err := x.Get(repo)
  745. if err != nil {
  746. return err
  747. } else if !has {
  748. return ErrRepoNotExist{repoID, uid, ""}
  749. }
  750. // In case is a organization.
  751. org, err := GetUserByID(uid)
  752. if err != nil {
  753. return err
  754. }
  755. if org.IsOrganization() {
  756. if err = org.GetTeams(); err != nil {
  757. return err
  758. }
  759. }
  760. sess := x.NewSession()
  761. defer sessionRelease(sess)
  762. if err = sess.Begin(); err != nil {
  763. return err
  764. }
  765. if org.IsOrganization() {
  766. for _, t := range org.Teams {
  767. if !t.hasRepository(sess, repoID) {
  768. continue
  769. } else if err = t.removeRepository(sess, repo, false); err != nil {
  770. return err
  771. }
  772. }
  773. }
  774. if _, err = sess.Delete(&Repository{ID: repoID}); err != nil {
  775. return err
  776. } else if _, err = sess.Delete(&Access{RepoID: repo.ID}); err != nil {
  777. return err
  778. } else if _, err = sess.Delete(&Action{RepoID: repo.ID}); err != nil {
  779. return err
  780. } else if _, err = sess.Delete(&Watch{RepoID: repoID}); err != nil {
  781. return err
  782. } else if _, err = sess.Delete(&Mirror{RepoID: repoID}); err != nil {
  783. return err
  784. } else if _, err = sess.Delete(&IssueUser{RepoID: repoID}); err != nil {
  785. return err
  786. } else if _, err = sess.Delete(&Milestone{RepoID: repoID}); err != nil {
  787. return err
  788. } else if _, err = sess.Delete(&Release{RepoId: repoID}); err != nil {
  789. return err
  790. } else if _, err = sess.Delete(&Collaboration{RepoID: repoID}); err != nil {
  791. return err
  792. }
  793. // Delete comments.
  794. issues := make([]*Issue, 0, 25)
  795. if err = sess.Where("repo_id=?", repoID).Find(&issues); err != nil {
  796. return err
  797. }
  798. for i := range issues {
  799. if _, err = sess.Delete(&Comment{IssueID: issues[i].ID}); err != nil {
  800. return err
  801. }
  802. }
  803. if _, err = sess.Delete(&Issue{RepoID: repoID}); err != nil {
  804. return err
  805. }
  806. if repo.IsFork {
  807. if _, err = sess.Exec("UPDATE `repository` SET num_forks=num_forks-1 WHERE id=?", repo.ForkID); err != nil {
  808. return err
  809. }
  810. }
  811. if _, err = sess.Exec("UPDATE `user` SET num_repos=num_repos-1 WHERE id=?", uid); err != nil {
  812. return err
  813. }
  814. // Remove repository files.
  815. if err = os.RemoveAll(RepoPath(userName, repo.Name)); err != nil {
  816. desc := fmt.Sprintf("delete repository files(%s/%s): %v", userName, repo.Name, err)
  817. log.Warn(desc)
  818. if err = CreateRepositoryNotice(desc); err != nil {
  819. log.Error(4, "add notice: %v", err)
  820. }
  821. }
  822. return sess.Commit()
  823. }
  824. // GetRepositoryByRef returns a Repository specified by a GFM reference.
  825. // See https://help.github.com/articles/writing-on-github#references for more information on the syntax.
  826. func GetRepositoryByRef(ref string) (*Repository, error) {
  827. n := strings.IndexByte(ref, byte('/'))
  828. if n < 2 {
  829. return nil, ErrInvalidReference
  830. }
  831. userName, repoName := ref[:n], ref[n+1:]
  832. user, err := GetUserByName(userName)
  833. if err != nil {
  834. return nil, err
  835. }
  836. return GetRepositoryByName(user.Id, repoName)
  837. }
  838. // GetRepositoryByName returns the repository by given name under user if exists.
  839. func GetRepositoryByName(uid int64, repoName string) (*Repository, error) {
  840. repo := &Repository{
  841. OwnerID: uid,
  842. LowerName: strings.ToLower(repoName),
  843. }
  844. has, err := x.Get(repo)
  845. if err != nil {
  846. return nil, err
  847. } else if !has {
  848. return nil, ErrRepoNotExist{0, uid, repoName}
  849. }
  850. return repo, err
  851. }
  852. func getRepositoryByID(e Engine, id int64) (*Repository, error) {
  853. repo := new(Repository)
  854. has, err := e.Id(id).Get(repo)
  855. if err != nil {
  856. return nil, err
  857. } else if !has {
  858. return nil, ErrRepoNotExist{id, 0, ""}
  859. }
  860. return repo, nil
  861. }
  862. // GetRepositoryByID returns the repository by given id if exists.
  863. func GetRepositoryByID(id int64) (*Repository, error) {
  864. return getRepositoryByID(x, id)
  865. }
  866. // GetRepositories returns a list of repositories of given user.
  867. func GetRepositories(uid int64, private bool) ([]*Repository, error) {
  868. repos := make([]*Repository, 0, 10)
  869. sess := x.Desc("updated")
  870. if !private {
  871. sess.Where("is_private=?", false)
  872. }
  873. return repos, sess.Find(&repos, &Repository{OwnerID: uid})
  874. }
  875. // GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
  876. func GetRecentUpdatedRepositories(num int) (repos []*Repository, err error) {
  877. err = x.Where("is_private=?", false).Limit(num).Desc("updated").Find(&repos)
  878. return repos, err
  879. }
  880. // GetRepositoryCount returns the total number of repositories of user.
  881. func GetRepositoryCount(u *User) (int64, error) {
  882. return x.Count(&Repository{OwnerID: u.Id})
  883. }
  884. type SearchOption struct {
  885. Keyword string
  886. Uid int64
  887. Limit int
  888. Private bool
  889. }
  890. // SearchRepositoryByName returns given number of repositories whose name contains keyword.
  891. func SearchRepositoryByName(opt SearchOption) (repos []*Repository, err error) {
  892. if len(opt.Keyword) == 0 {
  893. return repos, nil
  894. }
  895. opt.Keyword = strings.ToLower(opt.Keyword)
  896. repos = make([]*Repository, 0, opt.Limit)
  897. // Append conditions.
  898. sess := x.Limit(opt.Limit)
  899. if opt.Uid > 0 {
  900. sess.Where("owner_id=?", opt.Uid)
  901. }
  902. if !opt.Private {
  903. sess.And("is_private=false")
  904. }
  905. sess.And("lower_name like ?", "%"+opt.Keyword+"%").Find(&repos)
  906. return repos, err
  907. }
  908. // DeleteRepositoryArchives deletes all repositories' archives.
  909. func DeleteRepositoryArchives() error {
  910. return x.Where("id > 0").Iterate(new(Repository),
  911. func(idx int, bean interface{}) error {
  912. repo := bean.(*Repository)
  913. if err := repo.GetOwner(); err != nil {
  914. return err
  915. }
  916. return os.RemoveAll(filepath.Join(RepoPath(repo.Owner.Name, repo.Name), "archives"))
  917. })
  918. }
  919. // RewriteRepositoryUpdateHook rewrites all repositories' update hook.
  920. func RewriteRepositoryUpdateHook() error {
  921. return x.Where("id > 0").Iterate(new(Repository),
  922. func(idx int, bean interface{}) error {
  923. repo := bean.(*Repository)
  924. if err := repo.GetOwner(); err != nil {
  925. return err
  926. }
  927. return createUpdateHook(RepoPath(repo.Owner.Name, repo.Name))
  928. })
  929. }
  930. var (
  931. // Prevent duplicate tasks.
  932. isMirrorUpdating = false
  933. isGitFscking = false
  934. isCheckingRepos = false
  935. )
  936. // MirrorUpdate checks and updates mirror repositories.
  937. func MirrorUpdate() {
  938. if isMirrorUpdating {
  939. return
  940. }
  941. isMirrorUpdating = true
  942. defer func() { isMirrorUpdating = false }()
  943. mirrors := make([]*Mirror, 0, 10)
  944. if err := x.Iterate(new(Mirror), func(idx int, bean interface{}) error {
  945. m := bean.(*Mirror)
  946. if m.NextUpdate.After(time.Now()) {
  947. return nil
  948. }
  949. repoPath := filepath.Join(setting.RepoRootPath, m.RepoName+".git")
  950. if _, stderr, err := process.ExecDir(10*time.Minute,
  951. repoPath, fmt.Sprintf("MirrorUpdate: %s", repoPath),
  952. "git", "remote", "update", "--prune"); err != nil {
  953. desc := fmt.Sprintf("Fail to update mirror repository(%s): %s", repoPath, stderr)
  954. log.Error(4, desc)
  955. if err = CreateRepositoryNotice(desc); err != nil {
  956. log.Error(4, "Fail to add notice: %v", err)
  957. }
  958. return nil
  959. }
  960. m.NextUpdate = time.Now().Add(time.Duration(m.Interval) * time.Hour)
  961. mirrors = append(mirrors, m)
  962. return nil
  963. }); err != nil {
  964. log.Error(4, "MirrorUpdate: %v", err)
  965. }
  966. for i := range mirrors {
  967. if err := UpdateMirror(mirrors[i]); err != nil {
  968. log.Error(4, "UpdateMirror", fmt.Sprintf("%s: %v", mirrors[i].RepoName, err))
  969. }
  970. }
  971. }
  972. // GitFsck calls 'git fsck' to check repository health.
  973. func GitFsck() {
  974. if isGitFscking {
  975. return
  976. }
  977. isGitFscking = true
  978. defer func() { isGitFscking = false }()
  979. args := append([]string{"fsck"}, setting.Git.Fsck.Args...)
  980. if err := x.Where("id > 0").Iterate(new(Repository),
  981. func(idx int, bean interface{}) error {
  982. repo := bean.(*Repository)
  983. if err := repo.GetOwner(); err != nil {
  984. return err
  985. }
  986. repoPath := RepoPath(repo.Owner.Name, repo.Name)
  987. _, _, err := process.ExecDir(-1, repoPath, "Repository health check", "git", args...)
  988. if err != nil {
  989. desc := fmt.Sprintf("Fail to health check repository(%s)", repoPath)
  990. log.Warn(desc)
  991. if err = CreateRepositoryNotice(desc); err != nil {
  992. log.Error(4, "Fail to add notice: %v", err)
  993. }
  994. }
  995. return nil
  996. }); err != nil {
  997. log.Error(4, "repo.Fsck: %v", err)
  998. }
  999. }
  1000. func GitGcRepos() error {
  1001. args := append([]string{"gc"}, setting.Git.GcArgs...)
  1002. return x.Where("id > 0").Iterate(new(Repository),
  1003. func(idx int, bean interface{}) error {
  1004. repo := bean.(*Repository)
  1005. if err := repo.GetOwner(); err != nil {
  1006. return err
  1007. }
  1008. _, stderr, err := process.ExecDir(-1, RepoPath(repo.Owner.Name, repo.Name), "Repository garbage collection", "git", args...)
  1009. if err != nil {
  1010. return fmt.Errorf("%v: %v", err, stderr)
  1011. }
  1012. return nil
  1013. })
  1014. }
  1015. func CheckRepoStats() {
  1016. if isCheckingRepos {
  1017. return
  1018. }
  1019. isCheckingRepos = true
  1020. defer func() { isCheckingRepos = false }()
  1021. // Check count watchers
  1022. results_watch, err := x.Query("SELECT r.id FROM `repository` r WHERE r.num_watches!=(SELECT count(*) FROM `watch` WHERE repo_id=r.id)")
  1023. if err != nil {
  1024. log.Error(4, "select repository check 'watch': %v", err)
  1025. }
  1026. for _, repo_id := range results_watch {
  1027. log.Info("updating repository count 'watch'")
  1028. repoID := com.StrTo(repo_id["id"]).MustInt64()
  1029. _, err := x.Exec("UPDATE `repository` SET num_watches=(SELECT count(*) FROM `watch` WHERE repo_id=?) WHERE id=?", repoID, repoID)
  1030. if err != nil {
  1031. log.Error(4, "update repository check 'watch', repo %v: %v", repo_id, err)
  1032. }
  1033. }
  1034. // Check count stars
  1035. results_star, err := x.Query("SELECT s.id FROM `repository` s WHERE s.num_stars!=(SELECT count(*) FROM `star` WHERE repo_id=s.id)")
  1036. if err != nil {
  1037. log.Error(4, "select repository check 'star': %v", err)
  1038. }
  1039. for _, repo_id := range results_star {
  1040. log.Info("updating repository count 'star'")
  1041. repoID := com.StrTo(repo_id["id"]).MustInt64()
  1042. _, err := x.Exec("UPDATE `repository` SET .num_stars=(SELECT count(*) FROM `star` WHERE repo_id=?) WHERE id=?", repoID, repoID)
  1043. if err != nil {
  1044. log.Error(4, "update repository check 'star', repo %v: %v", repo_id, err)
  1045. }
  1046. }
  1047. }
  1048. // _________ .__ .__ ___. __ .__
  1049. // \_ ___ \ ____ | | | | _____ \_ |__ ________________ _/ |_|__| ____ ____
  1050. // / \ \/ / _ \| | | | \__ \ | __ \ / _ \_ __ \__ \\ __\ |/ _ \ / \
  1051. // \ \___( <_> ) |_| |__/ __ \| \_\ ( <_> ) | \// __ \| | | ( <_> ) | \
  1052. // \______ /\____/|____/____(____ /___ /\____/|__| (____ /__| |__|\____/|___| /
  1053. // \/ \/ \/ \/ \/
  1054. // A Collaboration is a relation between an individual and a repository
  1055. type Collaboration struct {
  1056. ID int64 `xorm:"pk autoincr"`
  1057. RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  1058. UserID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  1059. Created time.Time `xorm:"CREATED"`
  1060. }
  1061. // Add collaborator and accompanying access
  1062. func (repo *Repository) AddCollaborator(u *User) error {
  1063. collaboration := &Collaboration{
  1064. RepoID: repo.ID,
  1065. UserID: u.Id,
  1066. }
  1067. has, err := x.Get(collaboration)
  1068. if err != nil {
  1069. return err
  1070. } else if has {
  1071. return nil
  1072. }
  1073. if err = repo.GetOwner(); err != nil {
  1074. return fmt.Errorf("GetOwner: %v", err)
  1075. }
  1076. sess := x.NewSession()
  1077. defer sessionRelease(sess)
  1078. if err = sess.Begin(); err != nil {
  1079. return err
  1080. }
  1081. if _, err = sess.InsertOne(collaboration); err != nil {
  1082. return err
  1083. }
  1084. if repo.Owner.IsOrganization() {
  1085. err = repo.recalculateTeamAccesses(sess, 0)
  1086. } else {
  1087. err = repo.recalculateAccesses(sess)
  1088. }
  1089. if err != nil {
  1090. return fmt.Errorf("recalculateAccesses 'team=%v': %v", repo.Owner.IsOrganization(), err)
  1091. }
  1092. return sess.Commit()
  1093. }
  1094. func (repo *Repository) getCollaborators(e Engine) ([]*User, error) {
  1095. collaborations := make([]*Collaboration, 0)
  1096. if err := e.Find(&collaborations, &Collaboration{RepoID: repo.ID}); err != nil {
  1097. return nil, err
  1098. }
  1099. users := make([]*User, len(collaborations))
  1100. for i, c := range collaborations {
  1101. user, err := getUserByID(e, c.UserID)
  1102. if err != nil {
  1103. return nil, err
  1104. }
  1105. users[i] = user
  1106. }
  1107. return users, nil
  1108. }
  1109. // GetCollaborators returns the collaborators for a repository
  1110. func (repo *Repository) GetCollaborators() ([]*User, error) {
  1111. return repo.getCollaborators(x)
  1112. }
  1113. // Delete collaborator and accompanying access
  1114. func (repo *Repository) DeleteCollaborator(u *User) (err error) {
  1115. collaboration := &Collaboration{
  1116. RepoID: repo.ID,
  1117. UserID: u.Id,
  1118. }
  1119. sess := x.NewSession()
  1120. defer sessionRelease(sess)
  1121. if err = sess.Begin(); err != nil {
  1122. return err
  1123. }
  1124. if has, err := sess.Delete(collaboration); err != nil || has == 0 {
  1125. return err
  1126. } else if err = repo.recalculateAccesses(sess); err != nil {
  1127. return err
  1128. }
  1129. return sess.Commit()
  1130. }
  1131. // __ __ __ .__
  1132. // / \ / \_____ _/ |_ ____ | |__
  1133. // \ \/\/ /\__ \\ __\/ ___\| | \
  1134. // \ / / __ \| | \ \___| Y \
  1135. // \__/\ / (____ /__| \___ >___| /
  1136. // \/ \/ \/ \/
  1137. // Watch is connection request for receiving repository notification.
  1138. type Watch struct {
  1139. ID int64 `xorm:"pk autoincr"`
  1140. UserID int64 `xorm:"UNIQUE(watch)"`
  1141. RepoID int64 `xorm:"UNIQUE(watch)"`
  1142. }
  1143. // IsWatching checks if user has watched given repository.
  1144. func IsWatching(uid, repoId int64) bool {
  1145. has, _ := x.Get(&Watch{0, uid, repoId})
  1146. return has
  1147. }
  1148. func watchRepo(e Engine, uid, repoId int64, watch bool) (err error) {
  1149. if watch {
  1150. if IsWatching(uid, repoId) {
  1151. return nil
  1152. }
  1153. if _, err = e.Insert(&Watch{RepoID: repoId, UserID: uid}); err != nil {
  1154. return err
  1155. }
  1156. _, err = e.Exec("UPDATE `repository` SET num_watches = num_watches + 1 WHERE id = ?", repoId)
  1157. } else {
  1158. if !IsWatching(uid, repoId) {
  1159. return nil
  1160. }
  1161. if _, err = e.Delete(&Watch{0, uid, repoId}); err != nil {
  1162. return err
  1163. }
  1164. _, err = e.Exec("UPDATE `repository` SET num_watches=num_watches-1 WHERE id=?", repoId)
  1165. }
  1166. return err
  1167. }
  1168. // Watch or unwatch repository.
  1169. func WatchRepo(uid, repoId int64, watch bool) (err error) {
  1170. return watchRepo(x, uid, repoId, watch)
  1171. }
  1172. func getWatchers(e Engine, rid int64) ([]*Watch, error) {
  1173. watches := make([]*Watch, 0, 10)
  1174. err := e.Find(&watches, &Watch{RepoID: rid})
  1175. return watches, err
  1176. }
  1177. // GetWatchers returns all watchers of given repository.
  1178. func GetWatchers(rid int64) ([]*Watch, error) {
  1179. return getWatchers(x, rid)
  1180. }
  1181. func notifyWatchers(e Engine, act *Action) error {
  1182. // Add feeds for user self and all watchers.
  1183. watches, err := getWatchers(e, act.RepoID)
  1184. if err != nil {
  1185. return fmt.Errorf("get watchers: %v", err)
  1186. }
  1187. // Add feed for actioner.
  1188. act.UserID = act.ActUserID
  1189. if _, err = e.InsertOne(act); err != nil {
  1190. return fmt.Errorf("insert new actioner: %v", err)
  1191. }
  1192. for i := range watches {
  1193. if act.ActUserID == watches[i].UserID {
  1194. continue
  1195. }
  1196. act.ID = 0
  1197. act.UserID = watches[i].UserID
  1198. if _, err = e.InsertOne(act); err != nil {
  1199. return fmt.Errorf("insert new action: %v", err)
  1200. }
  1201. }
  1202. return nil
  1203. }
  1204. // NotifyWatchers creates batch of actions for every watcher.
  1205. func NotifyWatchers(act *Action) error {
  1206. return notifyWatchers(x, act)
  1207. }
  1208. // _________ __
  1209. // / _____// |______ _______
  1210. // \_____ \\ __\__ \\_ __ \
  1211. // / \| | / __ \| | \/
  1212. // /_______ /|__| (____ /__|
  1213. // \/ \/
  1214. type Star struct {
  1215. Id int64
  1216. Uid int64 `xorm:"UNIQUE(s)"`
  1217. RepoId int64 `xorm:"UNIQUE(s)"`
  1218. }
  1219. // Star or unstar repository.
  1220. func StarRepo(uid, repoId int64, star bool) (err error) {
  1221. if star {
  1222. if IsStaring(uid, repoId) {
  1223. return nil
  1224. }
  1225. if _, err = x.Insert(&Star{Uid: uid, RepoId: repoId}); err != nil {
  1226. return err
  1227. } else if _, err = x.Exec("UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoId); err != nil {
  1228. return err
  1229. }
  1230. _, err = x.Exec("UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", uid)
  1231. } else {
  1232. if !IsStaring(uid, repoId) {
  1233. return nil
  1234. }
  1235. if _, err = x.Delete(&Star{0, uid, repoId}); err != nil {
  1236. return err
  1237. } else if _, err = x.Exec("UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoId); err != nil {
  1238. return err
  1239. }
  1240. _, err = x.Exec("UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", uid)
  1241. }
  1242. return err
  1243. }
  1244. // IsStaring checks if user has starred given repository.
  1245. func IsStaring(uid, repoId int64) bool {
  1246. has, _ := x.Get(&Star{0, uid, repoId})
  1247. return has
  1248. }
  1249. // ___________ __
  1250. // \_ _____/__________| | __
  1251. // | __)/ _ \_ __ \ |/ /
  1252. // | \( <_> ) | \/ <
  1253. // \___ / \____/|__| |__|_ \
  1254. // \/ \/
  1255. // HasForkedRepo checks if given user has already forked a repository with given ID.
  1256. func HasForkedRepo(ownerID, repoID int64) (*Repository, bool) {
  1257. repo := new(Repository)
  1258. has, _ := x.Where("owner_id=? AND fork_id=?", ownerID, repoID).Get(repo)
  1259. return repo, has
  1260. }
  1261. func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Repository, err error) {
  1262. repo := &Repository{
  1263. OwnerID: u.Id,
  1264. Owner: u,
  1265. Name: name,
  1266. LowerName: strings.ToLower(name),
  1267. Description: desc,
  1268. IsPrivate: oldRepo.IsPrivate,
  1269. IsFork: true,
  1270. ForkID: oldRepo.ID,
  1271. }
  1272. sess := x.NewSession()
  1273. defer sessionRelease(sess)
  1274. if err = sess.Begin(); err != nil {
  1275. return nil, err
  1276. }
  1277. if err = createRepository(sess, u, repo); err != nil {
  1278. return nil, err
  1279. }
  1280. if _, err = sess.Exec("UPDATE `repository` SET num_forks=num_forks+1 WHERE id=?", oldRepo.ID); err != nil {
  1281. return nil, err
  1282. }
  1283. oldRepoPath, err := oldRepo.RepoPath()
  1284. if err != nil {
  1285. return nil, fmt.Errorf("get old repository path: %v", err)
  1286. }
  1287. repoPath := RepoPath(u.Name, repo.Name)
  1288. _, stderr, err := process.ExecTimeout(10*time.Minute,
  1289. fmt.Sprintf("ForkRepository(git clone): %s/%s", u.Name, repo.Name),
  1290. "git", "clone", "--bare", oldRepoPath, repoPath)
  1291. if err != nil {
  1292. return nil, fmt.Errorf("git clone: %v", stderr)
  1293. }
  1294. _, stderr, err = process.ExecDir(-1,
  1295. repoPath, fmt.Sprintf("ForkRepository(git update-server-info): %s", repoPath),
  1296. "git", "update-server-info")
  1297. if err != nil {
  1298. return nil, fmt.Errorf("git update-server-info: %v", err)
  1299. }
  1300. if err = createUpdateHook(repoPath); err != nil {
  1301. return nil, fmt.Errorf("createUpdateHook: %v", err)
  1302. }
  1303. return repo, sess.Commit()
  1304. }