Skip to content

Commit f3eaeb3

Browse files
sguggerLysandreJik
andauthored
Fix is_git_repo by ignoring tokens (#141)
* Fix is_git_repo by ignoring tokens * Style * Initializing twice should not fail * Style * The local path shouldn't have the token either * Apply to debug statement as well Co-authored-by: Lysandre <[email protected]>
1 parent 66636bc commit f3eaeb3

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/huggingface_hub/repository.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def is_local_clone(folder: Union[str, Path], remote_url: str):
4141

4242
# Remove token for the test with remotes.
4343
remote_url = re.sub(r"https://.*@", "https://", remote_url)
44-
return remote_url in remotes.split()
44+
remotes = [re.sub(r"https://.*@", "https://", remote) for remote in remotes.split()]
45+
return remote_url in remotes
4546

4647

4748
class Repository:
@@ -163,6 +164,9 @@ def clone_from(self, repo_url: str, use_auth_token: Union[bool, str, None] = Non
163164
repo_url = repo_url.replace(
164165
"https://", f"https://user:{huggingface_token}@"
165166
)
167+
168+
# For error messages, it's cleaner to show the repo url without the token.
169+
clean_repo_url = re.sub(r"https://.*@", "https://", repo_url)
166170
try:
167171
subprocess.run(
168172
"git lfs install".split(),
@@ -174,7 +178,7 @@ def clone_from(self, repo_url: str, use_auth_token: Union[bool, str, None] = Non
174178

175179
# checks if repository is initialized in a empty repository or in one with files
176180
if len(os.listdir(self.local_dir)) == 0:
177-
logger.debug(f"Cloning {repo_url} into local empty directory.")
181+
logger.debug(f"Cloning {clean_repo_url} into local empty directory.")
178182
subprocess.run(
179183
["git", "clone", repo_url, "."],
180184
stderr=subprocess.PIPE,
@@ -190,7 +194,7 @@ def clone_from(self, repo_url: str, use_auth_token: Union[bool, str, None] = Non
190194
if in_repository:
191195
if is_local_clone(self.local_dir, repo_url):
192196
logger.debug(
193-
f"{self.local_dir} is already a clone of {repo_url}. Make sure you pull the latest"
197+
f"{self.local_dir} is already a clone of {clean_repo_url}. Make sure you pull the latest"
194198
"changes with `repo.git_pull()`."
195199
)
196200
else:
@@ -203,11 +207,14 @@ def clone_from(self, repo_url: str, use_auth_token: Union[bool, str, None] = Non
203207
)
204208

205209
error_msg = (
206-
f"Tried to clone {repo_url} in an unrelated git repository.\nIf you believe this is an "
207-
f"error, please add a remote with the following URL: {repo_url}."
210+
f"Tried to clone {clean_repo_url} in an unrelated git repository.\nIf you believe this is "
211+
f"an error, please add a remote with the following URL: {clean_repo_url}."
208212
)
209213
if output.returncode == 0:
210-
error_msg += f"\nLocal path has its origin defined as: {output.stdout}"
214+
clean_local_remote_url = re.sub(
215+
r"https://.*@", "https://", output.stdout
216+
)
217+
error_msg += f"\nLocal path has its origin defined as: {clean_local_remote_url}"
211218

212219
raise EnvironmentError(error_msg)
213220

tests/test_repository.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ def test_init_clone_in_nonempty_non_linked_git_repo(self):
124124

125125
self._api.delete_repo(token=self._token, name=f"{REPO_NAME}-temp")
126126

127+
def test_init_clone_in_nonempty_linked_git_repo_with_token(self):
128+
Repository(
129+
WORKING_REPO_DIR, clone_from=self._repo_url, use_auth_token=self._token
130+
)
131+
Repository(
132+
WORKING_REPO_DIR, clone_from=self._repo_url, use_auth_token=self._token
133+
)
134+
127135
def test_init_clone_in_nonempty_linked_git_repo(self):
128136
# Clone the repository to disk
129137
Repository(WORKING_REPO_DIR, clone_from=self._repo_url)

0 commit comments

Comments
 (0)