Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3c88c94

Browse files
author
Christophe Triquet
committedJun 19, 2024·
Backward compatibility for git suffixed existing repo
1 parent 1b122fb commit 3c88c94

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed
 

‎nbgitpuller/handlers.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
),
2121
)
2222

23+
def _get_repo_basename(repo_parent_dir, repo):
24+
repo_basename = os.path.splitext(os.path.basename(repo))[0]
25+
local_repo_dir = os.path.join(repo_parent_dir, repo_basename)
26+
# For backward compatibility
27+
# restore .git extension
28+
# if a repo with that name exists
29+
if os.path.exists(local_repo_dir + ".git"):
30+
repo_basename += ".git"
31+
return repo_basename
32+
2333
class SyncHandler(JupyterHandler):
2434
def __init__(self, *args, **kwargs):
2535
super().__init__(*args, **kwargs)
@@ -78,8 +88,7 @@ async def get(self):
7888
# must be expanded.
7989
repo_parent_dir = os.path.join(os.path.expanduser(self.settings['server_root_dir']),
8090
os.getenv('NBGITPULLER_PARENTPATH', ''))
81-
repo_basename = os.path.splitext(os.path.basename(repo))[0]
82-
repo_dir = os.path.join(repo_parent_dir, self.get_argument('targetpath', repo_basename))
91+
repo_dir = os.path.join(repo_parent_dir, self.get_argument('targetpath', _get_repo_basename(repo_parent_dir, repo)))
8392

8493
# We gonna send out event streams!
8594
self.set_header('content-type', 'text/event-stream')
@@ -155,9 +164,10 @@ async def get(self):
155164
self.get_argument('subPath', '.')
156165
app = self.get_argument('app', app_env)
157166
parent_reldir = os.getenv('NBGITPULLER_PARENTPATH', '')
158-
repo_basename = os.path.splitext(os.path.basename(repo))[0]
167+
repo_parent_dir = os.path.join(os.path.expanduser(self.settings['server_root_dir']),
168+
os.getenv('NBGITPULLER_PARENTPATH', ''))
159169
targetpath = self.get_argument('targetpath', None) or \
160-
self.get_argument('targetPath', repo_basename)
170+
self.get_argument('targetPath', _get_repo_basename(repo_parent_dir, repo))
161171

162172
if urlPath:
163173
path = urlPath

‎tests/test_api.py

+50
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import tempfile
23
from http.client import HTTPConnection
34
import subprocess
45
import time
@@ -122,6 +123,55 @@ def test_clone_default(jupyterdir, jupyter_server):
122123
assert os.path.isdir(os.path.join(target_path, '.git'))
123124

124125

126+
def test_clone_suffix_dropped(jupyterdir, jupyter_server):
127+
"""
128+
Tests drop of .git suffix from source repo path.
129+
"""
130+
target = str(uuid4())
131+
with Remote(path=os.path.join(tempfile.gettempdir(), target + '.git')) as remote, Pusher(remote) as pusher:
132+
pusher.push_file('README.md', 'Testing some content')
133+
print(f'path: {remote.path}')
134+
params = {
135+
'repo': remote.path,
136+
'branch': 'master',
137+
}
138+
r = request_api(params)
139+
assert r.code == 200
140+
s = r.read().decode()
141+
print(s)
142+
target_path = os.path.join(jupyterdir, target)
143+
assert f"Cloning into '{target_path}" in s
144+
assert os.path.isdir(target_path)
145+
assert not os.path.isdir(target_path + '.git')
146+
147+
def test_clone_suffix_dropped_legacy(jupyterdir, jupyter_server):
148+
"""
149+
Tests keep using legacy .git suffixed destination directory.
150+
"""
151+
with Remote() as remote, Pusher(remote) as pusher:
152+
pusher.push_file('README.md', 'Testing some content')
153+
print(f'path: {remote.path}')
154+
params = {
155+
'repo': remote.path,
156+
'branch': 'master',
157+
}
158+
r = request_api(params)
159+
assert r.code == 200
160+
s = r.read().decode()
161+
print(s)
162+
target_path = os.path.join(jupyterdir, os.path.basename(remote.path))
163+
assert f"Cloning into '{target_path}" in s
164+
assert os.path.isdir(target_path)
165+
166+
# rename target and run puller again
167+
# simulate a cloned repo with nbgitpuller previous version
168+
os.rename(target_path, target_path + '.git')
169+
r = request_api(params)
170+
assert r.code == 200
171+
s = r.read().decode()
172+
print(s)
173+
assert "Already up to date" in s
174+
125175
def test_clone_auth(jupyterdir, jupyter_server):
126176
"""
127177
Tests use of 'repo' and 'branch' parameters.

0 commit comments

Comments
 (0)
Please sign in to comment.