Commit 58fe662d authored by maruel@chromium.org's avatar maruel@chromium.org

Add support for empty files, __init__.py is a common example.

Use p.is_new signal and set it manually with rietveld patches.
Add regression test for +x.

R=dpranke@chromium.org
BUG=
TEST=

Review URL: http://codereview.chromium.org/7054057

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@87858 0039d316-1c4b-4281-b951-d872f2087c98
parent 86eb9e75
...@@ -22,7 +22,8 @@ import scm ...@@ -22,7 +22,8 @@ import scm
def main(): def main():
parser = optparse.OptionParser(description=sys.modules[__name__].__doc__) parser = optparse.OptionParser(description=sys.modules[__name__].__doc__)
parser.add_option( parser.add_option(
'-v', '--verbose', action='count', help='Prints debugging infos') '-v', '--verbose', action='count', default=0,
help='Prints debugging infos')
parser.add_option( parser.add_option(
'-i', '--issue', type='int', help='Rietveld issue number') '-i', '--issue', type='int', help='Rietveld issue number')
parser.add_option( parser.add_option(
...@@ -38,13 +39,10 @@ def main(): ...@@ -38,13 +39,10 @@ def main():
default='http://codereview.chromium.org', default='http://codereview.chromium.org',
help='Rietveld server') help='Rietveld server')
options, args = parser.parse_args() options, args = parser.parse_args()
LOG_FORMAT = '%(levelname)s %(filename)s(%(lineno)d): %(message)s' logging.basicConfig(
if not options.verbose: format='%(levelname)s %(filename)s(%(lineno)d): %(message)s',
logging.basicConfig(level=logging.WARNING, format=LOG_FORMAT) level=[logging.WARNING, logging.INFO, logging.DEBUG][
elif options.verbose == 1: min(2, options.verbose)])
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
elif options.verbose > 1:
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
if args: if args:
parser.error('Extra argument(s) "%s" not understood' % ' '.join(args)) parser.error('Extra argument(s) "%s" not understood' % ' '.join(args))
if not options.issue: if not options.issue:
......
...@@ -127,10 +127,14 @@ class RawCheckout(CheckoutBase): ...@@ -127,10 +127,14 @@ class RawCheckout(CheckoutBase):
with open(os.path.join(filename), 'wb') as f: with open(os.path.join(filename), 'wb') as f:
f.write(p.get()) f.write(p.get())
else: else:
if p.diff_hunks:
stdout = subprocess2.check_output( stdout = subprocess2.check_output(
['patch', '-p%s' % p.patchlevel], ['patch', '-p%s' % p.patchlevel],
stdin=p.get(), stdin=p.get(),
cwd=self.project_path) cwd=self.project_path)
elif p.is_new:
# There is only a header. Just create the file.
open(os.path.join(self.project_path, p.filename), 'w').close()
for post in post_processor: for post in post_processor:
post(self, p) post(self, p)
except OSError, e: except OSError, e:
...@@ -258,8 +262,6 @@ class SvnCheckout(CheckoutBase, SvnMixIn): ...@@ -258,8 +262,6 @@ class SvnCheckout(CheckoutBase, SvnMixIn):
stdout += self._check_output_svn( stdout += self._check_output_svn(
['delete', p.filename, '--force'], credentials=False) ['delete', p.filename, '--force'], credentials=False)
else: else:
new = not os.path.exists(p.filename)
# svn add while creating directories otherwise svn add on the # svn add while creating directories otherwise svn add on the
# contained files will silently fail. # contained files will silently fail.
# First, find the root directory that exists. # First, find the root directory that exists.
...@@ -278,10 +280,14 @@ class SvnCheckout(CheckoutBase, SvnMixIn): ...@@ -278,10 +280,14 @@ class SvnCheckout(CheckoutBase, SvnMixIn):
with open(os.path.join(self.project_path, p.filename), 'wb') as f: with open(os.path.join(self.project_path, p.filename), 'wb') as f:
f.write(p.get()) f.write(p.get())
else: else:
if p.diff_hunks:
cmd = ['patch', '-p%s' % p.patchlevel, '--forward', '--force'] cmd = ['patch', '-p%s' % p.patchlevel, '--forward', '--force']
stdout += subprocess2.check_output( stdout += subprocess2.check_output(
cmd, stdin=p.get(), cwd=self.project_path) cmd, stdin=p.get(), cwd=self.project_path)
if new: elif p.is_new:
# There is only a header. Just create the file.
open(os.path.join(self.project_path, p.filename), 'w').close()
if p.is_new:
stdout += self._check_output_svn( stdout += self._check_output_svn(
['add', p.filename, '--force'], credentials=False) ['add', p.filename, '--force'], credentials=False)
for prop in p.svn_properties: for prop in p.svn_properties:
...@@ -410,6 +416,8 @@ class GitCheckoutBase(CheckoutBase): ...@@ -410,6 +416,8 @@ class GitCheckoutBase(CheckoutBase):
f.write(p.get()) f.write(p.get())
stdout += self._check_output_git(['add', p.filename]) stdout += self._check_output_git(['add', p.filename])
else: else:
# No need to do anything special with p.is_new or if not
# p.diff_hunks. git apply manages all that already.
stdout += self._check_output_git( stdout += self._check_output_git(
['apply', '--index', '-p%s' % p.patchlevel], stdin=p.get()) ['apply', '--index', '-p%s' % p.patchlevel], stdin=p.get())
for prop in p.svn_properties: for prop in p.svn_properties:
......
...@@ -142,12 +142,17 @@ class Rietveld(object): ...@@ -142,12 +142,17 @@ class Rietveld(object):
props, props,
is_new=(status[0] == 'A'))) is_new=(status[0] == 'A')))
else: else:
if state['num_chunks']: # Ignores num_chunks since it may only contain an header.
try:
diff = self.get_file_diff(issue, patchset, state['id']) diff = self.get_file_diff(issue, patchset, state['id'])
else: except urllib2.HTTPError, e:
if e.code == 404:
raise patch.UnsupportedPatchFormat( raise patch.UnsupportedPatchFormat(
filename, 'File doesn\'t have a diff.') filename, 'File doesn\'t have a diff.')
out.append(patch.FilePatchDiff(filename, diff, props)) out.append(patch.FilePatchDiff(filename, diff, props))
if status[0] == 'A':
# It won't be set for empty file.
out[-1].is_new = True
else: else:
# Line too long (N/80) # Line too long (N/80)
# pylint: disable=C0301 # pylint: disable=C0301
......
...@@ -512,6 +512,15 @@ class PatchTest(unittest.TestCase): ...@@ -512,6 +512,15 @@ class PatchTest(unittest.TestCase):
self.assertEquals( self.assertEquals(
[('svn:executable', '*')], [('svn:executable', '*')],
patch.FilePatchDiff('natsort_test.py', diff, []).svn_properties) patch.FilePatchDiff('natsort_test.py', diff, []).svn_properties)
diff = (
'diff --git a/natsort_test.py b/natsort_test.py\n'
'new file mode 100644\n'
'--- /dev/null\n'
'+++ b/natsort_test.py\n'
'@@ -0,0 +1,1 @@\n'
'+#!/usr/bin/env python\n')
self.assertEquals(
[], patch.FilePatchDiff('natsort_test.py', diff, []).svn_properties)
if __name__ == '__main__': if __name__ == '__main__':
......
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