Skip to content

Commit

Permalink
perf(clone): clone faster without blobs
Browse files Browse the repository at this point in the history
When git 2.27 or newer is installed, we can add `--filter=blob:none` to avoid getting useless information from the git server. This makes clone much faster if your template has a big history.

Close #450 (it does not exactly fix it, but maybe makes it irrelevant).
  • Loading branch information
yajo committed Nov 2, 2021
1 parent 080e3f6 commit 3fd8226
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Summary:
complicated anyway (warn added to docs).
- Changed `--force` to be the same as `--defaults --overwrite`.
- Copied files will reflect permissions on the same files in the template.
- Copier now uses `git clone --filter=blob:none` when cloning, to be faster.

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ A library and CLI app for rendering project templates.
## Installation

1. Install Python 3.6.1 or newer (3.8 or newer if you're on Windows).
1. Install Git 2.24 or newer.
1. Install Git 2.27 or newer.
1. To use as a CLI app: `pipx install copier`
1. To use as a library: `pip install copier`

Expand Down
8 changes: 7 additions & 1 deletion copier/vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pathlib import Path

from packaging import version
from packaging.version import Version
from plumbum import TF, ProcessExecutionError, colors, local
from plumbum.cmd import git

Expand All @@ -13,6 +14,7 @@

GIT_PREFIX = ("git@", "git://", "git+")
GIT_POSTFIX = (".git",)
GIT_VERSION = Version(git("version").split()[-1].replace(".windows", "+windows"))

This comment has been minimized.

Copy link
@jacobstr

jacobstr Nov 11, 2021

Contributor

@yajo this makes my copier unhappy with the system git in osx and will present a problem to users:

git version
git version 2.24.3 (Apple Git-128)

Version("git version 2.24.3 (Apple Git-128)".split()[-1].replace(".windows", "+windows"))

raise InvalidVersion(f"Invalid version: '{version}'")
packaging.version.InvalidVersion: Invalid version: 'Git-128)

Personally worked around this by brew install git but it's a bit kooky. Could potentially parse the semver with a capturing regex but that feels brittle as well.

This comment has been minimized.

Copy link
@yajo

yajo Nov 12, 2021

Author Member

Fix in #475 (thanks BTW 😉)

REPLACEMENTS = (
(re.compile(r"^gh:/?(.*\.git)$"), r"https://github.com/\1"),
(re.compile(r"^gh:/?(.*)$"), r"https://github.com/\1.git"),
Expand Down Expand Up @@ -117,7 +119,11 @@ def clone(url: str, ref: OptStr = None) -> str:
Reference to checkout. For Git repos, defaults to `HEAD`.
"""
location = tempfile.mkdtemp(prefix=f"{__name__}.clone.")
git("clone", "--no-checkout", url, location)
_clone = git["clone", "--no-checkout", url, location]
# Faster clones if possible
if GIT_VERSION >= Version("2.27"):
_clone = _clone["--filter=blob:none"]
_clone()
with local.cwd(location):
git("checkout", ref or "HEAD")
git("submodule", "update", "--checkout", "--init", "--recursive", "--force")
Expand Down

0 comments on commit 3fd8226

Please sign in to comment.