Skip to content

Commit 6a897ca

Browse files
committed
Option to set default branch at repository creation
Fix go-gitea#9542 Signed-off-by: Andrew Thornton <[email protected]>
1 parent c61b902 commit 6a897ca

File tree

7 files changed

+38
-23
lines changed

7 files changed

+38
-23
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func generateRepoCommit(repo, templateRepo, generateRepo *models.Repository, tmp
180180
return fmt.Errorf("git remote add: %v", err)
181181
}
182182

183-
return initRepoCommit(tmpDir, repo, repo.Owner)
183+
return initRepoCommit(tmpDir, repo, repo.Owner, "master")
184184
}
185185

186186
func generateGitContent(ctx models.DBContext, repo, templateRepo, generateRepo *models.Repository) (err error) {

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
}

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}}

0 commit comments

Comments
 (0)