2020-10-30 21:59:02 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-10-30 21:59:02 +00:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PushUpdateOptions defines the push update options
|
|
|
|
type PushUpdateOptions struct {
|
|
|
|
PusherID int64
|
|
|
|
PusherName string
|
|
|
|
RepoUserName string
|
|
|
|
RepoName string
|
2023-05-26 01:04:48 +00:00
|
|
|
RefFullName git.RefName // branch, tag or other name to push
|
2020-10-30 21:59:02 +00:00
|
|
|
OldCommitID string
|
|
|
|
NewCommitID string
|
2024-03-31 13:27:59 +00:00
|
|
|
TimeNano int64
|
2020-10-30 21:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsNewRef return true if it's a first-time push to a branch, tag or etc.
|
2021-11-24 09:08:13 +00:00
|
|
|
func (opts *PushUpdateOptions) IsNewRef() bool {
|
2023-12-19 07:20:47 +00:00
|
|
|
commitID, err := git.NewIDFromString(opts.OldCommitID)
|
2023-12-13 21:02:00 +00:00
|
|
|
return err == nil && commitID.IsZero()
|
2020-10-30 21:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsDelRef return true if it's a deletion to a branch or tag
|
2021-11-24 09:08:13 +00:00
|
|
|
func (opts *PushUpdateOptions) IsDelRef() bool {
|
2023-12-19 07:20:47 +00:00
|
|
|
commitID, err := git.NewIDFromString(opts.NewCommitID)
|
2023-12-13 21:02:00 +00:00
|
|
|
return err == nil && commitID.IsZero()
|
2020-10-30 21:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsUpdateRef return true if it's an update operation
|
2021-11-24 09:08:13 +00:00
|
|
|
func (opts *PushUpdateOptions) IsUpdateRef() bool {
|
2020-10-30 21:59:02 +00:00
|
|
|
return !opts.IsNewRef() && !opts.IsDelRef()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNewTag return true if it's a creation to a tag
|
2021-11-24 09:08:13 +00:00
|
|
|
func (opts *PushUpdateOptions) IsNewTag() bool {
|
2023-05-26 01:04:48 +00:00
|
|
|
return opts.RefFullName.IsTag() && opts.IsNewRef()
|
2020-10-30 21:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsDelTag return true if it's a deletion to a tag
|
2021-11-24 09:08:13 +00:00
|
|
|
func (opts *PushUpdateOptions) IsDelTag() bool {
|
2023-05-26 01:04:48 +00:00
|
|
|
return opts.RefFullName.IsTag() && opts.IsDelRef()
|
2020-10-30 21:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsNewBranch return true if it's the first-time push to a branch
|
2021-11-24 09:08:13 +00:00
|
|
|
func (opts *PushUpdateOptions) IsNewBranch() bool {
|
2023-05-26 01:04:48 +00:00
|
|
|
return opts.RefFullName.IsBranch() && opts.IsNewRef()
|
2020-10-30 21:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsUpdateBranch return true if it's not the first push to a branch
|
2021-11-24 09:08:13 +00:00
|
|
|
func (opts *PushUpdateOptions) IsUpdateBranch() bool {
|
2023-05-26 01:04:48 +00:00
|
|
|
return opts.RefFullName.IsBranch() && opts.IsUpdateRef()
|
2020-10-30 21:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsDelBranch return true if it's a deletion to a branch
|
2021-11-24 09:08:13 +00:00
|
|
|
func (opts *PushUpdateOptions) IsDelBranch() bool {
|
2023-05-26 01:04:48 +00:00
|
|
|
return opts.RefFullName.IsBranch() && opts.IsDelRef()
|
2020-10-30 21:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RefName returns simple name for ref
|
2021-11-24 09:08:13 +00:00
|
|
|
func (opts *PushUpdateOptions) RefName() string {
|
2023-05-26 01:04:48 +00:00
|
|
|
return opts.RefFullName.ShortName()
|
2020-10-30 21:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RepoFullName returns repo full name
|
2021-11-24 09:08:13 +00:00
|
|
|
func (opts *PushUpdateOptions) RepoFullName() string {
|
2020-10-30 21:59:02 +00:00
|
|
|
return opts.RepoUserName + "/" + opts.RepoName
|
|
|
|
}
|