Skip to content

Commit fafaf30

Browse files
committed
Fix the cloning of tags
Can't use refs/tags/xxx for git clone so clone it assuming it is a tag and check afterwards. Fall back to fetching the full history and checking out the tag and submodules manually
1 parent 95e05c8 commit fafaf30

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

easybuild/tools/filetools.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2477,7 +2477,7 @@ def get_source_tarball_from_git(filename, targetdir, git_config):
24772477
clone_cmd.extend(['--depth', '1'])
24782478

24792479
if tag:
2480-
clone_cmd.extend(['--branch', 'refs/tags/' + tag])
2480+
clone_cmd.extend(['--branch', tag])
24812481
if recursive:
24822482
clone_cmd.append('--recursive')
24832483
else:
@@ -2487,22 +2487,41 @@ def get_source_tarball_from_git(filename, targetdir, git_config):
24872487

24882488
tmpdir = tempfile.mkdtemp()
24892489
cwd = change_dir(tmpdir)
2490-
run.run_cmd(' '.join(clone_cmd), log_all=True, log_ok=False, simple=False, regexp=False)
2490+
run.run_cmd(' '.join(clone_cmd), log_all=True, simple=True, regexp=False)
24912491

24922492
# if a specific commit is asked for, check it out
24932493
if commit:
24942494
checkout_cmd = ['git', 'checkout', commit]
24952495
if recursive:
24962496
checkout_cmd.extend(['&&', 'git', 'submodule', 'update', '--init', '--recursive'])
24972497

2498-
run.run_cmd(' '.join(checkout_cmd), log_all=True, log_ok=False, simple=False, regexp=False, path=repo_name)
2498+
run.run_cmd(' '.join(checkout_cmd), log_all=True, simple=True, regexp=False, path=repo_name)
2499+
elif not build_option('extended_dry_run'):
2500+
# If we wanted to get a tag make sure we actually got a tag and not a branch with the same name
2501+
# This doesn't make sense in dry-run mode as we don't have anything to check
2502+
cmd = 'git describe --exact-match --tags HEAD'
2503+
# Note: Disable logging to also disable the error handling in run_cmd
2504+
(out, ec) = run.run_cmd(cmd, log_ok=False, log_all=False, regexp=False, path=repo_name)
2505+
if ec != 0 or tag not in out.splitlines():
2506+
cmds = []
2507+
if not keep_git_dir:
2508+
# Make the repo unshallow, same as git fetch --unshallow in git 1.8.3+
2509+
# The first fetch seemingly does nothing, no idea why.
2510+
cmds.append('git fetch --depth=2147483647 && git fetch --depth=2147483647')
2511+
cmds.append('git checkout refs/tags/' + tag)
2512+
# Clean all untracked files, e.g. from left-over submodules
2513+
cmds.append('git clean --force -d -x')
2514+
if recursive:
2515+
cmds.append('git submodule update --init --recursive')
2516+
for cmd in cmds:
2517+
run.run_cmd(cmd, log_all=True, simple=True, regexp=False, path=repo_name)
24992518

25002519
# create an archive and delete the git repo directory
25012520
if keep_git_dir:
25022521
tar_cmd = ['tar', 'cfvz', targetpath, repo_name]
25032522
else:
25042523
tar_cmd = ['tar', 'cfvz', targetpath, '--exclude', '.git', repo_name]
2505-
run.run_cmd(' '.join(tar_cmd), log_all=True, log_ok=False, simple=False, regexp=False)
2524+
run.run_cmd(' '.join(tar_cmd), log_all=True, simple=True, regexp=False)
25062525

25072526
# cleanup (repo_name dir does not exist in dry run mode)
25082527
change_dir(cwd)

test/framework/filetools.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,7 +2517,7 @@ def run_check():
25172517
}
25182518
git_repo = {'git_repo': '[email protected]:easybuilders/testrepository.git'} # Just to make the below shorter
25192519
expected = '\n'.join([
2520-
r' running command "git clone --depth 1 --branch refs/tags/tag_for_tests %(git_repo)s"',
2520+
r' running command "git clone --depth 1 --branch tag_for_tests %(git_repo)s"',
25212521
r" \(in .*/tmp.*\)",
25222522
r' running command "tar cfvz .*/target/test.tar.gz --exclude .git testrepository"',
25232523
r" \(in .*/tmp.*\)",
@@ -2526,7 +2526,7 @@ def run_check():
25262526

25272527
git_config['recursive'] = True
25282528
expected = '\n'.join([
2529-
r' running command "git clone --depth 1 --branch refs/tags/tag_for_tests --recursive %(git_repo)s"',
2529+
r' running command "git clone --depth 1 --branch tag_for_tests --recursive %(git_repo)s"',
25302530
r" \(in .*/tmp.*\)",
25312531
r' running command "tar cfvz .*/target/test.tar.gz --exclude .git testrepository"',
25322532
r" \(in .*/tmp.*\)",
@@ -2535,7 +2535,7 @@ def run_check():
25352535

25362536
git_config['keep_git_dir'] = True
25372537
expected = '\n'.join([
2538-
r' running command "git clone --branch refs/tags/tag_for_tests --recursive %(git_repo)s"',
2538+
r' running command "git clone --branch tag_for_tests --recursive %(git_repo)s"',
25392539
r" \(in .*/tmp.*\)",
25402540
r' running command "tar cfvz .*/target/test.tar.gz testrepository"',
25412541
r" \(in .*/tmp.*\)",
@@ -2566,12 +2566,12 @@ def run_check():
25662566
]) % git_repo
25672567
run_check()
25682568

2569-
# Test with real data
2569+
# Test with real data.
25702570
init_config()
25712571
git_config = {
25722572
'repo_name': 'testrepository',
25732573
'url': 'https://github.com/easybuilders',
2574-
'tag': 'tag_for_tests',
2574+
'tag': 'branch_tag_for_test',
25752575
}
25762576

25772577
try:
@@ -2581,11 +2581,21 @@ def run_check():
25812581
self.assertEqual(res, test_file)
25822582
self.assertTrue(os.path.isfile(test_file))
25832583
self.assertEqual(os.listdir(target_dir), ['test.tar.gz'])
2584+
# Check that we indeed downloaded the right tag
2585+
extracted_dir = tempfile.mkdtemp(prefix='extracted_dir')
2586+
extracted_repo_dir = ft.extract_file(test_file, extracted_dir, change_into_dir=False)
2587+
self.assertTrue(os.path.isfile(os.path.join(extracted_repo_dir, 'this-is-a-branch.txt')))
2588+
os.remove(test_file)
25842589

2585-
# Check that we indeed downloaded the tag and not a branch
2590+
# use a tag that clashes with a branch name and make sure this is handled correctly
2591+
git_config['tag'] = 'tag_for_tests'
2592+
res = ft.get_source_tarball_from_git('test.tar.gz', target_dir, git_config)
2593+
self.assertEqual(res, test_file)
2594+
self.assertTrue(os.path.isfile(test_file))
2595+
# Check that we indeed downloaded the tag and not the branch
25862596
extracted_dir = tempfile.mkdtemp(prefix='extracted_dir')
2587-
target_dir = ft.extract_file(test_file, extracted_dir, change_into_dir=False)
2588-
self.assertTrue(os.path.isfile(os.path.join(target_dir, 'this-is-a-tag.txt')))
2597+
extracted_repo_dir = ft.extract_file(test_file, extracted_dir, change_into_dir=False)
2598+
self.assertTrue(os.path.isfile(os.path.join(extracted_repo_dir, 'this-is-a-tag.txt')))
25892599

25902600
del git_config['tag']
25912601
git_config['commit'] = '8456f86'

0 commit comments

Comments
 (0)