mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-04 09:03:22 +00:00 
			
		
		
		
	Use the type RefName for all the needed places and fix pull mirror sync bugs (#24634)
This PR replaces all string refName as a type `git.RefName` to make the code more maintainable. Fix #15367 Replaces #23070 It also fixed a bug that tags are not sync because `git remote --prune origin` will not remove local tags if remote removed. We in fact should use `git fetch --prune --tags origin` but not `git remote update origin` to do the sync. Some answer from ChatGPT as ref. > If the git fetch --prune --tags command is not working as expected, there could be a few reasons why. Here are a few things to check: > >Make sure that you have the latest version of Git installed on your system. You can check the version by running git --version in your terminal. If you have an outdated version, try updating Git and see if that resolves the issue. > >Check that your Git repository is properly configured to track the remote repository's tags. You can check this by running git config --get-all remote.origin.fetch and verifying that it includes +refs/tags/*:refs/tags/*. If it does not, you can add it by running git config --add remote.origin.fetch "+refs/tags/*:refs/tags/*". > >Verify that the tags you are trying to prune actually exist on the remote repository. You can do this by running git ls-remote --tags origin to list all the tags on the remote repository. > >Check if any local tags have been created that match the names of tags on the remote repository. If so, these local tags may be preventing the git fetch --prune --tags command from working properly. You can delete local tags using the git tag -d command. --------- Co-authored-by: delvh <dev.lh@web.de>
This commit is contained in:
		@@ -6,6 +6,8 @@ package git
 | 
			
		||||
import (
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/modules/util"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
@@ -13,8 +15,6 @@ const (
 | 
			
		||||
	RemotePrefix = "refs/remotes/"
 | 
			
		||||
	// PullPrefix is the base directory of the pull information of git.
 | 
			
		||||
	PullPrefix = "refs/pull/"
 | 
			
		||||
 | 
			
		||||
	pullLen = len(PullPrefix)
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// refNamePatternInvalid is regular expression with unallowed characters in git reference name
 | 
			
		||||
@@ -53,19 +53,32 @@ func (ref *Reference) Commit() (*Commit, error) {
 | 
			
		||||
	return ref.repo.getCommit(ref.Object)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ShortName returns the short name of the reference
 | 
			
		||||
func (ref *Reference) ShortName() string {
 | 
			
		||||
	return RefName(ref.Name).ShortName()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RefGroup returns the group type of the reference
 | 
			
		||||
func (ref *Reference) RefGroup() string {
 | 
			
		||||
	return RefName(ref.Name).RefGroup()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RefName represents a git reference name
 | 
			
		||||
// 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
 | 
			
		||||
type RefName string
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (ref RefName) IsBranch() bool {
 | 
			
		||||
	return strings.HasPrefix(string(ref), BranchPrefix)
 | 
			
		||||
}
 | 
			
		||||
@@ -74,20 +87,71 @@ func (ref RefName) IsTag() bool {
 | 
			
		||||
	return strings.HasPrefix(string(ref), TagPrefix)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ShortName returns the short name of the reference name
 | 
			
		||||
func (ref RefName) ShortName() string {
 | 
			
		||||
	refName := string(ref)
 | 
			
		||||
	if strings.HasPrefix(refName, BranchPrefix) {
 | 
			
		||||
		return strings.TrimPrefix(refName, BranchPrefix)
 | 
			
		||||
	if ref.IsBranch() {
 | 
			
		||||
		return ref.BranchName()
 | 
			
		||||
	}
 | 
			
		||||
	if strings.HasPrefix(refName, TagPrefix) {
 | 
			
		||||
		return strings.TrimPrefix(refName, TagPrefix)
 | 
			
		||||
	if ref.IsTag() {
 | 
			
		||||
		return ref.TagName()
 | 
			
		||||
	}
 | 
			
		||||
	if strings.HasPrefix(refName, RemotePrefix) {
 | 
			
		||||
		return strings.TrimPrefix(refName, RemotePrefix)
 | 
			
		||||
	if ref.IsRemote() {
 | 
			
		||||
		return ref.RemoteName()
 | 
			
		||||
	}
 | 
			
		||||
	if strings.HasPrefix(refName, PullPrefix) && strings.IndexByte(refName[pullLen:], '/') > -1 {
 | 
			
		||||
		return refName[pullLen : strings.IndexByte(refName[pullLen:], '/')+pullLen]
 | 
			
		||||
	if ref.IsPull() {
 | 
			
		||||
		return ref.PullName()
 | 
			
		||||
	}
 | 
			
		||||
	if ref.IsFor() {
 | 
			
		||||
		return ref.ForBranchName()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return refName
 | 
			
		||||
@@ -95,18 +159,37 @@ func (ref RefName) ShortName() string {
 | 
			
		||||
 | 
			
		||||
// RefGroup returns the group type of the reference
 | 
			
		||||
func (ref RefName) RefGroup() string {
 | 
			
		||||
	refName := string(ref)
 | 
			
		||||
	if strings.HasPrefix(refName, BranchPrefix) {
 | 
			
		||||
	if ref.IsBranch() {
 | 
			
		||||
		return "heads"
 | 
			
		||||
	}
 | 
			
		||||
	if strings.HasPrefix(refName, TagPrefix) {
 | 
			
		||||
	if ref.IsTag() {
 | 
			
		||||
		return "tags"
 | 
			
		||||
	}
 | 
			
		||||
	if strings.HasPrefix(refName, RemotePrefix) {
 | 
			
		||||
	if ref.IsRemote() {
 | 
			
		||||
		return "remotes"
 | 
			
		||||
	}
 | 
			
		||||
	if strings.HasPrefix(refName, PullPrefix) && strings.IndexByte(refName[pullLen:], '/') > -1 {
 | 
			
		||||
	if ref.IsPull() {
 | 
			
		||||
		return "pull"
 | 
			
		||||
	}
 | 
			
		||||
	if ref.IsFor() {
 | 
			
		||||
		return "for"
 | 
			
		||||
	}
 | 
			
		||||
	return ""
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										38
									
								
								modules/git/ref_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								modules/git/ref_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,38 @@
 | 
			
		||||
// Copyright 2020 The Gitea Authors. All rights reserved.
 | 
			
		||||
// SPDX-License-Identifier: MIT
 | 
			
		||||
 | 
			
		||||
package git
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/stretchr/testify/assert"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestRefName(t *testing.T) {
 | 
			
		||||
	// Test branch names (with and without slash).
 | 
			
		||||
	assert.Equal(t, "foo", RefName("refs/heads/foo").BranchName())
 | 
			
		||||
	assert.Equal(t, "feature/foo", RefName("refs/heads/feature/foo").BranchName())
 | 
			
		||||
 | 
			
		||||
	// Test tag names (with and without slash).
 | 
			
		||||
	assert.Equal(t, "foo", RefName("refs/tags/foo").TagName())
 | 
			
		||||
	assert.Equal(t, "release/foo", RefName("refs/tags/release/foo").TagName())
 | 
			
		||||
 | 
			
		||||
	// Test pull names
 | 
			
		||||
	assert.Equal(t, "1", RefName("refs/pull/1/head").PullName())
 | 
			
		||||
	assert.Equal(t, "my/pull", RefName("refs/pull/my/pull/head").PullName())
 | 
			
		||||
 | 
			
		||||
	// Test for branch names
 | 
			
		||||
	assert.Equal(t, "main", RefName("refs/for/main").ForBranchName())
 | 
			
		||||
	assert.Equal(t, "my/branch", RefName("refs/for/my/branch").ForBranchName())
 | 
			
		||||
 | 
			
		||||
	// Test commit hashes.
 | 
			
		||||
	assert.Equal(t, "c0ffee", RefName("c0ffee").ShortName())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestRefURL(t *testing.T) {
 | 
			
		||||
	repoURL := "/user/repo"
 | 
			
		||||
	assert.Equal(t, repoURL+"/src/branch/foo", RefURL(repoURL, "refs/heads/foo"))
 | 
			
		||||
	assert.Equal(t, repoURL+"/src/tag/foo", RefURL(repoURL, "refs/tags/foo"))
 | 
			
		||||
	assert.Equal(t, repoURL+"/src/commit/c0ffee", RefURL(repoURL, "c0ffee"))
 | 
			
		||||
}
 | 
			
		||||
@@ -14,14 +14,6 @@ import (
 | 
			
		||||
// BranchPrefix base dir of the branch information file store on git
 | 
			
		||||
const BranchPrefix = "refs/heads/"
 | 
			
		||||
 | 
			
		||||
// AGit Flow
 | 
			
		||||
 | 
			
		||||
// PullRequestPrefix special ref to create a pull request: refs/for/<targe-branch>/<topic-branch>
 | 
			
		||||
// or refs/for/<targe-branch> -o topic='<topic-branch>'
 | 
			
		||||
const PullRequestPrefix = "refs/for/"
 | 
			
		||||
 | 
			
		||||
// TODO: /refs/for-review for suggest change interface
 | 
			
		||||
 | 
			
		||||
// IsReferenceExist returns true if given reference exists in the repository.
 | 
			
		||||
func IsReferenceExist(ctx context.Context, repoPath, name string) bool {
 | 
			
		||||
	_, _, err := NewCommand(ctx, "show-ref", "--verify").AddDashesAndList(name).RunStdString(&RunOpts{Dir: repoPath})
 | 
			
		||||
 
 | 
			
		||||
@@ -10,8 +10,6 @@ import (
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"sync"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/modules/util"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// ObjectCache provides thread-safe cache operations.
 | 
			
		||||
@@ -78,48 +76,6 @@ func ConcatenateError(err error, stderr string) error {
 | 
			
		||||
	return fmt.Errorf("%w - %s", err, stderr)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RefEndName return the end name of a ref name
 | 
			
		||||
func RefEndName(refStr string) string {
 | 
			
		||||
	if strings.HasPrefix(refStr, BranchPrefix) {
 | 
			
		||||
		return refStr[len(BranchPrefix):]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if strings.HasPrefix(refStr, TagPrefix) {
 | 
			
		||||
		return refStr[len(TagPrefix):]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return refStr
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RefURL returns the absolute URL for a ref in a repository
 | 
			
		||||
func RefURL(repoURL, ref string) string {
 | 
			
		||||
	refName := util.PathEscapeSegments(RefEndName(ref))
 | 
			
		||||
	switch {
 | 
			
		||||
	case strings.HasPrefix(ref, BranchPrefix):
 | 
			
		||||
		return repoURL + "/src/branch/" + refName
 | 
			
		||||
	case strings.HasPrefix(ref, TagPrefix):
 | 
			
		||||
		return repoURL + "/src/tag/" + refName
 | 
			
		||||
	case !IsValidSHAPattern(ref):
 | 
			
		||||
		// assume they mean a branch
 | 
			
		||||
		return repoURL + "/src/branch/" + refName
 | 
			
		||||
	default:
 | 
			
		||||
		return repoURL + "/src/commit/" + refName
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// SplitRefName splits a full refname to reftype and simple refname
 | 
			
		||||
func SplitRefName(refStr string) (string, string) {
 | 
			
		||||
	if strings.HasPrefix(refStr, BranchPrefix) {
 | 
			
		||||
		return BranchPrefix, refStr[len(BranchPrefix):]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if strings.HasPrefix(refStr, TagPrefix) {
 | 
			
		||||
		return TagPrefix, refStr[len(TagPrefix):]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return "", refStr
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ParseBool returns the boolean value represented by the string as per git's git_config_bool
 | 
			
		||||
// true will be returned for the result if the string is empty, but valid will be false.
 | 
			
		||||
// "true", "yes", "on" are all true, true
 | 
			
		||||
 
 | 
			
		||||
@@ -1,30 +0,0 @@
 | 
			
		||||
// Copyright 2020 The Gitea Authors. All rights reserved.
 | 
			
		||||
// SPDX-License-Identifier: MIT
 | 
			
		||||
 | 
			
		||||
package git
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/stretchr/testify/assert"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestRefEndName(t *testing.T) {
 | 
			
		||||
	// Test branch names (with and without slash).
 | 
			
		||||
	assert.Equal(t, "foo", RefEndName("refs/heads/foo"))
 | 
			
		||||
	assert.Equal(t, "feature/foo", RefEndName("refs/heads/feature/foo"))
 | 
			
		||||
 | 
			
		||||
	// Test tag names (with and without slash).
 | 
			
		||||
	assert.Equal(t, "foo", RefEndName("refs/tags/foo"))
 | 
			
		||||
	assert.Equal(t, "release/foo", RefEndName("refs/tags/release/foo"))
 | 
			
		||||
 | 
			
		||||
	// Test commit hashes.
 | 
			
		||||
	assert.Equal(t, "c0ffee", RefEndName("c0ffee"))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestRefURL(t *testing.T) {
 | 
			
		||||
	repoURL := "/user/repo"
 | 
			
		||||
	assert.Equal(t, repoURL+"/src/branch/foo", RefURL(repoURL, "refs/heads/foo"))
 | 
			
		||||
	assert.Equal(t, repoURL+"/src/tag/foo", RefURL(repoURL, "refs/tags/foo"))
 | 
			
		||||
	assert.Equal(t, repoURL+"/src/commit/c0ffee", RefURL(repoURL, "c0ffee"))
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user