From 94b381f9bd615250837d1bb803d6b7e3279d2429 Mon Sep 17 00:00:00 2001 From: Ondrej Nosek Date: Apr 02 2019 07:36:34 +0000 Subject: Depth param for clone Added '--depth' argument for 'git clone' command. It creates a shallow clone with a history truncated to the specified number of commits. Additional parameter '--no-single-branch' is added together with '--depth' to apply history truncation to all cloned branches. JIRA: COMPOSE-2812 Fixes: #363 Signed-off-by: Ondrej Nosek --- diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py index 85a1d81..9fca8e1 100644 --- a/pyrpkg/__init__.py +++ b/pyrpkg/__init__.py @@ -1474,7 +1474,7 @@ class Commands(object): return def clone(self, repo, path=None, branch=None, bare_dir=None, - anon=False, target=None): + anon=False, target=None, depth=None): """Clone a repo, optionally check out a specific branch. :param str repo: the name of the repository to clone. @@ -1487,6 +1487,8 @@ class Commands(object): `False`. :param str target: an optional name of the folder in which to clone the repo. + :param int depth: create a shallow clone with a history truncated + to the specified number of commits. """ if not path: @@ -1503,6 +1505,10 @@ class Commands(object): cmd = ['git', 'clone'] if self.quiet: cmd.append('-q') + if depth: + # argument '--depth' goes with '--no-single-branch' together + # to apply history truncation to all cloned branches + cmd.extend(['--depth', depth, "--no-single-branch"]) # do the clone if branch and bare_dir: raise rpkgError('Cannot combine bare cloning with a branch') @@ -1548,7 +1554,7 @@ class Commands(object): return repo.split("/")[-1] return repo - def clone_with_dirs(self, repo, anon=False, target=None): + def clone_with_dirs(self, repo, anon=False, target=None, depth=None): """Clone a repo old style with subdirs for each branch. :param str repo: name of the repository to clone. @@ -1556,6 +1562,8 @@ class Commands(object): `False`. :param str target: an optional name of the folder in which to clone the repo. + :param int depth: create a shallow clone with a history truncated + to the specified number of commits. """ self._push_url = None @@ -1581,7 +1589,7 @@ class Commands(object): # Create a bare clone first. This gives us a good list of branches try: - self.clone(repo, top_path, bare_dir=repo_path, anon=anon) + self.clone(repo, top_path, bare_dir=repo_path, anon=anon, depth=depth) except Exception as e: # Clean out our directory shutil.rmtree(top_path) diff --git a/pyrpkg/cli.py b/pyrpkg/cli.py index 89a8df4..384f7cb 100644 --- a/pyrpkg/cli.py +++ b/pyrpkg/cli.py @@ -644,6 +644,18 @@ defined, packages will be built sequentially.""" % {'name': self.name}) clone_parser.add_argument( "clone_target", default=None, nargs="?", help='Directory in which to clone the repository') + + def validator_integer_string(raw_value): + """checks if input is string that contains integer number""" + try: + value = str(int(raw_value)) + except (ValueError, TypeError): + raise argparse.ArgumentTypeError("argument has to be a number") + return value + clone_parser.add_argument( + '--depth', type=validator_integer_string, + help='Create a shallow clone with a history truncated ' + 'to the specified number of commits') clone_parser.set_defaults(command=self.clone) # Add an alias for historical reasons @@ -1762,12 +1774,14 @@ see API KEY section of copr-cli(1) man page. if self.args.branches: self.cmd.clone_with_dirs(self.args.repo[0], anon=self.args.anonymous, - target=self.args.clone_target) + target=self.args.clone_target, + depth=self.args.depth) else: self.cmd.clone(self.args.repo[0], branch=self.args.branch, anon=self.args.anonymous, - target=self.args.clone_target) + target=self.args.clone_target, + depth=self.args.depth) def commit(self): if self.args.with_changelog and not self.args.message: