Skip to content

Commit 736ee8d

Browse files
committed
fix
1 parent 7681d58 commit 736ee8d

File tree

13 files changed

+96
-66
lines changed

13 files changed

+96
-66
lines changed

models/repo/repo.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ func (repo *Repository) IsBroken() bool {
225225
return repo.Status == RepositoryBroken
226226
}
227227

228+
// MarkAsBrokenEmpty marks the repo as broken and empty
229+
func (repo *Repository) MarkAsBrokenEmpty() {
230+
repo.Status = RepositoryBroken
231+
repo.IsEmpty = true
232+
}
233+
228234
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
229235
func (repo *Repository) AfterLoad() {
230236
repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
@@ -729,7 +735,7 @@ func IsRepositoryExist(ctx context.Context, u *user_model.User, repoName string)
729735
return false, err
730736
}
731737
isDir, err := util.IsDir(RepoPath(u.Name, repoName))
732-
return has && isDir, err
738+
return has || isDir, err
733739
}
734740

735741
// GetTemplateRepo populates repo.TemplateRepo for a generated repository and

modules/context/context.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func (ctx *Context) serverErrorInternal(logMsg string, logErr error) {
301301

302302
// it's safe to show internal error to admin users, and it helps
303303
if !setting.IsProd || (ctx.Doer != nil && ctx.Doer.IsAdmin) {
304-
ctx.Data["ErrorMsg"] = logErr
304+
ctx.Data["ErrorMsg"] = fmt.Sprintf("%s, %s", logMsg, logErr)
305305
}
306306
}
307307

modules/context/repo.go

+22-13
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ func (r *Repository) CanCreateIssueDependencies(user *user_model.User, isPull bo
184184

185185
// GetCommitsCount returns cached commit count for current view
186186
func (r *Repository) GetCommitsCount() (int64, error) {
187+
if r.Commit == nil {
188+
return 0, nil
189+
}
187190
var contextName string
188191
if r.IsViewBranch {
189192
contextName = r.BranchName
@@ -642,8 +645,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
642645
if err != nil {
643646
if strings.Contains(err.Error(), "repository does not exist") || strings.Contains(err.Error(), "no such file or directory") {
644647
log.Error("Repository %-v has a broken repository on the file system: %s Error: %v", ctx.Repo.Repository, ctx.Repo.Repository.RepoPath(), err)
645-
ctx.Repo.Repository.Status = repo_model.RepositoryBroken
646-
ctx.Repo.Repository.IsEmpty = true
648+
ctx.Repo.Repository.MarkAsBrokenEmpty()
647649
ctx.Data["BranchName"] = ctx.Repo.Repository.DefaultBranch
648650
// Only allow access to base of repo or settings
649651
if !isHomeOrSettings {
@@ -878,6 +880,10 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
878880
return func(ctx *Context) (cancel context.CancelFunc) {
879881
// Empty repository does not have reference information.
880882
if ctx.Repo.Repository.IsEmpty {
883+
// assume the user is viewing the (non-existent) default branch
884+
ctx.Repo.IsViewBranch = true
885+
ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
886+
ctx.Data["TreePath"] = ctx.Repo.BranchName
881887
return
882888
}
883889

@@ -907,27 +913,30 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
907913
refName = ctx.Repo.Repository.DefaultBranch
908914
if !ctx.Repo.GitRepo.IsBranchExist(refName) {
909915
brs, _, err := ctx.Repo.GitRepo.GetBranchNames(0, 0)
910-
if err != nil {
911-
ctx.ServerError("GetBranches", err)
912-
return
916+
if err == nil && len(brs) != 0 {
917+
refName = brs[0]
913918
} else if len(brs) == 0 {
914-
err = fmt.Errorf("No branches in non-empty repository %s",
915-
ctx.Repo.GitRepo.Path)
916-
ctx.ServerError("GetBranches", err)
917-
return
919+
log.Error("No branches in non-empty repository %s", ctx.Repo.GitRepo.Path)
920+
ctx.Repo.Repository.MarkAsBrokenEmpty()
921+
} else {
922+
log.Error("GetBranches error: %v", err)
923+
ctx.Repo.Repository.MarkAsBrokenEmpty()
918924
}
919-
refName = brs[0]
920925
}
921926
ctx.Repo.RefName = refName
922927
ctx.Repo.BranchName = refName
923928
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName)
924-
if err != nil {
929+
if err == nil {
930+
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
931+
} else if strings.Contains(err.Error(), "fatal: not a git repository") || strings.Contains(err.Error(), "object does not exist") {
932+
// if the repository is broken, we can continue to the handler code, to show "Settings -> Delete Repository" for end users
933+
log.Error("GetBranchCommit: %v", err)
934+
ctx.Repo.Repository.MarkAsBrokenEmpty()
935+
} else {
925936
ctx.ServerError("GetBranchCommit", err)
926937
return
927938
}
928-
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
929939
ctx.Repo.IsViewBranch = true
930-
931940
} else {
932941
refName = getRefName(ctx, refType)
933942
ctx.Repo.RefName = refName

modules/git/command.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,18 @@ type RunOpts struct {
211211
Env []string
212212
Timeout time.Duration
213213
UseContextTimeout bool
214-
Dir string
215-
Stdout, Stderr io.Writer
216-
Stdin io.Reader
217-
PipelineFunc func(context.Context, context.CancelFunc) error
214+
215+
// Dir is the working dir for the git command, however:
216+
// FIXME: this could be incorrect in many cases, for example:
217+
// * /some/path/.git
218+
// * /some/path/.git/gitea-data/data/repositories/user/repo.git
219+
// If "user/repo.git" is invalid/broken, then running git command in it will use "/some/path/.git", and produce unexpected results
220+
// The correct approach is to use `--git-dir" global argument
221+
Dir string
222+
223+
Stdout, Stderr io.Writer
224+
Stdin io.Reader
225+
PipelineFunc func(context.Context, context.CancelFunc) error
218226
}
219227

220228
func commonBaseEnvs() []string {

modules/git/repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func InitRepository(ctx context.Context, repoPath string, bare bool) error {
8080
// IsEmpty Check if repository is empty.
8181
func (repo *Repository) IsEmpty() (bool, error) {
8282
var errbuf, output strings.Builder
83-
if err := NewCommand(repo.Ctx, "show-ref", "--head", "^HEAD$").
83+
if err := NewCommand(repo.Ctx).AddOptionFormat("--git-dir=%s", repo.Path).AddArguments("show-ref", "--head", "^HEAD$").
8484
Run(&RunOpts{
8585
Dir: repo.Path,
8686
Stdout: &output,

modules/git/repo_base_nogogit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
6161
}
6262

6363
repo.batchWriter, repo.batchReader, repo.batchCancel = CatFileBatch(ctx, repoPath)
64-
repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(ctx, repo.Path)
64+
repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(ctx, repoPath)
6565

6666
return repo, nil
6767
}

routers/web/repo/editor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
8282
}
8383

8484
// Check if the filename (and additional path) is specified in the querystring
85-
// (filename is a misnomer, but kept for compatibility with Github)
85+
// (filename is a misnomer, but kept for compatibility with GitHub)
8686
filePath, fileName := path.Split(ctx.Req.URL.Query().Get("filename"))
8787
filePath = strings.Trim(filePath, "/")
8888
treeNames, treePaths := getParentTreeFields(path.Join(ctx.Repo.TreePath, filePath))

routers/web/repo/view.go

+19-22
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,6 @@ func renderDirectory(ctx *context.Context, treeLink string) {
154154
ctx.Data["Title"] = ctx.Tr("repo.file.title", ctx.Repo.Repository.Name+"/"+path.Base(ctx.Repo.TreePath), ctx.Repo.RefName)
155155
}
156156

157-
// Check permission to add or upload new file.
158-
if ctx.Repo.CanWrite(unit_model.TypeCode) && ctx.Repo.IsViewBranch {
159-
ctx.Data["CanAddFile"] = !ctx.Repo.Repository.IsArchived
160-
ctx.Data["CanUploadFile"] = setting.Repository.Upload.Enabled && !ctx.Repo.Repository.IsArchived
161-
}
162-
163-
if ctx.Written() {
164-
return
165-
}
166-
167157
subfolder, readmeFile, err := findReadmeFileInEntries(ctx, entries, true)
168158
if err != nil {
169159
ctx.ServerError("findReadmeFileInEntries", err)
@@ -868,21 +858,25 @@ func renderRepoTopics(ctx *context.Context) {
868858

869859
func renderCode(ctx *context.Context) {
870860
ctx.Data["PageIsViewCode"] = true
861+
ctx.Data["RepositoryUploadEnabled"] = setting.Repository.Upload.Enabled
871862

872-
if ctx.Repo.Repository.IsEmpty {
873-
reallyEmpty := true
863+
if ctx.Repo.Commit == nil || ctx.Repo.Repository.IsEmpty || ctx.Repo.Repository.IsBroken() {
864+
showEmpty := true
874865
var err error
875866
if ctx.Repo.GitRepo != nil {
876-
reallyEmpty, err = ctx.Repo.GitRepo.IsEmpty()
867+
showEmpty, err = ctx.Repo.GitRepo.IsEmpty()
877868
if err != nil {
878-
ctx.ServerError("GitRepo.IsEmpty", err)
879-
return
869+
log.Error("GitRepo.IsEmpty: %v", err)
870+
ctx.Repo.Repository.Status = repo_model.RepositoryBroken
871+
showEmpty = true
872+
ctx.Flash.Error(ctx.Tr("error.occurred"), true)
880873
}
881874
}
882-
if reallyEmpty {
875+
if showEmpty {
883876
ctx.HTML(http.StatusOK, tplRepoEMPTY)
884877
return
885878
}
879+
886880
// the repo is not really empty, so we should update the modal in database
887881
// such problem may be caused by:
888882
// 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) {
898892
ctx.ServerError("UpdateRepoSize", err)
899893
return
900894
}
895+
896+
// the repo's IsEmpty has been updated, redirect to this page to make sure middlewares can get the correct values
897+
link := ctx.Link
898+
if ctx.Req.URL.RawQuery != "" {
899+
link += "?" + ctx.Req.URL.RawQuery
900+
}
901+
ctx.Redirect(link)
902+
return
901903
}
902904

903905
title := ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name
@@ -927,12 +929,7 @@ func renderCode(ctx *context.Context) {
927929
return
928930
}
929931

930-
if !ctx.Repo.Repository.IsEmpty {
931-
checkCitationFile(ctx, entry)
932-
if ctx.Written() {
933-
return
934-
}
935-
}
932+
checkCitationFile(ctx, entry)
936933

937934
renderLanguageStats(ctx)
938935
if ctx.Written() {

routers/web/web.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ func RegisterRoutes(m *web.Route) {
11841184
m.Post("/upload-file", repo.UploadFileToServer)
11851185
m.Post("/upload-remove", web.Bind(forms.RemoveUploadFileForm{}), repo.RemoveUploadFileFromServer)
11861186
}, repo.MustBeEditable, repo.MustBeAbleToUpload)
1187-
}, context.RepoRef(), canEnableEditor, context.RepoMustNotBeArchived(), repo.MustBeNotEmpty)
1187+
}, context.RepoRef(), canEnableEditor, context.RepoMustNotBeArchived())
11881188

11891189
m.Group("/branches", func() {
11901190
m.Group("/_new", func() {

templates/repo/editor/commit_form.tmpl

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
</label>
4040
</div>
4141
</div>
42+
{{if not .Repository.IsEmpty}}
4243
<div class="field">
4344
{{$pullRequestEnabled := .Repository.UnitEnabled $.Context $.UnitTypePullRequests}}
4445
{{$prUnit := .Repository.MustGetUnit $.Context $.UnitTypePullRequests}}
@@ -65,6 +66,7 @@
6566
<span class="text-muted js-quick-pull-normalization-info"></span>
6667
</div>
6768
</div>
69+
{{end}}
6870
</div>
6971
</div>
7072
<button id="commit-button" type="submit" class="ui green button">

templates/repo/empty.tmpl

+15-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,21 @@
2121
<div class="ui attached guide table segment empty-repo-guide">
2222
<div class="item">
2323
<h3>{{.locale.Tr "repo.clone_this_repo"}} <small>{{.locale.Tr "repo.clone_helper" "http://git-scm.com/book/en/Git-Basics-Getting-a-Git-Repository" | Str2html}}</small></h3>
24-
<div class="ui action small input">
25-
{{template "repo/clone_buttons" .}}
24+
25+
<div class="gt-df">
26+
{{if and .CanWriteCode (not .Repository.IsArchived)}}
27+
<a class="ui small button" href="{{.RepoLink}}/_new/{{.BranchName | PathEscapeSegments}}/">
28+
{{.locale.Tr "repo.editor.new_file"}}
29+
</a>
30+
{{if .RepositoryUploadEnabled}}
31+
<a class="ui small button" href="{{.RepoLink}}/_upload/{{.BranchName | PathEscapeSegments}}/">
32+
{{.locale.Tr "repo.editor.upload_file"}}
33+
</a>
34+
{{end}}
35+
{{end}}
36+
<div class="ui action small input gt-df gt-f1">
37+
{{template "repo/clone_buttons" .}}
38+
</div>
2639
</div>
2740
</div>
2841

templates/repo/home.tmpl

+13-15
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,27 @@
7171
{{end}}
7272
<a href="{{.Repository.Link}}/find/{{.BranchNameSubURL}}" class="ui compact basic button">{{.locale.Tr "repo.find_file.go_to_file"}}</a>
7373
{{end}}
74-
{{if or .CanAddFile .CanUploadFile}}
74+
75+
{{if and .CanWriteCode .IsViewBranch (not .Repository.IsArchived)}}
7576
<button class="ui basic compact dropdown jump icon button gt-mr-2"{{if not .Repository.CanEnableEditor}} disabled{{end}}>
7677
<span class="text">{{.locale.Tr "repo.editor.add_file"}}</span>
7778
<div class="menu">
78-
{{if .CanAddFile}}
79-
<a class="item" href="{{.RepoLink}}/_new/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">
80-
{{.locale.Tr "repo.editor.new_file"}}
81-
</a>
82-
{{end}}
83-
{{if .CanUploadFile}}
84-
<a class="item" href="{{.RepoLink}}/_upload/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">
85-
{{.locale.Tr "repo.editor.upload_file"}}
86-
</a>
87-
{{end}}
88-
{{if .CanAddFile}}
89-
<a class="item" href="{{.RepoLink}}/_diffpatch/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">
90-
{{.locale.Tr "repo.editor.patch"}}
91-
</a>
79+
<a class="item" href="{{.RepoLink}}/_new/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">
80+
{{.locale.Tr "repo.editor.new_file"}}
81+
</a>
82+
{{if .RepositoryUploadEnabled}}
83+
<a class="item" href="{{.RepoLink}}/_upload/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">
84+
{{.locale.Tr "repo.editor.upload_file"}}
85+
</a>
9286
{{end}}
87+
<a class="item" href="{{.RepoLink}}/_diffpatch/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">
88+
{{.locale.Tr "repo.editor.patch"}}
89+
</a>
9390
</div>
9491
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
9592
</button>
9693
{{end}}
94+
9795
{{if and (eq $n 0) (.Repository.IsTemplate)}}
9896
<a role="button" class="ui primary compact button" href="{{AppSubUrl}}/repo/create?template_id={{.Repository.ID}}">
9997
{{.locale.Tr "repo.use_template"}}

web_src/css/repository.css

+1-4
Original file line numberDiff line numberDiff line change
@@ -1911,15 +1911,12 @@
19111911
border-radius: var(--border-radius) 0 0 var(--border-radius);
19121912
}
19131913

1914-
.repository.quickstart .guide .ui.action.small.input {
1915-
width: 100%;
1916-
}
1917-
19181914
.repository.quickstart .guide #repo-clone-url {
19191915
border-radius: 0;
19201916
padding: 5px 10px;
19211917
font-size: 1.2em;
19221918
line-height: 1.4;
1919+
flex: 1
19231920
}
19241921

19251922
.repository.release #release-list {

0 commit comments

Comments
 (0)