wiki.go 4.0 KB

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