Merge pull request #1813 from EliahKagan/logging · gitpython-developers/GitPython@10cdd03 · GitHub
Skip to content

Commit 10cdd03

Browse files
authored
Merge pull request #1813 from EliahKagan/logging
Don't suppress messages when logging is not configured
2 parents 9a7cec1 + bc42ee5 commit 10cdd03

11 files changed

Lines changed: 57 additions & 68 deletions

File tree

git/cmd.py

Lines changed: 10 additions & 11 deletions

git/config.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@
6060

6161
__all__ = ("GitConfigParser", "SectionConstraint")
6262

63-
64-
log = logging.getLogger("git.config")
65-
log.addHandler(logging.NullHandler())
66-
63+
_logger = logging.getLogger(__name__)
6764

6865
CONFIG_LEVELS: ConfigLevels_Tup = ("system", "user", "global", "repository")
6966
"""The configuration level of a configuration file."""
@@ -412,7 +409,7 @@ def release(self) -> None:
412409
try:
413410
self.write()
414411
except IOError:
415-
log.error("Exception during destruction of GitConfigParser", exc_info=True)
412+
_logger.error("Exception during destruction of GitConfigParser", exc_info=True)
416413
except ReferenceError:
417414
# This happens in Python 3... and usually means that some state cannot be
418415
# written as the sections dict cannot be iterated. This usually happens when
@@ -712,7 +709,7 @@ def write(self) -> None:
712709
# END assert multiple files
713710

714711
if self._has_includes():
715-
log.debug(
712+
_logger.debug(
716713
"Skipping write-back of configuration file as include files were merged in."
717714
+ "Set merge_includes=False to prevent this."
718715
)

git/objects/commit.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@
5252

5353
# ------------------------------------------------------------------------
5454

55-
log = logging.getLogger("git.objects.commit")
56-
log.addHandler(logging.NullHandler())
55+
_logger = logging.getLogger(__name__)
5756

5857
__all__ = ("Commit",)
5958

@@ -767,7 +766,7 @@ def _deserialize(self, stream: BytesIO) -> "Commit":
767766
self.author_tz_offset,
768767
) = parse_actor_and_date(author_line.decode(self.encoding, "replace"))
769768
except UnicodeDecodeError:
770-
log.error(
769+
_logger.error(
771770
"Failed to decode author line '%s' using encoding %s",
772771
author_line,
773772
self.encoding,
@@ -781,7 +780,7 @@ def _deserialize(self, stream: BytesIO) -> "Commit":
781780
self.committer_tz_offset,
782781
) = parse_actor_and_date(committer_line.decode(self.encoding, "replace"))
783782
except UnicodeDecodeError:
784-
log.error(
783+
_logger.error(
785784
"Failed to decode committer line '%s' using encoding %s",
786785
committer_line,
787786
self.encoding,
@@ -795,7 +794,7 @@ def _deserialize(self, stream: BytesIO) -> "Commit":
795794
try:
796795
self.message = self.message.decode(self.encoding, "replace")
797796
except UnicodeDecodeError:
798-
log.error(
797+
_logger.error(
799798
"Failed to decode message '%s' using encoding %s",
800799
self.message,
801800
self.encoding,

git/objects/submodule/base.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141

4242
# typing ----------------------------------------------------------------------
43+
4344
from typing import Callable, Dict, Mapping, Sequence, TYPE_CHECKING, cast
4445
from typing import Any, Iterator, Union
4546

@@ -50,14 +51,11 @@
5051
from git.repo import Repo
5152
from git.refs import Head
5253

53-
5454
# -----------------------------------------------------------------------------
5555

5656
__all__ = ["Submodule", "UpdateProgress"]
5757

58-
59-
log = logging.getLogger("git.objects.submodule.base")
60-
log.addHandler(logging.NullHandler())
58+
_logger = logging.getLogger(__name__)
6159

6260

6361
class UpdateProgress(RemoteProgress):
@@ -731,7 +729,7 @@ def update(
731729
)
732730
mrepo.head.reference.set_tracking_branch(remote_branch)
733731
except (IndexError, InvalidGitRepositoryError):
734-
log.warning("Failed to checkout tracking branch %s", self.branch_path)
732+
_logger.warning("Failed to checkout tracking branch %s", self.branch_path)
735733
# END handle tracking branch
736734

737735
# NOTE: Have to write the repo config file as well, otherwise the
@@ -761,14 +759,14 @@ def update(
761759
binsha = rcommit.binsha
762760
hexsha = rcommit.hexsha
763761
else:
764-
log.error(
762+
_logger.error(
765763
"%s a tracking branch was not set for local branch '%s'",
766764
msg_base,
767765
mrepo.head.reference,
768766
)
769767
# END handle remote ref
770768
else:
771-
log.error("%s there was no local tracking branch", msg_base)
769+
_logger.error("%s there was no local tracking branch", msg_base)
772770
# END handle detached head
773771
# END handle to_latest_revision option
774772

@@ -786,15 +784,15 @@ def update(
786784
if force:
787785
msg = "Will force checkout or reset on local branch that is possibly in the future of"
788786
msg += " the commit it will be checked out to, effectively 'forgetting' new commits"
789-
log.debug(msg)
787+
_logger.debug(msg)
790788
else:
791789
msg = "Skipping %s on branch '%s' of submodule repo '%s' as it contains un-pushed commits"
792790
msg %= (
793791
is_detached and "checkout" or "reset",
794792
mrepo.head,
795793
mrepo,
796794
)
797-
log.info(msg)
795+
_logger.info(msg)
798796
may_reset = False
799797
# END handle force
800798
# END handle if we are in the future
@@ -834,7 +832,7 @@ def update(
834832
except Exception as err:
835833
if not keep_going:
836834
raise
837-
log.error(str(err))
835+
_logger.error(str(err))
838836
# END handle keep_going
839837

840838
# HANDLE RECURSION

git/objects/submodule/root.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
__all__ = ["RootModule", "RootUpdateProgress"]
2424

25-
log = logging.getLogger("git.objects.submodule.root")
26-
log.addHandler(logging.NullHandler())
25+
_logger = logging.getLogger(__name__)
2726

2827

2928
class RootUpdateProgress(UpdateProgress):
@@ -321,7 +320,7 @@ def update(
321320
# this way, it will be checked out in the next step.
322321
# This will change the submodule relative to us, so
323322
# the user will be able to commit the change easily.
324-
log.warning(
323+
_logger.warning(
325324
"Current sha %s was not contained in the tracking\
326325
branch at the new remote, setting it the the remote's tracking branch",
327326
sm.hexsha,
@@ -393,7 +392,7 @@ def update(
393392
except Exception as err:
394393
if not keep_going:
395394
raise
396-
log.error(str(err))
395+
_logger.error(str(err))
397396
# END handle keep_going
398397

399398
# FINALLY UPDATE ALL ACTUAL SUBMODULES

git/remote.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@
5858

5959
# -------------------------------------------------------------
6060

61-
62-
log = logging.getLogger("git.remote")
63-
log.addHandler(logging.NullHandler())
64-
61+
_logger = logging.getLogger(__name__)
6562

6663
__all__ = ("RemoteProgress", "PushInfo", "FetchInfo", "Remote")
6764

@@ -846,7 +843,7 @@ def _get_fetch_info_from_stderr(
846843
stderr_text = progress.error_lines and "\n".join(progress.error_lines) or ""
847844
proc.wait(stderr=stderr_text)
848845
if stderr_text:
849-
log.warning("Error lines received while fetching: %s", stderr_text)
846+
_logger.warning("Error lines received while fetching: %s", stderr_text)
850847

851848
for line in progress.other_lines:
852849
line = force_text(line)
@@ -867,9 +864,9 @@ def _get_fetch_info_from_stderr(
867864
msg += "length of progress lines %i should be equal to lines in FETCH_HEAD file %i\n"
868865
msg += "Will ignore extra progress lines or fetch head lines."
869866
msg %= (l_fil, l_fhi)
870-
log.debug(msg)
871-
log.debug(b"info lines: " + str(fetch_info_lines).encode("UTF-8"))
872-
log.debug(b"head info: " + str(fetch_head_info).encode("UTF-8"))
867+
_logger.debug(msg)
868+
_logger.debug(b"info lines: " + str(fetch_info_lines).encode("UTF-8"))
869+
_logger.debug(b"head info: " + str(fetch_head_info).encode("UTF-8"))
873870
if l_fil < l_fhi:
874871
fetch_head_info = fetch_head_info[:l_fil]
875872
else:
@@ -881,8 +878,8 @@ def _get_fetch_info_from_stderr(
881878
try:
882879
output.append(FetchInfo._from_line(self.repo, err_line, fetch_line))
883880
except ValueError as exc:
884-
log.debug("Caught error while parsing line: %s", exc)
885-
log.warning("Git informed while fetching: %s", err_line.strip())
881+
_logger.debug("Caught error while parsing line: %s", exc)
882+
_logger.warning("Git informed while fetching: %s", err_line.strip())
886883
return output
887884

888885
def _get_push_info(
@@ -924,7 +921,7 @@ def stdout_handler(line: str) -> None:
924921
if not output:
925922
raise
926923
elif stderr_text:
927-
log.warning("Error lines received while fetching: %s", stderr_text)
924+
_logger.warning("Error lines received while fetching: %s", stderr_text)
928925
output.error = e
929926

930927
return output

git/repo/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989

9090
# -----------------------------------------------------------
9191

92-
log = logging.getLogger(__name__)
92+
_logger = logging.getLogger(__name__)
9393

9494
__all__ = ("Repo",)
9595

@@ -772,7 +772,7 @@ def is_valid_object(self, sha: str, object_type: Union[str, None] = None) -> boo
772772
if object_info.type == object_type.encode():
773773
return True
774774
else:
775-
log.debug(
775+
_logger.debug(
776776
"Commit hash points to an object of type '%s'. Requested were objects of type '%s'",
777777
object_info.type.decode(),
778778
object_type,
@@ -781,7 +781,7 @@ def is_valid_object(self, sha: str, object_type: Union[str, None] = None) -> boo
781781
else:
782782
return True
783783
except BadObject:
784-
log.debug("Commit hash is invalid.")
784+
_logger.debug("Commit hash is invalid.")
785785
return False
786786

787787
def _get_daemon_export(self) -> bool:
@@ -1298,7 +1298,7 @@ def _clone(
12981298
cmdline = getattr(proc, "args", "")
12991299
cmdline = remove_password_if_present(cmdline)
13001300

1301-
log.debug("Cmd(%s)'s unused stdout: %s", cmdline, stdout)
1301+
_logger.debug("Cmd(%s)'s unused stdout: %s", cmdline, stdout)
13021302
finalize_process(proc, stderr=stderr)
13031303

13041304
# Our git command could have a different working dir than our actual

git/util.py

Lines changed: 4 additions & 4 deletions

0 commit comments

Comments
 (0)