2018-11-28 00:52:20 +03:00
// Copyright 2018 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2018-11-28 00:52:20 +03:00
package git
2022-09-04 13:47:56 +03:00
import (
"regexp"
"strings"
2023-05-26 04:04:48 +03:00
"code.gitea.io/gitea/modules/util"
2022-09-04 13:47:56 +03:00
)
2020-11-08 20:21:54 +03:00
2021-12-02 10:28:08 +03:00
const (
// RemotePrefix is the base directory of the remotes information of git.
RemotePrefix = "refs/remotes/"
// PullPrefix is the base directory of the pull information of git.
PullPrefix = "refs/pull/"
)
2022-09-04 13:47:56 +03:00
// refNamePatternInvalid is regular expression with unallowed characters in git reference name
// They cannot have ASCII control characters (i.e. bytes whose values are lower than \040, or \177 DEL), space, tilde ~, caret ^, or colon : anywhere.
// They cannot have question-mark ?, asterisk *, or open bracket [ anywhere
var refNamePatternInvalid = regexp . MustCompile (
` [\000-\037\177 \\~^:?*[]| ` + // No absolutely invalid characters
` (?:^[/.])| ` + // Not HasPrefix("/") or "."
` (?:/\.)| ` + // no "/."
` (?:\.lock$)|(?:\.lock/)| ` + // No ".lock/"" or ".lock" at the end
` (?:\.\.)| ` + // no ".." anywhere
` (?://)| ` + // no "//" anywhere
` (?:@ { )| ` + // no "@{"
` (?:[/.]$)| ` + // no terminal '/' or '.'
` (?:^@$) ` ) // Not "@"
// IsValidRefPattern ensures that the provided string could be a valid reference
func IsValidRefPattern ( name string ) bool {
return ! refNamePatternInvalid . MatchString ( name )
}
func SanitizeRefPattern ( name string ) string {
return refNamePatternInvalid . ReplaceAllString ( name , "_" )
}
2018-11-28 00:52:20 +03:00
// Reference represents a Git ref.
type Reference struct {
Name string
repo * Repository
Object SHA1 // The id of this commit object
Type string
}
// Commit return the commit of the reference
func ( ref * Reference ) Commit ( ) ( * Commit , error ) {
return ref . repo . getCommit ( ref . Object )
}
2020-11-08 20:21:54 +03:00
2022-12-01 14:56:04 +03:00
// RefGroup returns the group type of the reference
func ( ref * Reference ) RefGroup ( ) string {
return RefName ( ref . Name ) . RefGroup ( )
}
2023-05-26 04:04:48 +03:00
// ForPrefix special ref to create a pull request: refs/for/<target-branch>/<topic-branch>
// or refs/for/<targe-branch> -o topic='<topic-branch>'
const ForPrefix = "refs/for/"
// TODO: /refs/for-review for suggest change interface
// RefName represents a full git reference name
2022-12-01 14:56:04 +03:00
type RefName string
2023-05-26 04:04:48 +03:00
func RefNameFromBranch ( shortName string ) RefName {
return RefName ( BranchPrefix + shortName )
}
func RefNameFromTag ( shortName string ) RefName {
return RefName ( TagPrefix + shortName )
}
func ( ref RefName ) String ( ) string {
return string ( ref )
}
2022-12-01 14:56:04 +03:00
func ( ref RefName ) IsBranch ( ) bool {
return strings . HasPrefix ( string ( ref ) , BranchPrefix )
}
func ( ref RefName ) IsTag ( ) bool {
return strings . HasPrefix ( string ( ref ) , TagPrefix )
}
2023-05-26 04:04:48 +03:00
func ( ref RefName ) IsRemote ( ) bool {
return strings . HasPrefix ( string ( ref ) , RemotePrefix )
}
func ( ref RefName ) IsPull ( ) bool {
return strings . HasPrefix ( string ( ref ) , PullPrefix ) && strings . IndexByte ( string ( ref ) [ len ( PullPrefix ) : ] , '/' ) > - 1
}
func ( ref RefName ) IsFor ( ) bool {
return strings . HasPrefix ( string ( ref ) , ForPrefix )
}
func ( ref RefName ) nameWithoutPrefix ( prefix string ) string {
if strings . HasPrefix ( string ( ref ) , prefix ) {
return strings . TrimPrefix ( string ( ref ) , prefix )
}
return ""
}
// TagName returns simple tag name if it's an operation to a tag
func ( ref RefName ) TagName ( ) string {
return ref . nameWithoutPrefix ( TagPrefix )
}
// BranchName returns simple branch name if it's an operation to branch
func ( ref RefName ) BranchName ( ) string {
return ref . nameWithoutPrefix ( BranchPrefix )
}
// PullName returns the pull request name part of refs like refs/pull/<pull_name>/head
func ( ref RefName ) PullName ( ) string {
refName := string ( ref )
lastIdx := strings . LastIndexByte ( refName [ len ( PullPrefix ) : ] , '/' )
if strings . HasPrefix ( refName , PullPrefix ) && lastIdx > - 1 {
return refName [ len ( PullPrefix ) : lastIdx + len ( PullPrefix ) ]
}
return ""
}
// ForBranchName returns the branch name part of refs like refs/for/<branch_name>
func ( ref RefName ) ForBranchName ( ) string {
return ref . nameWithoutPrefix ( ForPrefix )
}
func ( ref RefName ) RemoteName ( ) string {
return ref . nameWithoutPrefix ( RemotePrefix )
}
2022-12-01 14:56:04 +03:00
// ShortName returns the short name of the reference name
func ( ref RefName ) ShortName ( ) string {
refName := string ( ref )
2023-05-26 04:04:48 +03:00
if ref . IsBranch ( ) {
return ref . BranchName ( )
}
if ref . IsTag ( ) {
return ref . TagName ( )
2020-11-08 20:21:54 +03:00
}
2023-05-26 04:04:48 +03:00
if ref . IsRemote ( ) {
return ref . RemoteName ( )
2020-11-08 20:21:54 +03:00
}
2023-05-26 04:04:48 +03:00
if ref . IsPull ( ) {
return ref . PullName ( )
2020-11-08 20:21:54 +03:00
}
2023-05-26 04:04:48 +03:00
if ref . IsFor ( ) {
return ref . ForBranchName ( )
2020-11-08 20:21:54 +03:00
}
2022-12-01 14:56:04 +03:00
return refName
2020-11-08 20:21:54 +03:00
}
// RefGroup returns the group type of the reference
2022-12-01 14:56:04 +03:00
func ( ref RefName ) RefGroup ( ) string {
2023-05-26 04:04:48 +03:00
if ref . IsBranch ( ) {
2020-11-08 20:21:54 +03:00
return "heads"
}
2023-05-26 04:04:48 +03:00
if ref . IsTag ( ) {
2020-11-08 20:21:54 +03:00
return "tags"
}
2023-05-26 04:04:48 +03:00
if ref . IsRemote ( ) {
2020-11-08 20:21:54 +03:00
return "remotes"
}
2023-05-26 04:04:48 +03:00
if ref . IsPull ( ) {
2020-11-08 20:21:54 +03:00
return "pull"
}
2023-05-26 04:04:48 +03:00
if ref . IsFor ( ) {
return "for"
}
2020-11-08 20:21:54 +03:00
return ""
}
2023-05-26 04:04:48 +03:00
// RefURL returns the absolute URL for a ref in a repository
func RefURL ( repoURL , ref string ) string {
refFullName := RefName ( ref )
refName := util . PathEscapeSegments ( refFullName . ShortName ( ) )
switch {
case refFullName . IsBranch ( ) :
return repoURL + "/src/branch/" + refName
case refFullName . IsTag ( ) :
return repoURL + "/src/tag/" + refName
case ! IsValidSHAPattern ( ref ) :
// assume they mean a branch
return repoURL + "/src/branch/" + refName
default :
return repoURL + "/src/commit/" + refName
}
}