Skip to content

Commit 54db65f

Browse files
Add configuration to exclude paths (jupyterlab#1163)
* Add configuration to exclude paths * Update help message * Fix failing test * Add test * Updated README.md * Update README.md Co-authored-by: Frédéric Collonval <[email protected]> * Update jupyterlab_git/git.py Co-authored-by: Frédéric Collonval <[email protected]> * Update jupyterlab_git/handlers.py Co-authored-by: Frédéric Collonval <[email protected]> * Update jupyterlab_git/tests/test_handlers.py Co-authored-by: Frédéric Collonval <[email protected]> * Update conftest.py Co-authored-by: Frédéric Collonval <[email protected]> * Update jupyterlab_git/handlers.py Co-authored-by: Frédéric Collonval <[email protected]> * Run lint Co-authored-by: Frédéric Collonval <[email protected]>
1 parent a08a929 commit 54db65f

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Once installed, extension behavior can be modified via the following settings wh
112112
It is possible to provide a list of commands to be executed in a folder after it is initialized as Git repository.
113113
- `JupyterLabGit.credential_helper`: Git credential helper to set to cache the credentials.
114114
The default value is `cache --timeout=3600` to cache the credentials for an hour. If you want to cache them for 10 hours, set `cache --timeout=36000`.
115+
- `JupyterLabGit.excluded_paths`: Set path patterns to exclude from this extension. You can use wildcard and interrogation mark for respectively everything or any single character in the pattern.
115116

116117
<details>
117118
<summary><b>How to set server settings?</b></summary>

conftest.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44

55

66
@pytest.fixture
7-
def jp_server_config(jp_server_config):
8-
return {"ServerApp": {"jpserver_extensions": {"jupyterlab_git": True}}}
7+
def jp_server_config(jp_server_config, jp_root_dir):
8+
return {
9+
"ServerApp": {"jpserver_extensions": {"jupyterlab_git": True}},
10+
"JupyterLabGit": {"excluded_paths": ["/ignored-path/*"]},
11+
}

jupyterlab_git/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class JupyterLabGit(Configurable):
3737
# TODO Validate
3838
)
3939

40+
excluded_paths = List(help="Paths to be excluded", config=True, trait=Unicode())
41+
4042
credential_helper = Unicode(
4143
help="""
4244
The value of Git credential helper will be set to this value when the Git credential caching mechanism is activated by this extension.

jupyterlab_git/git.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,3 +1750,11 @@ def ensure_git_credential_cache_daemon(
17501750

17511751
elif self._GIT_CREDENTIAL_CACHE_DAEMON_PROCESS.poll():
17521752
self.ensure_git_credential_cache_daemon(socket, debug, True, cwd, env)
1753+
1754+
@property
1755+
def excluded_paths(self) -> List[str]:
1756+
"""Wildcard-style path patterns that do not support git commands.
1757+
1758+
You can use ``*`` to match everything or ``?`` to match any single character.
1759+
"""
1760+
return self._config.excluded_paths

jupyterlab_git/handlers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from jupyter_server.services.contents.manager import ContentsManager
1313
from jupyter_server.utils import url2path, url_path_join
1414
from packaging.version import parse
15+
import fnmatch
1516

1617
try:
1718
import hybridcontents
@@ -37,6 +38,18 @@ class GitHandler(APIHandler):
3738
def git(self) -> Git:
3839
return self.settings["git"]
3940

41+
def prepare(self):
42+
"""Check if the path should be skipped"""
43+
super().prepare()
44+
path = self.path_kwargs.get("path")
45+
if path is not None:
46+
excluded_paths = self.git.excluded_paths
47+
for excluded_path in excluded_paths:
48+
if fnmatch.fnmatchcase(path, excluded_path):
49+
self.set_status(404)
50+
self.finish()
51+
break
52+
4053
@functools.lru_cache()
4154
def url2localpath(
4255
self, path: str, with_contents_manager: bool = False

jupyterlab_git/tests/test_handlers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from jupyterlab_git.handlers import NAMESPACE, setup_handlers, GitHandler
99

1010
from .testutils import assert_http_error, maybe_future
11+
from tornado.httpclient import HTTPClientError
1112

1213

1314
def test_mapping_added():
@@ -106,6 +107,23 @@ async def test_git_show_prefix(mock_execute, jp_fetch, jp_root_dir):
106107
)
107108

108109

110+
async def test_git_show_prefix_for_excluded_path(
111+
jp_fetch, jp_server_config, jp_root_dir
112+
):
113+
local_path = jp_root_dir / "ignored-path"
114+
115+
try:
116+
response = await jp_fetch(
117+
NAMESPACE,
118+
local_path.name + "/subdir",
119+
"show_prefix",
120+
body="{}",
121+
method="POST",
122+
)
123+
except HTTPClientError as e:
124+
assert e.code == 404
125+
126+
109127
@patch("jupyterlab_git.git.execute")
110128
async def test_git_show_prefix_not_a_git_repo(mock_execute, jp_fetch, jp_root_dir):
111129
# Given

0 commit comments

Comments
 (0)