Skip to content

Commit 52cfd27

Browse files
authored
Option to set default branch at repository creation (#10803)
* Option to set default branch at repository creation * Handle template repos with non-default master branch * Add DefaultBranch handling on creation to API Fix #9542 Signed-off-by: Andrew Thornton <[email protected]>
1 parent b1c331c commit 52cfd27

File tree

10 files changed

+57
-33
lines changed

10 files changed

+57
-33
lines changed

models/repo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,7 @@ type CreateRepoOptions struct {
929929
IssueLabels string
930930
License string
931931
Readme string
932+
DefaultBranch string
932933
IsPrivate bool
933934
IsMirror bool
934935
AutoInit bool

modules/auth/repo_form.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@ import (
2727

2828
// CreateRepoForm form for creating repository
2929
type CreateRepoForm struct {
30-
UID int64 `binding:"Required"`
31-
RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
32-
Private bool
33-
Description string `binding:"MaxSize(255)"`
34-
AutoInit bool
35-
Gitignores string
36-
IssueLabels string
37-
License string
38-
Readme string
30+
UID int64 `binding:"Required"`
31+
RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
32+
Private bool
33+
Description string `binding:"MaxSize(255)"`
34+
DefaultBranch string `binding:"GitRefName;MaxSize(100)"`
35+
AutoInit bool
36+
Gitignores string
37+
IssueLabels string
38+
License string
39+
Readme string
3940

4041
RepoTemplate int64
4142
GitContent bool

modules/repository/generate.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ func generateRepoCommit(repo, templateRepo, generateRepo *models.Repository, tmp
113113
// Clone to temporary path and do the init commit.
114114
templateRepoPath := templateRepo.RepoPath()
115115
if err := git.Clone(templateRepoPath, tmpDir, git.CloneRepoOptions{
116-
Depth: 1,
116+
Depth: 1,
117+
Branch: templateRepo.DefaultBranch,
117118
}); err != nil {
118119
return fmt.Errorf("git clone: %v", err)
119120
}
@@ -180,7 +181,7 @@ func generateRepoCommit(repo, templateRepo, generateRepo *models.Repository, tmp
180181
return fmt.Errorf("git remote add: %v", err)
181182
}
182183

183-
return initRepoCommit(tmpDir, repo, repo.Owner)
184+
return initRepoCommit(tmpDir, repo, repo.Owner, templateRepo.DefaultBranch)
184185
}
185186

186187
func generateGitContent(ctx models.DBContext, repo, templateRepo, generateRepo *models.Repository) (err error) {
@@ -204,7 +205,7 @@ func generateGitContent(ctx models.DBContext, repo, templateRepo, generateRepo *
204205
return fmt.Errorf("getRepositoryByID: %v", err)
205206
}
206207

207-
repo.DefaultBranch = "master"
208+
repo.DefaultBranch = templateRepo.DefaultBranch
208209
if err = models.UpdateRepositoryCtx(ctx, repo, false); err != nil {
209210
return fmt.Errorf("updateRepository: %v", err)
210211
}

modules/repository/init.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func prepareRepoCommit(ctx models.DBContext, repo *models.Repository, tmpDir, re
9898
}
9999

100100
// initRepoCommit temporarily changes with work directory.
101-
func initRepoCommit(tmpPath string, repo *models.Repository, u *models.User) (err error) {
101+
func initRepoCommit(tmpPath string, repo *models.Repository, u *models.User, defaultBranch string) (err error) {
102102
commitTimeStr := time.Now().Format(time.RFC3339)
103103

104104
sig := u.NewGitSig()
@@ -145,7 +145,11 @@ func initRepoCommit(tmpPath string, repo *models.Repository, u *models.User) (er
145145
return fmt.Errorf("git commit: %v", err)
146146
}
147147

148-
if stdout, err := git.NewCommand("push", "origin", "master").
148+
if len(defaultBranch) == 0 {
149+
defaultBranch = "master"
150+
}
151+
152+
if stdout, err := git.NewCommand("push", "origin", "master:"+defaultBranch).
149153
SetDescription(fmt.Sprintf("initRepoCommit (git push): %s", tmpPath)).
150154
RunInDirWithEnv(tmpPath, models.InternalPushingEnvironment(u, repo)); err != nil {
151155
log.Error("Failed to push back to master: Stdout: %s\nError: %v", stdout, err)
@@ -190,7 +194,7 @@ func initRepository(ctx models.DBContext, repoPath string, u *models.User, repo
190194
}
191195

192196
// Apply changes and commit.
193-
if err = initRepoCommit(tmpDir, repo, u); err != nil {
197+
if err = initRepoCommit(tmpDir, repo, u, opts.DefaultBranch); err != nil {
194198
return fmt.Errorf("initRepoCommit: %v", err)
195199
}
196200
}
@@ -206,6 +210,10 @@ func initRepository(ctx models.DBContext, repoPath string, u *models.User, repo
206210
}
207211

208212
repo.DefaultBranch = "master"
213+
if len(opts.DefaultBranch) > 0 {
214+
repo.DefaultBranch = opts.DefaultBranch
215+
}
216+
209217
if err = models.UpdateRepositoryCtx(ctx, repo, false); err != nil {
210218
return fmt.Errorf("updateRepository: %v", err)
211219
}

modules/structs/repo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ type CreateRepoOption struct {
112112
License string `json:"license"`
113113
// Readme of the repository to create
114114
Readme string `json:"readme"`
115+
// DefaultBranch of the repository (used when initializes and in template)
116+
DefaultBranch string `json:"default_branch" binding:"GitRefName;MaxSize(100)"`
115117
}
116118

117119
// EditRepoOption options when editing a repository's properties

routers/api/v1/repo/repo.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,15 @@ func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateR
218218
opt.Readme = "Default"
219219
}
220220
repo, err := repo_service.CreateRepository(ctx.User, owner, models.CreateRepoOptions{
221-
Name: opt.Name,
222-
Description: opt.Description,
223-
IssueLabels: opt.IssueLabels,
224-
Gitignores: opt.Gitignores,
225-
License: opt.License,
226-
Readme: opt.Readme,
227-
IsPrivate: opt.Private,
228-
AutoInit: opt.AutoInit,
221+
Name: opt.Name,
222+
Description: opt.Description,
223+
IssueLabels: opt.IssueLabels,
224+
Gitignores: opt.Gitignores,
225+
License: opt.License,
226+
Readme: opt.Readme,
227+
IsPrivate: opt.Private,
228+
AutoInit: opt.AutoInit,
229+
DefaultBranch: opt.DefaultBranch,
229230
})
230231
if err != nil {
231232
if models.IsErrRepoAlreadyExist(err) {

routers/repo/repo.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,15 @@ func CreatePost(ctx *context.Context, form auth.CreateRepoForm) {
221221
}
222222
} else {
223223
repo, err = repo_service.CreateRepository(ctx.User, ctxUser, models.CreateRepoOptions{
224-
Name: form.RepoName,
225-
Description: form.Description,
226-
Gitignores: form.Gitignores,
227-
IssueLabels: form.IssueLabels,
228-
License: form.License,
229-
Readme: form.Readme,
230-
IsPrivate: form.Private || setting.Repository.ForcePrivate,
231-
AutoInit: form.AutoInit,
224+
Name: form.RepoName,
225+
Description: form.Description,
226+
Gitignores: form.Gitignores,
227+
IssueLabels: form.IssueLabels,
228+
License: form.License,
229+
Readme: form.Readme,
230+
IsPrivate: form.Private || setting.Repository.ForcePrivate,
231+
DefaultBranch: form.DefaultBranch,
232+
AutoInit: form.AutoInit,
232233
})
233234
if err == nil {
234235
log.Trace("Repository created [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name)

templates/repo/create.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@
162162
<label>{{.i18n.Tr "repo.auto_init"}}</label>
163163
</div>
164164
</div>
165+
<div class="inline field">
166+
<label for="default_branch">{{.i18n.Tr "repo.default_branch"}}</label>
167+
<input id="default_branch" name="default_branch" value="{{.default_branch}}" placeholder="master">
168+
</div>
165169
</div>
166170

167171
<br/>

templates/repo/empty.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ git init
5252
git add README.md
5353
git commit -m "first commit"
5454
git remote add origin <span class="clone-url">{{if $.DisableSSH}}{{$.CloneLink.HTTPS}}{{else}}{{$.CloneLink.SSH}}{{end}}</span>
55-
git push -u origin master</code></pre>
55+
git push -u origin {{if ne .Repository.DefaultBranch "master"}}master:{{.Repository.DefaultBranch}}{{else}}master{{end}}</code></pre>
5656
</div>
5757
</div>
5858
<div class="ui divider"></div>
@@ -61,7 +61,7 @@ git push -u origin master</code></pre>
6161
<h3>{{.i18n.Tr "repo.push_exist_repo"}}</h3>
6262
<div class="markdown">
6363
<pre><code>git remote add origin <span class="clone-url">{{if $.DisableSSH}}{{$.CloneLink.HTTPS}}{{else}}{{$.CloneLink.SSH}}{{end}}</span>
64-
git push -u origin master</code></pre>
64+
git push -u origin {{.Repository.DefaultBranch}}</code></pre>
6565
</div>
6666
</div>
6767
{{end}}

templates/swagger/v1_json.tmpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10600,6 +10600,11 @@
1060010600
"type": "boolean",
1060110601
"x-go-name": "AutoInit"
1060210602
},
10603+
"default_branch": {
10604+
"description": "DefaultBranch of the repository (used when initializes and in template)",
10605+
"type": "string",
10606+
"x-go-name": "DefaultBranch"
10607+
},
1060310608
"description": {
1060410609
"description": "Description of the repository to create",
1060510610
"type": "string",

0 commit comments

Comments
 (0)