Skip to content

Commit

Permalink
Mark git filter required on installation
Browse files Browse the repository at this point in the history
By [default](https://git-scm.com/docs/gitattributes#_filter), "A missing filter driver definition in the config, or a filter driver that exits with a non-zero status, is not an error but makes the filter a no-op passthru." i.e. if the `nbstripout` is misconfigured or fails, the filter is ignored.

Declare the filter to be `required` when installing, such that it *must* succeed.

Closes #191
  • Loading branch information
kynan committed Nov 17, 2024
1 parent 6d181dc commit ff7b16f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,14 @@ Show help and usage instructions:

### Using as a Git filter

Set up the git filter and attributes as described in the manual installation
instructions below:
Set up the [git filter](https://git-scm.com/docs/gitattributes#_filter) and
attributes as described in the manual installation instructions below:

nbstripout --install

Note: The filter is declared as `required`, meaning the filter *must* succeed.
Failures and misconfigurations will not simply cause the filter to be ignored.

Set up the git filter using `.gitattributes`:

nbstripout --install --attributes .gitattributes
Expand Down
2 changes: 2 additions & 0 deletions nbstripout/_nbstripout.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def install(git_config, install_location=INSTALL_LOCATION_LOCAL, python=None, at
filepath = f'"{PureWindowsPath(python or sys.executable).as_posix()}" -m nbstripout'
check_call(git_config + ['filter.nbstripout.clean', filepath])
check_call(git_config + ['filter.nbstripout.smudge', 'cat'])
check_call(git_config + ['filter.nbstripout.required', 'true'])
check_call(git_config + ['diff.ipynb.textconv', filepath + ' -t'])
attrfile = _get_attrfile(git_config, install_location, attrfile)
except FileNotFoundError:
Expand Down Expand Up @@ -251,6 +252,7 @@ def uninstall(git_config, install_location=INSTALL_LOCATION_LOCAL, attrfile=None
try:
call(git_config + ['--unset', 'filter.nbstripout.clean'], stdout=open(devnull, 'w'), stderr=STDOUT)
call(git_config + ['--unset', 'filter.nbstripout.smudge'], stdout=open(devnull, 'w'), stderr=STDOUT)
call(git_config + ['--unset', 'filter.nbstripout.required'], stdout=open(devnull, 'w'), stderr=STDOUT)
call(git_config + ['--remove-section', 'diff.ipynb'], stdout=open(devnull, 'w'), stderr=STDOUT)
attrfile = _get_attrfile(git_config, install_location, attrfile)
except FileNotFoundError:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_git_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def test_install(pytester: pytest.Pytester):
config = ConfigParser()
config.read(".git/config")
assert re.match(r".*python.* -m nbstripout", config['filter "nbstripout"']["clean"])
assert config['filter "nbstripout"']["required"] == "true"
assert config['filter "nbstripout"']["smudge"] == "cat"
assert re.match(r".*python.* -m nbstripout -t", config['diff "ipynb"']["textconv"])

Expand All @@ -40,6 +41,7 @@ def test_install_different_python(pytester: pytest.Pytester):
r".*DIFFERENTPYTHON.* -m nbstripout", config['filter "nbstripout"']["clean"]
)
assert sys.executable not in config['filter "nbstripout"']["clean"]
assert config['filter "nbstripout"']["required"] == "true"
assert config['filter "nbstripout"']["smudge"] == "cat"
assert re.match(
r".*DIFFERENTPYTHON.* -m nbstripout -t", config['diff "ipynb"']["textconv"]
Expand Down

0 comments on commit ff7b16f

Please sign in to comment.