Commit f61b1e5a authored by Ayush Tiwari's avatar Ayush Tiwari

gitclone: Add support for cloning submodules by default

Also, add functionality to fetch the submodules while fetching the
the main repo and checkout the submodule(s) repos in case the pointer
to the submodule in the main repo has been updated.

Also, some indentation cleanups.
parent 5e6da988
...@@ -121,7 +121,7 @@ try to redownload resource with wrong md5sum. ...@@ -121,7 +121,7 @@ try to redownload resource with wrong md5sum.
slapos.recipe.build:gitclone slapos.recipe.build:gitclone
============================== ==============================
Checkout a git repository. Checkout a git repository and its submodules by default.
Supports slapos.libnetworkcache if present, and if boolean 'use-cache' option Supports slapos.libnetworkcache if present, and if boolean 'use-cache' option
is true. is true.
...@@ -391,7 +391,7 @@ Then, when update occurs, nothing is done:: ...@@ -391,7 +391,7 @@ Then, when update occurs, nothing is done::
Unable to update: Unable to update:
Traceback (most recent call last): Traceback (most recent call last):
... ...
...CalledProcessError: Command '['git', 'fetch', '--all']' returned non-zero exit status 1 ...CalledProcessError: Command '['git', 'fetch', '--recurse-submodules', '--all']' returned non-zero exit status 1
<BLANKLINE> <BLANKLINE>
... ...
fatal: unable to access 'http://git.erp5.org/repos/nowhere/': The requested URL returned error: 500 fatal: unable to access 'http://git.erp5.org/repos/nowhere/': The requested URL returned error: 500
...@@ -450,6 +450,21 @@ boolean option:: ...@@ -450,6 +450,21 @@ boolean option::
repository = https://example.net/example.git/ repository = https://example.net/example.git/
ignore-ssl-certificate = true ignore-ssl-certificate = true
Ignore cloning submodules
~~~~~~~~~~~~~~~~~~~~~~~~~
By default, cloning the repository will clone its submodules also. You can force
git to ignore cloinig submodules by defining `ignore-cloning-submodules` boolean
option to 'true'::
[buildout]
parts = git-clone
[git-clone]
recipe = slapos.recipe.build:gitclone
repository = https://lab.nexedi.com/tiwariayush/test_erp5
ignore-cloning-submodules = true
Other options Other options
~~~~~~~~~~~~~ ~~~~~~~~~~~~~
......
This diff is collapsed.
...@@ -34,7 +34,7 @@ import time ...@@ -34,7 +34,7 @@ import time
import traceback import traceback
from zc.buildout import UserError from zc.buildout import UserError
from subprocess import call, check_call, CalledProcessError from subprocess import call, check_call, check_output, CalledProcessError
import subprocess import subprocess
try: try:
...@@ -145,7 +145,8 @@ class Recipe(object): ...@@ -145,7 +145,8 @@ class Recipe(object):
self.git_command = 'git' self.git_command = 'git'
self.sparse = options.get('sparse-checkout', '').strip() self.sparse = options.get('sparse-checkout', '').strip()
# Set boolean values # Set boolean values
for key in ('develop', 'shared', 'use-cache', 'ignore-ssl-certificate'): for key in ('develop', 'shared', 'use-cache', 'ignore-ssl-certificate',
'ignore-cloning-submodules'):
setattr(self, key.replace('-', '_'), options.get(key, '').lower() in TRUE_VALUES) setattr(self, key.replace('-', '_'), options.get(key, '').lower() in TRUE_VALUES)
if self.shared: if self.shared:
self.use_cache = False self.use_cache = False
...@@ -213,6 +214,13 @@ class Recipe(object): ...@@ -213,6 +214,13 @@ class Recipe(object):
if config and self.use_cache: if config and self.use_cache:
raise NotImplementedError raise NotImplementedError
if not self.ignore_cloning_submodules:
# `--recurse-submodules` to the git clone command will automatically
# initialize and update each submodule in the repository.
config.append('submodule.recurse=true')
git_clone_command.append('--recurse-submodules')
for config in config: for config in config:
git_clone_command += '--config', config git_clone_command += '--config', config
...@@ -230,6 +238,11 @@ class Recipe(object): ...@@ -230,6 +238,11 @@ class Recipe(object):
if self.use_cache: if self.use_cache:
upload_network_cached(os.path.join(self.location, '.git'), upload_network_cached(os.path.join(self.location, '.git'),
self.repository, self.revision, self.networkcache) self.repository, self.revision, self.networkcache)
if not self.ignore_cloning_submodules:
# Update submodule repository to the commit which is being pointed to
# in main repo
check_call([self.git_command, 'submodule', 'update', '--recursive'],
cwd=self.location)
except CalledProcessError: except CalledProcessError:
print("Unable to download from git repository." print("Unable to download from git repository."
" Trying from network cache...") " Trying from network cache...")
...@@ -262,6 +275,9 @@ class Recipe(object): ...@@ -262,6 +275,9 @@ class Recipe(object):
Do a git fetch. Do a git fetch.
If user doesn't develop, reset to remote revision (or branch if revision is If user doesn't develop, reset to remote revision (or branch if revision is
not specified). not specified).
With support for submodules, we also update the submodule repository if
there has been update to the pointer of the submodules in the parent repo.
This however puts the updated submodules in a DETACHED state.
""" """
try: try:
# first cleanup pyc files # first cleanup pyc files
...@@ -272,9 +288,15 @@ class Recipe(object): ...@@ -272,9 +288,15 @@ class Recipe(object):
revision_already_fetched = \ revision_already_fetched = \
self.revision and \ self.revision and \
call([self.git_command, 'rev-parse', '--verify', self.revision], call([self.git_command, 'rev-parse', '--verify', self.revision],
cwd=self.location) == 0 cwd=self.location) == 0 and \
check_output([self.git_command, 'rev-parse', 'HEAD'],
cwd=self.location).strip() == self.revision
if not revision_already_fetched: if not revision_already_fetched:
check_call([self.git_command, 'fetch', '--all'], cwd=self.location) # With --recurse-submodules, it will fetch the updated branches of the
# submodules as well as of the main repo.
check_call([self.git_command, 'fetch', '--recurse-submodules', '--all'],
cwd=self.location)
# If develop parameter is set, don't reset/update. # If develop parameter is set, don't reset/update.
# Otherwise, reset --hard # Otherwise, reset --hard
...@@ -283,6 +305,14 @@ class Recipe(object): ...@@ -283,6 +305,14 @@ class Recipe(object):
self.gitReset(self.revision) self.gitReset(self.revision)
else: else:
self.gitReset('@{upstream}') self.gitReset('@{upstream}')
if not revision_already_fetched:
# Also update the submodule directory to the commit which parent
# repository is pointing to.
# NOTE: This will put the submodule repo in a `Detached` state.
check_call([self.git_command, 'submodule', 'update', '--recursive'],
cwd=self.location)
except: except:
if not self.develop: if not self.develop:
raise raise
...@@ -309,7 +339,7 @@ def uninstall(name, options): ...@@ -309,7 +339,7 @@ def uninstall(name, options):
force_keep = True force_keep = True
p = subprocess.Popen([git_path, p = subprocess.Popen([git_path,
'log', '--branches', '--not', '--remotes'], 'log', '--branches', '--not', '--remotes'],
cwd=options['location'], cwd=options['location'],
stdout=subprocess.PIPE) stdout=subprocess.PIPE)
if p.communicate()[0].strip(): if p.communicate()[0].strip():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment