wiki.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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(name string) string {
  60. return url.QueryUnescape(strings.Replace(name, "-", " ", -1))
  61. }
  62. // WikiCloneLink returns clone URLs of repository wiki.
  63. func (repo *Repository) WikiCloneLink() (cl *CloneLink) {
  64. return repo.cloneLink(true)
  65. }
  66. // WikiPath returns wiki data path by given user and repository name.
  67. func WikiPath(userName, repoName string) string {
  68. return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".wiki.git")
  69. }
  70. func (repo *Repository) WikiPath() string {
  71. return WikiPath(repo.MustOwner().Name, repo.Name)
  72. }
  73. // HasWiki returns true if repository has wiki.
  74. func (repo *Repository) HasWiki() bool {
  75. return com.IsDir(repo.WikiPath())
  76. }
  77. // InitWiki initializes a wiki for repository,
  78. // it does nothing when repository already has wiki.
  79. func (repo *Repository) InitWiki() error {
  80. if repo.HasWiki() {
  81. return nil
  82. }
  83. if err := git.InitRepository(repo.WikiPath(), true); err != nil {
  84. return fmt.Errorf("InitRepository: %v", err)
  85. }
  86. return nil
  87. }
  88. func (repo *Repository) LocalWikiPath() string {
  89. return path.Join(setting.AppDataPath, "tmp/local-wiki", com.ToStr(repo.ID))
  90. }
  91. // UpdateLocalWiki makes sure the local copy of repository wiki is up-to-date.
  92. func (repo *Repository) UpdateLocalWiki() error {
  93. return updateLocalCopy(repo.WikiPath(), repo.LocalWikiPath())
  94. }
  95. // updateWikiPage adds new page to repository wiki.
  96. func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, message string, isNew bool) (err error) {
  97. wikiWorkingPool.CheckIn(com.ToStr(repo.ID))
  98. defer wikiWorkingPool.CheckOut(com.ToStr(repo.ID))
  99. if err = repo.InitWiki(); err != nil {
  100. return fmt.Errorf("InitWiki: %v", err)
  101. }
  102. localPath := repo.LocalWikiPath()
  103. // Discard local commits make sure even to remote when local copy exists.
  104. if com.IsExist(localPath) {
  105. // No need to check if nothing in the repository.
  106. if git.IsBranchExist(localPath, "master") {
  107. if err = git.ResetHEAD(localPath, true, "origin/master"); err != nil {
  108. return fmt.Errorf("Reset: %v", err)
  109. }
  110. }
  111. }
  112. if err = repo.UpdateLocalWiki(); err != nil {
  113. return fmt.Errorf("UpdateLocalWiki: %v", err)
  114. }
  115. title = ToWikiPageName(strings.Replace(title, "/", " ", -1))
  116. filename := path.Join(localPath, title+".md")
  117. // If not a new file, show perform update not create.
  118. if isNew {
  119. if com.IsExist(filename) {
  120. return ErrWikiAlreadyExist{filename}
  121. }
  122. } else {
  123. os.Remove(path.Join(localPath, oldTitle+".md"))
  124. }
  125. if err = ioutil.WriteFile(filename, []byte(content), 0666); err != nil {
  126. return fmt.Errorf("WriteFile: %v", err)
  127. }
  128. if len(message) == 0 {
  129. message = "Update page '" + title + "'"
  130. }
  131. if err = git.AddChanges(localPath, true); err != nil {
  132. return fmt.Errorf("AddChanges: %v", err)
  133. } else if err = git.CommitChanges(localPath, message, doer.NewGitSig()); err != nil {
  134. return fmt.Errorf("CommitChanges: %v", err)
  135. } else if err = git.Push(localPath, "origin", "master"); err != nil {
  136. return fmt.Errorf("Push: %v", err)
  137. }
  138. return nil
  139. }
  140. func (repo *Repository) AddWikiPage(doer *User, title, content, message string) error {
  141. return repo.updateWikiPage(doer, "", title, content, message, true)
  142. }
  143. func (repo *Repository) EditWikiPage(doer *User, oldTitle, title, content, message string) error {
  144. return repo.updateWikiPage(doer, oldTitle, title, content, message, false)
  145. }