Mark git repo as a safe directory

Fixes problems due to git not liking the ownership
ogrepository-integration
Vadim vtroshchinskiy 2025-02-06 13:15:21 +01:00
parent 46732216eb
commit 3ebc728fb9
1 changed files with 18 additions and 10 deletions

View File

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