Add recipe support.
This commit is contained in:
26
recipes/GitBranch/recipe.py
Normal file
26
recipes/GitBranch/recipe.py
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
from git import Repo
|
||||
from git.exc import GitError
|
||||
import hashlib
|
||||
import os
|
||||
from SCons.Script import *
|
||||
|
||||
def cook(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 for SDL 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?
|
||||
if not os.path.exists(worktree_dir):
|
||||
print(f'Checking out into {worktree_dir}.')
|
||||
origin.fetch()
|
||||
os.makedirs(worktree_dir)
|
||||
repo.git.worktree('add', worktree_dir, git_ref)
|
||||
return {
|
||||
'checkout_root': worktree_dir
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user