Skip to content

Commit 2282475

Browse files
authored
repo/editor: fix breadcrumb path cuts parent dirs (#3859) (#2032)
1 parent 75b5be2 commit 2282475

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

routers/repo/editor.go

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,29 @@ func renderCommitRights(ctx *context.Context) bool {
4040
return canCommit
4141
}
4242

43+
// getParentTreeFields returns list of parent tree names and corresponding tree paths
44+
// based on given tree path.
45+
func getParentTreeFields(treePath string) (treeNames []string, treePaths []string) {
46+
if len(treePath) == 0 {
47+
return treeNames, treePaths
48+
}
49+
50+
treeNames = strings.Split(treePath, "/")
51+
treePaths = make([]string, len(treeNames))
52+
for i := range treeNames {
53+
treePaths[i] = strings.Join(treeNames[:i+1], "/")
54+
}
55+
return treeNames, treePaths
56+
}
57+
4358
func editFile(ctx *context.Context, isNewFile bool) {
4459
ctx.Data["PageIsEdit"] = true
4560
ctx.Data["IsNewFile"] = isNewFile
4661
ctx.Data["RequireHighlightJS"] = true
4762
ctx.Data["RequireSimpleMDE"] = true
4863
canCommit := renderCommitRights(ctx)
4964

50-
var treeNames []string
51-
if len(ctx.Repo.TreePath) > 0 {
52-
treeNames = strings.Split(ctx.Repo.TreePath, "/")
53-
}
65+
treeNames, treePaths := getParentTreeFields(ctx.Repo.TreePath)
5466

5567
if !isNewFile {
5668
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
@@ -100,6 +112,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
100112
}
101113

102114
ctx.Data["TreeNames"] = treeNames
115+
ctx.Data["TreePaths"] = treePaths
103116
ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName
104117
ctx.Data["commit_summary"] = ""
105118
ctx.Data["commit_message"] = ""
@@ -146,14 +159,11 @@ func editFilePost(ctx *context.Context, form auth.EditRepoFileForm, isNewFile bo
146159
}
147160

148161
form.TreePath = strings.Trim(form.TreePath, " /")
149-
150-
var treeNames []string
151-
if len(form.TreePath) > 0 {
152-
treeNames = strings.Split(form.TreePath, "/")
153-
}
162+
treeNames, treePaths := getParentTreeFields(form.TreePath)
154163

155164
ctx.Data["TreePath"] = form.TreePath
156165
ctx.Data["TreeNames"] = treeNames
166+
ctx.Data["TreePaths"] = treePaths
157167
ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + branchName
158168
ctx.Data["FileContent"] = form.Content
159169
ctx.Data["commit_summary"] = form.CommitSummary
@@ -428,13 +438,14 @@ func UploadFile(ctx *context.Context) {
428438
renderUploadSettings(ctx)
429439
canCommit := renderCommitRights(ctx)
430440

431-
// We must at least have one element for user to input.
432-
treeNames := []string{""}
433-
if len(ctx.Repo.TreePath) > 0 {
434-
treeNames = strings.Split(ctx.Repo.TreePath, "/")
441+
treeNames, treePaths := getParentTreeFields(ctx.Repo.TreePath)
442+
if len(treeNames) == 0 {
443+
// We must at least have one element for user to input.
444+
treeNames = []string{""}
435445
}
436446

437447
ctx.Data["TreeNames"] = treeNames
448+
ctx.Data["TreePaths"] = treePaths
438449
ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName
439450
ctx.Data["commit_summary"] = ""
440451
ctx.Data["commit_message"] = ""
@@ -462,15 +473,15 @@ func UploadFilePost(ctx *context.Context, form auth.UploadRepoFileForm) {
462473
}
463474

464475
form.TreePath = strings.Trim(form.TreePath, " /")
465-
466-
// We must at least have one element for user to input.
467-
treeNames := []string{""}
468-
if len(form.TreePath) > 0 {
469-
treeNames = strings.Split(form.TreePath, "/")
476+
treeNames, treePaths := getParentTreeFields(form.TreePath)
477+
if len(treeNames) == 0 {
478+
// We must at least have one element for user to input.
479+
treeNames = []string{""}
470480
}
471481

472482
ctx.Data["TreePath"] = form.TreePath
473483
ctx.Data["TreeNames"] = treeNames
484+
ctx.Data["TreePaths"] = treePaths
474485
ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + branchName
475486
ctx.Data["commit_summary"] = form.CommitSummary
476487
ctx.Data["commit_message"] = form.CommitMessage

templates/repo/editor/edit.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<input id="file-name" value="{{$v}}" placeholder="{{$.i18n.Tr "repo.editor.name_your_file"}}" data-ec-url-prefix="{{$.EditorconfigURLPrefix}}" required autofocus>
1919
<span class="octicon octicon-info poping up" data-content="{{$.i18n.Tr "repo.editor.filename_help"}}" data-position="bottom center" data-variation="tiny inverted"></span>
2020
{{else}}
21-
<span class="section"><a href="{{EscapePound $.BranchLink}}/{{EscapePound $v}}">{{$v}}</a></span>
21+
<span class="section"><a href="{{EscapePound $.BranchLink}}/{{index $.TreePaths $i | EscapePound}}">{{$v}}</a></span>
2222
{{end}}
2323
{{end}}
2424
<span>{{.i18n.Tr "repo.editor.or"}} <a href="{{EscapePound $.BranchLink}}{{if not .IsNewFile}}/{{EscapePound .TreePath}}{{end}}">{{.i18n.Tr "repo.editor.cancel_lower"}}</a></span>

templates/repo/editor/upload.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<input type="text" id="file-name" value="{{$v}}" placeholder="{{$.i18n.Tr "repo.editor.add_subdir"}}" autofocus>
1818
<span class="octicon octicon-info poping up" data-content="{{$.i18n.Tr "repo.editor.filename_help"}}" data-position="bottom center" data-variation="tiny inverted"></span>
1919
{{else}}
20-
<span class="section"><a href="{{EscapePound $.BranchLink}}/{{EscapePound $v}}">{{$v}}</a></span>
20+
<span class="section"><a href="{{EscapePound $.BranchLink}}/{{index $.TreePaths $i | EscapePound}}">{{$v}}</a></span>
2121
{{end}}
2222
{{end}}
2323
<span>{{.i18n.Tr "repo.editor.or"}} <a href="{{EscapePound $.BranchLink}}{{if not .IsNewFile}}/{{EscapePound .TreePath}}{{end}}">{{.i18n.Tr "repo.editor.cancel_lower"}}</a></span>

0 commit comments

Comments
 (0)