Skip to content
Snippets Groups Projects
Commit b725c65f authored by chrg's avatar chrg
Browse files

Make executable type better

parent 27e94d43
Branches
No related tags found
No related merge requests found
...@@ -14,6 +14,27 @@ from dataclasses import dataclass, field ...@@ -14,6 +14,27 @@ from dataclasses import dataclass, field
# Some code borrowed from https://github.com/newren/git-filter-repo/blob/main/contrib/filter-repo-demos/lint-history # Some code borrowed from https://github.com/newren/git-filter-repo/blob/main/contrib/filter-repo-demos/lint-history
class Executable(click.ParamType):
name = "executable"
def convert(self, value, param, ctx):
if "/" in value:
exe = Path(value)
exe = exe.absolute()
if not os.access(exe, os.X_OK):
self.fail(f"{value!r} is not an executable file", param, ctx)
else:
pp = shutil.which(value)
if pp is None:
self.fail(
f"Could not find {value!r} on path, add './' if it is a file.",
param,
ctx,
)
exe = Path(pp)
return exe
@contextmanager @contextmanager
def blob_reader(): def blob_reader():
process = subprocess.Popen( process = subprocess.Popen(
...@@ -44,7 +65,7 @@ class BlobHandler: ...@@ -44,7 +65,7 @@ class BlobHandler:
is_relevant: Callable[[Path], bool] = lambda _: True is_relevant: Callable[[Path], bool] = lambda _: True
transform: Callable[[Path, bytes], bytes] = lambda _, b: b transform: Callable[[Path, bytes], bytes] = lambda _, b: b
def __call__(self, commit, metadata): def __call__(self, commit: fr.Commit, metadata):
assert self.filter is not None assert self.filter is not None
for change in commit.file_changes: for change in commit.file_changes:
filename = Path(change.filename.decode("utf-8")) filename = Path(change.filename.decode("utf-8"))
...@@ -111,7 +132,7 @@ def transform_program(program: Path, args: tuple[str], folder: Path): ...@@ -111,7 +132,7 @@ def transform_program(program: Path, args: tuple[str], folder: Path):
@click.argument( @click.argument(
"program", "program",
# help="The program to execute on each file", # help="The program to execute on each file",
type=click.Path(executable=True, path_type=Path), type=Executable(),
) )
@click.argument( @click.argument(
"args", "args",
...@@ -137,13 +158,6 @@ def regit( ...@@ -137,13 +158,6 @@ def regit(
else: else:
repo = git.Repo.clone_from(url=repo, to_path=output) repo = git.Repo.clone_from(url=repo, to_path=output)
if program.exists():
program = program.absolute()
else:
pp = shutil.which(str(program))
assert pp is not None
program = Path(pp)
os.chdir(output) os.chdir(output)
options = fr.FilteringOptions.parse_args([], error_on_empty=False) options = fr.FilteringOptions.parse_args([], error_on_empty=False)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment