Skip to content

Commit 871065b

Browse files
committed
feat(repo): enable delete directory
1 parent 457f17f commit 871065b

File tree

4 files changed

+64
-15
lines changed

4 files changed

+64
-15
lines changed

options/locale/locale_en-US.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,8 @@ editor.delete_this_file = Delete File
13581358
editor.delete_this_directory = Delete Directory
13591359
editor.must_have_write_access = You must have write access to make or propose changes to this file.
13601360
editor.file_delete_success = File "%s" has been deleted.
1361+
editor.directory_delete_success = Directory "%s" has been deleted.
1362+
editor.delete_directory = Delete directory '%s'
13611363
editor.name_your_file = Name your file…
13621364
editor.filename_help = Add a directory by typing its name followed by a slash ('/'). Remove a directory by typing backspace at the beginning of the input field.
13631365
editor.or = or

routers/web/repo/editor.go

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -384,35 +384,81 @@ func DeleteFile(ctx *context.Context) {
384384
ctx.HTML(http.StatusOK, tplDeleteFile)
385385
}
386386

387-
// DeleteFilePost response for deleting file
387+
// DeleteFilePost response for deleting file or directory
388388
func DeleteFilePost(ctx *context.Context) {
389389
parsed := prepareEditorCommitSubmittedForm[*forms.DeleteRepoFileForm](ctx)
390390
if ctx.Written() {
391391
return
392392
}
393393

394394
treePath := ctx.Repo.TreePath
395-
_, err := files_service.ChangeRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, &files_service.ChangeRepoFilesOptions{
396-
LastCommitID: parsed.form.LastCommit,
397-
OldBranch: parsed.OldBranchName,
398-
NewBranch: parsed.NewBranchName,
399-
Files: []*files_service.ChangeRepoFile{
395+
396+
// Check if the path is a directory
397+
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treePath)
398+
if err != nil {
399+
ctx.NotFoundOrServerError("GetTreeEntryByPath", git.IsErrNotExist, err)
400+
return
401+
}
402+
403+
var filesToDelete []*files_service.ChangeRepoFile
404+
var commitMessage string
405+
406+
if entry.IsDir() {
407+
// Get all files in the directory recursively
408+
tree, err := ctx.Repo.Commit.SubTree(treePath)
409+
if err != nil {
410+
ctx.ServerError("SubTree", err)
411+
return
412+
}
413+
414+
entries, err := tree.ListEntriesRecursiveFast()
415+
if err != nil {
416+
ctx.ServerError("ListEntriesRecursiveFast", err)
417+
return
418+
}
419+
420+
// Create delete operations for all files in the directory
421+
for _, e := range entries {
422+
if !e.IsDir() && !e.IsSubModule() {
423+
filesToDelete = append(filesToDelete, &files_service.ChangeRepoFile{
424+
Operation: "delete",
425+
TreePath: treePath + "/" + e.Name(),
426+
})
427+
}
428+
}
429+
430+
commitMessage = parsed.GetCommitMessage(ctx.Locale.TrString("repo.editor.delete_directory", treePath))
431+
} else {
432+
// Single file deletion
433+
filesToDelete = []*files_service.ChangeRepoFile{
400434
{
401435
Operation: "delete",
402436
TreePath: treePath,
403437
},
404-
},
405-
Message: parsed.GetCommitMessage(ctx.Locale.TrString("repo.editor.delete", treePath)),
406-
Signoff: parsed.form.Signoff,
407-
Author: parsed.GitCommitter,
408-
Committer: parsed.GitCommitter,
438+
}
439+
commitMessage = parsed.GetCommitMessage(ctx.Locale.TrString("repo.editor.delete", treePath))
440+
}
441+
442+
_, err = files_service.ChangeRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, &files_service.ChangeRepoFilesOptions{
443+
LastCommitID: parsed.form.LastCommit,
444+
OldBranch: parsed.OldBranchName,
445+
NewBranch: parsed.NewBranchName,
446+
Files: filesToDelete,
447+
Message: commitMessage,
448+
Signoff: parsed.form.Signoff,
449+
Author: parsed.GitCommitter,
450+
Committer: parsed.GitCommitter,
409451
})
410452
if err != nil {
411453
editorHandleFileOperationError(ctx, parsed.NewBranchName, err)
412454
return
413455
}
414456

415-
ctx.Flash.Success(ctx.Tr("repo.editor.file_delete_success", treePath))
457+
if entry.IsDir() {
458+
ctx.Flash.Success(ctx.Tr("repo.editor.directory_delete_success", treePath))
459+
} else {
460+
ctx.Flash.Success(ctx.Tr("repo.editor.file_delete_success", treePath))
461+
}
416462
redirectTreePath := getClosestParentWithFiles(ctx.Repo.GitRepo, parsed.NewBranchName, treePath)
417463
redirectForCommitChoice(ctx, parsed, redirectTreePath)
418464
}

templates/repo/view_content.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
<a class="item" data-clipboard-text="{{AppUrl}}{{StringUtils.TrimPrefix .Repository.Link "/"}}/src/commit/{{.CommitID}}/{{PathEscapeSegments .TreePath}}">
9494
{{svg "octicon-link" 16 "tw-mr-2"}}{{ctx.Locale.Tr "repo.file_copy_permalink"}}
9595
</a>
96-
{{if and (.Permission.CanWrite ctx.Consts.RepoUnitTypeCode) (not .Repository.IsArchived)}}
96+
{{if and (.Permission.CanWrite ctx.Consts.RepoUnitTypeCode) (not .Repository.IsArchived) (not $isTreePathRoot)}}
9797
<div class="divider"></div>
9898
<a class="item" href="{{.RepoLink}}/_delete/{{.BranchName | PathEscapeSegments}}/{{.TreePath | PathEscapeSegments}}">
9999
{{svg "octicon-trash" 16 "tw-mr-2"}}{{ctx.Locale.Tr "repo.editor.delete_this_directory"}}

web_src/js/components/ViewFileTree.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ function handleSearchResultClick(filePath: string) {
151151
@mouseenter="selectedIndex = idx"
152152
:title="result.matchResult.join('')"
153153
>
154-
<span v-html="svg('octicon-file', 16)"></span>
154+
<!-- eslint-disable-next-line vue/no-v-html -->
155+
<span v-html="svg('octicon-file', 16)"/>
155156
<span class="file-tree-search-result-path">
156157
<span
157158
v-for="(part, index) in result.matchResult"
@@ -209,7 +210,7 @@ function handleSearchResultClick(filePath: string) {
209210
border-bottom: 1px solid var(--color-secondary);
210211
}
211212
212-
.file-tree-search-result-item svg {
213+
.file-tree-search-result-item > span:first-child {
213214
flex-shrink: 0;
214215
margin-top: 0.125rem;
215216
}

0 commit comments

Comments
 (0)