Commit 351c61da authored by Andrii Shyshkalov's avatar Andrii Shyshkalov Committed by Commit Bot

Fix git cl on windows for git-numberer repos.

Git cl decides if git-numberer is enabled on a repository by writing
Gerrit's project.config from refs/meta/config into a tempfile, which is
then queried using `git config -f tempfile --get ...`.  The file itself
is only flushed, but not closed after writing because Python's
tempfile.NamedTemporaryFile is deleted on closing. This worked fine on
Linix/Mac, but not on Windows, where `git config` apparently doesn't see
file or its contents.

This CL rewrites the above using yet another contexmanager temp
directory into which a file is written and closed before git config is
ran.

R=machenbach@chromium.org,grt@chromium.org
BUG=683202

Change-Id: I7974d66b1b2b0478ab4b6f7ac04e547a4981c46c
Reviewed-on: https://chromium-review.googlesource.com/430719
Commit-Queue: Andrii Shyshkalov <tandrii@chromium.org>
Reviewed-by: 's avatarVadim Shtayura <vadimsh@chromium.org>
parent 857bc443
......@@ -5,6 +5,7 @@
"""Generic utils."""
import codecs
import contextlib
import cStringIO
import datetime
import logging
......@@ -143,6 +144,16 @@ def FileWrite(filename, content, mode='w'):
f.write(content)
@contextlib.contextmanager
def temporary_directory(**kwargs):
tdir = tempfile.mkdtemp(**kwargs)
try:
yield tdir
finally:
if tdir:
rmtree(tdir)
def safe_rename(old, new):
"""Renames a file reliably.
......
......@@ -23,7 +23,6 @@ import os
import re
import stat
import sys
import tempfile
import textwrap
import traceback
import urllib
......@@ -973,20 +972,19 @@ class _GitNumbererState(object):
return False
# Gerrit's project.config is really a git config file.
# So, parse it as such.
with tempfile.NamedTemporaryFile(prefix='git_cl_proj_config') as f:
f.write(project_config_data)
# Make sure OS sees this, but don't close the file just yet,
# as NamedTemporaryFile deletes it on closing.
f.flush()
with gclient_utils.temporary_directory() as tempdir:
project_config_file = os.path.join(tempdir, 'project.config')
gclient_utils.FileWrite(project_config_file, project_config_data)
def get_opts(x):
code, out = cls._run_git_with_code(
['config', '-f', f.name, '--get-all',
['config', '-f', project_config_file, '--get-all',
'plugin.git-numberer.validate-%s-refglob' % x])
if code == 0:
return out.strip().splitlines()
return []
enabled, disabled = map(get_opts, ['enabled', 'disabled'])
logging.info('validator config enabled %s disabled %s refglobs for '
'(this ref: %s)', enabled, disabled, ref)
......
......@@ -1102,13 +1102,15 @@ class TestGitCl(TestCase):
self.assertEqual(res.should_add_git_number, False)
def test_GitNumbererState_valid_configs(self):
class NamedTempFileStab(StringIO.StringIO):
@classmethod
with git_cl.gclient_utils.temporary_directory() as tempdir:
@contextlib.contextmanager
def create(cls, *_, **__):
yield cls()
name = 'tempfile'
self.mock(git_cl.tempfile, 'NamedTemporaryFile', NamedTempFileStab.create)
def fake_temporary_directory(**kwargs):
yield tempdir
self.mock(git_cl.gclient_utils, 'temporary_directory',
fake_temporary_directory)
self._test_GitNumbererState_valid_configs_inner(tempdir)
def _test_GitNumbererState_valid_configs_inner(self, tempdir):
self.calls = [
((['git', 'fetch', 'https://chromium.googlesource.com/chromium/src',
'+refs/meta/config:refs/git_cl/meta/config',
......@@ -1130,12 +1132,12 @@ class TestGitCl(TestCase):
validate-disabled-refglob = refs/heads/disabled
validate-disabled-refglob = refs/branch-heads/*
'''),
((['git', 'config', '-f', 'tempfile', '--get-all',
'plugin.git-numberer.validate-enabled-refglob'],),
((['git', 'config', '-f', os.path.join(tempdir, 'project.config') ,
'--get-all', 'plugin.git-numberer.validate-enabled-refglob'],),
'refs/else/*\n'
'refs/heads/*\n'),
((['git', 'config', '-f', 'tempfile', '--get-all',
'plugin.git-numberer.validate-disabled-refglob'],),
((['git', 'config', '-f', os.path.join(tempdir, 'project.config') ,
'--get-all', 'plugin.git-numberer.validate-disabled-refglob'],),
'refs/heads/disabled\n'
'refs/branch-heads/*\n'),
] * 4 # 4 tests below have exactly same IO.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment