From 8bebeb619adf857ca8f308f51595fffc413f4959 Mon Sep 17 00:00:00 2001 From: Vadim Troshchinskiy Date: Thu, 6 Feb 2025 16:14:17 +0100 Subject: [PATCH] Branch deletion --- api/gitapi.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/api/gitapi.py b/api/gitapi.py index 84b61fb..dc2dfc2 100755 --- a/api/gitapi.py +++ b/api/gitapi.py @@ -543,6 +543,49 @@ class GitRepoBranches(Resource): "branches": branches } + def delete(self, repo): + """Delete a given branch in a given repository + + Args: + repo (str): The name of the repository. + + Returns: + Response: A JSON response containing a list of branch names or an error message if the repository is not found. + - 200: A JSON object with a "status" key containing "deleted" + - 404: A JSON object with an "error" key containing the message "Repository not found" or "Branch not found" + """ + + data = request.json + + if data is None: + log.error("Can't delete branch, JSON post data missing") + return {"error" : "Parameters missing"}, 400 + + if not "branch" in data: + log.error("Can't delete branch, 'branch' key in JSON data is missing") + return {"error" : "Parameters missing"}, 400 + + branch = data['branch'] + + + repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git") + if not os.path.isdir(repo_path): + log.error("Can't get branches of repository repository %s, not found. Looked in %s", repo, repo_path, extra = {"repository" : repo, "path" : repo_path }) + return {"error": "Repository not found"}, 404 + + git_repo = git.Repo(repo_path) + git_repo.git.config('--global', '--add', 'safe.directory', repo_path) + + + if not branch in git_repo.branches: + log.error("Can't delete branch %s, not found in repository %s", branch, repo, extra = {"repository" : repo, "branch" : branch}) + return {"error": "Branch not found"}, 404 + + git_repo.delete_head(branch) + log.info("Branch %s of repository %s deleted", branch, repo, extra = {"repository" : repo, "branch" : branch}) + return {"status": "deleted"}, 200 + +