buildman: Use git worktrees instead of git clones when possible

This patch makes buildman create linked working trees instead of clones
of the source repository, but keeps updating the older clones of the
repository that might already exist. These worktrees share "everything
except working directory specific files such as HEAD, index, etc." with
the source repository. See the git-worktree(1) manual page for more
information.

If git-worktree isn't available, silently falls back to cloning the
repository.

Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Alper Nebi Yasak
2020-09-03 15:51:03 +03:00
committed by Simon Glass
parent f5bbd9a3a6
commit 76de29fc4f
3 changed files with 84 additions and 8 deletions

View File

@@ -259,6 +259,48 @@ def Fetch(git_dir=None, work_tree=None):
if result.return_code != 0:
raise OSError('git fetch: %s' % result.stderr)
def CheckWorktreeIsAvailable(git_dir):
"""Check if git-worktree functionality is available
Args:
git_dir: The repository to test in
Returns:
True if git-worktree commands will work, False otherwise.
"""
pipe = ['git', '--git-dir', git_dir, 'worktree', 'list']
result = command.RunPipe([pipe], capture=True, capture_stderr=True,
raise_on_error=False)
return result.return_code == 0
def AddWorktree(git_dir, output_dir, commit_hash=None):
"""Create and checkout a new git worktree for this build
Args:
git_dir: The repository to checkout the worktree from
output_dir: Path for the new worktree
commit_hash: Commit hash to checkout
"""
# We need to pass --detach to avoid creating a new branch
pipe = ['git', '--git-dir', git_dir, 'worktree', 'add', '.', '--detach']
if commit_hash:
pipe.append(commit_hash)
result = command.RunPipe([pipe], capture=True, cwd=output_dir,
capture_stderr=True)
if result.return_code != 0:
raise OSError('git worktree add: %s' % result.stderr)
def PruneWorktrees(git_dir):
"""Remove administrative files for deleted worktrees
Args:
git_dir: The repository whose deleted worktrees should be pruned
"""
pipe = ['git', '--git-dir', git_dir, 'worktree', 'prune']
result = command.RunPipe([pipe], capture=True, capture_stderr=True)
if result.return_code != 0:
raise OSError('git worktree prune: %s' % result.stderr)
def CreatePatches(branch, start, count, ignore_binary, series):
"""Create a series of patches from the top of the current branch.