]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
repo_utils: Use shallow clones of git repos
authorZack Cerza <zack@redhat.com>
Thu, 1 Dec 2016 18:17:31 +0000 (11:17 -0700)
committerZack Cerza <zack@redhat.com>
Fri, 2 Dec 2016 17:27:10 +0000 (10:27 -0700)
Signed-off-by: Zack Cerza <zack@redhat.com>
teuthology/repo_utils.py

index 82a76455b8b9c978d8c94d31bc0611a4be18256e..6e80bde4ea0e3f7b402b9e28e453fb4b10008b1e 100644 (file)
@@ -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 <branch>"
 
     :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)