diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-04-19 21:40:42 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-19 21:40:42 +0800 |
commit | e422342eebc18034ef586ec58f1e2fff0340091d (patch) | |
tree | 307264b46c1683915429083d54e9634ee4f2fc4d /routers | |
parent | 01214c8ada993bf5f54a4149979d140443d69410 (diff) | |
download | gitea-e422342eebc18034ef586ec58f1e2fff0340091d.tar.gz gitea-e422342eebc18034ef586ec58f1e2fff0340091d.zip |
Allow adding new files to an empty repo (#24164)
![image](https://user-images.githubusercontent.com/2114189/232561612-2bfcfd0a-fc04-47ba-965f-5d0bcea46c54.png)
Diffstat (limited to 'routers')
-rw-r--r-- | routers/init.go | 2 | ||||
-rw-r--r-- | routers/web/repo/editor.go | 44 | ||||
-rw-r--r-- | routers/web/repo/view.go | 42 | ||||
-rw-r--r-- | routers/web/web.go | 2 |
4 files changed, 49 insertions, 41 deletions
diff --git a/routers/init.go b/routers/init.go index c539975aca..af768abbf4 100644 --- a/routers/init.go +++ b/routers/init.go @@ -108,6 +108,7 @@ func GlobalInitInstalled(ctx context.Context) { } mustInitCtx(ctx, git.InitFull) + log.Info("Gitea Version: %s%s", setting.AppVer, setting.AppBuiltWith) log.Info("Git Version: %s (home: %s)", git.VersionInfo(), git.HomeDir()) log.Info("AppPath: %s", setting.AppPath) log.Info("AppWorkPath: %s", setting.AppWorkPath) @@ -115,7 +116,6 @@ func GlobalInitInstalled(ctx context.Context) { log.Info("Log path: %s", setting.Log.RootPath) log.Info("Configuration file: %s", setting.CustomConf) log.Info("Run Mode: %s", util.ToTitleCase(setting.RunMode)) - log.Info("Gitea v%s%s", setting.AppVer, setting.AppBuiltWith) // Setup i18n translation.InitLocales(ctx) diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index 476c1d5ddd..63387df281 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -82,7 +82,7 @@ func editFile(ctx *context.Context, isNewFile bool) { } // Check if the filename (and additional path) is specified in the querystring - // (filename is a misnomer, but kept for compatibility with Github) + // (filename is a misnomer, but kept for compatibility with GitHub) filePath, fileName := path.Split(ctx.Req.URL.Query().Get("filename")) filePath = strings.Trim(filePath, "/") treeNames, treePaths := getParentTreeFields(path.Join(ctx.Repo.TreePath, filePath)) @@ -327,6 +327,10 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b } } + if ctx.Repo.Repository.IsEmpty { + _ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, IsEmpty: false}, "is_empty") + } + if form.CommitChoice == frmCommitChoiceNewBranch && ctx.Repo.Repository.UnitEnabled(ctx, unit.TypePullRequests) { ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ctx.Repo.BranchName) + "..." + util.PathEscapeSegments(form.NewBranchName)) } else { @@ -617,25 +621,25 @@ func UploadFilePost(ctx *context.Context) { return } - var newTreePath string - for _, part := range treeNames { - newTreePath = path.Join(newTreePath, part) - entry, err := ctx.Repo.Commit.GetTreeEntryByPath(newTreePath) - if err != nil { - if git.IsErrNotExist(err) { - // Means there is no item with that name, so we're good - break + if !ctx.Repo.Repository.IsEmpty { + var newTreePath string + for _, part := range treeNames { + newTreePath = path.Join(newTreePath, part) + entry, err := ctx.Repo.Commit.GetTreeEntryByPath(newTreePath) + if err != nil { + if git.IsErrNotExist(err) { + break // Means there is no item with that name, so we're good + } + ctx.ServerError("Repo.Commit.GetTreeEntryByPath", err) + return } - ctx.ServerError("Repo.Commit.GetTreeEntryByPath", err) - return - } - - // User can only upload files to a directory. - if !entry.IsDir() { - ctx.Data["Err_TreePath"] = true - ctx.RenderWithErr(ctx.Tr("repo.editor.directory_is_a_file", part), tplUploadFile, &form) - return + // User can only upload files to a directory, the directory name shouldn't be an existing file. + if !entry.IsDir() { + ctx.Data["Err_TreePath"] = true + ctx.RenderWithErr(ctx.Tr("repo.editor.directory_is_a_file", part), tplUploadFile, &form) + return + } } } @@ -714,6 +718,10 @@ func UploadFilePost(ctx *context.Context) { return } + if ctx.Repo.Repository.IsEmpty { + _ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, IsEmpty: false}, "is_empty") + } + if form.CommitChoice == frmCommitChoiceNewBranch && ctx.Repo.Repository.UnitEnabled(ctx, unit.TypePullRequests) { ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ctx.Repo.BranchName) + "..." + util.PathEscapeSegments(form.NewBranchName)) } else { diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 150050f76b..5a11073ba9 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -154,16 +154,6 @@ func renderDirectory(ctx *context.Context, treeLink string) { ctx.Data["Title"] = ctx.Tr("repo.file.title", ctx.Repo.Repository.Name+"/"+path.Base(ctx.Repo.TreePath), ctx.Repo.RefName) } - // Check permission to add or upload new file. - if ctx.Repo.CanWrite(unit_model.TypeCode) && ctx.Repo.IsViewBranch { - ctx.Data["CanAddFile"] = !ctx.Repo.Repository.IsArchived - ctx.Data["CanUploadFile"] = setting.Repository.Upload.Enabled && !ctx.Repo.Repository.IsArchived - } - - if ctx.Written() { - return - } - subfolder, readmeFile, err := findReadmeFileInEntries(ctx, entries, true) if err != nil { ctx.ServerError("findReadmeFileInEntries", err) @@ -868,21 +858,25 @@ func renderRepoTopics(ctx *context.Context) { func renderCode(ctx *context.Context) { ctx.Data["PageIsViewCode"] = true + ctx.Data["RepositoryUploadEnabled"] = setting.Repository.Upload.Enabled - if ctx.Repo.Repository.IsEmpty { - reallyEmpty := true + if ctx.Repo.Commit == nil || ctx.Repo.Repository.IsEmpty || ctx.Repo.Repository.IsBroken() { + showEmpty := true var err error if ctx.Repo.GitRepo != nil { - reallyEmpty, err = ctx.Repo.GitRepo.IsEmpty() + showEmpty, err = ctx.Repo.GitRepo.IsEmpty() if err != nil { - ctx.ServerError("GitRepo.IsEmpty", err) - return + log.Error("GitRepo.IsEmpty: %v", err) + ctx.Repo.Repository.Status = repo_model.RepositoryBroken + showEmpty = true + ctx.Flash.Error(ctx.Tr("error.occurred"), true) } } - if reallyEmpty { + if showEmpty { ctx.HTML(http.StatusOK, tplRepoEMPTY) return } + // the repo is not really empty, so we should update the modal in database // such problem may be caused by: // 1) an error occurs during pushing/receiving. 2) the user replaces an empty git repo manually @@ -898,6 +892,14 @@ func renderCode(ctx *context.Context) { ctx.ServerError("UpdateRepoSize", err) return } + + // the repo's IsEmpty has been updated, redirect to this page to make sure middlewares can get the correct values + link := ctx.Link + if ctx.Req.URL.RawQuery != "" { + link += "?" + ctx.Req.URL.RawQuery + } + ctx.Redirect(link) + return } title := ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name @@ -927,11 +929,9 @@ func renderCode(ctx *context.Context) { return } - if !ctx.Repo.Repository.IsEmpty { - checkCitationFile(ctx, entry) - if ctx.Written() { - return - } + checkCitationFile(ctx, entry) + if ctx.Written() { + return } renderLanguageStats(ctx) diff --git a/routers/web/web.go b/routers/web/web.go index a4a1b7113c..30a8314691 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1184,7 +1184,7 @@ func RegisterRoutes(m *web.Route) { m.Post("/upload-file", repo.UploadFileToServer) m.Post("/upload-remove", web.Bind(forms.RemoveUploadFileForm{}), repo.RemoveUploadFileFromServer) }, repo.MustBeEditable, repo.MustBeAbleToUpload) - }, context.RepoRef(), canEnableEditor, context.RepoMustNotBeArchived(), repo.MustBeNotEmpty) + }, context.RepoRef(), canEnableEditor, context.RepoMustNotBeArchived()) m.Group("/branches", func() { m.Group("/_new", func() { |