Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repos: Add KAS_GIT_SHALLOW to implement git shallow clone/fetch #105

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/command-line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ Environment variables
| | space-separated, its replacement. E.g.: |
| | "http://.*\.someurl\.io/ http://localmirror.net/"|
+--------------------------+--------------------------------------------------+
| ``KAS_GIT_SHALLOW`` | Perform shallow git clone/fetch using --depth=N |
| | specified by this variable. This is useful in |
| | case CI always starts with empty work directory |
| | and this directory is always discarded after the |
| | CI run. |
+--------------------------+--------------------------------------------------+
| ``SSH_PRIVATE_KEY`` | Variable containing the private key that should |
| | be added to an internal ssh-agent. This key |
| | cannot be password protected. This setting is |
Expand Down
10 changes: 10 additions & 0 deletions kas/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,11 @@ def add_cmd(self):

def clone_cmd(self, srcdir, createref):
cmd = ['git', 'clone', '-q']

depth = os.environ.get('KAS_GIT_SHALLOW')
if depth:
cmd.extend(['--depth', depth])

if createref:
cmd.extend([self.effective_url, '--bare', srcdir])
elif srcdir:
Expand All @@ -498,6 +503,11 @@ def contains_refspec_cmd(self):

def fetch_cmd(self):
cmd = ['git', 'fetch', '-q']

depth = os.environ.get('KAS_GIT_SHALLOW')
if depth:
cmd.extend(['--depth', depth])

if self.tag:
cmd.append('--tags')

Expand Down
14 changes: 14 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import yaml
import pytest
from kas import kas
from kas.libkas import run_cmd
from kas.libkas import TaskExecError


Expand Down Expand Up @@ -83,6 +84,19 @@ def test_checkout_create_refs(changedir, tmpdir):
assert os.path.exists('kas/.git/objects/info/alternates')


def test_checkout_shallow(changedir, tmpdir):
tdir = str(tmpdir / 'test_commands')
shutil.copytree('tests/test_commands', tdir)
os.chdir(tdir)
os.environ['KAS_GIT_SHALLOW'] = '1'
kas.kas(['checkout', 'test-shallow.yml'])
del os.environ['KAS_GIT_SHALLOW']
(rc, output) = run_cmd(['git', 'rev-list', '--count', 'HEAD'], cwd='kas',
fail=False, liveupdate=False)
assert rc == 0
assert output.strip() == '1'


def test_repo_includes(changedir, tmpdir):
tdir = str(tmpdir / 'test_commands')
shutil.copytree('tests/test_repo_includes', tdir)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_commands/test-shallow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
header:
version: 14

repos:
this:

kas:
url: https://github.com/siemens/kas.git
commit: master