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.

repo.go 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package git
  6. import (
  7. "bytes"
  8. "container/list"
  9. "errors"
  10. "fmt"
  11. "os"
  12. "path"
  13. "path/filepath"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "github.com/Unknwon/com"
  18. "gopkg.in/src-d/go-billy.v4/osfs"
  19. gogit "gopkg.in/src-d/go-git.v4"
  20. "gopkg.in/src-d/go-git.v4/plumbing/cache"
  21. "gopkg.in/src-d/go-git.v4/storage/filesystem"
  22. )
  23. // Repository represents a Git repository.
  24. type Repository struct {
  25. Path string
  26. tagCache *ObjectCache
  27. gogitRepo *gogit.Repository
  28. gogitStorage *filesystem.Storage
  29. }
  30. const prettyLogFormat = `--pretty=format:%H`
  31. func (repo *Repository) parsePrettyFormatLogToList(logs []byte) (*list.List, error) {
  32. l := list.New()
  33. if len(logs) == 0 {
  34. return l, nil
  35. }
  36. parts := bytes.Split(logs, []byte{'\n'})
  37. for _, commitID := range parts {
  38. commit, err := repo.GetCommit(string(commitID))
  39. if err != nil {
  40. return nil, err
  41. }
  42. l.PushBack(commit)
  43. }
  44. return l, nil
  45. }
  46. // IsRepoURLAccessible checks if given repository URL is accessible.
  47. func IsRepoURLAccessible(url string) bool {
  48. _, err := NewCommand("ls-remote", "-q", "-h", url, "HEAD").Run()
  49. return err == nil
  50. }
  51. // InitRepository initializes a new Git repository.
  52. func InitRepository(repoPath string, bare bool) error {
  53. err := os.MkdirAll(repoPath, os.ModePerm)
  54. if err != nil {
  55. return err
  56. }
  57. cmd := NewCommand("init")
  58. if bare {
  59. cmd.AddArguments("--bare")
  60. }
  61. _, err = cmd.RunInDir(repoPath)
  62. return err
  63. }
  64. // OpenRepository opens the repository at the given path.
  65. func OpenRepository(repoPath string) (*Repository, error) {
  66. repoPath, err := filepath.Abs(repoPath)
  67. if err != nil {
  68. return nil, err
  69. } else if !isDir(repoPath) {
  70. return nil, errors.New("no such file or directory")
  71. }
  72. fs := osfs.New(repoPath)
  73. _, err = fs.Stat(".git")
  74. if err == nil {
  75. fs, err = fs.Chroot(".git")
  76. if err != nil {
  77. return nil, err
  78. }
  79. }
  80. storage := filesystem.NewStorageWithOptions(fs, cache.NewObjectLRUDefault(), filesystem.Options{KeepDescriptors: true})
  81. gogitRepo, err := gogit.Open(storage, fs)
  82. if err != nil {
  83. return nil, err
  84. }
  85. return &Repository{
  86. Path: repoPath,
  87. gogitRepo: gogitRepo,
  88. gogitStorage: storage,
  89. tagCache: newObjectCache(),
  90. }, nil
  91. }
  92. // IsEmpty Check if repository is empty.
  93. func (repo *Repository) IsEmpty() (bool, error) {
  94. var errbuf strings.Builder
  95. if err := NewCommand("log", "-1").RunInDirPipeline(repo.Path, nil, &errbuf); err != nil {
  96. if strings.Contains(errbuf.String(), "fatal: bad default revision 'HEAD'") ||
  97. strings.Contains(errbuf.String(), "fatal: your current branch 'master' does not have any commits yet") {
  98. return true, nil
  99. }
  100. return true, fmt.Errorf("check empty: %v - %s", err, errbuf.String())
  101. }
  102. return false, nil
  103. }
  104. // CloneRepoOptions options when clone a repository
  105. type CloneRepoOptions struct {
  106. Timeout time.Duration
  107. Mirror bool
  108. Bare bool
  109. Quiet bool
  110. Branch string
  111. Shared bool
  112. NoCheckout bool
  113. }
  114. // Clone clones original repository to target path.
  115. func Clone(from, to string, opts CloneRepoOptions) (err error) {
  116. toDir := path.Dir(to)
  117. if err = os.MkdirAll(toDir, os.ModePerm); err != nil {
  118. return err
  119. }
  120. cmd := NewCommand("clone")
  121. if opts.Mirror {
  122. cmd.AddArguments("--mirror")
  123. }
  124. if opts.Bare {
  125. cmd.AddArguments("--bare")
  126. }
  127. if opts.Quiet {
  128. cmd.AddArguments("--quiet")
  129. }
  130. if opts.Shared {
  131. cmd.AddArguments("-s")
  132. }
  133. if opts.NoCheckout {
  134. cmd.AddArguments("--no-checkout")
  135. }
  136. if len(opts.Branch) > 0 {
  137. cmd.AddArguments("-b", opts.Branch)
  138. }
  139. cmd.AddArguments("--", from, to)
  140. if opts.Timeout <= 0 {
  141. opts.Timeout = -1
  142. }
  143. _, err = cmd.RunTimeout(opts.Timeout)
  144. return err
  145. }
  146. // PullRemoteOptions options when pull from remote
  147. type PullRemoteOptions struct {
  148. Timeout time.Duration
  149. All bool
  150. Rebase bool
  151. Remote string
  152. Branch string
  153. }
  154. // Pull pulls changes from remotes.
  155. func Pull(repoPath string, opts PullRemoteOptions) error {
  156. cmd := NewCommand("pull")
  157. if opts.Rebase {
  158. cmd.AddArguments("--rebase")
  159. }
  160. if opts.All {
  161. cmd.AddArguments("--all")
  162. } else {
  163. cmd.AddArguments("--", opts.Remote, opts.Branch)
  164. }
  165. if opts.Timeout <= 0 {
  166. opts.Timeout = -1
  167. }
  168. _, err := cmd.RunInDirTimeout(opts.Timeout, repoPath)
  169. return err
  170. }
  171. // PushOptions options when push to remote
  172. type PushOptions struct {
  173. Remote string
  174. Branch string
  175. Force bool
  176. Env []string
  177. }
  178. // Push pushs local commits to given remote branch.
  179. func Push(repoPath string, opts PushOptions) error {
  180. cmd := NewCommand("push")
  181. if opts.Force {
  182. cmd.AddArguments("-f")
  183. }
  184. cmd.AddArguments("--", opts.Remote, opts.Branch)
  185. _, err := cmd.RunInDirWithEnv(repoPath, opts.Env)
  186. return err
  187. }
  188. // CheckoutOptions options when heck out some branch
  189. type CheckoutOptions struct {
  190. Timeout time.Duration
  191. Branch string
  192. OldBranch string
  193. }
  194. // Checkout checkouts a branch
  195. func Checkout(repoPath string, opts CheckoutOptions) error {
  196. cmd := NewCommand("checkout")
  197. if len(opts.OldBranch) > 0 {
  198. cmd.AddArguments("-b")
  199. }
  200. if opts.Timeout <= 0 {
  201. opts.Timeout = -1
  202. }
  203. cmd.AddArguments(opts.Branch)
  204. if len(opts.OldBranch) > 0 {
  205. cmd.AddArguments(opts.OldBranch)
  206. }
  207. _, err := cmd.RunInDirTimeout(opts.Timeout, repoPath)
  208. return err
  209. }
  210. // ResetHEAD resets HEAD to given revision or head of branch.
  211. func ResetHEAD(repoPath string, hard bool, revision string) error {
  212. cmd := NewCommand("reset")
  213. if hard {
  214. cmd.AddArguments("--hard")
  215. }
  216. _, err := cmd.AddArguments(revision).RunInDir(repoPath)
  217. return err
  218. }
  219. // MoveFile moves a file to another file or directory.
  220. func MoveFile(repoPath, oldTreeName, newTreeName string) error {
  221. _, err := NewCommand("mv").AddArguments(oldTreeName, newTreeName).RunInDir(repoPath)
  222. return err
  223. }
  224. // CountObject represents repository count objects report
  225. type CountObject struct {
  226. Count int64
  227. Size int64
  228. InPack int64
  229. Packs int64
  230. SizePack int64
  231. PrunePack int64
  232. Garbage int64
  233. SizeGarbage int64
  234. }
  235. const (
  236. statCount = "count: "
  237. statSize = "size: "
  238. statInpack = "in-pack: "
  239. statPacks = "packs: "
  240. statSizePack = "size-pack: "
  241. statPrunePackage = "prune-package: "
  242. statGarbage = "garbage: "
  243. statSizeGarbage = "size-garbage: "
  244. )
  245. // GetRepoSize returns disk consumption for repo in path
  246. func GetRepoSize(repoPath string) (*CountObject, error) {
  247. cmd := NewCommand("count-objects", "-v")
  248. stdout, err := cmd.RunInDir(repoPath)
  249. if err != nil {
  250. return nil, err
  251. }
  252. return parseSize(stdout), nil
  253. }
  254. // parseSize parses the output from count-objects and return a CountObject
  255. func parseSize(objects string) *CountObject {
  256. repoSize := new(CountObject)
  257. for _, line := range strings.Split(objects, "\n") {
  258. switch {
  259. case strings.HasPrefix(line, statCount):
  260. repoSize.Count = com.StrTo(line[7:]).MustInt64()
  261. case strings.HasPrefix(line, statSize):
  262. repoSize.Size = com.StrTo(line[6:]).MustInt64() * 1024
  263. case strings.HasPrefix(line, statInpack):
  264. repoSize.InPack = com.StrTo(line[9:]).MustInt64()
  265. case strings.HasPrefix(line, statPacks):
  266. repoSize.Packs = com.StrTo(line[7:]).MustInt64()
  267. case strings.HasPrefix(line, statSizePack):
  268. repoSize.SizePack = com.StrTo(line[11:]).MustInt64() * 1024
  269. case strings.HasPrefix(line, statPrunePackage):
  270. repoSize.PrunePack = com.StrTo(line[16:]).MustInt64()
  271. case strings.HasPrefix(line, statGarbage):
  272. repoSize.Garbage = com.StrTo(line[9:]).MustInt64()
  273. case strings.HasPrefix(line, statSizeGarbage):
  274. repoSize.SizeGarbage = com.StrTo(line[14:]).MustInt64() * 1024
  275. }
  276. }
  277. return repoSize
  278. }
  279. // GetLatestCommitTime returns time for latest commit in repository (across all branches)
  280. func GetLatestCommitTime(repoPath string) (time.Time, error) {
  281. cmd := NewCommand("for-each-ref", "--sort=-committerdate", "refs/heads/", "--count", "1", "--format=%(committerdate)")
  282. stdout, err := cmd.RunInDir(repoPath)
  283. if err != nil {
  284. return time.Time{}, err
  285. }
  286. commitTime := strings.TrimSpace(stdout)
  287. return time.Parse(GitTimeLayout, commitTime)
  288. }
  289. // DivergeObject represents commit count diverging commits
  290. type DivergeObject struct {
  291. Ahead int
  292. Behind int
  293. }
  294. func checkDivergence(repoPath string, baseBranch string, targetBranch string) (int, error) {
  295. branches := fmt.Sprintf("%s..%s", baseBranch, targetBranch)
  296. cmd := NewCommand("rev-list", "--count", branches)
  297. stdout, err := cmd.RunInDir(repoPath)
  298. if err != nil {
  299. return -1, err
  300. }
  301. outInteger, errInteger := strconv.Atoi(strings.Trim(stdout, "\n"))
  302. if errInteger != nil {
  303. return -1, errInteger
  304. }
  305. return outInteger, nil
  306. }
  307. // GetDivergingCommits returns the number of commits a targetBranch is ahead or behind a baseBranch
  308. func GetDivergingCommits(repoPath string, baseBranch string, targetBranch string) (DivergeObject, error) {
  309. // $(git rev-list --count master..feature) commits ahead of master
  310. ahead, errorAhead := checkDivergence(repoPath, baseBranch, targetBranch)
  311. if errorAhead != nil {
  312. return DivergeObject{}, errorAhead
  313. }
  314. // $(git rev-list --count feature..master) commits behind master
  315. behind, errorBehind := checkDivergence(repoPath, targetBranch, baseBranch)
  316. if errorBehind != nil {
  317. return DivergeObject{}, errorBehind
  318. }
  319. return DivergeObject{ahead, behind}, nil
  320. }