Branch deletion

ogrepository-fixes
Vadim vtroshchinskiy 2025-02-06 16:14:17 +01:00
parent 115df98905
commit 8bebeb619a
1 changed files with 43 additions and 0 deletions

View File

@ -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