+def git_extract(from_what, tag, git_options, where, logger, environment=None):
+ '''Extracts sources from a git repository.
+
+ :param from_what str: The remote git repository.
+ :param tag str: The tag.
+ :param git_options str: git options
+ :param where str: The path where to extract.
+ :param logger Logger: The logger instance to use.
+ :param environment src.environment.Environ: The environment to source when extracting.
+ :return: True if the extraction is successful
+ :rtype: boolean
+ '''
+ DBG.write("git_extract", [from_what, tag, str(where)])
+ if not where.exists():
+ where.make()
+ where_git = os.path.join(str(where), ".git")
+ if tag == "master" or tag == "HEAD":
+ if src.architecture.is_windows():
+ cmd = "git clone %(git_options)s %(remote)s %(where)s"
+ else:
+ cmd = r"""
+set -x
+git clone %(git_options)s %(remote)s %(where)s
+touch -d "$(git --git-dir=%(where_git)s log -1 --format=date_format)" %(where)s
+"""
+ cmd = cmd % {'git_options': git_options, 'remote': from_what, 'tag': tag, 'where': str(where), 'where_git': where_git}
+ else:
+ # NOTICE: this command only works with recent version of git
+ # because --work-tree does not work with an absolute path
+ if src.architecture.is_windows():
+ cmd = "rm -rf %(where)s && git clone %(git_options)s %(remote)s %(where)s && git --git-dir=%(where_git)s --work-tree=%(where)s checkout %(tag)s"
+ else:
+# for sat compile --update : changes the date of directory, only for branches, not tag
+ cmd = r"""
+set -x
+rm -rf %(where)s
+git clone %(git_options)s %(remote)s %(where)s && \
+git --git-dir=%(where_git)s --work-tree=%(where)s checkout %(tag)s
+res=$?
+git --git-dir=%(where_git)s status|grep HEAD
+if [ $? -ne 0 ]; then touch -d "$(git --git-dir=%(where_git)s log -1 --format=date_format)" %(where)s;fi
+exit $res
+"""
+ cmd = cmd % {'git_options': git_options,
+ 'remote': from_what,
+ 'tag': tag,
+ 'where': str(where),
+ 'where_git': where_git}
+
+
+ cmd=cmd.replace('date_format','"%ai"')
+ logger.logTxtFile.write("\n" + cmd + "\n")
+ logger.logTxtFile.flush()
+
+ DBG.write("cmd", cmd)
+ # git commands may fail sometimes for various raisons
+ # (big module, network troubles, tuleap maintenance)
+ # therefore we give several tries
+ i_try = 0
+ max_number_of_tries = 3
+ sleep_delay = 30 # seconds
+ while (True):
+ i_try += 1
+ rc = UTS.Popen(cmd, cwd=str(where.dir()), env=environment.environ.environ, logger=logger)
+ if rc.isOk() or (i_try>=max_number_of_tries):
+ break
+ logger.write('\ngit command failed! Wait %d seconds and give an other try (%d/%d)\n' % \
+ (sleep_delay, i_try + 1, max_number_of_tries), 3)
+ time.sleep(sleep_delay) # wait a little
+
+ return rc.isOk()
+
+
+def git_extract_sub_dir(from_what, tag, git_options, where, sub_dir, logger, environment=None):
+ '''Extracts sources from a subtree sub_dir of a git repository.
+
+ :param from_what str: The remote git repository.
+ :param tag str: The tag.
+ :param git_options str: git options
+ :param where str: The path where to extract.
+ :param sub_dir str: The relative path of subtree to extract.
+ :param logger Logger: The logger instance to use.
+ :param environment src.environment.Environ: The environment to source when extracting.
+ :return: True if the extraction is successful
+ :rtype: boolean
+ '''
+ strWhere = str(where)
+ tmpWhere = strWhere + '_tmp'
+ parentWhere = os.path.dirname(strWhere)
+ if not os.path.exists(parentWhere):
+ logger.error("not existing directory: %s" % parentWhere)
+ return False
+ if os.path.isdir(strWhere):
+ logger.error("do not override existing directory: %s" % strWhere)
+ return False
+ aDict = {'git_options': git_options,
+ 'remote': from_what,
+ 'tag': tag,
+ 'sub_dir': sub_dir,
+ 'where': strWhere,
+ 'parentWhere': parentWhere,
+ 'tmpWhere': tmpWhere,
+ }
+ DBG.write("git_extract_sub_dir", aDict)
+ if not src.architecture.is_windows():
+ cmd = r"""
+set -x
+export tmpDir=%(tmpWhere)s && \
+rm -rf $tmpDir
+git clone %(git_options)s %(remote)s $tmpDir && \
+cd $tmpDir && \
+git checkout %(tag)s && \
+mv %(sub_dir)s %(where)s && \
+git log -1 > %(where)s/README_git_log.txt && \
+rm -rf $tmpDir
+""" % aDict
+ else:
+ cmd = r"""
+
+set tmpDir=%(tmpWhere)s && \
+rm -rf $tmpDir
+git clone %(git_options)s %(remote)s $tmpDir && \
+cd $tmpDir && \
+git checkout %(tag)s && \
+mv %(sub_dir)s %(where)s && \
+git log -1 > %(where)s/README_git_log.txt && \
+rm -rf $tmpDir
+""" % aDict
+
+ DBG.write("cmd", cmd)
+
+ for nbtry in range(0,3): # retries case of network problem
+ rc = UTS.Popen(cmd, cwd=parentWhere, env=environment.environ.environ, logger=logger)
+ if rc.isOk(): break
+ time.sleep(30) # wait a little
+
+ return rc.isOk()