Skip to content

Commit 6c61f49

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

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
@@ -122,6 +122,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate
122122
if err != nil {
123123
return err
124124
}
125+
r.DefaultBranch = repo.DefaultBranch
125126

126127
r, err = repository.MigrateRepositoryGitData(g.doer, owner, r, base.MigrateOptions{
127128
RepoName: g.repoName,

modules/migrations/github.go

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

146+
defaultBranch := ""
147+
if gr.DefaultBranch != nil {
148+
defaultBranch = *gr.DefaultBranch
149+
}
150+
146151
// convert github repo to stand Repo
147152
return &base.Repository{
148-
Owner: g.repoOwner,
149-
Name: gr.GetName(),
150-
IsPrivate: *gr.Private,
151-
Description: gr.GetDescription(),
152-
OriginalURL: gr.GetHTMLURL(),
153-
CloneURL: gr.GetCloneURL(),
153+
Owner: g.repoOwner,
154+
Name: gr.GetName(),
155+
IsPrivate: *gr.Private,
156+
Description: gr.GetDescription(),
157+
OriginalURL: gr.GetHTMLURL(),
158+
CloneURL: gr.GetCloneURL(),
159+
DefaultBranch: defaultBranch,
154160
}, nil
155161
}
156162

modules/migrations/github_test.go

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

8283
topics, err := downloader.GetTopics()

modules/migrations/gitlab.go

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

140140
// convert gitlab repo to stand Repo
141141
return &base.Repository{
142-
Owner: owner,
143-
Name: gr.Name,
144-
IsPrivate: private,
145-
Description: gr.Description,
146-
OriginalURL: gr.WebURL,
147-
CloneURL: gr.HTTPURLToRepo,
142+
Owner: owner,
143+
Name: gr.Name,
144+
IsPrivate: private,
145+
Description: gr.Description,
146+
OriginalURL: gr.WebURL,
147+
CloneURL: gr.HTTPURLToRepo,
148+
DefaultBranch: gr.DefaultBranch,
148149
}, nil
149150
}
150151

modules/migrations/gitlab_test.go

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

4748
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)