Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match unqualified references when syncing pulls as well #23070

Merged
merged 2 commits into from
May 29, 2023
Merged

Match unqualified references when syncing pulls as well #23070

merged 2 commits into from
May 29, 2023

Conversation

lflare
Copy link
Contributor

@lflare lflare commented Feb 22, 2023

It seems that opts.RefFullName may occassionally be set to just the branch name, without the refs/heads/ prefixing.

@lflare lflare changed the title Allow for short-hand references in Sync Pushes. Allow for matching short-hand references in sync pulls Feb 22, 2023
@wxiaoguang
Copy link
Contributor

Good catch. There are some details about git ref.

IMO the bug is in parseRemoteUpdateOutput, it should prepare a correct refName (with refs/xxx prefix) in all cases.

In other functions, when they see a variable called refName , the refName should be guaranteed to have the refs/ prefix if it's a branch name or a tag name, to avoid guessing ref names like refs/heads/main or refs/heads/refs/heads/main.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Feb 22, 2023
@lflare
Copy link
Contributor Author

lflare commented Feb 22, 2023

Good catch. There are some details about git ref.

IMO the bug is in parseRemoteUpdateOutput, it should prepare a correct refName (with refs/xxx prefix) in all cases.

In other functions, when they see a variable called refName , the refName should be guaranteed to have the refs/ prefix if it's a branch name or a tag name, to avoid guessing ref names like refs/heads/main or refs/heads/refs/heads/main.

I actually expected someone to point that out, unfortunately, it seems to be parsing remote output to get the tag, and from my logging as depicted below,

	for i := range lines {
		// Make sure reference name is presented before continue
		idx := strings.Index(lines[i], "-> ")
		if idx == -1 {
			continue
		}

		refName := lines[i][idx+3:]

		log.Info("stats_indexer.parseRemoteUpdateOutput(%d) RefFullName: %s, %s", 0, refName, lines[i])

		switch {
		case strings.HasPrefix(lines[i], " * "): // New reference
			if strings.HasPrefix(lines[i], " * [new tag]") {
				refName = git.TagPrefix + refName
			} else if strings.HasPrefix(lines[i], " * [new branch]") {
				refName = git.BranchPrefix + refName
			}
			results = append(results, &mirrorSyncResult{
				refName:    

This is the output it processes when it is doing a pull,

2023/02/22 11:22:38 ...irror/mirror_pull.go:100:parseRemoteUpdateOutput() [I] [63f5fafd-9] stats_indexer.parseRemoteUpdateOutput(0) RefFullName: main,    feedaf2..cfd6d4d  main       -> main
2023/02/22 11:22:38 ...irror/mirror_pull.go:158:parseRemoteUpdateOutput() [I] [63f5fafd-9] stats_indexer.parseRemoteUpdateOutput(1) RefFullName: main,    feedaf2..cfd6d4d  main       -> main

Specifically the line feedaf2..cfd6d4d main -> main. This could be a tag, like refs/tag/main, or it could be a branch, like refs/head/main, no way to tell.

Personally, I'm not opposed to just assuming it's a branch by default, or with my current PR, matching it regardless of whether it has a prefix. I am more concerned about not letting this issue remain an issue, when the "fix" for it, is fairly clear.

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Feb 22, 2023

According to my test. git remote update only outputs the following logs for branches:

From github.com:wxiaoguang/gitea
   f74293f9c2..90a7bba1f2  test-branch-1 -> wxiaoguang/test-branch-1

If I change/delete the tags on remote, git remote update outputs nothing. So I think it's fine to make refName as branch in parseRemoteUpdateOutput in such case.

@lflare
Copy link
Contributor Author

lflare commented Feb 22, 2023

According to my test. git remote update only outputs the following logs for branches:

From github.com:wxiaoguang/gitea
   f74293f9c2..90a7bba1f2  test-branch-1 -> wxiaoguang/test-branch-1

If I change/delete the tags on remote, git remote update outputs nothing. So I think it's fine to make refName as branch in parseRemoteUpdateOutput in such case.

Given that the code in this PR is only targeting UpdateRepoIndexer(), I don't believe there's any serious implications if we just allowed (opts.RefFullName == git.BranchPrefix+repo.DefaultBranch || opts.RefFullName == repo.DefaultBranch)?

@wxiaoguang
Copy link
Contributor

Im my mind, storing a non-ref value into a variable called ref seems misleading ....

Maybe @zeripath could help to take a look at this problem, he has spent a lot of time cleaning up the legacy ref problems in Gitea.

@lunny
Copy link
Member

lunny commented Feb 24, 2023

Maybe we should change func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bool) to make the returned result as full ref name.

   342b573..cfce25f  main         -> origin/main
   c00418e..fff1af1  release/v0.9 -> origin/release/v0.9
 * [new tag]         v0.9.2       -> v0.9.2
 * [new tag]         v0.9.1       -> v0.9.1

Maybe it's not about missing refs but have an extra origin/? So this should be a bug of parseRemoteUpdateOutput when handle new commits for a branch. The branch name became origin/main, but it should be refs/heads/main.

@lflare
Copy link
Contributor Author

lflare commented Feb 25, 2023

Maybe we should change func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bool) to make the returned result as full ref name.

   342b573..cfce25f  main         -> origin/main
   c00418e..fff1af1  release/v0.9 -> origin/release/v0.9
 * [new tag]         v0.9.2       -> v0.9.2
 * [new tag]         v0.9.1       -> v0.9.1

Maybe it's not about missing refs but have an extra origin/? So this should be a bug of parseRemoteUpdateOutput when handle new commits for a branch. The branch name became origin/main, but it should be refs/heads/main.

There are also instances where origin/ may not even be in the response. As mentioned in my previous reply, there are times where the output is,

    feedaf2..cfd6d4d  main       -> main

@lunny
Copy link
Member

lunny commented Mar 2, 2023

Maybe we should change func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bool) to make the returned result as full ref name.

   342b573..cfce25f  main         -> origin/main
   c00418e..fff1af1  release/v0.9 -> origin/release/v0.9
 * [new tag]         v0.9.2       -> v0.9.2
 * [new tag]         v0.9.1       -> v0.9.1

Maybe it's not about missing refs but have an extra origin/? So this should be a bug of parseRemoteUpdateOutput when handle new commits for a branch. The branch name became origin/main, but it should be refs/heads/main.

There are also instances where origin/ may not even be in the response. As mentioned in my previous reply, there are times where the output is,

    feedaf2..cfd6d4d  main       -> main

Yes, so I mean maybe we should correct the function runSync.

@lflare
Copy link
Contributor Author

lflare commented Apr 23, 2023

Sorry @lunny, I have been out of touch of this issue for awhile as I've been running my own custom build with this, but how do you suppose we can rectify this issue? Is there a drawback to just calling a re-index every time (opts.RefFullName == git.BranchPrefix+repo.DefaultBranch || opts.RefFullName == repo.DefaultBranch) matches? I'm looking through documentations and I'm not entirely sure how to go about getting git to return the correct tags for parsing.

@wxiaoguang
Copy link
Contributor

I have no objection for this change.

Leaving enough comments, then it could look good to me.

@pull-request-size pull-request-size bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label May 10, 2023
@lflare
Copy link
Contributor Author

lflare commented May 10, 2023

I have no objection for this change.

Leaving enough comments, then it could look good to me.

Great to hear, what kind of comments are you expecting from this PR?

@wxiaoguang
Copy link
Contributor

I have no objection for this change.
Leaving enough comments, then it could look good to me.

Great to hear, what kind of comments are you expecting from this PR?

Some explanation about why need this logic? And take some test result from the commens like f74293f9c2..90a7bba1f2 test-branch-1 -> wxiaoguang/test-branch-1 ?

@pull-request-size pull-request-size bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels May 10, 2023
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels May 10, 2023
@lunny
Copy link
Member

lunny commented May 10, 2023

I have sent #24634. I think that PR did more work than this PR.

@lflare
Copy link
Contributor Author

lflare commented May 11, 2023

I have sent #24634. I think that PR did more work than this PR.

No problem with that, so long as that PR doesn't take a couple of months to review and merge, given how much more complicated it is.

Copy link
Member

@lunny lunny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since my PR will take more time, I think we can merge this first.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels May 23, 2023
techknowlogick pushed a commit that referenced this pull request May 26, 2023
…nc 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>
@lunny
Copy link
Member

lunny commented May 26, 2023

Since #24634 is merged but not backport to v1.19 because there are too many conflicts, I think this patch could be sent to v1.19.

@silverwind
Copy link
Member

Conflict needs to be fixed.

@lflare
Copy link
Contributor Author

lflare commented May 27, 2023

Conflict needs to be fixed.

Am I suppose to fix the conflict based off v1.19 code?

@silverwind
Copy link
Member

Conflict needs to be fixed.

Am I suppose to fix the conflict based off v1.19 code?

No, just fix it against the main branch. 1.19 would be handled with a separate backport pull request.

@wxiaoguang
Copy link
Contributor

Conflict needs to be fixed.

Am I suppose to fix the conflict based off v1.19 code?

No, just fix it against the main branch. 1.19 would be handled with a separate backport pull request.

The original issue's problem has been fixed by Use the type RefName for all the needed places and fix pull mirror sync bugs #24634 in 1.20

So this PR could be applied to 1.19

@silverwind
Copy link
Member

Ok, let me try changing PR base.

@silverwind silverwind changed the base branch from main to release/v1.19 May 27, 2023 10:09
@silverwind silverwind changed the base branch from release/v1.19 to main May 27, 2023 10:09
@silverwind
Copy link
Member

silverwind commented May 27, 2023

No, didn't work, it showed a massive diff. I think we may need a fresh PR targeting release/v1.19.

@lflare lflare changed the base branch from main to release/v1.19 May 27, 2023 11:38
@lflare
Copy link
Contributor Author

lflare commented May 27, 2023

@silverwind, does this look good for merging?

@wxiaoguang wxiaoguang added this to the 1.19.4 milestone May 27, 2023
@silverwind
Copy link
Member

Seems good, but @lunny should take a final look as well.

@lunny lunny added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label May 29, 2023
@delvh delvh changed the title Allow for matching short-hand references in sync pulls Match unqualified references when syncing pulls as well May 29, 2023
@delvh delvh merged commit 7dc46ff into go-gitea:release/v1.19 May 29, 2023
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label May 29, 2023
@dboreham
Copy link

fwiw I tested release/v1.19 just now and it does not seem to fix #24824
(#24634 in main does fix it)

@wxiaoguang
Copy link
Contributor

fwiw I tested release/v1.19 just now and it does not seem to fix #24824 (#24634 in main does fix it)

This PR is for stats_indexer.UpdateRepoIndexer only, no fix for action.

That's why the #24634 is the complete fix.

@dboreham
Copy link

That's why the #24634 is the complete fix.

Oh, that's too bad.

@wxiaoguang
Copy link
Contributor

wxiaoguang commented May 29, 2023

That's why the #24634 is the complete fix.

Oh, that's too bad.

That refactoring PR is too big, it's not suitable to backport.

I think 1.20 is pretty stable, and its release day is not too far.

Maybe you could try 1.20 nightly, any bug could be fixed in the first time.

@lunny
Copy link
Member

lunny commented May 29, 2023

Maybe #24868 could be reopen and retarget to 1.19? @sillyguodong

@dboreham
Copy link

That refactoring PR is too big, it's not suitable to backport.

I was thinking of this one-line fix: #24868
Noticed that there is a comment on that PR saying it was closed in preference to this one, but in fact it doesn't fix the same problem.

I think 1.20 is pretty stable, and its release day is not too far.

Maybe you could try 1.20 nightly, any bug could be fixed in the first time.

We run 1.20 in dev, but for production I have to deploy something stable :(

@sillyguodong
Copy link
Contributor

@dboreham create a new PR #24994, try to merge into v1.19

Codeberg-org pushed a commit to Codeberg-org/gitea that referenced this pull request Jun 3, 2023
It seems that `opts.RefFullName` may occassionally be set
to just the branch name, without the `refs/heads/` prefixing.

(cherry picked from commit 7dc46ff)
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Aug 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants