Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Support new file/upload file buttons on empty repo home page #6918

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type Repository struct {

// CanEnableEditor returns true if repository is editable and user has proper access level.
func (r *Repository) CanEnableEditor() bool {
return r.Permission.CanWrite(unit_model.TypeCode) && r.Repository.CanEnableEditor() && r.IsViewBranch && !r.Repository.IsArchived
return r.Permission.CanWrite(unit_model.TypeCode) && r.Repository.CanEnableEditor() && (r.IsViewBranch || r.Repository.IsEmpty) && !r.Repository.IsArchived
}

// CanCreateBranch returns true if repository is editable and user has proper access level.
Expand Down Expand Up @@ -753,6 +753,10 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
return func(ctx *Context) (cancel context.CancelFunc) {
// Empty repository does not have reference information.
if ctx.Repo.Repository.IsEmpty {
ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
ctx.Repo.TreePath = ""
ctx.Data["TreePath"] = ctx.Repo.TreePath
ctx.Repo.IsViewBranch = true
return
}

Expand Down
22 changes: 18 additions & 4 deletions modules/repofiles/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *Up
opts.NewBranch = opts.OldBranch
}

// oldBranch must exist for this operation
if _, err := repo_module.GetBranch(repo, opts.OldBranch); err != nil {
return nil, err
if !repo.IsEmpty {
// oldBranch must exist for this operation
if _, err := repo_module.GetBranch(repo, opts.OldBranch); err != nil {
return nil, err
}
}

// A NewBranch can be specified for the file to be created/updated in a new branch.
Expand Down Expand Up @@ -177,6 +179,19 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *Up

author, committer := GetAuthorAndCommitterUsers(opts.Author, opts.Committer, doer)

if repo.IsEmpty {
err := repo_module.CheckInitRepository(repo.OwnerName, repo.Name)
if err != nil && !models.IsErrRepoFilesAlreadyExist(err) {
return nil, err
}

repo.IsEmpty = false
repo.DefaultBranch = opts.OldBranch
if err := models.UpdateRepository(repo, false); err != nil {
return nil, err
}
}

t, err := NewTemporaryUploadRepository(repo)
if err != nil {
log.Error("%v", err)
Expand Down Expand Up @@ -204,7 +219,6 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *Up
return nil, fmt.Errorf("DeleteRepoFile: Invalid last commit ID: %v", err)
}
opts.LastCommitID = lastCommitID.String()

}

encoding := "UTF-8"
Expand Down
2 changes: 1 addition & 1 deletion modules/repository/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func GenerateRepository(ctx context.Context, doer, owner *models.User, templateR
}
}

if err = checkInitRepository(owner.Name, generateRepo.Name); err != nil {
if err = CheckInitRepository(owner.Name, generateRepo.Name); err != nil {
return generateRepo, err
}

Expand Down
5 changes: 3 additions & 2 deletions modules/repository/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ func initRepoCommit(tmpPath string, repo *models.Repository, u *models.User, def
return nil
}

func checkInitRepository(owner, name string) (err error) {
// CheckInitRepository check and init git repository
func CheckInitRepository(owner, name string) (err error) {
// Somehow the directory could exist.
repoPath := models.RepoPath(owner, name)
isExist, err := util.IsExist(repoPath)
Expand All @@ -198,7 +199,7 @@ func checkInitRepository(owner, name string) (err error) {

// InitRepository initializes README and .gitignore if needed.
func initRepository(ctx context.Context, repoPath string, u *models.User, repo *models.Repository, opts models.CreateRepoOptions) (err error) {
if err = checkInitRepository(repo.OwnerName, repo.Name); err != nil {
if err = CheckInitRepository(repo.OwnerName, repo.Name); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ download_archive = Download Repository

no_desc = No Description
quick_guide = Quick Guide
clone_this_repo = Clone this repository
clone_this_repo = Upload files or Clone this repository
create_new_repo_command = Creating a new repository on the command line
push_exist_repo = Pushing an existing repository from the command line
empty_message = This repository does not contain any content.
Expand Down
4 changes: 4 additions & 0 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,10 @@ func renderCode(ctx *context.Context) {
ctx.Data["PageIsViewCode"] = true

if ctx.Repo.Repository.IsEmpty {
if ctx.Repo.CanWrite(unit_model.TypeCode) {
ctx.Data["CanAddFile"] = true
ctx.Data["CanUploadFile"] = setting.Repository.Upload.Enabled
}
ctx.HTML(http.StatusOK, tplRepoEMPTY)
return
}
Expand Down
3 changes: 3 additions & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,9 @@ func RegisterRoutes(m *web.Route) {
Get(repo.UploadFile).
Post(bindIgnErr(forms.UploadRepoFileForm{}), repo.UploadFilePost)
}, context.RepoRefByType(context.RepoRefBranch), repo.MustBeEditable)
}, context.RepoMustNotBeArchived(), reqRepoCodeWriter)

m.Group("", func() {
m.Group("", func() {
m.Post("/upload-file", repo.UploadFileToServer)
m.Post("/upload-remove", bindIgnErr(forms.RemoveUploadFileForm{}), repo.RemoveUploadFileFromServer)
Expand Down
22 changes: 20 additions & 2 deletions templates/repo/empty.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,26 @@
<div class="ui attached guide table segment">
<div class="item">
<h3>{{.i18n.Tr "repo.clone_this_repo"}} <small>{{.i18n.Tr "repo.clone_helper" "http://git-scm.com/book/en/Git-Basics-Getting-a-Git-Repository" | Str2html}}</small></h3>
<div class="ui action small input">
{{template "repo/clone_buttons" .}}
<div class="left fitted item mr-0 dib" id="file-buttons">
<div class="ui primary buttons">
{{if .Repository.CanEnableEditor}}
{{if .CanAddFile}}
<a href="{{.RepoLink}}/_new/{{EscapePound .BranchName}}/" class="ui basic clone button no-transition">
{{.i18n.Tr "repo.editor.new_file"}}
</a>
{{end}}
{{if .CanUploadFile}}
<a href="{{.RepoLink}}/_upload/{{EscapePound .BranchName}}/" class="ui basic clone button no-transition">
{{.i18n.Tr "repo.editor.upload_file"}}
</a>
{{end}}
{{end}}
</div>
</div>
<div class="fitted item dib">
<div class="ui action small input">
{{template "repo/clone_buttons" .}}
</div>
</div>
</div>

Expand Down