Skip to content

Commit bc8f205

Browse files
committed
Add handler to delete a branch
1 parent 2b685a3 commit bc8f205

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

jupyterlab_git/git.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,15 @@ async def branch(self, current_path):
497497
"current_branch": heads["current_branch"],
498498
}
499499

500+
async def branch_delete(self, current_path, branch):
501+
"""Execute 'git branch -D <branchname>'"""
502+
cmd = ["git", "branch", "-D", branch]
503+
code, _, error = await execute(
504+
cmd, cwd=os.path.join(self.root_dir, current_path)
505+
)
506+
if code != 0:
507+
return {"code": code, "command": " ".join(cmd), "message": error}
508+
500509
async def branch_heads(self, current_path):
501510
"""
502511
Execute 'git for-each-ref' command on refs/heads & return the result.

jupyterlab_git/handlers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,31 @@ async def post(self):
234234
self.finish(json.dumps(result))
235235

236236

237+
class GitBranchDeleteHandler(GitHandler):
238+
"""
239+
Handler for 'git branch -D <branch>'
240+
"""
241+
242+
@web.authenticated
243+
async def post(self):
244+
"""
245+
POST request handler, delete branch in current repository.
246+
247+
Body: {
248+
"current_path": Git repository path relatively to the server root,
249+
"branch": Branch name to be deleted
250+
}
251+
"""
252+
data = self.get_json_body()
253+
result = await self.git.delete_branch(data["current_path"], data["branch"])
254+
255+
if result["code"] != 0:
256+
self.set_status(500)
257+
self.finish(json.dumps(result))
258+
else:
259+
self.set_status(204)
260+
261+
237262
class GitAddHandler(GitHandler):
238263
"""
239264
Handler for git add <filename>'.
@@ -747,6 +772,7 @@ def setup_handlers(web_app):
747772
("/git/add_all_untracked", GitAddAllUntrackedHandler),
748773
("/git/all_history", GitAllHistoryHandler),
749774
("/git/branch", GitBranchHandler),
775+
("/git/branch_delete", GitBranchDeleteHandler),
750776
("/git/changed_files", GitChangedFilesHandler),
751777
("/git/checkout", GitCheckoutHandler),
752778
("/git/clone", GitCloneHandler),

0 commit comments

Comments
 (0)