repo.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Copyright 2014 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. "errors"
  7. "fmt"
  8. "io/ioutil"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "time"
  13. "unicode/utf8"
  14. "github.com/Unknwon/com"
  15. git "github.com/libgit2/git2go"
  16. "github.com/gogits/gogs/modules/base"
  17. "github.com/gogits/gogs/modules/log"
  18. )
  19. type Repository struct {
  20. Id int64
  21. OwnerId int64 `xorm:"unique(s)"`
  22. ForkId int64
  23. LowerName string `xorm:"unique(s) index not null"`
  24. Name string `xorm:"index not null"`
  25. Description string
  26. Private bool
  27. NumWatchs int
  28. NumStars int
  29. NumForks int
  30. Created time.Time `xorm:"created"`
  31. Updated time.Time `xorm:"updated"`
  32. }
  33. type Star struct {
  34. Id int64
  35. RepoId int64
  36. UserId int64
  37. Created time.Time `xorm:"created"`
  38. }
  39. var (
  40. LanguageIgns, Licenses []string
  41. )
  42. var (
  43. ErrRepoAlreadyExist = errors.New("Repository already exist")
  44. )
  45. func init() {
  46. LanguageIgns = strings.Split(base.Cfg.MustValue("repository", "LANG_IGNS"), "|")
  47. Licenses = strings.Split(base.Cfg.MustValue("repository", "LICENSES"), "|")
  48. }
  49. // check if repository is exist
  50. func IsRepositoryExist(user *User, repoName string) (bool, error) {
  51. repo := Repository{OwnerId: user.Id}
  52. has, err := orm.Where("lower_name = ?", strings.ToLower(repoName)).Get(&repo)
  53. if err != nil {
  54. return has, err
  55. }
  56. s, err := os.Stat(RepoPath(user.Name, repoName))
  57. if err != nil {
  58. return false, nil
  59. }
  60. return s.IsDir(), nil
  61. }
  62. // CreateRepository creates a repository for given user or orgnaziation.
  63. func CreateRepository(user *User, repoName, desc, repoLang, license string, private bool, initReadme bool) (*Repository, error) {
  64. isExist, err := IsRepositoryExist(user, repoName)
  65. if err != nil {
  66. return nil, err
  67. } else if isExist {
  68. return nil, ErrRepoAlreadyExist
  69. }
  70. repo := &Repository{
  71. OwnerId: user.Id,
  72. Name: repoName,
  73. LowerName: strings.ToLower(repoName),
  74. Description: desc,
  75. Private: private,
  76. }
  77. f := RepoPath(user.Name, repoName)
  78. if err = initRepository(f, user, repo, initReadme, repoLang, license); err != nil {
  79. return nil, err
  80. }
  81. session := orm.NewSession()
  82. defer session.Close()
  83. session.Begin()
  84. if _, err = session.Insert(repo); err != nil {
  85. if err2 := os.RemoveAll(f); err2 != nil {
  86. return nil, errors.New(fmt.Sprintf(
  87. "delete repo directory %s/%s failed", user.Name, repoName))
  88. }
  89. session.Rollback()
  90. return nil, err
  91. }
  92. // TODO: RemoveAll may fail due to not root access.
  93. access := Access{
  94. UserName: user.Name,
  95. RepoName: repo.Name,
  96. Mode: AU_WRITABLE,
  97. }
  98. if _, err = session.Insert(&access); err != nil {
  99. session.Rollback()
  100. if err2 := os.RemoveAll(f); err2 != nil {
  101. return nil, errors.New(fmt.Sprintf(
  102. "delete repo directory %s/%s failed", user.Name, repoName))
  103. }
  104. return nil, err
  105. }
  106. if _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id); err != nil {
  107. session.Rollback()
  108. if err2 := os.RemoveAll(f); err2 != nil {
  109. return nil, errors.New(fmt.Sprintf(
  110. "delete repo directory %s/%s failed", user.Name, repoName))
  111. }
  112. return nil, err
  113. }
  114. if err = session.Commit(); err != nil {
  115. session.Rollback()
  116. if err2 := os.RemoveAll(f); err2 != nil {
  117. return nil, errors.New(fmt.Sprintf(
  118. "delete repo directory %s/%s failed", user.Name, repoName))
  119. }
  120. return nil, err
  121. }
  122. return repo, nil
  123. }
  124. // InitRepository initializes README and .gitignore if needed.
  125. func initRepository(f string, user *User, repo *Repository, initReadme bool, repoLang, license string) error {
  126. fileName := map[string]string{}
  127. if initReadme {
  128. fileName["readme"] = "README.md"
  129. }
  130. if repoLang != "" {
  131. fileName["gitign"] = ".gitignore"
  132. }
  133. if license != "" {
  134. fileName["license"] = "LICENSE"
  135. }
  136. workdir := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()))
  137. os.MkdirAll(workdir, os.ModePerm)
  138. sig := user.NewGitSig()
  139. // README
  140. if initReadme {
  141. defaultReadme := repo.Name + "\n" + strings.Repeat("=",
  142. utf8.RuneCountInString(repo.Name)) + "\n\n" + repo.Description
  143. if err := ioutil.WriteFile(filepath.Join(workdir, fileName["readme"]),
  144. []byte(defaultReadme), 0644); err != nil {
  145. return err
  146. }
  147. }
  148. if repoLang != "" {
  149. // .gitignore
  150. filePath := "conf/gitignore/" + repoLang
  151. if com.IsFile(filePath) {
  152. if _, err := com.Copy(filePath,
  153. filepath.Join(workdir, fileName["gitign"])); err != nil {
  154. return err
  155. }
  156. }
  157. }
  158. if license != "" {
  159. // LICENSE
  160. filePath := "conf/license/" + license
  161. if com.IsFile(filePath) {
  162. if _, err := com.Copy(filePath,
  163. filepath.Join(workdir, fileName["license"])); err != nil {
  164. return err
  165. }
  166. }
  167. }
  168. rp, err := git.InitRepository(f, true)
  169. if err != nil {
  170. return err
  171. }
  172. rp.SetWorkdir(workdir, false)
  173. idx, err := rp.Index()
  174. if err != nil {
  175. return err
  176. }
  177. for _, name := range fileName {
  178. if err = idx.AddByPath(name); err != nil {
  179. return err
  180. }
  181. }
  182. treeId, err := idx.WriteTree()
  183. if err != nil {
  184. return err
  185. }
  186. message := "Init commit"
  187. tree, err := rp.LookupTree(treeId)
  188. if err != nil {
  189. return err
  190. }
  191. if _, err = rp.CreateCommit("HEAD", sig, sig, message, tree); err != nil {
  192. return err
  193. }
  194. return nil
  195. }
  196. // GetRepositories returns the list of repositories of given user.
  197. func GetRepositories(user *User) ([]Repository, error) {
  198. repos := make([]Repository, 0, 10)
  199. err := orm.Find(&repos, &Repository{OwnerId: user.Id})
  200. return repos, err
  201. }
  202. func GetRepositoryCount(user *User) (int64, error) {
  203. return orm.Count(&Repository{OwnerId: user.Id})
  204. }
  205. const (
  206. RFile = iota + 1
  207. RDir
  208. )
  209. type RepoFile struct {
  210. Type int
  211. Name string
  212. Created time.Time
  213. }
  214. func GetReposFiles(userName, reposName, treeName, rpath string) ([]RepoFile, error) {
  215. f := RepoPath(userName, reposName)
  216. repo, err := git.OpenRepository(f)
  217. if err != nil {
  218. return nil, err
  219. }
  220. obj, err := repo.RevparseSingle("HEAD")
  221. if err != nil {
  222. return nil, err
  223. }
  224. lastCommit := obj.(*git.Commit)
  225. var repofiles []RepoFile
  226. tree, err := lastCommit.Tree()
  227. if err != nil {
  228. return nil, err
  229. }
  230. var i uint64 = 0
  231. for ; i < tree.EntryCount(); i++ {
  232. entry := tree.EntryByIndex(i)
  233. repofiles = append(repofiles, RepoFile{
  234. entry.Filemode,
  235. entry.Name,
  236. time.Now(),
  237. })
  238. }
  239. return repofiles, nil
  240. }
  241. func StarReposiory(user *User, repoName string) error {
  242. return nil
  243. }
  244. func UnStarRepository() {
  245. }
  246. func WatchRepository() {
  247. }
  248. func UnWatchRepository() {
  249. }
  250. func ForkRepository(reposName string, userId int64) {
  251. }
  252. func RepoPath(userName, repoName string) string {
  253. return filepath.Join(UserPath(userName), repoName+".git")
  254. }
  255. // DeleteRepository deletes a repository for a user or orgnaztion.
  256. func DeleteRepository(user *User, reposName string) (err error) {
  257. session := orm.NewSession()
  258. if _, err = session.Delete(&Repository{OwnerId: user.Id, Name: reposName}); err != nil {
  259. session.Rollback()
  260. return err
  261. }
  262. if _, err = session.Exec("update user set num_repos = num_repos - 1 where id = ?", user.Id); err != nil {
  263. session.Rollback()
  264. return err
  265. }
  266. if err = session.Commit(); err != nil {
  267. session.Rollback()
  268. return err
  269. }
  270. if err = os.RemoveAll(RepoPath(user.Name, reposName)); err != nil {
  271. // TODO: log and delete manully
  272. log.Error("delete repo %s/%s failed", user.Name, reposName)
  273. return err
  274. }
  275. return nil
  276. }