Skip to content

Make fetching source tarball by git commit more robust #4680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion easybuild/tools/filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2741,7 +2741,9 @@ def get_source_tarball_from_git(filename, target_dir, git_config):
# checkout is done separately below for specific commits
clone_cmd.append('--no-checkout')

clone_cmd.append(f'{url}/{repo_name}.git')
repo_url = f'{url}/{repo_name}.git'

clone_cmd.append(repo_url)

if clone_into:
clone_cmd.append(clone_into)
Expand All @@ -2761,6 +2763,10 @@ def get_source_tarball_from_git(filename, target_dir, git_config):
# if a specific commit is asked for, check it out
if commit:
checkout_cmd.append(f"{commit}")
# The commit might not be reachable from the default branch that is fetched, so fetch it explicitely
# Only works for long commit hashes
if len(commit) == 40:
run_shell_cmd(f'{git_cmd} fetch {repo_url}', hidden=True, verbose_dry_run=True, work_dir=tmpdir)
elif tag:
checkout_cmd.append(f"refs/tags/{tag}")

Expand Down
48 changes: 30 additions & 18 deletions test/framework/filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3073,16 +3073,31 @@ def run_check():
del git_config['extra_config_params']

del git_config['tag']
git_config['commit'] = '90366eac4408c5d615d69c5444ed784734b167c8'
string_args['commit'] = git_config['commit']
expected = '\n'.join([
r' running command "git clone --no-checkout %(git_repo)s"',
r" \(in .*/tmp.*\)",
r' running command "git fetch %(git_repo)s %(commit)s"',
r" \(in testrepository\)",
r' running command "git checkout %(commit)s && git submodule update --init --recursive"',
r" \(in testrepository\)",
r' running command "tar cfvz .*/target/test.tar.gz --exclude .git testrepository"',
r" \(in .*/tmp.*\)",
]) % string_args
run_check()

# clone & fetch does not work for short hashes
git_config['commit'] = '8456f86'
string_args['commit'] = git_config['commit']
expected = '\n'.join([
r' running shell command "git clone --no-checkout {git_repo}"',
r" \(in .*/tmp.*\)",
r' running shell command "git checkout 8456f86"',
r" \(in .*/{repo_name}\)",
r' running shell command "git submodule update --init --recursive"',
r" \(in .*/{repo_name}\)",
r"Archiving '.*/{repo_name}' into '{test_prefix}/target/test.tar.xz'...",
]).format(**string_args, repo_name='testrepository')
r' running command "git checkout %(commit)s && git submodule update --init --recursive"',
r" \(in testrepository\)",
r' running command "tar cfvz .*/target/test.tar.gz --exclude .git testrepository"',
r" \(in .*/tmp.*\)",
]) % string_args
run_check()

git_config['recurse_submodules'] = ['!vcflib', '!sdsl-lite']
Expand Down Expand Up @@ -3170,18 +3185,15 @@ def run_check():
self.assertFalse(os.path.isdir(os.path.join(extracted_repo_dir, '.git')))

del git_config['tag']
git_config['commit'] = '90366ea'
res = ft.get_source_tarball_from_git('test2', target_dir, git_config)
test_file = os.path.join(target_dir, 'test2.tar.xz')
self.assertEqual(res, test_file)
self.assertTrue(os.path.isfile(test_file))
test_tar_files.append(os.path.basename(test_file))
self.assertCountEqual(sorted(os.listdir(target_dir)), test_tar_files)
extracted_dir = tempfile.mkdtemp(prefix='extracted_dir')
with self.mocked_stdout_stderr():
extracted_repo_dir = ft.extract_file(test_file, extracted_dir, change_into_dir=False)
self.assertTrue(os.path.isfile(os.path.join(extracted_repo_dir, 'README.md')))
self.assertFalse(os.path.isdir(os.path.join(extracted_repo_dir, '.git')))
test_tar_gzs = []
for i, commit in enumerate(['90366eac4408c5d615d69c5444ed784734b167c8', '90366ea']):
git_config['commit'] = commit
test_file = os.path.join(target_dir, 'test2-%s.tar.gz' % i)
res = ft.get_source_tarball_from_git(os.path.basename(test_file), target_dir, git_config)
self.assertEqual(res, test_file)
self.assertTrue(os.path.isfile(test_file))
test_tar_gzs.append(os.path.basename(test_file))
self.assertEqual(sorted(os.listdir(target_dir)), test_tar_gzs)

git_config['keep_git_dir'] = True
res = ft.get_source_tarball_from_git('test3', target_dir, git_config)
Expand Down
Loading