[to #44857956]fix: disable git command username/password prompt

Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/10106602

    * [to #44857956]fix: disable git command username/password prompt
This commit is contained in:
mulin.lyh
2022-09-14 06:44:04 +08:00
committed by wenmeng.zwm
parent 0877725434
commit d0edfcf50e
2 changed files with 22 additions and 2 deletions

View File

@@ -39,14 +39,21 @@ class GitCommandWrapper(metaclass=Singleton):
subprocess.CompletedProcess: the command response
"""
logger.debug(' '.join(args))
git_env = os.environ.copy()
git_env['GIT_TERMINAL_PROMPT'] = '0'
response = subprocess.run(
[self.git_path, *args],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE) # compatible for python3.6
stderr=subprocess.PIPE,
env=git_env,
) # compatible for python3.6
try:
response.check_returncode()
return response
except subprocess.CalledProcessError as error:
logger.error(
'There are error run git command, you may need to login first.'
)
raise GitError(
'stdout: %s, stderr: %s' %
(response.stdout.decode('utf8'), error.stderr.decode('utf8')))

View File

@@ -10,7 +10,8 @@ from modelscope.hub.errors import GitError
from modelscope.hub.repository import Repository
from modelscope.utils.constant import ModelFile
from .test_utils import (TEST_ACCESS_TOKEN1, TEST_ACCESS_TOKEN2,
TEST_MODEL_CHINESE_NAME, TEST_MODEL_ORG)
TEST_MODEL_CHINESE_NAME, TEST_MODEL_ORG,
delete_credential)
DEFAULT_GIT_PATH = 'git'
@@ -65,6 +66,18 @@ class HubPrivateRepositoryTest(unittest.TestCase):
print(repo2.model_dir)
assert repo1.model_dir == repo2.model_dir
def test_clone_private_model_without_token(self):
delete_credential()
temporary_dir = tempfile.mkdtemp()
local_dir = os.path.join(temporary_dir, self.model_name)
with self.assertRaises(GitError) as cm:
Repository(local_dir, clone_from=self.model_id)
print(cm.exception)
assert not os.path.exists(os.path.join(local_dir, ModelFile.README))
self.api.login(TEST_ACCESS_TOKEN1) # re-login for delete
if __name__ == '__main__':
unittest.main()