Skip to content

Commit 67c1ec3

Browse files
committed
fix: properly use GitPython subcommands
1 parent 4e7bf67 commit 67c1ec3

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

src/gitingest/utils/git_utils.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,18 @@ async def ensure_git_installed() -> None:
9898
"""
9999
try:
100100
# Use GitPython to check git availability
101-
git.Git().version()
101+
git_cmd = git.Git()
102+
git_cmd.version()
102103
except git.GitCommandError as exc:
103104
msg = "Git is not installed or not accessible. Please install Git first."
104105
raise RuntimeError(msg) from exc
105106
except Exception as exc:
106107
msg = "Git is not installed or not accessible. Please install Git first."
107108
raise RuntimeError(msg) from exc
108-
109+
109110
if sys.platform == "win32":
110111
try:
111-
longpaths_value = git.Git().config("core.longpaths")
112+
longpaths_value = git_cmd.config("core.longpaths")
112113
if longpaths_value.lower() != "true":
113114
logger.warning(
114115
"Git clone may fail on Windows due to long file paths. "
@@ -215,29 +216,29 @@ async def fetch_remote_branches_or_tags(url: str, *, ref_type: str, token: str |
215216
raise ValueError(msg)
216217

217218
await ensure_git_installed()
218-
219+
219220
# Use GitPython to get remote references
220221
try:
221222
git_cmd = git.Git()
222-
223+
223224
# Prepare environment with authentication if needed
224225
env = None
225226
if token and is_github_host(url):
226227
auth_url = _add_token_to_url(url, token)
227228
url = auth_url
228-
229+
229230
fetch_tags = ref_type == "tags"
230231
to_fetch = "tags" if fetch_tags else "heads"
231-
232+
232233
# Build ls-remote command
233-
cmd_args = ["ls-remote", f"--{to_fetch}"]
234+
cmd_args = [f"--{to_fetch}"]
234235
if fetch_tags:
235236
cmd_args.append("--refs") # Filter out peeled tag objects
236237
cmd_args.append(url)
237-
238-
# Run the command
239-
output = git_cmd.execute(cmd_args, env=env)
240-
238+
239+
# Run the command using git_cmd.ls_remote() method
240+
output = git_cmd.ls_remote(*cmd_args)
241+
241242
# Parse output
242243
return [
243244
line.split(f"refs/{to_fetch}/", 1)[1]
@@ -269,14 +270,14 @@ def create_git_repo(local_path: str, url: str, token: str | None = None) -> git.
269270
"""
270271
try:
271272
repo = git.Repo(local_path)
272-
273+
273274
# Configure authentication if needed
274275
if token and is_github_host(url):
275276
auth_header = create_git_auth_header(token, url=url)
276277
# Set the auth header in git config for this repo
277-
key, value = auth_header.split('=', 1)
278+
key, value = auth_header.split("=", 1)
278279
repo.git.config(key, value)
279-
280+
280281
return repo
281282
except git.InvalidGitRepositoryError as exc:
282283
msg = f"Invalid git repository at {local_path}"
@@ -416,7 +417,7 @@ async def checkout_partial_clone(config: CloneConfig, token: str | None) -> None
416417
if config.blob:
417418
# Remove the file name from the subpath when ingesting from a file url (e.g. blob/branch/path/file.txt)
418419
subpath = str(Path(subpath).parent.as_posix())
419-
420+
420421
try:
421422
repo = create_git_repo(config.local_path, config.url, token)
422423
repo.git.execute(["sparse-checkout", "set", subpath])
@@ -480,16 +481,16 @@ async def _resolve_ref_to_sha(url: str, pattern: str, token: str | None = None)
480481
"""
481482
try:
482483
git_cmd = git.Git()
483-
484+
484485
# Prepare authentication if needed
485486
auth_url = url
486487
if token and is_github_host(url):
487488
auth_url = _add_token_to_url(url, token)
488-
489+
489490
# Execute ls-remote command
490-
output = git_cmd.execute(["ls-remote", auth_url, pattern])
491+
output = git_cmd.ls_remote(auth_url, pattern)
491492
lines = output.splitlines()
492-
493+
493494
sha = _pick_commit_sha(lines)
494495
if not sha:
495496
msg = f"{pattern!r} not found in {url}"
@@ -553,18 +554,20 @@ def _add_token_to_url(url: str, token: str) -> str:
553554
554555
"""
555556
from urllib.parse import urlparse, urlunparse
556-
557+
557558
parsed = urlparse(url)
558559
# Add token as username in URL (GitHub supports this)
559560
netloc = f"x-oauth-basic:{token}@{parsed.hostname}"
560561
if parsed.port:
561562
netloc += f":{parsed.port}"
562-
563-
return urlunparse((
564-
parsed.scheme,
565-
netloc,
566-
parsed.path,
567-
parsed.params,
568-
parsed.query,
569-
parsed.fragment
570-
))
563+
564+
return urlunparse(
565+
(
566+
parsed.scheme,
567+
netloc,
568+
parsed.path,
569+
parsed.params,
570+
parsed.query,
571+
parsed.fragment,
572+
),
573+
)

0 commit comments

Comments
 (0)