Skip to content

Commit f5dd276

Browse files
committed
allow create new file on empty repository
1 parent ccacb8a commit f5dd276

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

modules/context/repo.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type Repository struct {
7474

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

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

modules/repofiles/update.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,11 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *Up
131131
opts.NewBranch = opts.OldBranch
132132
}
133133

134-
// oldBranch must exist for this operation
135-
if _, err := repo_module.GetBranch(repo, opts.OldBranch); err != nil {
136-
return nil, err
134+
if !repo.IsEmpty {
135+
// oldBranch must exist for this operation
136+
if _, err := repo_module.GetBranch(repo, opts.OldBranch); err != nil {
137+
return nil, err
138+
}
137139
}
138140

139141
// A NewBranch can be specified for the file to be created/updated in a new branch.
@@ -153,6 +155,8 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *Up
153155
return nil, err
154156
}
155157

158+
fmt.Println("2-------")
159+
156160
// If FromTreePath is not set, set it to the opts.TreePath
157161
if opts.TreePath != "" && opts.FromTreePath == "" {
158162
opts.FromTreePath = opts.TreePath
@@ -204,7 +208,6 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *Up
204208
return nil, fmt.Errorf("DeleteRepoFile: Invalid last commit ID: %v", err)
205209
}
206210
opts.LastCommitID = lastCommitID.String()
207-
208211
}
209212

210213
encoding := "UTF-8"

options/locale/locale_en-US.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ download_archive = Download Repository
942942

943943
no_desc = No Description
944944
quick_guide = Quick Guide
945-
clone_this_repo = Clone this repository
945+
clone_this_repo = Upload files or Clone this repository
946946
create_new_repo_command = Creating a new repository on the command line
947947
push_exist_repo = Pushing an existing repository from the command line
948948
empty_message = This repository does not contain any content.

routers/web/repo/view.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -826,15 +826,11 @@ func renderCode(ctx *context.Context) {
826826
ctx.Data["PageIsViewCode"] = true
827827

828828
if ctx.Repo.Repository.IsEmpty {
829-
<<<<<<< HEAD:routers/web/repo/view.go
830-
ctx.HTML(http.StatusOK, tplRepoEMPTY)
831-
=======
832829
if ctx.Repo.CanWrite(models.UnitTypeCode) {
833830
ctx.Data["CanAddFile"] = true
834831
ctx.Data["CanUploadFile"] = setting.Repository.Upload.Enabled
835832
}
836-
ctx.HTML(200, tplRepoEMPTY)
837-
>>>>>>> 7d4422307 (Add buttons on empty repository page):routers/repo/view.go
833+
ctx.HTML(http.StatusOK, tplRepoEMPTY)
838834
return
839835
}
840836

routers/web/web.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,16 @@ func RegisterRoutes(m *web.Route) {
791791
Get(repo.UploadFile).
792792
Post(bindIgnErr(forms.UploadRepoFileForm{}), repo.UploadFilePost)
793793
}, context.RepoRefByType(context.RepoRefBranch), repo.MustBeEditable)
794+
}, context.RepoMustNotBeArchived(), reqRepoCodeWriter)
795+
796+
m.Group("", func() {
797+
m.Group("", func() {
798+
m.Combo("/_edit/*").Get(repo.EditFile).
799+
Post(bindIgnErr(auth.EditRepoFileForm{}), repo.EditFilePost)
800+
m.Post("/_preview/*", bindIgnErr(auth.EditPreviewDiffForm{}), repo.DiffPreviewPost)
801+
m.Combo("/_delete/*").Get(repo.DeleteFile).
802+
Post(bindIgnErr(auth.DeleteRepoFileForm{}), repo.DeleteFilePost)
803+
}, context.RepoRefByType(context.RepoRefBranch), repo.MustBeEditable)
794804
m.Group("", func() {
795805
m.Post("/upload-file", repo.UploadFileToServer)
796806
m.Post("/upload-remove", bindIgnErr(forms.RemoveUploadFileForm{}), repo.RemoveUploadFileFromServer)

0 commit comments

Comments
 (0)