wiki.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. "net/url"
  9. "os"
  10. "path"
  11. "path/filepath"
  12. "strings"
  13. "sync"
  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. // discardLocalWikiChanges discards local commits make sure
  97. // it is even to remote branch when local copy exists.
  98. func discardLocalWikiChanges(localPath string) error {
  99. if !com.IsExist(localPath) {
  100. return nil
  101. }
  102. // No need to check if nothing in the repository.
  103. if !git.IsBranchExist(localPath, "master") {
  104. return nil
  105. }
  106. if err := git.ResetHEAD(localPath, true, "origin/master"); err != nil {
  107. return fmt.Errorf("ResetHEAD: %v", err)
  108. }
  109. return nil
  110. }
  111. // updateWikiPage adds new page to repository wiki.
  112. func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, message string, isNew bool) (err error) {
  113. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  114. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  115. if err = repo.InitWiki(); err != nil {
  116. return fmt.Errorf("InitWiki: %v", err)
  117. }
  118. localPath := repo.LocalWikiPath()
  119. if err = discardLocalWikiChanges(localPath); err != nil {
  120. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  121. } else if err = repo.UpdateLocalWiki(); err != nil {
  122. return fmt.Errorf("UpdateLocalWiki: %v", err)
  123. }
  124. title = ToWikiPageName(strings.Replace(title, "/", " ", -1))
  125. filename := path.Join(localPath, title+".md")
  126. // If not a new file, show perform update not create.
  127. if isNew {
  128. if com.IsExist(filename) {
  129. return ErrWikiAlreadyExist{filename}
  130. }
  131. } else {
  132. os.Remove(path.Join(localPath, oldTitle+".md"))
  133. }
  134. if err = ioutil.WriteFile(filename, []byte(content), 0666); err != nil {
  135. return fmt.Errorf("WriteFile: %v", err)
  136. }
  137. if len(message) == 0 {
  138. message = "Update page '" + title + "'"
  139. }
  140. if err = git.AddChanges(localPath, true); err != nil {
  141. return fmt.Errorf("AddChanges: %v", err)
  142. } else if err = git.CommitChanges(localPath, message, doer.NewGitSig()); err != nil {
  143. return fmt.Errorf("CommitChanges: %v", err)
  144. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  145. return fmt.Errorf("Push: %v", err)
  146. }
  147. return nil
  148. }
  149. func (repo *Repository) AddWikiPage(doer *User, title, content, message string) error {
  150. return repo.updateWikiPage(doer, "", title, content, message, true)
  151. }
  152. func (repo *Repository) EditWikiPage(doer *User, oldTitle, title, content, message string) error {
  153. return repo.updateWikiPage(doer, oldTitle, title, content, message, false)
  154. }
  155. func (repo *Repository) DeleteWikiPage(doer *User, title string) (err error) {
  156. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  157. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  158. localPath := repo.LocalWikiPath()
  159. if err = discardLocalWikiChanges(localPath); err != nil {
  160. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  161. } else if err = repo.UpdateLocalWiki(); err != nil {
  162. return fmt.Errorf("UpdateLocalWiki: %v", err)
  163. }
  164. title = ToWikiPageName(strings.Replace(title, "/", " ", -1))
  165. filename := path.Join(localPath, title+".md")
  166. os.Remove(filename)
  167. message := "Delete page '" + title + "'"
  168. if err = git.AddChanges(localPath, true); err != nil {
  169. return fmt.Errorf("AddChanges: %v", err)
  170. } else if err = git.CommitChanges(localPath, message, doer.NewGitSig()); err != nil {
  171. return fmt.Errorf("CommitChanges: %v", err)
  172. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  173. return fmt.Errorf("Push: %v", err)
  174. }
  175. return nil
  176. }