From ca2b8e764e406261e47bcd809577f60bda5968f0 Mon Sep 17 00:00:00 2001 From: "maruel@chromium.org" Date: Thu, 24 May 2012 20:13:24 +0000 Subject: [PATCH] Automatically adds quotes on Windows when necessary So that a command like "git config rietveld.viewvc-url http://src.chromium.org/viewvc/chrome?view=rev&revision=" works fine. Fix the description automatically generated for the hooks to be using the same code than for the one presented to the user. R=cmp@chromium.org BUG= TEST=Tested manually on cygwin, win32, linux Review URL: https://chromiumcodereview.appspot.com/10447021 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@138874 0039d316-1c4b-4281-b951-d872f2087c98 --- git_cl.py | 19 +++++++++++++++---- tests/git_cl_test.py | 8 ++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/git_cl.py b/git_cl.py index 2c63d7330..7fa0c2d31 100755 --- a/git_cl.py +++ b/git_cl.py @@ -51,9 +51,21 @@ def DieWithError(message): sys.exit(1) +def QuoteCommand(command): + """Quotes command on Windows so it runs fine even with & and | in the string. + """ + if sys.platform == 'win32': + def fix(arg): + if ('&' in arg or '|' in arg) and '"' not in arg: + arg = '"%s"' % arg + return arg + command = [fix(arg) for arg in command] + return command + + def RunCommand(args, error_ok=False, error_message=None, **kwargs): try: - return subprocess2.check_output(args, shell=False, **kwargs) + return subprocess2.check_output(QuoteCommand(args), **kwargs) except subprocess2.CalledProcessError, e: if not error_ok: DieWithError( @@ -533,8 +545,7 @@ or verify this branch is set up to track another (via the --track argument to # If the change was never uploaded, use the log messages of all commits # up to the branch point, as git cl upload will prefill the description # with these log messages. - description = RunCommand(['git', 'log', '--pretty=format:%s%n%n%b', - '%s...' % (upstream_branch)]).strip() + description = CreateDescriptionFromLog([upstream_branch + '..']) if not author: author = RunGit(['config', 'user.email']).strip() or None @@ -879,7 +890,7 @@ def CreateDescriptionFromLog(args): log_args = [args[0] + '..' + args[1]] else: log_args = args[:] # Hope for the best! - return RunGit(['log', '--pretty=format:%s\n\n%b'] + log_args) + return RunGit(['log', '--pretty=format:%s%n%n%b'] + log_args) def ConvertToInteger(inputval): diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py index 29e5cc5f5..4e5bd5de7 100755 --- a/tests/git_cl_test.py +++ b/tests/git_cl_test.py @@ -112,11 +112,11 @@ class TestGitCl(TestCase): 'M\t.gitignore\n'), ((['git', 'config', 'branch.master.rietveldissue'],), ''), ((['git', 'config', 'branch.master.rietveldpatchset'],), ''), - ((['git', 'log', '--pretty=format:%s%n%n%b', 'master...'],), 'foo'), + ((['git', 'log', '--pretty=format:%s%n%n%b', 'master..'],), 'foo'), ((['git', 'config', 'user.email'],), 'me@example.com'), ((['git', 'diff', '--no-ext-diff', '--stat', '-M', 'master...'],), '+dat'), - ((['git', 'log', '--pretty=format:%s\n\n%b', 'master..'],), 'desc\n'), + ((['git', 'log', '--pretty=format:%s%n%n%b', 'master..'],), 'desc\n'), ] @staticmethod @@ -345,7 +345,7 @@ class TestGitCl(TestCase): 'M\t.gitignore\n'), ((['git', 'config', 'branch.master.rietveldissue'],), ''), ((['git', 'config', 'branch.master.rietveldpatchset'],), ''), - ((['git', 'log', '--pretty=format:%s%n%n%b', 'master...'],), 'foo'), + ((['git', 'log', '--pretty=format:%s%n%n%b', 'master..'],), 'foo'), ((['git', 'config', 'user.email'],), 'me@example.com'), ((['git', 'diff', '--no-ext-diff', '--stat', '-M', 'master...'],), '+dat'), @@ -354,7 +354,7 @@ class TestGitCl(TestCase): @staticmethod def _gerrit_upload_calls(description, reviewers): calls = [ - ((['git', 'log', '--pretty=format:%s\n\n%b', 'master..'],), + ((['git', 'log', '--pretty=format:%s%n%n%b', 'master..'],), description), ((['git', 'config', 'rietveld.cc'],), '') ]