Skip to content

Fix missing reference prefix of commits when sync mirror repository #24868

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion services/mirror/mirror_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func parseRemoteUpdateOutput(output string) []*mirrorSyncResult {
continue
}
results = append(results, &mirrorSyncResult{
refName: refName,
refName: git.BranchPrefix + refName, // the reference name of commits should also has prefix.
oldCommitID: shas[0],
newCommitID: shas[1],
})
Expand Down
84 changes: 84 additions & 0 deletions services/mirror/mirror_pull_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package mirror

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseRemoteUpdateOutput(t *testing.T) {
tests := []struct {
input string
results []*mirrorSyncResult
}{
{
// create tag
input: "From https://xxx.com/xxx/xxx\n * [new tag] v1.4 -> v1.4\n",
results: []*mirrorSyncResult{
{
refName: "refs/tags/v1.4",
oldCommitID: gitShortEmptySha,
newCommitID: "",
},
},
},
{
// delete tag and create branch
input: "From https://xxx.com/xxx/xxx\n - [deleted] (none) -> v1.0.1\n * [new branch] test/t3 -> test/t3\n * [new branch] test/t4 -> test/t4\n",
results: []*mirrorSyncResult{
{
refName: "v1.0.1",
oldCommitID: "",
newCommitID: gitShortEmptySha,
},
{
refName: "refs/heads/test/t3",
oldCommitID: gitShortEmptySha,
newCommitID: "",
},
{
refName: "refs/heads/test/t4",
oldCommitID: gitShortEmptySha,
newCommitID: "",
},
},
},
{
// delete branch
input: "From https://xxx.com/xxx/xxx\n - [deleted] (none) -> test/t2\n",
results: []*mirrorSyncResult{
{
refName: "test/t2",
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it need prefix?

Copy link
Contributor Author

@sillyguodong sillyguodong May 24, 2023

Choose a reason for hiding this comment

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

I think so, but it seems impossible to parse out whether the current reference is a tag or a branch from the following text:

From https://xxx.com/xxx/xxx\n - [deleted] (none) -> v1.0.1\n
From https://xxx.com/xxx/xxx\n - [deleted] (none) -> test/t2\n

maybe we can use command git show-ref --tags/heads?
image

Copy link
Contributor

Choose a reason for hiding this comment

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

Then I think it needs to be commented.

Copy link
Contributor

Choose a reason for hiding this comment

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

And the comment // delete branch is not right, the example contains tags.

Copy link
Member

Choose a reason for hiding this comment

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

I think I did similar things in my refname PR

oldCommitID: "",
newCommitID: gitShortEmptySha,
},
},
},
{
// new commits
input: "From https://xxx.com/xxx/xxx\n aeb77a8..425ec44 test/t3 -> test/t3\n 93b2801..3e1d4c2 test/t4 -> test/t4\n",
results: []*mirrorSyncResult{
{
refName: "refs/heads/test/t3",
oldCommitID: "aeb77a8",
newCommitID: "425ec44",
},
{
refName: "refs/heads/test/t4",
oldCommitID: "93b2801",
newCommitID: "3e1d4c2",
},
},
},
}

for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
results := parseRemoteUpdateOutput(test.input)
assert.EqualValues(t, test.results, results, fmt.Sprintf("%#v", results))
})
}
}