@@ -40,17 +40,29 @@ func renderCommitRights(ctx *context.Context) bool {
40
40
return canCommit
41
41
}
42
42
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
+
43
58
func editFile (ctx * context.Context , isNewFile bool ) {
44
59
ctx .Data ["PageIsEdit" ] = true
45
60
ctx .Data ["IsNewFile" ] = isNewFile
46
61
ctx .Data ["RequireHighlightJS" ] = true
47
62
ctx .Data ["RequireSimpleMDE" ] = true
48
63
canCommit := renderCommitRights (ctx )
49
64
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 )
54
66
55
67
if ! isNewFile {
56
68
entry , err := ctx .Repo .Commit .GetTreeEntryByPath (ctx .Repo .TreePath )
@@ -100,6 +112,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
100
112
}
101
113
102
114
ctx .Data ["TreeNames" ] = treeNames
115
+ ctx .Data ["TreePaths" ] = treePaths
103
116
ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/" + ctx .Repo .BranchName
104
117
ctx .Data ["commit_summary" ] = ""
105
118
ctx .Data ["commit_message" ] = ""
@@ -146,14 +159,11 @@ func editFilePost(ctx *context.Context, form auth.EditRepoFileForm, isNewFile bo
146
159
}
147
160
148
161
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 )
154
163
155
164
ctx .Data ["TreePath" ] = form .TreePath
156
165
ctx .Data ["TreeNames" ] = treeNames
166
+ ctx .Data ["TreePaths" ] = treePaths
157
167
ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/" + branchName
158
168
ctx .Data ["FileContent" ] = form .Content
159
169
ctx .Data ["commit_summary" ] = form .CommitSummary
@@ -428,13 +438,14 @@ func UploadFile(ctx *context.Context) {
428
438
renderUploadSettings (ctx )
429
439
canCommit := renderCommitRights (ctx )
430
440
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 { "" }
435
445
}
436
446
437
447
ctx .Data ["TreeNames" ] = treeNames
448
+ ctx .Data ["TreePaths" ] = treePaths
438
449
ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/" + ctx .Repo .BranchName
439
450
ctx .Data ["commit_summary" ] = ""
440
451
ctx .Data ["commit_message" ] = ""
@@ -462,15 +473,15 @@ func UploadFilePost(ctx *context.Context, form auth.UploadRepoFileForm) {
462
473
}
463
474
464
475
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 {"" }
470
480
}
471
481
472
482
ctx .Data ["TreePath" ] = form .TreePath
473
483
ctx .Data ["TreeNames" ] = treeNames
484
+ ctx .Data ["TreePaths" ] = treePaths
474
485
ctx .Data ["BranchLink" ] = ctx .Repo .RepoLink + "/src/" + branchName
475
486
ctx .Data ["commit_summary" ] = form .CommitSummary
476
487
ctx .Data ["commit_message" ] = form .CommitMessage
0 commit comments