Skip to content

Commit

Permalink
Add submodules clone checkbox (#1188)
Browse files Browse the repository at this point in the history
* Add submodules clone checkbox

* Reorder getValue() type hint

* Fix test_clone.py formatting with Black
  • Loading branch information
seignovert authored Nov 28, 2022
1 parent da6ed22 commit 69c6122
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
5 changes: 4 additions & 1 deletion jupyterlab_git/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ async def changed_files(self, path, base=None, remote=None, single_commit=None):

return response

async def clone(self, path, repo_url, auth=None, versioning=True):
async def clone(self, path, repo_url, auth=None, versioning=True, submodules=False):
"""
Execute `git clone`.
When no auth is provided, disables prompts for the password to avoid the terminal hanging.
Expand All @@ -281,13 +281,16 @@ async def clone(self, path, repo_url, auth=None, versioning=True):
:param repo_url: the URL of the repository to be cloned.
:param auth: OPTIONAL dictionary with 'username' and 'password' fields
:param versioning: OPTIONAL whether to clone or download a snapshot of the remote repository; default clone
:param submodules: OPTIONAL whether to clone submodules content; default False
:return: response with status code and error message.
"""
env = os.environ.copy()
cmd = ["git", "clone"]
if not versioning:
cmd.append("--depth=1")
current_content = set(os.listdir(path))
if submodules:
cmd.append("--recurse-submodules")
cmd.append(unquote(repo_url))

if auth:
Expand Down
1 change: 1 addition & 0 deletions jupyterlab_git/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ async def post(self, path: str = ""):
data["clone_url"],
data.get("auth", None),
data["versioning"],
data["submodules"],
)

if response["code"] != 0:
Expand Down
24 changes: 24 additions & 0 deletions jupyterlab_git/tests/test_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@ def create_fake_git_repo(*args, **kwargs):
assert not git_folder.exists()


@pytest.mark.asyncio
async def test_git_submodules_success(tmp_path):
with patch("os.environ", {"TEST": "test"}):
with patch("jupyterlab_git.git.execute") as mock_execute:
# Given
output = "output"
mock_execute.return_value = maybe_future((0, output, "error"))

# When
actual_response = await Git().clone(
path=str(Path("/bin/test_curr_path")),
repo_url="ghjkhjkl",
submodules=True,
)

# Then
mock_execute.assert_called_once_with(
["git", "clone", "--recurse-submodules", "ghjkhjkl"],
cwd=str(Path("/bin") / "test_curr_path"),
env={"TEST": "test", "GIT_TERMINAL_PROMPT": "0"},
)
assert {"code": 0, "message": output} == actual_response


@pytest.mark.asyncio
async def test_git_clone_failure_from_git():
"""
Expand Down
42 changes: 31 additions & 11 deletions src/widgets/GitCloneForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class GitCloneForm extends Widget {
/**
* Returns the input value.
*/
getValue(): { url: string; versioning: boolean } {
getValue(): { url: string; versioning: boolean; submodules: boolean } {
return {
url: encodeURIComponent(
(
Expand All @@ -25,7 +25,12 @@ export class GitCloneForm extends Widget {
),
versioning: Boolean(
encodeURIComponent(
(this.node.querySelector('#checkbox') as HTMLInputElement).checked
(this.node.querySelector('#download') as HTMLInputElement).checked
)
),
submodules: Boolean(
encodeURIComponent(
(this.node.querySelector('#submodules') as HTMLInputElement).checked
)
)
};
Expand All @@ -38,33 +43,48 @@ export class GitCloneForm extends Widget {
const inputLink = document.createElement('input');
const linkText = document.createElement('span');
const checkboxWrapper = document.createElement('div');
const checkboxLabel = document.createElement('label');
const checkbox = document.createElement('input');
const subModulesLabel = document.createElement('label');
const subModules = document.createElement('input');
const downloadLabel = document.createElement('label');
const download = document.createElement('input');

node.className = 'jp-CredentialsBox';
inputWrapper.className = 'jp-RedirectForm';
checkboxWrapper.className = 'jp-CredentialsBox-wrapper';
checkboxLabel.className = 'jp-CredentialsBox-label-checkbox';
checkbox.id = 'checkbox';
subModulesLabel.className = 'jp-CredentialsBox-label-checkbox';
downloadLabel.className = 'jp-CredentialsBox-label-checkbox';
subModules.id = 'submodules';
download.id = 'download';
inputLink.id = 'input-link';

linkText.textContent = trans.__(
'Enter the URI of the remote Git repository'
);
inputLink.placeholder = 'https://host.com/org/repo.git';
checkboxLabel.textContent = trans.__('Download the repository');
checkboxLabel.title = trans.__(

subModulesLabel.textContent = trans.__('Include submodules');
subModulesLabel.title = trans.__(
'If checked, the remote submodules in the repository will be cloned recursively'
);
subModules.setAttribute('type', 'checkbox');
subModules.setAttribute('checked', 'checked');

downloadLabel.textContent = trans.__('Download the repository');
downloadLabel.title = trans.__(
'If checked, the remote repository default branch will be downloaded instead of cloned'
);
checkbox.setAttribute('type', 'checkbox');
download.setAttribute('type', 'checkbox');

inputLinkLabel.appendChild(linkText);
inputLinkLabel.appendChild(inputLink);

inputWrapper.append(inputLinkLabel);

checkboxLabel.prepend(checkbox);
checkboxWrapper.appendChild(checkboxLabel);
subModulesLabel.prepend(subModules);
checkboxWrapper.appendChild(subModulesLabel);

downloadLabel.prepend(download);
checkboxWrapper.appendChild(downloadLabel);

node.appendChild(inputWrapper);
node.appendChild(checkboxWrapper);
Expand Down

0 comments on commit 69c6122

Please sign in to comment.