wiki.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. // and removes leading characters './' to prevent changing files
  60. // that are not belong to wiki repository.
  61. func ToWikiPageName(urlString string) string {
  62. name, _ := url.QueryUnescape(strings.Replace(urlString, "-", " ", -1))
  63. return strings.Replace(strings.TrimLeft(name, "./"), "/", " ", -1)
  64. }
  65. // WikiCloneLink returns clone URLs of repository wiki.
  66. func (repo *Repository) WikiCloneLink() (cl *CloneLink) {
  67. return repo.cloneLink(true)
  68. }
  69. // WikiPath returns wiki data path by given user and repository name.
  70. func WikiPath(userName, repoName string) string {
  71. return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".wiki.git")
  72. }
  73. func (repo *Repository) WikiPath() string {
  74. return WikiPath(repo.MustOwner().Name, repo.Name)
  75. }
  76. // HasWiki returns true if repository has wiki.
  77. func (repo *Repository) HasWiki() bool {
  78. return com.IsDir(repo.WikiPath())
  79. }
  80. // InitWiki initializes a wiki for repository,
  81. // it does nothing when repository already has wiki.
  82. func (repo *Repository) InitWiki() error {
  83. if repo.HasWiki() {
  84. return nil
  85. }
  86. if err := git.InitRepository(repo.WikiPath(), true); err != nil {
  87. return fmt.Errorf("InitRepository: %v", err)
  88. }
  89. return nil
  90. }
  91. func (repo *Repository) LocalWikiPath() string {
  92. return path.Join(setting.AppDataPath, "tmp/local-wiki", com.ToStr(repo.ID))
  93. }
  94. // UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date.
  95. func (repo *Repository) UpdateLocalWiki() error {
  96. return updateLocalCopy(repo.WikiPath(), repo.LocalWikiPath())
  97. }
  98. // discardLocalWikiChanges discards local commits make sure
  99. // it is even to remote branch when local copy exists.
  100. func discardLocalWikiChanges(localPath string) error {
  101. if !com.IsExist(localPath) {
  102. return nil
  103. }
  104. // No need to check if nothing in the repository.
  105. if !git.IsBranchExist(localPath, "master") {
  106. return nil
  107. }
  108. if err := git.ResetHEAD(localPath, true, "origin/master"); err != nil {
  109. return fmt.Errorf("ResetHEAD: %v", err)
  110. }
  111. return nil
  112. }
  113. // updateWikiPage adds new page to repository wiki.
  114. func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, message string, isNew bool) (err error) {
  115. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  116. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  117. if err = repo.InitWiki(); err != nil {
  118. return fmt.Errorf("InitWiki: %v", err)
  119. }
  120. localPath := repo.LocalWikiPath()
  121. if err = discardLocalWikiChanges(localPath); err != nil {
  122. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  123. } else if err = repo.UpdateLocalWiki(); err != nil {
  124. return fmt.Errorf("UpdateLocalWiki: %v", err)
  125. }
  126. title = ToWikiPageName(title)
  127. filename := path.Join(localPath, title+".md")
  128. // If not a new file, show perform update not create.
  129. if isNew {
  130. if com.IsExist(filename) {
  131. return ErrWikiAlreadyExist{filename}
  132. }
  133. } else {
  134. os.Remove(path.Join(localPath, oldTitle+".md"))
  135. }
  136. // SECURITY: if new file is a symlink to non-exist critical file,
  137. // attack content can be written to the target file (e.g. authorized_keys2)
  138. // as a new page operation.
  139. // So we want to make sure the symlink is removed before write anything.
  140. // The new file we created will be in normal text format.
  141. os.Remove(filename)
  142. if err = ioutil.WriteFile(filename, []byte(content), 0666); err != nil {
  143. return fmt.Errorf("WriteFile: %v", err)
  144. }
  145. if len(message) == 0 {
  146. message = "Update page '" + title + "'"
  147. }
  148. if err = git.AddChanges(localPath, true); err != nil {
  149. return fmt.Errorf("AddChanges: %v", err)
  150. } else if err = git.CommitChanges(localPath, message, doer.NewGitSig()); err != nil {
  151. return fmt.Errorf("CommitChanges: %v", err)
  152. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  153. return fmt.Errorf("Push: %v", err)
  154. }
  155. return nil
  156. }
  157. func (repo *Repository) AddWikiPage(doer *User, title, content, message string) error {
  158. return repo.updateWikiPage(doer, "", title, content, message, true)
  159. }
  160. func (repo *Repository) EditWikiPage(doer *User, oldTitle, title, content, message string) error {
  161. return repo.updateWikiPage(doer, oldTitle, title, content, message, false)
  162. }
  163. func (repo *Repository) DeleteWikiPage(doer *User, title string) (err error) {
  164. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  165. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  166. localPath := repo.LocalWikiPath()
  167. if err = discardLocalWikiChanges(localPath); err != nil {
  168. return fmt.Errorf("discardLocalWikiChanges: %v", err)
  169. } else if err = repo.UpdateLocalWiki(); err != nil {
  170. return fmt.Errorf("UpdateLocalWiki: %v", err)
  171. }
  172. title = ToWikiPageName(title)
  173. filename := path.Join(localPath, title+".md")
  174. os.Remove(filename)
  175. message := "Delete page '" + title + "'"
  176. if err = git.AddChanges(localPath, true); err != nil {
  177. return fmt.Errorf("AddChanges: %v", err)
  178. } else if err = git.CommitChanges(localPath, message, doer.NewGitSig()); err != nil {
  179. return fmt.Errorf("CommitChanges: %v", err)
  180. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  181. return fmt.Errorf("Push: %v", err)
  182. }
  183. return nil
  184. }