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.

180 lines
4.8 KiB

9 years ago
9 years ago
  1. // Copyright 2015 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. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "path"
  10. "path/filepath"
  11. "strings"
  12. "sync"
  13. "net/url"
  14. "github.com/Unknwon/com"
  15. "github.com/gogits/git-module"
  16. "github.com/gogits/gogs/modules/setting"
  17. )
  18. // workingPool represents a pool of working status which makes sure
  19. // that only one instance of same task is performing at a time.
  20. // However, different type of tasks can performing at the same time.
  21. type workingPool struct {
  22. lock sync.Mutex
  23. pool map[string]*sync.Mutex
  24. count map[string]int
  25. }
  26. // CheckIn checks in a task and waits if others are running.
  27. func (p *workingPool) CheckIn(name string) {
  28. p.lock.Lock()
  29. lock, has := p.pool[name]
  30. if !has {
  31. lock = &sync.Mutex{}
  32. p.pool[name] = lock
  33. }
  34. p.count[name]++
  35. p.lock.Unlock()
  36. lock.Lock()
  37. }
  38. // CheckOut checks out a task to let other tasks run.
  39. func (p *workingPool) CheckOut(name string) {
  40. p.lock.Lock()
  41. defer p.lock.Unlock()
  42. p.pool[name].Unlock()
  43. if p.count[name] == 1 {
  44. delete(p.pool, name)
  45. delete(p.count, name)
  46. } else {
  47. p.count[name]--
  48. }
  49. }
  50. var wikiWorkingPool = &workingPool{
  51. pool: make(map[string]*sync.Mutex),
  52. count: make(map[string]int),
  53. }
  54. // ToWikiPageURL formats a string to corresponding wiki URL name.
  55. func ToWikiPageURL(name string) string {
  56. return url.QueryEscape(strings.Replace(name, " ", "-", -1))
  57. }
  58. // ToWikiPageName formats a URL back to corresponding wiki page name.
  59. func ToWikiPageName(urlString string) string {
  60. name, _ := url.QueryUnescape(strings.Replace(urlString, "-", " ", -1))
  61. return name
  62. }
  63. // WikiCloneLink returns clone URLs of repository wiki.
  64. func (repo *Repository) WikiCloneLink() (cl *CloneLink) {
  65. return repo.cloneLink(true)
  66. }
  67. // WikiPath returns wiki data path by given user and repository name.
  68. func WikiPath(userName, repoName string) string {
  69. return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".wiki.git")
  70. }
  71. func (repo *Repository) WikiPath() string {
  72. return WikiPath(repo.MustOwner().Name, repo.Name)
  73. }
  74. // HasWiki returns true if repository has wiki.
  75. func (repo *Repository) HasWiki() bool {
  76. return com.IsDir(repo.WikiPath())
  77. }
  78. // InitWiki initializes a wiki for repository,
  79. // it does nothing when repository already has wiki.
  80. func (repo *Repository) InitWiki() error {
  81. if repo.HasWiki() {
  82. return nil
  83. }
  84. if err := git.InitRepository(repo.WikiPath(), true); err != nil {
  85. return fmt.Errorf("InitRepository: %v", err)
  86. }
  87. return nil
  88. }
  89. func (repo *Repository) LocalWikiPath() string {
  90. return path.Join(setting.AppDataPath, "tmp/local-wiki", com.ToStr(repo.ID))
  91. }
  92. // UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date.
  93. func (repo *Repository) UpdateLocalWiki() error {
  94. return updateLocalCopy(repo.WikiPath(), repo.LocalWikiPath())
  95. }
  96. // updateWikiPage adds new page to repository wiki.
  97. func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, message string, isNew bool) (err error) {
  98. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  99. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  100. if err = repo.InitWiki(); err != nil {
  101. return fmt.Errorf("InitWiki: %v", err)
  102. }
  103. localPath := repo.LocalWikiPath()
  104. // Discard local commits make sure even to remote when local copy exists.
  105. if com.IsExist(localPath) {
  106. // No need to check if nothing in the repository.
  107. if git.IsBranchExist(localPath, "master") {
  108. if err = git.ResetHEAD(localPath, true, "origin/master"); err != nil {
  109. return fmt.Errorf("Reset: %v", err)
  110. }
  111. }
  112. }
  113. if err = repo.UpdateLocalWiki(); err != nil {
  114. return fmt.Errorf("UpdateLocalWiki: %v", err)
  115. }
  116. title = ToWikiPageName(strings.Replace(title, "/", " ", -1))
  117. filename := path.Join(localPath, title+".md")
  118. // If not a new file, show perform update not create.
  119. if isNew {
  120. if com.IsExist(filename) {
  121. return ErrWikiAlreadyExist{filename}
  122. }
  123. } else {
  124. os.Remove(path.Join(localPath, oldTitle+".md"))
  125. }
  126. if err = ioutil.WriteFile(filename, []byte(content), 0666); err != nil {
  127. return fmt.Errorf("WriteFile: %v", err)
  128. }
  129. if len(message) == 0 {
  130. message = "Update page '" + title + "'"
  131. }
  132. if err = git.AddChanges(localPath, true); err != nil {
  133. return fmt.Errorf("AddChanges: %v", err)
  134. } else if err = git.CommitChanges(localPath, message, doer.NewGitSig()); err != nil {
  135. return fmt.Errorf("CommitChanges: %v", err)
  136. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  137. return fmt.Errorf("Push: %v", err)
  138. }
  139. return nil
  140. }
  141. func (repo *Repository) AddWikiPage(doer *User, title, content, message string) error {
  142. return repo.updateWikiPage(doer, "", title, content, message, true)
  143. }
  144. func (repo *Repository) EditWikiPage(doer *User, oldTitle, title, content, message string) error {
  145. return repo.updateWikiPage(doer, oldTitle, title, content, message, false)
  146. }