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

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