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.

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