diff options
author | zeripath <art27@cantab.net> | 2020-03-26 19:14:51 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-26 19:14:51 +0000 |
commit | 52cfd2743c0e85b36081cf80a850e6a5901f1865 (patch) | |
tree | 9b13df2465992cf00fdeb375dd04bd7c17c09f65 | |
parent | b1c331c84596f73aeab60178daf92f3539e026b9 (diff) | |
download | gitea-52cfd2743c0e85b36081cf80a850e6a5901f1865.tar.gz gitea-52cfd2743c0e85b36081cf80a850e6a5901f1865.zip |
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 <art27@cantab.net>
-rw-r--r-- | models/repo.go | 1 | ||||
-rw-r--r-- | modules/auth/repo_form.go | 19 | ||||
-rw-r--r-- | modules/repository/generate.go | 7 | ||||
-rw-r--r-- | modules/repository/init.go | 14 | ||||
-rw-r--r-- | modules/structs/repo.go | 2 | ||||
-rw-r--r-- | routers/api/v1/repo/repo.go | 17 | ||||
-rw-r--r-- | routers/repo/repo.go | 17 | ||||
-rw-r--r-- | templates/repo/create.tmpl | 4 | ||||
-rw-r--r-- | templates/repo/empty.tmpl | 4 | ||||
-rw-r--r-- | templates/swagger/v1_json.tmpl | 5 |
10 files changed, 57 insertions, 33 deletions
diff --git a/models/repo.go b/models/repo.go index dd50db9a0b..74b5a021f4 100644 --- a/models/repo.go +++ b/models/repo.go @@ -929,6 +929,7 @@ type CreateRepoOptions struct { IssueLabels string License string Readme string + DefaultBranch string IsPrivate bool IsMirror bool AutoInit bool diff --git a/modules/auth/repo_form.go b/modules/auth/repo_form.go index 63a560728a..84ab35f649 100644 --- a/modules/auth/repo_form.go +++ b/modules/auth/repo_form.go @@ -27,15 +27,16 @@ import ( // CreateRepoForm form for creating repository type CreateRepoForm struct { - UID int64 `binding:"Required"` - RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"` - Private bool - Description string `binding:"MaxSize(255)"` - AutoInit bool - Gitignores string - IssueLabels string - License string - Readme string + UID int64 `binding:"Required"` + RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"` + Private bool + Description string `binding:"MaxSize(255)"` + DefaultBranch string `binding:"GitRefName;MaxSize(100)"` + AutoInit bool + Gitignores string + IssueLabels string + License string + Readme string RepoTemplate int64 GitContent bool diff --git a/modules/repository/generate.go b/modules/repository/generate.go index 52502e8eb3..6d80488de7 100644 --- a/modules/repository/generate.go +++ b/modules/repository/generate.go @@ -113,7 +113,8 @@ func generateRepoCommit(repo, templateRepo, generateRepo *models.Repository, tmp // Clone to temporary path and do the init commit. templateRepoPath := templateRepo.RepoPath() if err := git.Clone(templateRepoPath, tmpDir, git.CloneRepoOptions{ - Depth: 1, + Depth: 1, + Branch: templateRepo.DefaultBranch, }); err != nil { return fmt.Errorf("git clone: %v", err) } @@ -180,7 +181,7 @@ func generateRepoCommit(repo, templateRepo, generateRepo *models.Repository, tmp return fmt.Errorf("git remote add: %v", err) } - return initRepoCommit(tmpDir, repo, repo.Owner) + return initRepoCommit(tmpDir, repo, repo.Owner, templateRepo.DefaultBranch) } func generateGitContent(ctx models.DBContext, repo, templateRepo, generateRepo *models.Repository) (err error) { @@ -204,7 +205,7 @@ func generateGitContent(ctx models.DBContext, repo, templateRepo, generateRepo * return fmt.Errorf("getRepositoryByID: %v", err) } - repo.DefaultBranch = "master" + repo.DefaultBranch = templateRepo.DefaultBranch if err = models.UpdateRepositoryCtx(ctx, repo, false); err != nil { return fmt.Errorf("updateRepository: %v", err) } diff --git a/modules/repository/init.go b/modules/repository/init.go index 7b7d07f43e..320fba53fc 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -98,7 +98,7 @@ func prepareRepoCommit(ctx models.DBContext, repo *models.Repository, tmpDir, re } // initRepoCommit temporarily changes with work directory. -func initRepoCommit(tmpPath string, repo *models.Repository, u *models.User) (err error) { +func initRepoCommit(tmpPath string, repo *models.Repository, u *models.User, defaultBranch string) (err error) { commitTimeStr := time.Now().Format(time.RFC3339) sig := u.NewGitSig() @@ -145,7 +145,11 @@ func initRepoCommit(tmpPath string, repo *models.Repository, u *models.User) (er return fmt.Errorf("git commit: %v", err) } - if stdout, err := git.NewCommand("push", "origin", "master"). + if len(defaultBranch) == 0 { + defaultBranch = "master" + } + + if stdout, err := git.NewCommand("push", "origin", "master:"+defaultBranch). SetDescription(fmt.Sprintf("initRepoCommit (git push): %s", tmpPath)). RunInDirWithEnv(tmpPath, models.InternalPushingEnvironment(u, repo)); err != nil { 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 } // Apply changes and commit. - if err = initRepoCommit(tmpDir, repo, u); err != nil { + if err = initRepoCommit(tmpDir, repo, u, opts.DefaultBranch); err != nil { return fmt.Errorf("initRepoCommit: %v", err) } } @@ -206,6 +210,10 @@ func initRepository(ctx models.DBContext, repoPath string, u *models.User, repo } repo.DefaultBranch = "master" + if len(opts.DefaultBranch) > 0 { + repo.DefaultBranch = opts.DefaultBranch + } + if err = models.UpdateRepositoryCtx(ctx, repo, false); err != nil { return fmt.Errorf("updateRepository: %v", err) } diff --git a/modules/structs/repo.go b/modules/structs/repo.go index 04cc594f22..cabb5e12f9 100644 --- a/modules/structs/repo.go +++ b/modules/structs/repo.go @@ -112,6 +112,8 @@ type CreateRepoOption struct { License string `json:"license"` // Readme of the repository to create Readme string `json:"readme"` + // DefaultBranch of the repository (used when initializes and in template) + DefaultBranch string `json:"default_branch" binding:"GitRefName;MaxSize(100)"` } // EditRepoOption options when editing a repository's properties diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 047d8168b3..5eeef9fb9d 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -218,14 +218,15 @@ func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateR opt.Readme = "Default" } repo, err := repo_service.CreateRepository(ctx.User, owner, models.CreateRepoOptions{ - Name: opt.Name, - Description: opt.Description, - IssueLabels: opt.IssueLabels, - Gitignores: opt.Gitignores, - License: opt.License, - Readme: opt.Readme, - IsPrivate: opt.Private, - AutoInit: opt.AutoInit, + Name: opt.Name, + Description: opt.Description, + IssueLabels: opt.IssueLabels, + Gitignores: opt.Gitignores, + License: opt.License, + Readme: opt.Readme, + IsPrivate: opt.Private, + AutoInit: opt.AutoInit, + DefaultBranch: opt.DefaultBranch, }) if err != nil { if models.IsErrRepoAlreadyExist(err) { diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 5177add99c..3f135dd216 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -221,14 +221,15 @@ func CreatePost(ctx *context.Context, form auth.CreateRepoForm) { } } else { repo, err = repo_service.CreateRepository(ctx.User, ctxUser, models.CreateRepoOptions{ - Name: form.RepoName, - Description: form.Description, - Gitignores: form.Gitignores, - IssueLabels: form.IssueLabels, - License: form.License, - Readme: form.Readme, - IsPrivate: form.Private || setting.Repository.ForcePrivate, - AutoInit: form.AutoInit, + Name: form.RepoName, + Description: form.Description, + Gitignores: form.Gitignores, + IssueLabels: form.IssueLabels, + License: form.License, + Readme: form.Readme, + IsPrivate: form.Private || setting.Repository.ForcePrivate, + DefaultBranch: form.DefaultBranch, + AutoInit: form.AutoInit, }) if err == nil { log.Trace("Repository created [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name) diff --git a/templates/repo/create.tmpl b/templates/repo/create.tmpl index b63af09cec..bd97f9de1e 100644 --- a/templates/repo/create.tmpl +++ b/templates/repo/create.tmpl @@ -162,6 +162,10 @@ <label>{{.i18n.Tr "repo.auto_init"}}</label> </div> </div> + <div class="inline field"> + <label for="default_branch">{{.i18n.Tr "repo.default_branch"}}</label> + <input id="default_branch" name="default_branch" value="{{.default_branch}}" placeholder="master"> + </div> </div> <br/> diff --git a/templates/repo/empty.tmpl b/templates/repo/empty.tmpl index d6965d6fb9..8efb6d2866 100644 --- a/templates/repo/empty.tmpl +++ b/templates/repo/empty.tmpl @@ -52,7 +52,7 @@ git init git add README.md git commit -m "first commit" git remote add origin <span class="clone-url">{{if $.DisableSSH}}{{$.CloneLink.HTTPS}}{{else}}{{$.CloneLink.SSH}}{{end}}</span> -git push -u origin master</code></pre> +git push -u origin {{if ne .Repository.DefaultBranch "master"}}master:{{.Repository.DefaultBranch}}{{else}}master{{end}}</code></pre> </div> </div> <div class="ui divider"></div> @@ -61,7 +61,7 @@ git push -u origin master</code></pre> <h3>{{.i18n.Tr "repo.push_exist_repo"}}</h3> <div class="markdown"> <pre><code>git remote add origin <span class="clone-url">{{if $.DisableSSH}}{{$.CloneLink.HTTPS}}{{else}}{{$.CloneLink.SSH}}{{end}}</span> -git push -u origin master</code></pre> +git push -u origin {{.Repository.DefaultBranch}}</code></pre> </div> </div> {{end}} diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 07d760212e..2da0af95b9 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -10600,6 +10600,11 @@ "type": "boolean", "x-go-name": "AutoInit" }, + "default_branch": { + "description": "DefaultBranch of the repository (used when initializes and in template)", + "type": "string", + "x-go-name": "DefaultBranch" + }, "description": { "description": "Description of the repository to create", "type": "string", |