From 5c419db975eced488f79d3f526ade609fc7f947f Mon Sep 17 00:00:00 2001 From: Wing Fung Lau <4760060+hawflau@users.noreply.github.com> Date: Wed, 8 Jun 2022 10:06:36 -0700 Subject: [PATCH 1/4] Fix issue in cloning app templates repo in windows --- samcli/lib/utils/git_repo.py | 5 ++++- tests/unit/lib/utils/test_git_repo.py | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/samcli/lib/utils/git_repo.py b/samcli/lib/utils/git_repo.py index 47f6ede128..9b69d560dc 100644 --- a/samcli/lib/utils/git_repo.py +++ b/samcli/lib/utils/git_repo.py @@ -129,8 +129,11 @@ def clone(self, clone_dir: Path, clone_name: str, replace_existing: bool = False temp_path = os.path.normpath(os.path.join(tempdir, clone_name)) git_executable: str = GitRepo._git_executable() LOG.info("\nCloning from %s (process may take a moment)", self.url) + command = [git_executable, "clone", self.url, clone_name] + if platform.system().lower() == "windows": + command += ["--config", "core.longpaths=true"] check_output( - [git_executable, "clone", self.url, clone_name], + command, cwd=tempdir, stderr=subprocess.STDOUT, ) diff --git a/tests/unit/lib/utils/test_git_repo.py b/tests/unit/lib/utils/test_git_repo.py index c3ce27b6df..5812bc5f35 100644 --- a/tests/unit/lib/utils/test_git_repo.py +++ b/tests/unit/lib/utils/test_git_repo.py @@ -221,3 +221,29 @@ def test_clone_with_commit(self, platform_mock, popen_mock, check_output_mock, s shutil_mock.rmtree.assert_not_called() shutil_mock.copytree.assert_called_with(ANY, EXPECTED_DEFAULT_CLONE_PATH, ignore=ANY) shutil_mock.ignore_patterns.assert_called_with("*.git") + + @patch("samcli.lib.utils.git_repo.Path.exists") + @patch("samcli.lib.utils.git_repo.shutil") + @patch("samcli.lib.utils.git_repo.check_output") + @patch("samcli.lib.utils.git_repo.subprocess.Popen") + @patch("samcli.lib.utils.git_repo.platform.system") + def test_clone_with_longpaths_configured_in_windows( + self, platform_mock, popen_mock, check_output_mock, shutil_mock, path_exist_mock + ): + platform_mock.return_value = "windows" + path_exist_mock.return_value = False + self.repo.clone(clone_dir=self.local_clone_dir, clone_name=REPO_NAME) + self.local_clone_dir.mkdir.assert_called_once_with(mode=0o700, parents=True, exist_ok=True) + popen_mock.assert_called_once_with(["git"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + check_output_mock.assert_has_calls( + [ + call( + ["git", "clone", self.repo.url, REPO_NAME, "--config", "core.longpaths=true"], + cwd=ANY, + stderr=subprocess.STDOUT, + ) + ] + ) + shutil_mock.rmtree.assert_not_called() + shutil_mock.copytree.assert_called_with(ANY, EXPECTED_DEFAULT_CLONE_PATH, ignore=ANY) + shutil_mock.ignore_patterns.assert_called_with("*.git") From a2d09c8466c546e75c271850649c819e2648da01 Mon Sep 17 00:00:00 2001 From: Wing Fung Lau <4760060+hawflau@users.noreply.github.com> Date: Wed, 8 Jun 2022 13:48:38 -0700 Subject: [PATCH 2/4] Fix tests to address non windows cases --- tests/unit/lib/utils/test_git_repo.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unit/lib/utils/test_git_repo.py b/tests/unit/lib/utils/test_git_repo.py index 5812bc5f35..a7f789de25 100644 --- a/tests/unit/lib/utils/test_git_repo.py +++ b/tests/unit/lib/utils/test_git_repo.py @@ -53,6 +53,7 @@ def test_git_executable_fails(self, mock_popen): @patch("samcli.lib.utils.git_repo.subprocess.Popen") @patch("samcli.lib.utils.git_repo.platform.system") def test_clone_happy_case(self, platform_mock, popen_mock, check_output_mock, shutil_mock, path_exist_mock): + platform_mock.return_value = "Not Windows" path_exist_mock.return_value = False self.repo.clone(clone_dir=self.local_clone_dir, clone_name=REPO_NAME) self.local_clone_dir.mkdir.assert_called_once_with(mode=0o700, parents=True, exist_ok=True) @@ -207,6 +208,7 @@ def test_checkout_commit_when_commit_not_exist(self, check_output_mock, log_mock @patch("samcli.lib.utils.git_repo.subprocess.Popen") @patch("samcli.lib.utils.git_repo.platform.system") def test_clone_with_commit(self, platform_mock, popen_mock, check_output_mock, shutil_mock, path_exist_mock): + platform_mock.return_value = "Not Windows" path_exist_mock.return_value = False self.repo.clone(clone_dir=self.local_clone_dir, clone_name=REPO_NAME, commit=COMMIT) self.local_clone_dir.mkdir.assert_called_once_with(mode=0o700, parents=True, exist_ok=True) From 124e5769566badd2fe262db41a568e3827a4140c Mon Sep 17 00:00:00 2001 From: Wing Fung Lau <4760060+hawflau@users.noreply.github.com> Date: Wed, 8 Jun 2022 20:20:32 -0700 Subject: [PATCH 3/4] Add debug message --- samcli/lib/utils/git_repo.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/samcli/lib/utils/git_repo.py b/samcli/lib/utils/git_repo.py index 9b69d560dc..c87c6e0f78 100644 --- a/samcli/lib/utils/git_repo.py +++ b/samcli/lib/utils/git_repo.py @@ -131,6 +131,9 @@ def clone(self, clone_dir: Path, clone_name: str, replace_existing: bool = False LOG.info("\nCloning from %s (process may take a moment)", self.url) command = [git_executable, "clone", self.url, clone_name] if platform.system().lower() == "windows": + LOG.debug( + "Configure to core.longpaths=true in git clone. You might also need to enable long paths in Windows registry." + ) command += ["--config", "core.longpaths=true"] check_output( command, From ff8f42f8b44fbf87f95e26f79d47c2fc122c97a9 Mon Sep 17 00:00:00 2001 From: Wing Fung Lau <4760060+hawflau@users.noreply.github.com> Date: Thu, 9 Jun 2022 09:21:11 -0700 Subject: [PATCH 4/4] Fix pylint issue --- samcli/lib/utils/git_repo.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samcli/lib/utils/git_repo.py b/samcli/lib/utils/git_repo.py index c87c6e0f78..de3f1c4840 100644 --- a/samcli/lib/utils/git_repo.py +++ b/samcli/lib/utils/git_repo.py @@ -132,7 +132,8 @@ def clone(self, clone_dir: Path, clone_name: str, replace_existing: bool = False command = [git_executable, "clone", self.url, clone_name] if platform.system().lower() == "windows": LOG.debug( - "Configure to core.longpaths=true in git clone. You might also need to enable long paths in Windows registry." + "Configure core.longpaths=true in git clone. " + "You might also need to enable long paths in Windows registry." ) command += ["--config", "core.longpaths=true"] check_output(