From 4f528700f0351cf3eba659181bbd0b11b3143e73 Mon Sep 17 00:00:00 2001 From: Zack Cerza Date: Thu, 1 Dec 2016 11:17:31 -0700 Subject: [PATCH] repo_utils: Use shallow clones of git repos Signed-off-by: Zack Cerza --- teuthology/repo_utils.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/teuthology/repo_utils.py b/teuthology/repo_utils.py index 82a76455b8..6e80bde4ea 100644 --- a/teuthology/repo_utils.py +++ b/teuthology/repo_utils.py @@ -84,7 +84,7 @@ def enforce_repo_state(repo_url, dest_path, branch, remove_on_error=True): clone_repo(repo_url, dest_path, branch) elif not is_fresh(sentinel): set_remote(dest_path, repo_url) - fetch(dest_path) + fetch_branch(dest_path, branch) touch_file(sentinel) else: log.info("%s was just updated; assuming it is current", dest_path) @@ -97,20 +97,25 @@ def enforce_repo_state(repo_url, dest_path, branch, remove_on_error=True): raise -def clone_repo(repo_url, dest_path, branch): +def clone_repo(repo_url, dest_path, branch, shallow=True): """ Clone a repo into a path :param repo_url: The full URL to the repo (not including the branch) :param dest_path: The full path to the destination directory :param branch: The branch. + :param shallow: Whether to perform a shallow clone (--depth 1) :raises: BranchNotFoundError if the branch is not found; GitError for other errors """ validate_branch(branch) log.info("Cloning %s %s from upstream", repo_url, branch) + args = ['git', 'clone'] + if shallow: + args.extend(['--depth', '1']) + args.extend(['--branch', branch, repo_url, dest_path]) proc = subprocess.Popen( - ('git', 'clone', '--branch', branch, repo_url, dest_path), + args, cwd=os.path.dirname(dest_path), stdout=subprocess.PIPE, stderr=subprocess.STDOUT) @@ -139,7 +144,7 @@ def set_remote(repo_path, repo_url): :param repo_path: The full path to the repository :raises: GitError if the operation fails """ - log.debug("Setting repo remote to %s", repo_url) + log.debug("Setting repo remote to %s", repo_url) proc = subprocess.Popen( ('git', 'remote', 'set-url', 'origin', repo_url), cwd=repo_path, @@ -170,19 +175,24 @@ def fetch(repo_path): raise GitError("git fetch failed!") -def fetch_branch(repo_path, branch): +def fetch_branch(repo_path, branch, shallow=True): """ Call "git fetch -p origin " :param repo_path: The full path to the repository on-disk :param branch: The branch. + :param shallow: Whether to perform a shallow fetch (--depth 1) :raises: BranchNotFoundError if the branch is not found; GitError for other errors """ validate_branch(branch) log.info("Fetching %s from upstream", branch) + args = ['git', 'fetch'] + if shallow: + args.extend(['--depth', '1']) + args.extend(['-p', 'origin', branch]) proc = subprocess.Popen( - ('git', 'fetch', '-p', 'origin', branch), + args, cwd=repo_path, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) -- 2.39.5