mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-25 00:46:54 +02:00
Bump go-git
This commit is contained in:
124
vendor/github.com/jesseduffield/go-git/v5/plumbing/reference.go
generated
vendored
124
vendor/github.com/jesseduffield/go-git/v5/plumbing/reference.go
generated
vendored
@ -3,6 +3,7 @@ package plumbing
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -15,10 +16,11 @@ const (
|
||||
symrefPrefix = "ref: "
|
||||
)
|
||||
|
||||
// RefRevParseRules are a set of rules to parse references into short names.
|
||||
// These are the same rules as used by git in shorten_unambiguous_ref.
|
||||
// RefRevParseRules are a set of rules to parse references into short names, or expand into a full reference.
|
||||
// These are the same rules as used by git in shorten_unambiguous_ref and expand_ref.
|
||||
// See: https://github.com/git/git/blob/e0aaa1b6532cfce93d87af9bc813fb2e7a7ce9d7/refs.c#L417
|
||||
var RefRevParseRules = []string{
|
||||
"%s",
|
||||
"refs/%s",
|
||||
"refs/tags/%s",
|
||||
"refs/heads/%s",
|
||||
@ -28,6 +30,9 @@ var RefRevParseRules = []string{
|
||||
|
||||
var (
|
||||
ErrReferenceNotFound = errors.New("reference not found")
|
||||
|
||||
// ErrInvalidReferenceName is returned when a reference name is invalid.
|
||||
ErrInvalidReferenceName = errors.New("invalid reference name")
|
||||
)
|
||||
|
||||
// ReferenceType reference type's
|
||||
@ -113,7 +118,7 @@ func (r ReferenceName) String() string {
|
||||
func (r ReferenceName) Short() string {
|
||||
s := string(r)
|
||||
res := s
|
||||
for _, format := range RefRevParseRules {
|
||||
for _, format := range RefRevParseRules[1:] {
|
||||
_, err := fmt.Sscanf(s, format, &res)
|
||||
if err == nil {
|
||||
continue
|
||||
@ -123,9 +128,95 @@ func (r ReferenceName) Short() string {
|
||||
return res
|
||||
}
|
||||
|
||||
var (
|
||||
ctrlSeqs = regexp.MustCompile(`[\000-\037\177]`)
|
||||
)
|
||||
|
||||
// Validate validates a reference name.
|
||||
// This follows the git-check-ref-format rules.
|
||||
// See https://git-scm.com/docs/git-check-ref-format
|
||||
//
|
||||
// It is important to note that this function does not check if the reference
|
||||
// exists in the repository.
|
||||
// It only checks if the reference name is valid.
|
||||
// This functions does not support the --refspec-pattern, --normalize, and
|
||||
// --allow-onelevel options.
|
||||
//
|
||||
// Git imposes the following rules on how references are named:
|
||||
//
|
||||
// 1. They can include slash / for hierarchical (directory) grouping, but no
|
||||
// slash-separated component can begin with a dot . or end with the
|
||||
// sequence .lock.
|
||||
// 2. They must contain at least one /. This enforces the presence of a
|
||||
// category like heads/, tags/ etc. but the actual names are not
|
||||
// restricted. If the --allow-onelevel option is used, this rule is
|
||||
// waived.
|
||||
// 3. They cannot have two consecutive dots .. anywhere.
|
||||
// 4. They cannot have ASCII control characters (i.e. bytes whose values are
|
||||
// lower than \040, or \177 DEL), space, tilde ~, caret ^, or colon :
|
||||
// anywhere.
|
||||
// 5. They cannot have question-mark ?, asterisk *, or open bracket [
|
||||
// anywhere. See the --refspec-pattern option below for an exception to this
|
||||
// rule.
|
||||
// 6. They cannot begin or end with a slash / or contain multiple consecutive
|
||||
// slashes (see the --normalize option below for an exception to this rule).
|
||||
// 7. They cannot end with a dot ..
|
||||
// 8. They cannot contain a sequence @{.
|
||||
// 9. They cannot be the single character @.
|
||||
// 10. They cannot contain a \.
|
||||
func (r ReferenceName) Validate() error {
|
||||
s := string(r)
|
||||
if len(s) == 0 {
|
||||
return ErrInvalidReferenceName
|
||||
}
|
||||
|
||||
// HEAD is a special case
|
||||
if r == HEAD {
|
||||
return nil
|
||||
}
|
||||
|
||||
// rule 7
|
||||
if strings.HasSuffix(s, ".") {
|
||||
return ErrInvalidReferenceName
|
||||
}
|
||||
|
||||
// rule 2
|
||||
parts := strings.Split(s, "/")
|
||||
if len(parts) < 2 {
|
||||
return ErrInvalidReferenceName
|
||||
}
|
||||
|
||||
isBranch := r.IsBranch()
|
||||
isTag := r.IsTag()
|
||||
for i, part := range parts {
|
||||
// rule 6
|
||||
if len(part) == 0 {
|
||||
return ErrInvalidReferenceName
|
||||
}
|
||||
|
||||
if strings.HasPrefix(part, ".") || // rule 1
|
||||
strings.Contains(part, "..") || // rule 3
|
||||
ctrlSeqs.MatchString(part) || // rule 4
|
||||
strings.ContainsAny(part, "~^:?*[ \t\n") || // rule 4 & 5
|
||||
strings.Contains(part, "@{") || // rule 8
|
||||
part == "@" || // rule 9
|
||||
strings.Contains(part, "\\") || // rule 10
|
||||
strings.HasSuffix(part, ".lock") { // rule 1
|
||||
return ErrInvalidReferenceName
|
||||
}
|
||||
|
||||
if (isBranch || isTag) && strings.HasPrefix(part, "-") && (i == 2) { // branches & tags can't start with -
|
||||
return ErrInvalidReferenceName
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
const (
|
||||
HEAD ReferenceName = "HEAD"
|
||||
Master ReferenceName = "refs/heads/master"
|
||||
Main ReferenceName = "refs/heads/main"
|
||||
)
|
||||
|
||||
// Reference is a representation of git reference
|
||||
@ -168,22 +259,22 @@ func NewHashReference(n ReferenceName, h Hash) *Reference {
|
||||
}
|
||||
}
|
||||
|
||||
// Type return the type of a reference
|
||||
// Type returns the type of a reference
|
||||
func (r *Reference) Type() ReferenceType {
|
||||
return r.t
|
||||
}
|
||||
|
||||
// Name return the name of a reference
|
||||
// Name returns the name of a reference
|
||||
func (r *Reference) Name() ReferenceName {
|
||||
return r.n
|
||||
}
|
||||
|
||||
// Hash return the hash of a hash reference
|
||||
// Hash returns the hash of a hash reference
|
||||
func (r *Reference) Hash() Hash {
|
||||
return r.h
|
||||
}
|
||||
|
||||
// Target return the target of a symbolic reference
|
||||
// Target returns the target of a symbolic reference
|
||||
func (r *Reference) Target() ReferenceName {
|
||||
return r.target
|
||||
}
|
||||
@ -204,6 +295,21 @@ func (r *Reference) Strings() [2]string {
|
||||
}
|
||||
|
||||
func (r *Reference) String() string {
|
||||
s := r.Strings()
|
||||
return fmt.Sprintf("%s %s", s[1], s[0])
|
||||
ref := ""
|
||||
switch r.Type() {
|
||||
case HashReference:
|
||||
ref = r.Hash().String()
|
||||
case SymbolicReference:
|
||||
ref = symrefPrefix + r.Target().String()
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
|
||||
name := r.Name().String()
|
||||
var v strings.Builder
|
||||
v.Grow(len(ref) + len(name) + 1)
|
||||
v.WriteString(ref)
|
||||
v.WriteString(" ")
|
||||
v.WriteString(name)
|
||||
return v.String()
|
||||
}
|
||||
|
Reference in New Issue
Block a user