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.5KB

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