From b9a0e33238da353ff39258af3f0befbb83da981e Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 28 Jul 2021 21:39:46 -0600 Subject: Pre-fill suggested New File 'name' and 'content' with Query Params (#16556) * feature: add (GitHub-style) querystrings for pre-filling new file content * docs: add query parameters for new files --- routers/web/repo/editor.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'routers/web/repo/editor.go') diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index 792258da84..a99c1c7c61 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -81,7 +81,11 @@ func editFile(ctx *context.Context, isNewFile bool) { return } - treeNames, treePaths := getParentTreeFields(ctx.Repo.TreePath) + // Check if the filename (and additional path) is specified in the querystring + // (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)) if !isNewFile { entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath) @@ -136,7 +140,8 @@ func editFile(ctx *context.Context, isNewFile bool) { ctx.Data["FileContent"] = content } } else { - treeNames = append(treeNames, "") // Append empty string to allow user name the new file. + // Append filename from query, or empty string to allow user name the new file. + treeNames = append(treeNames, fileName) } ctx.Data["TreeNames"] = treeNames -- cgit v1.2.3