Skip to content

Commit 6305f07

Browse files
6543zeripath
andauthored
On Migration respect old DefaultBranch (#12843) (#12858)
* On Migration respect old DefaultBranch * add DefaultBranch int test set Co-authored-by: zeripath <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent ff9d99f commit 6305f07

File tree

7 files changed

+56
-41
lines changed

7 files changed

+56
-41
lines changed

modules/migrations/base/repo.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ package base
77

88
// Repository defines a standard repository information
99
type Repository struct {
10-
Name string
11-
Owner string
12-
IsPrivate bool
13-
IsMirror bool
14-
Description string
15-
AuthUsername string
16-
AuthPassword string
17-
CloneURL string
18-
OriginalURL string
10+
Name string
11+
Owner string
12+
IsPrivate bool
13+
IsMirror bool
14+
Description string
15+
AuthUsername string
16+
AuthPassword string
17+
CloneURL string
18+
OriginalURL string
19+
DefaultBranch string
1920
}

modules/migrations/gitea.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate
119119
if err != nil {
120120
return err
121121
}
122+
r.DefaultBranch = repo.DefaultBranch
122123

123124
r, err = repository.MigrateRepositoryGitData(g.doer, owner, r, structs.MigrateRepoOption{
124125
RepoName: g.repoName,

modules/migrations/github.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,20 @@ func (g *GithubDownloaderV3) GetRepoInfo() (*base.Repository, error) {
154154
}
155155
g.rate = &resp.Rate
156156

157+
defaultBranch := ""
158+
if gr.DefaultBranch != nil {
159+
defaultBranch = *gr.DefaultBranch
160+
}
161+
157162
// convert github repo to stand Repo
158163
return &base.Repository{
159-
Owner: g.repoOwner,
160-
Name: gr.GetName(),
161-
IsPrivate: *gr.Private,
162-
Description: gr.GetDescription(),
163-
OriginalURL: gr.GetHTMLURL(),
164-
CloneURL: gr.GetCloneURL(),
164+
Owner: g.repoOwner,
165+
Name: gr.GetName(),
166+
IsPrivate: *gr.Private,
167+
Description: gr.GetDescription(),
168+
OriginalURL: gr.GetHTMLURL(),
169+
CloneURL: gr.GetCloneURL(),
170+
DefaultBranch: defaultBranch,
165171
}, nil
166172
}
167173

modules/migrations/github_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ func TestGitHubDownloadRepo(t *testing.T) {
7171
repo, err := downloader.GetRepoInfo()
7272
assert.NoError(t, err)
7373
assert.EqualValues(t, &base.Repository{
74-
Name: "test_repo",
75-
Owner: "go-gitea",
76-
Description: "Test repository for testing migration from github to gitea",
77-
CloneURL: "https://github.com/go-gitea/test_repo.git",
78-
OriginalURL: "https://github.com/go-gitea/test_repo",
74+
Name: "test_repo",
75+
Owner: "go-gitea",
76+
Description: "Test repository for testing migration from github to gitea",
77+
CloneURL: "https://github.com/go-gitea/test_repo.git",
78+
OriginalURL: "https://github.com/go-gitea/test_repo",
79+
DefaultBranch: "master",
7980
}, repo)
8081

8182
topics, err := downloader.GetTopics()

modules/migrations/gitlab.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,13 @@ func (g *GitlabDownloader) GetRepoInfo() (*base.Repository, error) {
157157

158158
// convert gitlab repo to stand Repo
159159
return &base.Repository{
160-
Owner: owner,
161-
Name: gr.Name,
162-
IsPrivate: private,
163-
Description: gr.Description,
164-
OriginalURL: gr.WebURL,
165-
CloneURL: gr.HTTPURLToRepo,
160+
Owner: owner,
161+
Name: gr.Name,
162+
IsPrivate: private,
163+
Description: gr.Description,
164+
OriginalURL: gr.WebURL,
165+
CloneURL: gr.HTTPURLToRepo,
166+
DefaultBranch: gr.DefaultBranch,
166167
}, nil
167168
}
168169

modules/migrations/gitlab_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ func TestGitlabDownloadRepo(t *testing.T) {
3535
assert.NoError(t, err)
3636
// Repo Owner is blank in Gitlab Group repos
3737
assert.EqualValues(t, &base.Repository{
38-
Name: "test_repo",
39-
Owner: "",
40-
Description: "Test repository for testing migration from gitlab to gitea",
41-
CloneURL: "https://gitlab.com/gitea/test_repo.git",
42-
OriginalURL: "https://gitlab.com/gitea/test_repo",
38+
Name: "test_repo",
39+
Owner: "",
40+
Description: "Test repository for testing migration from gitlab to gitea",
41+
CloneURL: "https://gitlab.com/gitea/test_repo.git",
42+
OriginalURL: "https://gitlab.com/gitea/test_repo",
43+
DefaultBranch: "master",
4344
}, repo)
4445

4546
topics, err := downloader.GetTopics()

modules/repository/repo.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,22 @@ func MigrateRepositoryGitData(doer, u *models.User, repo *models.Repository, opt
102102
return repo, fmt.Errorf("git.IsEmpty: %v", err)
103103
}
104104

105-
if !opts.Releases && !repo.IsEmpty {
106-
// Try to get HEAD branch and set it as default branch.
107-
headBranch, err := gitRepo.GetHEADBranch()
108-
if err != nil {
109-
return repo, fmt.Errorf("GetHEADBranch: %v", err)
110-
}
111-
if headBranch != nil {
112-
repo.DefaultBranch = headBranch.Name
105+
if !repo.IsEmpty {
106+
if len(repo.DefaultBranch) == 0 {
107+
// Try to get HEAD branch and set it as default branch.
108+
headBranch, err := gitRepo.GetHEADBranch()
109+
if err != nil {
110+
return repo, fmt.Errorf("GetHEADBranch: %v", err)
111+
}
112+
if headBranch != nil {
113+
repo.DefaultBranch = headBranch.Name
114+
}
113115
}
114116

115-
if err = SyncReleasesWithTags(repo, gitRepo); err != nil {
116-
log.Error("Failed to synchronize tags to releases for repository: %v", err)
117+
if !opts.Releases {
118+
if err = SyncReleasesWithTags(repo, gitRepo); err != nil {
119+
log.Error("Failed to synchronize tags to releases for repository: %v", err)
120+
}
117121
}
118122
}
119123

0 commit comments

Comments
 (0)