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:
Lunny Xiao 2023-05-26 09:04:48 +08:00 committed by GitHub
parent 26fa94bc25
commit f9cfd6ce5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
41 changed files with 415 additions and 355 deletions

View file

@ -7,7 +7,6 @@ import (
"fmt"
"net/http"
"strconv"
"strings"
issues_model "code.gitea.io/gitea/models/issues"
repo_model "code.gitea.io/gitea/models/repo"
@ -47,7 +46,7 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
// tags. Updates to other refs (eg, refs/notes, refs/changes,
// or other less-standard refs spaces are ignored since there
// may be a very large number of them).
if strings.HasPrefix(refFullName, git.BranchPrefix) || strings.HasPrefix(refFullName, git.TagPrefix) {
if refFullName.IsBranch() || refFullName.IsTag() {
if repo == nil {
repo = loadRepository(ctx, ownerName, repoName)
if ctx.Written() {
@ -67,7 +66,7 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
RepoName: repoName,
}
updates = append(updates, option)
if repo.IsEmpty && option.IsBranch() && (option.BranchName() == "master" || option.BranchName() == "main") {
if repo.IsEmpty && (refFullName.BranchName() == "master" || refFullName.BranchName() == "main") {
// put the master/main branch first
copy(updates[1:], updates)
updates[0] = option
@ -79,7 +78,7 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
if err := repo_service.PushUpdates(updates); err != nil {
log.Error("Failed to Update: %s/%s Total Updates: %d", ownerName, repoName, len(updates))
for i, update := range updates {
log.Error("Failed to Update: %s/%s Update: %d/%d: Branch: %s", ownerName, repoName, i, len(updates), update.BranchName())
log.Error("Failed to Update: %s/%s Update: %d/%d: Branch: %s", ownerName, repoName, i, len(updates), update.RefFullName.BranchName())
}
log.Error("Failed to Update: %s/%s Error: %v", ownerName, repoName, err)
@ -124,7 +123,8 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
newCommitID := opts.NewCommitIDs[i]
// post update for agit pull request
if git.SupportProcReceive && strings.HasPrefix(refFullName, git.PullPrefix) {
// FIXME: use pr.Flow to test whether it's an Agit PR or a GH PR
if git.SupportProcReceive && refFullName.IsPull() {
if repo == nil {
repo = loadRepository(ctx, ownerName, repoName)
if ctx.Written() {
@ -132,9 +132,7 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
}
}
pullIndexStr := strings.TrimPrefix(refFullName, git.PullPrefix)
pullIndexStr = strings.Split(pullIndexStr, "/")[0]
pullIndex, _ := strconv.ParseInt(pullIndexStr, 10, 64)
pullIndex, _ := strconv.ParseInt(refFullName.PullName(), 10, 64)
if pullIndex <= 0 {
continue
}
@ -160,10 +158,8 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
continue
}
branch := git.RefEndName(opts.RefFullNames[i])
// If we've pushed a branch (and not deleted it)
if newCommitID != git.EmptySHA && strings.HasPrefix(refFullName, git.BranchPrefix) {
if newCommitID != git.EmptySHA && refFullName.IsBranch() {
// First ensure we have the repository loaded, we're allowed pulls requests and we can get the base repo
if repo == nil {
@ -197,6 +193,8 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
}
}
branch := refFullName.BranchName()
// If our branch is the default branch of an unforked repo - there's no PR to create or refer to
if !repo.IsFork && branch == baseRepo.DefaultBranch {
results = append(results, private.HookPostReceiveBranchResult{})