wiki.go 4.6 KB

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