Read the commit id from the Git submodule if available

parent 3427cf5f
......@@ -25,7 +25,7 @@ from __future__ import absolute_import
import os
from hdlmake.util import path as path_utils
import logging
from subprocess import Popen, PIPE
from subprocess import Popen, PIPE, CalledProcessError
from .constants import GIT
from .fetcher import Fetcher
......@@ -39,21 +39,45 @@ class Git(Fetcher):
pass
@staticmethod
def get_git_toplevel(module):
def get_git_toplevel():
"""Get the top level for the Git repository"""
cur_dir = module.pool.top_module.path
try:
os.chdir(path_utils.rel2abs(module.path))
if not os.path.exists(".gitmodules"):
return None
tree_root_cmd = Popen("git rev-parse --show-toplevel",
stdout=PIPE,
stdin=PIPE,
close_fds=not path_utils.check_windows(),
shell=True)
tree_root_line = tree_root_cmd.stdout.readlines()[0].strip()
return tree_root_line
finally:
os.chdir(cur_dir)
except CalledProcessError as e:
print e.output
logging.error("Cannot get the top level!")
quit()
@staticmethod
def get_submodule_commit(submodule_dir):
"""Get the commit for a repository if defined in Git submodules"""
try:
command_tmp = "git submodule status %s" % submodule_dir
status_cmd = Popen(command_tmp,
stdout=PIPE,
stdin=PIPE,
stderr=PIPE,
close_fds=not path_utils.check_windows(),
shell=True)
status_output = status_cmd.stdout.readlines()
if len(status_output) == 1:
status_line = status_output[0].split()
if len(status_line) == 2:
return status_line[0][1:]
else:
return None
else:
return None
except CalledProcessError as e:
print e.output
logging.error("Cannot get the submodule status!")
quit()
def fetch(self, module):
"""Get the code from the remote Git repository"""
......@@ -62,41 +86,37 @@ class Git(Fetcher):
raise ValueError("This backend should get git modules only.")
if not os.path.exists(fetchto):
os.mkdir(fetchto)
cur_dir = module.pool.top_module.path
if module.branch is None:
module.branch = "master"
basename = path_utils.url_basename(module.url)
mod_path = os.path.join(fetchto, basename)
logging.info("Fetching git module: %s", mod_path)
if basename.endswith(".git"):
basename = basename[:-4] # remove trailing .git
if module.isfetched:
update_only = True
if not module.isfetched:
logging.info("Fetching git module %s", mod_path)
cmd = "(cd {0} && git clone {1})"
cmd = cmd.format(fetchto, module.url)
if os.system(cmd) != 0:
return False
else:
update_only = False
if update_only:
logging.info("Updating module %s", mod_path)
cmd = "(cd {0} && git checkout {1})"
cmd = cmd.format(mod_path, module.branch)
logging.info("Updating git module %s", mod_path)
checkout_id = None
if module.branch is not None:
checkout_id = module.branch
logging.debug("Git branch requested: %s", checkout_id)
elif module.revision is not None:
checkout_id = module.revision
logging.debug("Git commit requested: %s", checkout_id)
else:
logging.info("Cloning module %s", mod_path)
cmd = "(cd {0} && git clone -b {2} {1})"
cmd = cmd.format(fetchto, module.url, module.branch)
success = True
logging.debug("Running %s", cmd)
if os.system(cmd) != 0:
success = False
if module.revision is not None and success is True:
logging.debug("cd %s", mod_path)
os.chdir(mod_path)
cmd = "git checkout " + module.revision
logging.debug("Running %s", cmd)
checkout_id = self.get_submodule_commit(module.path)
logging.debug("Git submodule commit: %s", checkout_id)
if checkout_id is not None:
logging.info("Checking out version %s", checkout_id)
cmd = "(cd {0} && git checkout {1})"
cmd = cmd.format(mod_path, checkout_id)
if os.system(cmd) != 0:
success = False
os.chdir(cur_dir)
return False
module.isfetched = True
module.path = mod_path
return success
return True
@staticmethod
def check_git_commit(path):
......
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