Skip to content

Commit 90c81a5

Browse files
authored
Merge pull request #1532 from marlamb/feature/reduce-resource-leaks
Fix some resource leaks by open file handles
2 parents 27a283b + e500466 commit 90c81a5

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

git/repo/base.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import re
1010
import shlex
1111
import warnings
12+
13+
from pathlib import Path
14+
1215
from gitdb.db.loose import LooseObjectDB
1316

1417
from gitdb.exc import BadObject
@@ -268,7 +271,7 @@ def __init__(
268271
pass
269272

270273
try:
271-
common_dir = open(osp.join(self.git_dir, "commondir"), "rt").readlines()[0].strip()
274+
common_dir = (Path(self.git_dir) / "commondir").read_text().splitlines()[0].strip()
272275
self._common_dir = osp.join(self.git_dir, common_dir)
273276
except OSError:
274277
self._common_dir = ""
@@ -1385,4 +1388,6 @@ def currently_rebasing_on(self) -> Commit | None:
13851388
rebase_head_file = osp.join(self.git_dir, "REBASE_HEAD")
13861389
if not osp.isfile(rebase_head_file):
13871390
return None
1388-
return self.commit(open(rebase_head_file, "rt").readline().strip())
1391+
with open(rebase_head_file, "rt") as f:
1392+
content = f.readline().strip()
1393+
return self.commit(content)

git/repo/fun.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import annotations
33
import os
44
import stat
5+
from pathlib import Path
56
from string import digits
67

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

8586
try:
86-
lines = open(dotgit, "r").readlines()
87+
lines = Path(dotgit).read_text().splitlines()
8788
for key, value in [line.strip().split(": ") for line in lines]:
8889
if key == "gitdir":
8990
return value

0 commit comments

Comments
 (0)