Read the commit id from the Git submodule if available

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