repo.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 git
  5. import (
  6. "bytes"
  7. "container/list"
  8. "errors"
  9. "os"
  10. "path"
  11. "path/filepath"
  12. "strings"
  13. "time"
  14. "github.com/Unknwon/com"
  15. "github.com/mcuadros/go-version"
  16. )
  17. // Repository represents a Git repository.
  18. type Repository struct {
  19. Path string
  20. commitCache *objectCache
  21. tagCache *objectCache
  22. }
  23. const _PRETTY_LOG_FORMAT = `--pretty=format:%H`
  24. func (repo *Repository) parsePrettyFormatLogToList(logs []byte) (*list.List, error) {
  25. l := list.New()
  26. if len(logs) == 0 {
  27. return l, nil
  28. }
  29. parts := bytes.Split(logs, []byte{'\n'})
  30. for _, commitId := range parts {
  31. commit, err := repo.GetCommit(string(commitId))
  32. if err != nil {
  33. return nil, err
  34. }
  35. l.PushBack(commit)
  36. }
  37. return l, nil
  38. }
  39. type NetworkOptions struct {
  40. URL string
  41. Timeout time.Duration
  42. }
  43. // IsRepoURLAccessible checks if given repository URL is accessible.
  44. func IsRepoURLAccessible(opts NetworkOptions) bool {
  45. cmd := NewCommand("ls-remote", "-q", "-h", opts.URL, "HEAD")
  46. if opts.Timeout <= 0 {
  47. opts.Timeout = -1
  48. }
  49. _, err := cmd.RunTimeout(opts.Timeout)
  50. if err != nil {
  51. return false
  52. }
  53. return true
  54. }
  55. // InitRepository initializes a new Git repository.
  56. func InitRepository(repoPath string, bare bool) error {
  57. os.MkdirAll(repoPath, os.ModePerm)
  58. cmd := NewCommand("init")
  59. if bare {
  60. cmd.AddArguments("--bare")
  61. }
  62. _, err := cmd.RunInDir(repoPath)
  63. return err
  64. }
  65. // OpenRepository opens the repository at the given path.
  66. func OpenRepository(repoPath string) (*Repository, error) {
  67. repoPath, err := filepath.Abs(repoPath)
  68. if err != nil {
  69. return nil, err
  70. } else if !isDir(repoPath) {
  71. return nil, errors.New("no such file or directory")
  72. }
  73. return &Repository{
  74. Path: repoPath,
  75. commitCache: newObjectCache(),
  76. tagCache: newObjectCache(),
  77. }, nil
  78. }
  79. type CloneRepoOptions struct {
  80. Mirror bool
  81. Bare bool
  82. Quiet bool
  83. Branch string
  84. Timeout time.Duration
  85. }
  86. // Clone clones original repository to target path.
  87. func Clone(from, to string, opts CloneRepoOptions) (err error) {
  88. toDir := path.Dir(to)
  89. if err = os.MkdirAll(toDir, os.ModePerm); err != nil {
  90. return err
  91. }
  92. cmd := NewCommand("clone")
  93. if opts.Mirror {
  94. cmd.AddArguments("--mirror")
  95. }
  96. if opts.Bare {
  97. cmd.AddArguments("--bare")
  98. }
  99. if opts.Quiet {
  100. cmd.AddArguments("--quiet")
  101. }
  102. if len(opts.Branch) > 0 {
  103. cmd.AddArguments("-b", opts.Branch)
  104. }
  105. cmd.AddArguments(from, to)
  106. if opts.Timeout <= 0 {
  107. opts.Timeout = -1
  108. }
  109. _, err = cmd.RunTimeout(opts.Timeout)
  110. return err
  111. }
  112. type FetchRemoteOptions struct {
  113. Prune bool
  114. Timeout time.Duration
  115. }
  116. // Fetch fetches changes from remotes without merging.
  117. func Fetch(repoPath string, opts FetchRemoteOptions) error {
  118. cmd := NewCommand("fetch")
  119. if opts.Prune {
  120. cmd.AddArguments("--prune")
  121. }
  122. if opts.Timeout <= 0 {
  123. opts.Timeout = -1
  124. }
  125. _, err := cmd.RunInDirTimeout(opts.Timeout, repoPath)
  126. return err
  127. }
  128. type PullRemoteOptions struct {
  129. All bool
  130. Rebase bool
  131. Remote string
  132. Branch string
  133. Timeout time.Duration
  134. }
  135. // Pull pulls changes from remotes.
  136. func Pull(repoPath string, opts PullRemoteOptions) error {
  137. cmd := NewCommand("pull")
  138. if opts.Rebase {
  139. cmd.AddArguments("--rebase")
  140. }
  141. if opts.All {
  142. cmd.AddArguments("--all")
  143. } else {
  144. cmd.AddArguments(opts.Remote)
  145. cmd.AddArguments(opts.Branch)
  146. }
  147. if opts.Timeout <= 0 {
  148. opts.Timeout = -1
  149. }
  150. _, err := cmd.RunInDirTimeout(opts.Timeout, repoPath)
  151. return err
  152. }
  153. // Push pushs local commits to given remote branch.
  154. func Push(repoPath, remote, branch string) error {
  155. _, err := NewCommand("push", remote, branch).RunInDir(repoPath)
  156. return err
  157. }
  158. type CheckoutOptions struct {
  159. Branch string
  160. OldBranch string
  161. Timeout time.Duration
  162. }
  163. // Checkout checkouts a branch
  164. func Checkout(repoPath string, opts CheckoutOptions) error {
  165. cmd := NewCommand("checkout")
  166. if len(opts.OldBranch) > 0 {
  167. cmd.AddArguments("-b")
  168. }
  169. cmd.AddArguments(opts.Branch)
  170. if len(opts.OldBranch) > 0 {
  171. cmd.AddArguments(opts.OldBranch)
  172. }
  173. if opts.Timeout <= 0 {
  174. opts.Timeout = -1
  175. }
  176. _, err := cmd.RunInDirTimeout(opts.Timeout, repoPath)
  177. return err
  178. }
  179. // ResetHEAD resets HEAD to given revision or head of branch.
  180. func ResetHEAD(repoPath string, hard bool, revision string) error {
  181. cmd := NewCommand("reset")
  182. if hard {
  183. cmd.AddArguments("--hard")
  184. }
  185. _, err := cmd.AddArguments(revision).RunInDir(repoPath)
  186. return err
  187. }
  188. // MoveFile moves a file to another file or directory.
  189. func MoveFile(repoPath, oldTreeName, newTreeName string) error {
  190. _, err := NewCommand("mv").AddArguments(oldTreeName, newTreeName).RunInDir(repoPath)
  191. return err
  192. }
  193. // CountObject represents disk usage report of Git repository.
  194. type CountObject struct {
  195. Count int64
  196. Size int64
  197. InPack int64
  198. Packs int64
  199. SizePack int64
  200. PrunePackable int64
  201. Garbage int64
  202. SizeGarbage int64
  203. }
  204. const (
  205. _STAT_COUNT = "count: "
  206. _STAT_SIZE = "size: "
  207. _STAT_IN_PACK = "in-pack: "
  208. _STAT_PACKS = "packs: "
  209. _STAT_SIZE_PACK = "size-pack: "
  210. _STAT_PRUNE_PACKABLE = "prune-packable: "
  211. _STAT_GARBAGE = "garbage: "
  212. _STAT_SIZE_GARBAGE = "size-garbage: "
  213. )
  214. // GetRepoSize returns disk usage report of repository in given path.
  215. func GetRepoSize(repoPath string) (*CountObject, error) {
  216. if version.Compare(gitVersion, "1.8.3", "<") {
  217. return nil, ErrUnsupportedVersion{"1.8.3"}
  218. }
  219. cmd := NewCommand("count-objects", "-v")
  220. stdout, err := cmd.RunInDir(repoPath)
  221. if err != nil {
  222. return nil, err
  223. }
  224. countObject := new(CountObject)
  225. for _, line := range strings.Split(stdout, "\n") {
  226. switch {
  227. case strings.HasPrefix(line, _STAT_COUNT):
  228. countObject.Count = com.StrTo(line[7:]).MustInt64()
  229. case strings.HasPrefix(line, _STAT_SIZE):
  230. countObject.Size = com.StrTo(line[6:]).MustInt64() * 1024
  231. case strings.HasPrefix(line, _STAT_IN_PACK):
  232. countObject.InPack = com.StrTo(line[9:]).MustInt64()
  233. case strings.HasPrefix(line, _STAT_PACKS):
  234. countObject.Packs = com.StrTo(line[7:]).MustInt64()
  235. case strings.HasPrefix(line, _STAT_SIZE_PACK):
  236. countObject.SizePack = com.StrTo(line[11:]).MustInt64() * 1024
  237. case strings.HasPrefix(line, _STAT_PRUNE_PACKABLE):
  238. countObject.PrunePackable = com.StrTo(line[16:]).MustInt64()
  239. case strings.HasPrefix(line, _STAT_GARBAGE):
  240. countObject.Garbage = com.StrTo(line[9:]).MustInt64()
  241. case strings.HasPrefix(line, _STAT_SIZE_GARBAGE):
  242. countObject.SizeGarbage = com.StrTo(line[14:]).MustInt64() * 1024
  243. }
  244. }
  245. return countObject, nil
  246. }