1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-04 04:16:12 +02:00

git-p4: don't select shell mode using the type of the command argument

Previously, the script would invoke subprocess functions setting the
shell argument True if the command argument was a string, setting it
False otherwise.

This patch replaces this implicit type-driven behaviour with explicit
shell arguments specified by the caller.

The apparent motive for the implict behaviour is that the subprocess
functions do not divide command strings into args. Invoking
subprocess.call("echo hello") will attempt to execute a program by the
name "echo hello". With subprocess.call("echo hello", shell=True), sh
-c "echo hello" will be executed instead, which will cause the command
and args to be divided by spaces.

Eventually, all usage of shell=True, that is not necessary for some
purpose beyond parsing command strings, should be removed. For now,
this patch makes the usage of shells explicit.

Signed-off-by: Joel Holdsworth <jholdsworth@nvidia.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Joel Holdsworth 2022-01-06 21:40:33 +00:00 committed by Junio C Hamano
parent e83ba647f7
commit 3d8a3038bc

126
git-p4.py
View File

@ -276,12 +276,11 @@ def run_hook_command(cmd, param):
return subprocess.call(cli, shell=use_shell)
def write_pipe(c, stdin):
def write_pipe(c, stdin, *k, **kw):
if verbose:
sys.stderr.write('Writing pipe: %s\n' % str(c))
expand = not isinstance(c, list)
p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand)
p = subprocess.Popen(c, stdin=subprocess.PIPE, *k, **kw)
pipe = p.stdin
val = pipe.write(stdin)
pipe.close()
@ -290,13 +289,13 @@ def write_pipe(c, stdin):
return val
def p4_write_pipe(c, stdin):
def p4_write_pipe(c, stdin, *k, **kw):
real_cmd = p4_build_cmd(c)
if bytes is not str and isinstance(stdin, str):
stdin = encode_text_stream(stdin)
return write_pipe(real_cmd, stdin)
return write_pipe(real_cmd, stdin, *k, **kw)
def read_pipe_full(c):
def read_pipe_full(c, *k, **kw):
""" Read output from command. Returns a tuple
of the return status, stdout text and stderr
text.
@ -304,19 +303,19 @@ def read_pipe_full(c):
if verbose:
sys.stderr.write('Reading pipe: %s\n' % str(c))
expand = not isinstance(c, list)
p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=expand)
p = subprocess.Popen(
c, stdout=subprocess.PIPE, stderr=subprocess.PIPE, *k, **kw)
(out, err) = p.communicate()
return (p.returncode, out, decode_text_stream(err))
def read_pipe(c, ignore_error=False, raw=False):
def read_pipe(c, ignore_error=False, raw=False, *k, **kw):
""" Read output from command. Returns the output text on
success. On failure, terminates execution, unless
ignore_error is True, when it returns an empty string.
If raw is True, do not attempt to decode output text.
"""
(retcode, out, err) = read_pipe_full(c)
(retcode, out, err) = read_pipe_full(c, *k, **kw)
if retcode != 0:
if ignore_error:
out = ""
@ -326,26 +325,25 @@ def read_pipe(c, ignore_error=False, raw=False):
out = decode_text_stream(out)
return out
def read_pipe_text(c):
def read_pipe_text(c, *k, **kw):
""" Read output from a command with trailing whitespace stripped.
On error, returns None.
"""
(retcode, out, err) = read_pipe_full(c)
(retcode, out, err) = read_pipe_full(c, *k, **kw)
if retcode != 0:
return None
else:
return decode_text_stream(out).rstrip()
def p4_read_pipe(c, ignore_error=False, raw=False):
def p4_read_pipe(c, ignore_error=False, raw=False, *k, **kw):
real_cmd = p4_build_cmd(c)
return read_pipe(real_cmd, ignore_error, raw=raw)
return read_pipe(real_cmd, ignore_error, raw=raw, *k, **kw)
def read_pipe_lines(c, raw=False):
def read_pipe_lines(c, raw=False, *k, **kw):
if verbose:
sys.stderr.write('Reading pipe: %s\n' % str(c))
expand = not isinstance(c, list)
p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
p = subprocess.Popen(c, stdout=subprocess.PIPE, *k, **kw)
pipe = p.stdout
lines = pipe.readlines()
if not raw:
@ -354,10 +352,10 @@ def read_pipe_lines(c, raw=False):
die('Command failed: %s' % str(c))
return lines
def p4_read_pipe_lines(c):
def p4_read_pipe_lines(c, *k, **kw):
"""Specifically invoke p4 on the command supplied. """
real_cmd = p4_build_cmd(c)
return read_pipe_lines(real_cmd)
return read_pipe_lines(real_cmd, *k, **kw)
def p4_has_command(cmd):
"""Ask p4 for help on this command. If it returns an error, the
@ -388,21 +386,19 @@ def p4_has_move_command():
# assume it failed because @... was invalid changelist
return True
def system(cmd, ignore_error=False):
expand = not isinstance(cmd, list)
def system(cmd, ignore_error=False, *k, **kw):
if verbose:
sys.stderr.write("executing %s\n" % str(cmd))
retcode = subprocess.call(cmd, shell=expand)
retcode = subprocess.call(cmd, *k, **kw)
if retcode and not ignore_error:
raise CalledProcessError(retcode, cmd)
return retcode
def p4_system(cmd):
def p4_system(cmd, *k, **kw):
"""Specifically invoke p4 as the system command. """
real_cmd = p4_build_cmd(cmd)
expand = not isinstance(real_cmd, list)
retcode = subprocess.call(real_cmd, shell=expand)
retcode = subprocess.call(real_cmd, *k, **kw)
if retcode:
raise CalledProcessError(retcode, real_cmd)
@ -723,14 +719,12 @@ def isModeExecChanged(src_mode, dst_mode):
return isModeExec(src_mode) != isModeExec(dst_mode)
def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
errors_as_exceptions=False):
errors_as_exceptions=False, *k, **kw):
if not isinstance(cmd, list):
cmd = "-G " + cmd
expand = True
else:
cmd = ["-G"] + cmd
expand = False
cmd = p4_build_cmd(cmd)
if verbose:
@ -751,10 +745,8 @@ def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
stdin_file.flush()
stdin_file.seek(0)
p4 = subprocess.Popen(cmd,
shell=expand,
stdin=stdin_file,
stdout=subprocess.PIPE)
p4 = subprocess.Popen(
cmd, stdin=stdin_file, stdout=subprocess.PIPE, *k, **kw)
result = []
try:
@ -807,8 +799,8 @@ def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
return result
def p4Cmd(cmd):
list = p4CmdList(cmd)
def p4Cmd(cmd, *k, **kw):
list = p4CmdList(cmd, *k, **kw)
result = {}
for entry in list:
result.update(entry)
@ -857,7 +849,7 @@ def isValidGitDir(path):
return git_dir(path) != None
def parseRevision(ref):
return read_pipe("git rev-parse %s" % ref).strip()
return read_pipe("git rev-parse %s" % ref, shell=True).strip()
def branchExists(ref):
rev = read_pipe(["git", "rev-parse", "-q", "--verify", ref],
@ -969,7 +961,7 @@ def p4BranchesInGit(branchesAreInRemotes=True):
else:
cmdline += "--branches"
for line in read_pipe_lines(cmdline):
for line in read_pipe_lines(cmdline, shell=True):
line = line.strip()
# only import to p4/
@ -1032,7 +1024,7 @@ def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent
originPrefix = "origin/p4/"
for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
for line in read_pipe_lines("git rev-parse --symbolic --remotes", shell=True):
line = line.strip()
if (not line.startswith(originPrefix)) or line.endswith("HEAD"):
continue
@ -1070,7 +1062,8 @@ def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent
remoteHead, ','.join(settings['depot-paths'])))
if update:
system("git update-ref %s %s" % (remoteHead, originHead))
system("git update-ref %s %s" % (remoteHead, originHead),
shell=True)
def originP4BranchesExist():
return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
@ -1184,7 +1177,7 @@ def getClientSpec():
"""Look at the p4 client spec, create a View() object that contains
all the mappings, and return it."""
specList = p4CmdList("client -o")
specList = p4CmdList("client -o", shell=True)
if len(specList) != 1:
die('Output from "client -o" is %d lines, expecting 1' %
len(specList))
@ -1213,7 +1206,7 @@ def getClientSpec():
def getClientRoot():
"""Grab the client directory."""
output = p4CmdList("client -o")
output = p4CmdList("client -o", shell=True)
if len(output) != 1:
die('Output from "client -o" is %d lines, expecting 1' % len(output))
@ -1468,7 +1461,7 @@ def p4UserId(self):
if self.myP4UserId:
return self.myP4UserId
results = p4CmdList("user -o")
results = p4CmdList("user -o", shell=True)
for r in results:
if 'User' in r:
self.myP4UserId = r['User']
@ -1493,7 +1486,7 @@ def getUserMapFromPerforceServer(self):
self.users = {}
self.emails = {}
for output in p4CmdList("users"):
for output in p4CmdList("users", shell=True):
if "User" not in output:
continue
self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
@ -1691,7 +1684,7 @@ def __init__(self):
die("Large file system not supported for git-p4 submit command. Please remove it from config.")
def check(self):
if len(p4CmdList("opened ...")) > 0:
if len(p4CmdList("opened ...", shell=True)) > 0:
die("You have files opened with perforce! Close them before starting the sync.")
def separate_jobs_from_description(self, message):
@ -1795,7 +1788,7 @@ def lastP4Changelist(self):
# then gets used to patch up the username in the change. If the same
# client spec is being used by multiple processes then this might go
# wrong.
results = p4CmdList("client -o") # find the current client
results = p4CmdList("client -o", shell=True) # find the current client
client = None
for r in results:
if 'Client' in r:
@ -1811,7 +1804,7 @@ def lastP4Changelist(self):
def modifyChangelistUser(self, changelist, newUser):
# fixup the user field of a changelist after it has been submitted.
changes = p4CmdList("change -o %s" % changelist)
changes = p4CmdList("change -o %s" % changelist, shell=True)
if len(changes) != 1:
die("Bad output from p4 change modifying %s to user %s" %
(changelist, newUser))
@ -1822,7 +1815,7 @@ def modifyChangelistUser(self, changelist, newUser):
# p4 does not understand format version 3 and above
input = marshal.dumps(c, 2)
result = p4CmdList("change -f -i", stdin=input)
result = p4CmdList("change -f -i", stdin=input, shell=True)
for r in result:
if 'code' in r:
if r['code'] == 'error':
@ -1928,7 +1921,7 @@ def edit_template(self, template_file):
if "P4EDITOR" in os.environ and (os.environ.get("P4EDITOR") != ""):
editor = os.environ.get("P4EDITOR")
else:
editor = read_pipe("git var GIT_EDITOR").strip()
editor = read_pipe("git var GIT_EDITOR", shell=True).strip()
system(["sh", "-c", ('%s "$@"' % editor), editor, template_file])
# If the file was not saved, prompt to see if this patch should
@ -1986,7 +1979,9 @@ def applyCommit(self, id):
(p4User, gitEmail) = self.p4UserForCommit(id)
diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (self.diffOpts, id, id))
diff = read_pipe_lines(
"git diff-tree -r %s \"%s^\" \"%s\"" % (self.diffOpts, id, id),
shell=True)
filesToAdd = set()
filesToChangeType = set()
filesToDelete = set()
@ -2122,7 +2117,7 @@ def applyCommit(self, id):
#
# Apply the patch for real, and do add/delete/+x handling.
#
system(applyPatchCmd)
system(applyPatchCmd, shell=True)
for f in filesToChangeType:
p4_edit(f, "-t", "auto")
@ -3446,7 +3441,7 @@ def getBranchMapping(self):
else:
command = "branches"
for info in p4CmdList(command):
for info in p4CmdList(command, shell=True):
details = p4Cmd(["branch", "-o", info["branch"]])
viewIdx = 0
while "View%s" % viewIdx in details:
@ -3537,7 +3532,9 @@ def gitCommitByP4Change(self, ref, change):
while True:
if self.verbose:
print("trying: earliest %s latest %s" % (earliestCommit, latestCommit))
next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip()
next = read_pipe(
"git rev-list --bisect %s %s" % (latestCommit, earliestCommit),
shell=True).strip()
if len(next) == 0:
if self.verbose:
print("argh")
@ -3692,7 +3689,7 @@ def sync_origin_only(self):
if self.hasOrigin:
if not self.silent:
print('Syncing with origin first, using "git fetch origin"')
system("git fetch origin")
system("git fetch origin", shell=True)
def importHeadRevision(self, revision):
print("Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch))
@ -3859,8 +3856,8 @@ def run(self, args):
if len(self.branch) == 0:
self.branch = self.refPrefix + "master"
if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
system("git update-ref %s refs/heads/p4" % self.branch)
system("git branch -D p4")
system("git update-ref %s refs/heads/p4" % self.branch, shell=True)
system("git branch -D p4", shell=True)
# accept either the command-line option, or the configuration variable
if self.useClientSpec:
@ -4063,7 +4060,7 @@ def run(self, args):
# Cleanup temporary branches created during import
if self.tempBranches != []:
for branch in self.tempBranches:
read_pipe("git update-ref -d %s" % branch)
read_pipe("git update-ref -d %s" % branch, shell=True)
os.rmdir(os.path.join(os.environ.get("GIT_DIR", ".git"), self.tempBranchLocation))
# Create a symbolic ref p4/HEAD pointing to p4/<branch> to allow
@ -4095,7 +4092,7 @@ def run(self, args):
def rebase(self):
if os.system("git update-index --refresh") != 0:
die("Some files in your working directory are modified and different than what is in your index. You can use git update-index <filename> to bring the index up to date or stash away all your changes with git stash.");
if len(read_pipe("git diff-index HEAD --")) > 0:
if len(read_pipe("git diff-index HEAD --", shell=True)) > 0:
die("You have uncommitted changes. Please commit them before rebasing or stash them away with git stash.");
[upstream, settings] = findUpstreamBranchPoint()
@ -4106,9 +4103,10 @@ def rebase(self):
upstream = re.sub("~[0-9]+$", "", upstream)
print("Rebasing the current branch onto %s" % upstream)
oldHead = read_pipe("git rev-parse HEAD").strip()
system("git rebase %s" % upstream)
system("git diff-tree --stat --summary -M %s HEAD --" % oldHead)
oldHead = read_pipe("git rev-parse HEAD", shell=True).strip()
system("git rebase %s" % upstream, shell=True)
system("git diff-tree --stat --summary -M %s HEAD --" % oldHead,
shell=True)
return True
class P4Clone(P4Sync):
@ -4185,7 +4183,7 @@ def run(self, args):
# auto-set this variable if invoked with --use-client-spec
if self.useClientSpec_from_options:
system("git config --bool git-p4.useclientspec true")
system("git config --bool git-p4.useclientspec true", shell=True)
return True
@ -4319,7 +4317,7 @@ def run(self, args):
cmdline = "git rev-parse --symbolic "
cmdline += " --remotes"
for line in read_pipe_lines(cmdline):
for line in read_pipe_lines(cmdline, shell=True):
line = line.strip()
if not line.startswith('p4/') or line == "p4/HEAD":
@ -4404,9 +4402,11 @@ def main():
cmd.gitdir = os.path.abspath(".git")
if not isValidGitDir(cmd.gitdir):
# "rev-parse --git-dir" without arguments will try $PWD/.git
cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
cmd.gitdir = read_pipe(
"git rev-parse --git-dir", shell=True).strip()
if os.path.exists(cmd.gitdir):
cdup = read_pipe("git rev-parse --show-cdup").strip()
cdup = read_pipe(
"git rev-parse --show-cdup", shell=True).strip()
if len(cdup) > 0:
chdir(cdup);