Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #7474 - Handles all redirects for Web UI File CRUD #7478

Merged
merged 10 commits into from
Jul 17, 2019
Prev Previous commit
Next Next commit
Fix for new branch creation and to unit test
  • Loading branch information
richmahn committed Jul 16, 2019
commit d19a1f8af73ad9e5a69073ab035aefbee46badb2
11 changes: 8 additions & 3 deletions routers/repo/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func DeleteFilePost(ctx *context.Context, form auth.DeleteRepoFileForm) {
return
}

if branchName != ctx.Repo.BranchName && !canCommit {
if branchName == ctx.Repo.BranchName && !canCommit {
ctx.Data["Err_NewBranchName"] = true
ctx.Data["commit_choice"] = frmCommitChoiceNewBranch
ctx.RenderWithErr(ctx.Tr("repo.editor.cannot_commit_to_protected_branch", branchName), tplDeleteFile, &form)
Expand Down Expand Up @@ -438,13 +438,18 @@ func DeleteFilePost(ctx *context.Context, form auth.DeleteRepoFileForm) {
ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + ctx.Repo.BranchName + "..." + form.NewBranchName)
} else {
treePath := filepath.Dir(ctx.Repo.TreePath)
if len(treePath) > 0 && treePath != "." {
if treePath == "." {
treePath = "" // the file deleted was in the root, so we return the user to the root directory
}
if len(treePath) > 0 {
// Need to get the latest commit since it changed
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.BranchName)
if err == nil && commit != nil {
// We have the comment, now find what directory we can return the user to
// (must have entries)
treePath = GetClosestParentWithFiles(treePath, commit)
} else {
treePath = ""
treePath = "" // otherwise return them to the root of the repo
}
}
ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(branchName) + "/" + util.PathEscapeSegments(treePath))
Expand Down
11 changes: 9 additions & 2 deletions routers/repo/editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ func TestGetClosestParentWithFiles(t *testing.T) {
gitRepo, _ := git.OpenRepository(repo.RepoPath())
commit, _ := gitRepo.GetBranchCommit(branch)
expectedTreePath := ""
treePath := GetClosestParentWithFiles("dir/dir/dir", commit)
assert.Equal(t, expectedTreePath, treePath)

expectedTreePath = "" // Should return the root dir, empty string, since there are no subdirs in this repo
for _, deletedFile := range []string{
"dir1/dir2/dir3/file.txt",
"file.txt",
} {
treePath := GetClosestParentWithFiles(deletedFile, commit)
assert.Equal(t, expectedTreePath, treePath)
}
}