Bazel: fix lfs to do checkout rather than smudging

On Windows `git lfs smudge` was not working as expected.
This commit is contained in:
Paolo Tranquilli
2024-04-08 10:01:32 +02:00
parent 38a7bc0580
commit fd77f1a7cb
3 changed files with 27 additions and 25 deletions

View File

@@ -0,0 +1,25 @@
#!/usr/bin/env python3
import sys
import pathlib
import subprocess
import os
sources = [pathlib.Path(arg).resolve() for arg in sys.argv[1:]]
source_dir = pathlib.Path(os.path.commonpath(src.parent for src in sources))
source_dir = subprocess.check_output(["git", "rev-parse", "--show-toplevel"], cwd=source_dir, text=True).strip()
def is_lfs_pointer(file):
lfs_header = "version https://git-lfs.github.com/spec".encode()
with open(file, 'rb') as fileobj:
return fileobj.read(len(lfs_header)) == lfs_header
for src in sources:
if is_lfs_pointer(src):
relative_src = src.relative_to(source_dir)
subprocess.run(["git", "lfs", "fetch", f"--include={relative_src}"], stdout=subprocess.DEVNULL, check=True,
cwd=source_dir)
subprocess.run(["git", "lfs", "checkout", relative_src], check=True, cwd=source_dir)
pathlib.Path(src.name).symlink_to(src)

View File

@@ -1,23 +0,0 @@
#!/usr/bin/env python3
import sys
import pathlib
import subprocess
import os
sources = [pathlib.Path(arg).resolve() for arg in sys.argv[1:]]
source_dir = pathlib.Path(os.path.commonpath(src.parent for src in sources))
def is_lfs_pointer(fileobj):
lfs_header = "version https://git-lfs.github.com/spec".encode()
actual_header = fileobj.read(len(lfs_header))
fileobj.seek(0)
return lfs_header == actual_header
for src in sources:
with open(src, 'rb') as input:
if is_lfs_pointer(input):
with open(src.name, 'wb') as output:
subprocess.run(["git", "lfs", "smudge", "--", src], stdin=input, stdout=output, check=True, cwd=source_dir)
continue
pathlib.Path(src.name).symlink_to(src)

View File

@@ -1,13 +1,13 @@
def lfs_smudge(repository_ctx, srcs):
for src in srcs:
repository_ctx.watch(src)
script = Label("//misc/bazel/internal:git_lfs_smudge.py")
script = Label("//misc/bazel/internal:git_lfs_checkout.py")
python = repository_ctx.which("python3") or repository_ctx.which("python")
if not python:
fail("Neither python3 nor python executables found")
res = repository_ctx.execute([python, script] + srcs, quiet = False)
if res.return_code != 0:
fail("git LFS smudging failed while instantiating @%s:\n%s" % (repository_ctx.name, res.stderr))
fail("git LFS checkout failed while instantiating @%s:\n%s" % (repository_ctx.name, res.stderr))
def _download_and_extract_lfs(repository_ctx):
attr = repository_ctx.attr