repo.go 63 KB

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