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 some resource leaks by open file handles #1532

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import re
import shlex
import warnings

from pathlib import Path

from gitdb.db.loose import LooseObjectDB

from gitdb.exc import BadObject
Expand Down Expand Up @@ -268,7 +271,7 @@ def __init__(
pass

try:
common_dir = open(osp.join(self.git_dir, "commondir"), "rt").readlines()[0].strip()
common_dir = (Path(self.git_dir) / "commondir").read_text().splitlines()[0].strip()
self._common_dir = osp.join(self.git_dir, common_dir)
except OSError:
self._common_dir = ""
Expand Down Expand Up @@ -1385,4 +1388,6 @@ def currently_rebasing_on(self) -> Commit | None:
rebase_head_file = osp.join(self.git_dir, "REBASE_HEAD")
if not osp.isfile(rebase_head_file):
return None
return self.commit(open(rebase_head_file, "rt").readline().strip())
with open(rebase_head_file, "rt") as f:
content = f.readline().strip()
return self.commit(content)
3 changes: 2 additions & 1 deletion git/repo/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations
import os
import stat
from pathlib import Path
from string import digits

from git.exc import WorkTreeRepositoryUnsupported
Expand Down Expand Up @@ -83,7 +84,7 @@ def find_worktree_git_dir(dotgit: "PathLike") -> Optional[str]:
return None

try:
lines = open(dotgit, "r").readlines()
lines = Path(dotgit).read_text().splitlines()
for key, value in [line.strip().split(": ") for line in lines]:
if key == "gitdir":
return value
Expand Down