Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IndexError when empty string for credential.helper #1860

Merged
merged 9 commits into from
Nov 27, 2023
12 changes: 7 additions & 5 deletions src/huggingface_hub/utils/_git_credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Contains utilities to manage Git credentials."""
import re
import subprocess
from typing import List, Optional

Expand All @@ -37,11 +38,12 @@ def list_credential_helpers(folder: Optional[str] = None) -> List[str]:
# NOTE: If user has set an helper for a custom URL, it will not we caught here.
# Example: `credential.https://huggingface.co.helper=store`
# See: https://github.com/huggingface/huggingface_hub/pull/1138#discussion_r1013324508
return sorted( # Sort for nice printing
{ # Might have some duplicates
line.split("=")[-1].split()[0] for line in output.split("\n") if "credential.helper" in line
}
)
for line in output.split("\n"):
l_cred_helper = sorted( # Sort for nice printing
# Might have some duplicates
re.findall(r"^\s*credential\.helper\s*=\s*(\w*)\s*$", line)
)
return l_cred_helper
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using re.findall is good. However, you don't need to run it on each line one by one. You can run on the whole output at once and use re.MULTILINE as flag. Here is how it looks like when you apply it only once:

Suggested change
for line in output.split("\n"):
l_cred_helper = sorted( # Sort for nice printing
# Might have some duplicates
re.findall(r"^\s*credential\.helper\s*=\s*(\w*)\s*$", line)
)
return l_cred_helper
return sorted( # Sort for nice printing
set( # Might have some duplicates
re.findall(
r"^\s*credential\.helper\s*=\s*(\w+)\s*$",
output,
flags=re.MULTILINE | re.IGNORECASE,
)
)
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Wauplin for the feedback. I've incorporated the above changes as indicated.

except subprocess.CalledProcessError as exc:
raise EnvironmentError(exc.stderr)

Expand Down