From 43503dfec6703ff9b56db52f5fc00dda3a5be838 Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Tue, 8 Jul 2025 18:40:27 +0200 Subject: [PATCH] Changed folder names for git worktrees to the name of the ref that is checked out for better readability. --- addons/gitbranch.py | 46 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/addons/gitbranch.py b/addons/gitbranch.py index f2a7bc3..0db5544 100644 --- a/addons/gitbranch.py +++ b/addons/gitbranch.py @@ -3,6 +3,8 @@ from git import Repo from git.exc import GitError import hashlib import inspect +import os +import shutil from SCons.Script import * Import('env') @@ -20,7 +22,41 @@ def _clone(env: Environment, repo_name: str, remote_url: str): def _git_branch(env: Environment, repo_name: str, remote_url: str, git_ref: str = 'main') -> dict: repo, origin = _clone(env, repo_name, remote_url) - worktree_dir = os.path.join(env['CLONE_DIR'], 'git', repo_name, hashlib.shake_128(git_ref.encode('utf-8')).hexdigest(6)) # TODO: commit hash would be better, right? -> not if it's a branch! + old_worktree_dir = os.path.join(env['CLONE_DIR'], 'git', repo_name, hashlib.shake_128(git_ref.encode('utf-8')).hexdigest(6)) + worktree_dir = os.path.join(env['CLONE_DIR'], 'git', repo_name, git_ref.replace('/', '_')) + if os.path.exists(old_worktree_dir) and not os.path.islink(old_worktree_dir): + if not os.path.exists(worktree_dir): + print(f'Found old Git worktree at {old_worktree_dir}, moving it to {worktree_dir}.') + try: + repo.git.worktree('move', old_worktree_dir, worktree_dir) + except GitError: + print('Error while moving worktree, manually moving and repairing it instead.') + shutil.move(old_worktree_dir, worktree_dir) + try: + repo.git.worktree('repair', worktree_dir) + except GitError: + print('Also didn\'t work, removing and redownloading it.') + try: + repo.git.worktree('remove', '-f', worktree_dir) + except GitError: ... + + try: + repo.git.worktree('remove', '-f', old_worktree_dir) + except GitError: ... + + if os.path.exists(worktree_dir): + shutil.rmtree(worktree_dir, ignore_errors=True) + # this is all we can do, I guess + else: + print(f'Found old Git worktree at {old_worktree_dir}, but the new one at {worktree_dir} already exists. Removing the old one.') + repo.git.worktree('remove', '-f', old_worktree_dir) + + print('Attempting to create a symlink for older S++ versions.') + try: + os.symlink(worktree_dir, old_worktree_dir, target_is_directory=True) + except Exception as e: + print(f'Failed: {e}') + update_submodules = False if not os.path.exists(worktree_dir): print(f'Checking out into {worktree_dir}.') @@ -38,9 +74,17 @@ def _git_branch(env: Environment, repo_name: str, remote_url: str, git_ref: str update_submodules = True else: print(f'Not updating git repository {worktree_dir} as it is not on a branch.') + else: + worktree_repo = Repo(worktree_dir) if update_submodules: for submodule in worktree_repo.submodules: submodule.update(init=True) + for submodule in worktree_repo.submodules: + if os.listdir(submodule.abspath) == ['.git']: + print(f'Submodule {submodule.name} seems borked, attempting to fix it.') + worktree_repo.git.submodule('deinit', '-f', submodule.path) + worktree_repo.git.submodule('init', submodule.path) + worktree_repo.git.submodule('update', submodule.path) return { 'checkout_root': worktree_dir, 'repo': repo,