Skip to content

Commit f7968d1

Browse files
committed
Put remove password in the utils and use it also in cmd.execute
1 parent f7180d5 commit f7968d1

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

‎git/cmd.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
is_win,
2929
)
3030
from git.exc import CommandError
31-
from git.util import is_cygwin_git, cygpath, expand_path
31+
from git.util import is_cygwin_git, cygpath, expand_path, remove_password_if_present
3232

3333
from .exc import (
3434
GitCommandError,
@@ -682,8 +682,10 @@ def execute(self, command,
682682
:note:
683683
If you add additional keyword arguments to the signature of this method,
684684
you must update the execute_kwargs tuple housed in this module."""
685+
# Remove password for the command if present
686+
redacted_command = remove_password_if_present(command)
685687
if self.GIT_PYTHON_TRACE and (self.GIT_PYTHON_TRACE != 'full' or as_process):
686-
log.info(' '.join(command))
688+
log.info(' '.join(redacted_command))
687689

688690
# Allow the user to have the command executed in their working dir.
689691
cwd = self._working_dir or os.getcwd()

‎git/repo/base.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import os
1010
import re
1111
import warnings
12-
from urllib.parse import urlsplit, urlunsplit
1312

1413
from git.cmd import (
1514
Git,
@@ -27,7 +26,7 @@
2726
from git.objects import Submodule, RootModule, Commit
2827
from git.refs import HEAD, Head, Reference, TagReference
2928
from git.remote import Remote, add_progress, to_progress_instance
30-
from git.util import Actor, finalize_process, decygpath, hex_to_bin, expand_path
29+
from git.util import Actor, finalize_process, decygpath, hex_to_bin, expand_path, remove_password_if_present
3130
import os.path as osp
3231

3332
from .fun import rev_parse, is_git_dir, find_submodule_git_dir, touch, find_worktree_git_dir
@@ -971,16 +970,8 @@ def _clone(cls, git, url, path, odb_default_type, progress, multi_options=None,
971970
else:
972971
(stdout, stderr) = proc.communicate()
973972
cmdline = getattr(proc, 'args', '')
974-
uri = cmdline[-2]
975-
try:
976-
url = urlsplit(uri)
977-
# Remove password from the URL if present
978-
if url.password:
979-
edited_url = url._replace(
980-
netloc=url.netloc.replace(url.password, "****"))
981-
cmdline[-2] = urlunsplit(edited_url)
982-
except ValueError:
983-
log.debug("Unable to parse the URL %s", url)
973+
cmdline = remove_password_if_present(cmdline)
974+
984975
log.debug("Cmd(%s)'s unused stdout: %s", cmdline, stdout)
985976
finalize_process(proc, stderr=stderr)
986977

‎git/util.py

+28
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from sys import maxsize
1717
import time
1818
from unittest import SkipTest
19+
from urllib.parse import urlsplit, urlunsplit
1920

2021
from gitdb.util import (# NOQA @IgnorePep8
2122
make_sha,
@@ -338,6 +339,33 @@ def expand_path(p, expand_vars=True):
338339
except Exception:
339340
return None
340341

342+
343+
def remove_password_if_present(cmdline):
344+
"""
345+
Parse any command line argument and if on of the element is an URL with a
346+
password, replace it by stars. If nothing found just returns a copy of the
347+
command line as-is.
348+
349+
This should be used for every log line that print a command line.
350+
"""
351+
redacted_cmdline = []
352+
for to_parse in cmdline:
353+
try:
354+
url = urlsplit(to_parse)
355+
# Remove password from the URL if present
356+
if url.password is None:
357+
raise ValueError()
358+
359+
edited_url = url._replace(
360+
netloc=url.netloc.replace(url.password, "*****"))
361+
redacted_cmdline.append(urlunsplit(edited_url))
362+
except ValueError:
363+
redacted_cmdline.append(to_parse)
364+
# This is not a valid URL
365+
pass
366+
return redacted_cmdline
367+
368+
341369
#} END utilities
342370

343371
#{ Classes

0 commit comments

Comments
 (0)