Skip to content

Commit caf4537

Browse files
committed
Fix support for git short hashes
A `git fetch <repo> <hash>` requires a full hash and fails with a short hash with > could not find remote ref Disable the depth=1 optimization and remove the fetch call for those cases.
1 parent cd805e2 commit caf4537

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

easybuild/tools/filetools.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2741,7 +2741,9 @@ def get_source_tarball_from_git(filename, target_dir, git_config):
27412741
# checkout is done separately below for specific commits
27422742
clone_cmd.append('--no-checkout')
27432743

2744-
clone_cmd.append(f'{url}/{repo_name}.git')
2744+
repo_url = f'{url}/{repo_name}.git'
2745+
2746+
clone_cmd.append(repo_url)
27452747

27462748
if clone_into:
27472749
clone_cmd.append(clone_into)
@@ -2761,8 +2763,10 @@ def get_source_tarball_from_git(filename, target_dir, git_config):
27612763
# if a specific commit is asked for, check it out
27622764
if commit:
27632765
checkout_cmd.append(f"{commit}")
2764-
# The commit might not be reachable from the default branch that is fetched, so fetch it explicitely
2765-
run_shell_cmd(f'{git_cmd} {fetch} {repo_url}', hidden=True, verbose_dry_run=True, work_dir=tmpdir)
2766+
# The commit might not be reachable from the default branch that is fetched, so fetch it explicitely
2767+
# Only works for long commit hashes
2768+
if len(commit) == 40:
2769+
run_shell_cmd(f'{git_cmd} fetch {repo_url}', hidden=True, verbose_dry_run=True, work_dir=tmpdir)
27662770
elif tag:
27672771
checkout_cmd.append(f"refs/tags/{tag}")
27682772

test/framework/filetools.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3073,16 +3073,31 @@ def run_check():
30733073
del git_config['extra_config_params']
30743074

30753075
del git_config['tag']
3076+
git_config['commit'] = '90366eac4408c5d615d69c5444ed784734b167c8'
3077+
string_args['commit'] = git_config['commit']
3078+
expected = '\n'.join([
3079+
r' running command "git clone --no-checkout %(git_repo)s"',
3080+
r" \(in .*/tmp.*\)",
3081+
r' running command "git fetch %(git_repo)s %(commit)s"',
3082+
r" \(in testrepository\)",
3083+
r' running command "git checkout %(commit)s && git submodule update --init --recursive"',
3084+
r" \(in testrepository\)",
3085+
r' running command "tar cfvz .*/target/test.tar.gz --exclude .git testrepository"',
3086+
r" \(in .*/tmp.*\)",
3087+
]) % string_args
3088+
run_check()
3089+
3090+
# clone & fetch does not work for short hashes
30763091
git_config['commit'] = '8456f86'
3092+
string_args['commit'] = git_config['commit']
30773093
expected = '\n'.join([
30783094
r' running shell command "git clone --no-checkout {git_repo}"',
30793095
r" \(in .*/tmp.*\)",
3080-
r' running shell command "git checkout 8456f86"',
3081-
r" \(in .*/{repo_name}\)",
3082-
r' running shell command "git submodule update --init --recursive"',
3083-
r" \(in .*/{repo_name}\)",
3084-
r"Archiving '.*/{repo_name}' into '{test_prefix}/target/test.tar.xz'...",
3085-
]).format(**string_args, repo_name='testrepository')
3096+
r' running command "git checkout %(commit)s && git submodule update --init --recursive"',
3097+
r" \(in testrepository\)",
3098+
r' running command "tar cfvz .*/target/test.tar.gz --exclude .git testrepository"',
3099+
r" \(in .*/tmp.*\)",
3100+
]) % string_args
30863101
run_check()
30873102

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

31723187
del git_config['tag']
3173-
git_config['commit'] = '90366ea'
3174-
res = ft.get_source_tarball_from_git('test2', target_dir, git_config)
3175-
test_file = os.path.join(target_dir, 'test2.tar.xz')
3176-
self.assertEqual(res, test_file)
3177-
self.assertTrue(os.path.isfile(test_file))
3178-
test_tar_files.append(os.path.basename(test_file))
3179-
self.assertCountEqual(sorted(os.listdir(target_dir)), test_tar_files)
3180-
extracted_dir = tempfile.mkdtemp(prefix='extracted_dir')
3181-
with self.mocked_stdout_stderr():
3182-
extracted_repo_dir = ft.extract_file(test_file, extracted_dir, change_into_dir=False)
3183-
self.assertTrue(os.path.isfile(os.path.join(extracted_repo_dir, 'README.md')))
3184-
self.assertFalse(os.path.isdir(os.path.join(extracted_repo_dir, '.git')))
3188+
test_tar_gzs = []
3189+
for i, commit in enumerate(['90366eac4408c5d615d69c5444ed784734b167c8', '90366ea']):
3190+
git_config['commit'] = commit
3191+
test_file = os.path.join(target_dir, 'test2-%s.tar.gz' % i)
3192+
res = ft.get_source_tarball_from_git(os.path.basename(test_file), target_dir, git_config)
3193+
self.assertEqual(res, test_file)
3194+
self.assertTrue(os.path.isfile(test_file))
3195+
test_tar_gzs.append(os.path.basename(test_file))
3196+
self.assertEqual(sorted(os.listdir(target_dir)), test_tar_gzs)
31853197

31863198
git_config['keep_git_dir'] = True
31873199
res = ft.get_source_tarball_from_git('test3', target_dir, git_config)

0 commit comments

Comments
 (0)