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.

push.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2020 The Gitea 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 repository
  5. import (
  6. "context"
  7. "strings"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. "code.gitea.io/gitea/modules/git"
  10. )
  11. // PushUpdateOptions defines the push update options
  12. type PushUpdateOptions struct {
  13. PusherID int64
  14. PusherName string
  15. RepoUserName string
  16. RepoName string
  17. RefFullName string // branch, tag or other name to push
  18. OldCommitID string
  19. NewCommitID string
  20. }
  21. // IsNewRef return true if it's a first-time push to a branch, tag or etc.
  22. func (opts *PushUpdateOptions) IsNewRef() bool {
  23. return opts.OldCommitID == git.EmptySHA
  24. }
  25. // IsDelRef return true if it's a deletion to a branch or tag
  26. func (opts *PushUpdateOptions) IsDelRef() bool {
  27. return opts.NewCommitID == git.EmptySHA
  28. }
  29. // IsUpdateRef return true if it's an update operation
  30. func (opts *PushUpdateOptions) IsUpdateRef() bool {
  31. return !opts.IsNewRef() && !opts.IsDelRef()
  32. }
  33. // IsTag return true if it's an operation to a tag
  34. func (opts *PushUpdateOptions) IsTag() bool {
  35. return strings.HasPrefix(opts.RefFullName, git.TagPrefix)
  36. }
  37. // IsNewTag return true if it's a creation to a tag
  38. func (opts *PushUpdateOptions) IsNewTag() bool {
  39. return opts.IsTag() && opts.IsNewRef()
  40. }
  41. // IsDelTag return true if it's a deletion to a tag
  42. func (opts *PushUpdateOptions) IsDelTag() bool {
  43. return opts.IsTag() && opts.IsDelRef()
  44. }
  45. // IsBranch return true if it's a push to branch
  46. func (opts *PushUpdateOptions) IsBranch() bool {
  47. return strings.HasPrefix(opts.RefFullName, git.BranchPrefix)
  48. }
  49. // IsNewBranch return true if it's the first-time push to a branch
  50. func (opts *PushUpdateOptions) IsNewBranch() bool {
  51. return opts.IsBranch() && opts.IsNewRef()
  52. }
  53. // IsUpdateBranch return true if it's not the first push to a branch
  54. func (opts *PushUpdateOptions) IsUpdateBranch() bool {
  55. return opts.IsBranch() && opts.IsUpdateRef()
  56. }
  57. // IsDelBranch return true if it's a deletion to a branch
  58. func (opts *PushUpdateOptions) IsDelBranch() bool {
  59. return opts.IsBranch() && opts.IsDelRef()
  60. }
  61. // TagName returns simple tag name if it's an operation to a tag
  62. func (opts *PushUpdateOptions) TagName() string {
  63. return opts.RefFullName[len(git.TagPrefix):]
  64. }
  65. // BranchName returns simple branch name if it's an operation to branch
  66. func (opts *PushUpdateOptions) BranchName() string {
  67. return opts.RefFullName[len(git.BranchPrefix):]
  68. }
  69. // RefName returns simple name for ref
  70. func (opts *PushUpdateOptions) RefName() string {
  71. if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
  72. return opts.RefFullName[len(git.TagPrefix):]
  73. } else if strings.HasPrefix(opts.RefFullName, git.BranchPrefix) {
  74. return opts.RefFullName[len(git.BranchPrefix):]
  75. }
  76. return ""
  77. }
  78. // RepoFullName returns repo full name
  79. func (opts *PushUpdateOptions) RepoFullName() string {
  80. return opts.RepoUserName + "/" + opts.RepoName
  81. }
  82. // IsForcePush detect if a push is a force push
  83. func IsForcePush(ctx context.Context, opts *PushUpdateOptions) (bool, error) {
  84. if !opts.IsUpdateBranch() {
  85. return false, nil
  86. }
  87. output, _, err := git.NewCommand(ctx, "rev-list", "--max-count=1", opts.OldCommitID, "^"+opts.NewCommitID).
  88. RunStdString(&git.RunOpts{Dir: repo_model.RepoPath(opts.RepoUserName, opts.RepoName)})
  89. if err != nil {
  90. return false, err
  91. } else if len(output) > 0 {
  92. return true, nil
  93. }
  94. return false, nil
  95. }