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