From 3ebc728fb9a2bf284242e132fe3a1e28a2e3d438 Mon Sep 17 00:00:00 2001 From: Vadim Troshchinskiy Date: Thu, 6 Feb 2025 13:15:21 +0100 Subject: [PATCH] Mark git repo as a safe directory Fixes problems due to git not liking the ownership --- api/gitapi.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/api/gitapi.py b/api/gitapi.py index 4c78f69..014f67a 100755 --- a/api/gitapi.py +++ b/api/gitapi.py @@ -128,7 +128,10 @@ def do_repo_backup(repo, params): bool: True if the backup was successful. """ - gitrepo = git.Repo(f"{REPOSITORIES_BASE_PATH}/{repo}.git") + git_repo_path = f"{REPOSITORIES_BASE_PATH}/{repo}.git" + git_repo = git.Repo(git_repo_path) + git_repo.git.config('--global', '--add', 'safe.directory', git_repo_path) + ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) @@ -138,7 +141,7 @@ def do_repo_backup(repo, params): with sftp.file(params["filename"], mode='wb+') as remote_file: - gitrepo.archive(remote_file, format="tar.gz") + git_repo.archive(remote_file, format="tar.gz") return True @@ -157,13 +160,16 @@ def do_repo_sync(repo, params): - "remote_ref" (str): The name of the remote reference. - "summary" (str): A summary of the push operation for the reference. """ - gitrepo = git.Repo(f"{REPOSITORIES_BASE_PATH}/{repo}.git") + git_repo_path = f"{REPOSITORIES_BASE_PATH}/{repo}.git" + git_repo = git.Repo(git_repo_path) + git_repo.git.config('--global', '--add', 'safe.directory', git_repo_path) + # Recreate the remote every time, it might change - if "backup" in gitrepo.remotes: - gitrepo.delete_remote("backup") + if "backup" in git_repo.remotes: + git_repo.delete_remote("backup") - backup_repo = gitrepo.create_remote("backup", params["remote_repository"]) + backup_repo = git_repo.create_remote("backup", params["remote_repository"]) pushed_references = backup_repo.push("*:*") results = [] @@ -183,11 +189,11 @@ def do_repo_gc(repo): Returns: bool: True if the garbage collection command was executed successfully. """ - gitrepo = git.Repo(f"{REPOSITORIES_BASE_PATH}/{repo}.git") - - gitrepo.git.gc() - return True + git_repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git") + git_repo = git.Repo(git_repo_path) + git_repo.git.config('--global', '--add', 'safe.directory', git_repo_path) + git_repo.git.gc() @app.errorhandler(HTTPException) def handle_exception(e): @@ -525,6 +531,8 @@ class GitRepoBranches(Resource): return {"error": "Repository not found"}, 404 git_repo = git.Repo(repo_path) + git_repo.git.config('--global', '--add', 'safe.directory', repo_path) + branches = [] for branch in git_repo.branches: