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 58KB

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