39 lines
1.4 KiB
Python
39 lines
1.4 KiB
Python
|
|
from git import Repo
|
|
from git.exc import GitError
|
|
import hashlib
|
|
import os
|
|
from SCons.Script import *
|
|
|
|
Import('env')
|
|
|
|
def _gitbranch(env: Environment, repo_name: str, remote_url: str, git_ref: str = "main") -> dict:
|
|
repo_dir = os.path.join(env['CLONE_DIR'], 'git', repo_name, '_bare')
|
|
try:
|
|
repo = Repo(repo_dir)
|
|
origin = repo.remotes['origin']
|
|
except GitError:
|
|
print(f'Initializing git repository at {repo_dir}.')
|
|
repo = Repo.init(repo_dir, bare=True)
|
|
origin = repo.create_remote('origin', 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!
|
|
if not os.path.exists(worktree_dir):
|
|
print(f'Checking out into {worktree_dir}.')
|
|
origin.fetch(tags=True)
|
|
os.makedirs(worktree_dir)
|
|
repo.git.worktree('add', worktree_dir, git_ref)
|
|
elif env['UPDATE_REPOSITORIES']:
|
|
worktree_repo = Repo(worktree_dir)
|
|
if not worktree_repo.head.is_detached:
|
|
print(f'Updating git repository at {worktree_dir}')
|
|
worktree_origin = worktree_repo.remotes['origin']
|
|
worktree_origin.pull()
|
|
else:
|
|
print(f'Not updating git repository {worktree_dir} as it is not on a branch.')
|
|
return {
|
|
'checkout_root': worktree_dir
|
|
}
|
|
|
|
|
|
env.AddMethod(_gitbranch, 'GitBranch')
|
|
Return('env') |