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 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package git
  5. import (
  6. "bytes"
  7. "context"
  8. "fmt"
  9. "io"
  10. "net/url"
  11. "os"
  12. "path"
  13. "path/filepath"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "code.gitea.io/gitea/modules/proxy"
  18. "code.gitea.io/gitea/modules/util"
  19. )
  20. // GPGSettings represents the default GPG settings for this repository
  21. type GPGSettings struct {
  22. Sign bool
  23. KeyID string
  24. Email string
  25. Name string
  26. PublicKeyContent string
  27. }
  28. const prettyLogFormat = `--pretty=format:%H`
  29. // GetAllCommitsCount returns count of all commits in repository
  30. func (repo *Repository) GetAllCommitsCount() (int64, error) {
  31. return AllCommitsCount(repo.Ctx, repo.Path, false)
  32. }
  33. func (repo *Repository) parsePrettyFormatLogToList(logs []byte) ([]*Commit, error) {
  34. var commits []*Commit
  35. if len(logs) == 0 {
  36. return commits, nil
  37. }
  38. parts := bytes.Split(logs, []byte{'\n'})
  39. for _, commitID := range parts {
  40. commit, err := repo.GetCommit(string(commitID))
  41. if err != nil {
  42. return nil, err
  43. }
  44. commits = append(commits, commit)
  45. }
  46. return commits, nil
  47. }
  48. // IsRepoURLAccessible checks if given repository URL is accessible.
  49. func IsRepoURLAccessible(ctx context.Context, url string) bool {
  50. _, _, err := NewCommand(ctx, "ls-remote", "-q", "-h").AddDynamicArguments(url, "HEAD").RunStdString(nil)
  51. return err == nil
  52. }
  53. // InitRepository initializes a new Git repository.
  54. func InitRepository(ctx context.Context, repoPath string, bare bool) error {
  55. err := os.MkdirAll(repoPath, os.ModePerm)
  56. if err != nil {
  57. return err
  58. }
  59. cmd := NewCommand(ctx, "init")
  60. if bare {
  61. cmd.AddArguments("--bare")
  62. }
  63. _, _, err = cmd.RunStdString(&RunOpts{Dir: repoPath})
  64. return err
  65. }
  66. // IsEmpty Check if repository is empty.
  67. func (repo *Repository) IsEmpty() (bool, error) {
  68. var errbuf, output strings.Builder
  69. if err := NewCommand(repo.Ctx).AddOptionFormat("--git-dir=%s", repo.Path).AddArguments("rev-list", "-n", "1", "--all").
  70. Run(&RunOpts{
  71. Dir: repo.Path,
  72. Stdout: &output,
  73. Stderr: &errbuf,
  74. }); err != nil {
  75. if (err.Error() == "exit status 1" && strings.TrimSpace(errbuf.String()) == "") || err.Error() == "exit status 129" {
  76. // git 2.11 exits with 129 if the repo is empty
  77. return true, nil
  78. }
  79. return true, fmt.Errorf("check empty: %w - %s", err, errbuf.String())
  80. }
  81. return strings.TrimSpace(output.String()) == "", nil
  82. }
  83. // CloneRepoOptions options when clone a repository
  84. type CloneRepoOptions struct {
  85. Timeout time.Duration
  86. Mirror bool
  87. Bare bool
  88. Quiet bool
  89. Branch string
  90. Shared bool
  91. NoCheckout bool
  92. Depth int
  93. Filter string
  94. SkipTLSVerify bool
  95. }
  96. // Clone clones original repository to target path.
  97. func Clone(ctx context.Context, from, to string, opts CloneRepoOptions) error {
  98. return CloneWithArgs(ctx, globalCommandArgs, from, to, opts)
  99. }
  100. // CloneWithArgs original repository to target path.
  101. func CloneWithArgs(ctx context.Context, args TrustedCmdArgs, from, to string, opts CloneRepoOptions) (err error) {
  102. toDir := path.Dir(to)
  103. if err = os.MkdirAll(toDir, os.ModePerm); err != nil {
  104. return err
  105. }
  106. cmd := NewCommandContextNoGlobals(ctx, args...).AddArguments("clone")
  107. if opts.SkipTLSVerify {
  108. cmd.AddArguments("-c", "http.sslVerify=false")
  109. }
  110. if opts.Mirror {
  111. cmd.AddArguments("--mirror")
  112. }
  113. if opts.Bare {
  114. cmd.AddArguments("--bare")
  115. }
  116. if opts.Quiet {
  117. cmd.AddArguments("--quiet")
  118. }
  119. if opts.Shared {
  120. cmd.AddArguments("-s")
  121. }
  122. if opts.NoCheckout {
  123. cmd.AddArguments("--no-checkout")
  124. }
  125. if opts.Depth > 0 {
  126. cmd.AddArguments("--depth").AddDynamicArguments(strconv.Itoa(opts.Depth))
  127. }
  128. if opts.Filter != "" {
  129. cmd.AddArguments("--filter").AddDynamicArguments(opts.Filter)
  130. }
  131. if len(opts.Branch) > 0 {
  132. cmd.AddArguments("-b").AddDynamicArguments(opts.Branch)
  133. }
  134. cmd.AddDashesAndList(from, to)
  135. if strings.Contains(from, "://") && strings.Contains(from, "@") {
  136. cmd.SetDescription(fmt.Sprintf("clone branch %s from %s to %s (shared: %t, mirror: %t, depth: %d)", opts.Branch, util.SanitizeCredentialURLs(from), to, opts.Shared, opts.Mirror, opts.Depth))
  137. } else {
  138. cmd.SetDescription(fmt.Sprintf("clone branch %s from %s to %s (shared: %t, mirror: %t, depth: %d)", opts.Branch, from, to, opts.Shared, opts.Mirror, opts.Depth))
  139. }
  140. if opts.Timeout <= 0 {
  141. opts.Timeout = -1
  142. }
  143. envs := os.Environ()
  144. u, err := url.Parse(from)
  145. if err == nil {
  146. envs = proxy.EnvWithProxy(u)
  147. }
  148. stderr := new(bytes.Buffer)
  149. if err = cmd.Run(&RunOpts{
  150. Timeout: opts.Timeout,
  151. Env: envs,
  152. Stdout: io.Discard,
  153. Stderr: stderr,
  154. }); err != nil {
  155. return ConcatenateError(err, stderr.String())
  156. }
  157. return nil
  158. }
  159. // PushOptions options when push to remote
  160. type PushOptions struct {
  161. Remote string
  162. Branch string
  163. Force bool
  164. Mirror bool
  165. Env []string
  166. Timeout time.Duration
  167. }
  168. // Push pushs local commits to given remote branch.
  169. func Push(ctx context.Context, repoPath string, opts PushOptions) error {
  170. cmd := NewCommand(ctx, "push")
  171. if opts.Force {
  172. cmd.AddArguments("-f")
  173. }
  174. if opts.Mirror {
  175. cmd.AddArguments("--mirror")
  176. }
  177. remoteBranchArgs := []string{opts.Remote}
  178. if len(opts.Branch) > 0 {
  179. remoteBranchArgs = append(remoteBranchArgs, opts.Branch)
  180. }
  181. cmd.AddDashesAndList(remoteBranchArgs...)
  182. if strings.Contains(opts.Remote, "://") && strings.Contains(opts.Remote, "@") {
  183. cmd.SetDescription(fmt.Sprintf("push branch %s to %s (force: %t, mirror: %t)", opts.Branch, util.SanitizeCredentialURLs(opts.Remote), opts.Force, opts.Mirror))
  184. } else {
  185. cmd.SetDescription(fmt.Sprintf("push branch %s to %s (force: %t, mirror: %t)", opts.Branch, opts.Remote, opts.Force, opts.Mirror))
  186. }
  187. stdout, stderr, err := cmd.RunStdString(&RunOpts{Env: opts.Env, Timeout: opts.Timeout, Dir: repoPath})
  188. if err != nil {
  189. if strings.Contains(stderr, "non-fast-forward") {
  190. return &ErrPushOutOfDate{StdOut: stdout, StdErr: stderr, Err: err}
  191. } else if strings.Contains(stderr, "! [remote rejected]") {
  192. err := &ErrPushRejected{StdOut: stdout, StdErr: stderr, Err: err}
  193. err.GenerateMessage()
  194. return err
  195. } else if strings.Contains(stderr, "matches more than one") {
  196. return &ErrMoreThanOne{StdOut: stdout, StdErr: stderr, Err: err}
  197. }
  198. return fmt.Errorf("push failed: %w - %s\n%s", err, stderr, stdout)
  199. }
  200. return nil
  201. }
  202. // GetLatestCommitTime returns time for latest commit in repository (across all branches)
  203. func GetLatestCommitTime(ctx context.Context, repoPath string) (time.Time, error) {
  204. cmd := NewCommand(ctx, "for-each-ref", "--sort=-committerdate", BranchPrefix, "--count", "1", "--format=%(committerdate)")
  205. stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repoPath})
  206. if err != nil {
  207. return time.Time{}, err
  208. }
  209. commitTime := strings.TrimSpace(stdout)
  210. return time.Parse(GitTimeLayout, commitTime)
  211. }
  212. // DivergeObject represents commit count diverging commits
  213. type DivergeObject struct {
  214. Ahead int
  215. Behind int
  216. }
  217. // GetDivergingCommits returns the number of commits a targetBranch is ahead or behind a baseBranch
  218. func GetDivergingCommits(ctx context.Context, repoPath, baseBranch, targetBranch string) (do DivergeObject, err error) {
  219. cmd := NewCommand(ctx, "rev-list", "--count", "--left-right").
  220. AddDynamicArguments(baseBranch + "..." + targetBranch)
  221. stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repoPath})
  222. if err != nil {
  223. return do, err
  224. }
  225. left, right, found := strings.Cut(strings.Trim(stdout, "\n"), "\t")
  226. if !found {
  227. return do, fmt.Errorf("git rev-list output is missing a tab: %q", stdout)
  228. }
  229. do.Behind, err = strconv.Atoi(left)
  230. if err != nil {
  231. return do, err
  232. }
  233. do.Ahead, err = strconv.Atoi(right)
  234. if err != nil {
  235. return do, err
  236. }
  237. return do, nil
  238. }
  239. // CreateBundle create bundle content to the target path
  240. func (repo *Repository) CreateBundle(ctx context.Context, commit string, out io.Writer) error {
  241. tmp, err := os.MkdirTemp(os.TempDir(), "gitea-bundle")
  242. if err != nil {
  243. return err
  244. }
  245. defer os.RemoveAll(tmp)
  246. env := append(os.Environ(), "GIT_OBJECT_DIRECTORY="+filepath.Join(repo.Path, "objects"))
  247. _, _, err = NewCommand(ctx, "init", "--bare").RunStdString(&RunOpts{Dir: tmp, Env: env})
  248. if err != nil {
  249. return err
  250. }
  251. _, _, err = NewCommand(ctx, "reset", "--soft").AddDynamicArguments(commit).RunStdString(&RunOpts{Dir: tmp, Env: env})
  252. if err != nil {
  253. return err
  254. }
  255. _, _, err = NewCommand(ctx, "branch", "-m", "bundle").RunStdString(&RunOpts{Dir: tmp, Env: env})
  256. if err != nil {
  257. return err
  258. }
  259. tmpFile := filepath.Join(tmp, "bundle")
  260. _, _, err = NewCommand(ctx, "bundle", "create").AddDynamicArguments(tmpFile, "bundle", "HEAD").RunStdString(&RunOpts{Dir: tmp, Env: env})
  261. if err != nil {
  262. return err
  263. }
  264. fi, err := os.Open(tmpFile)
  265. if err != nil {
  266. return err
  267. }
  268. defer fi.Close()
  269. _, err = io.Copy(out, fi)
  270. return err
  271. }