##// END OF EJS Templates
vcs: sanitize diff context values (Issue #306)...
Branko Majic -
r7049:55d2b08d stable
parent child Browse files
Show More
@@ -587,8 +587,30 b' class GitRepository(BaseRepository):'
587 :param ignore_whitespace: If set to ``True``, would not show whitespace
587 :param ignore_whitespace: If set to ``True``, would not show whitespace
588 changes. Defaults to ``False``.
588 changes. Defaults to ``False``.
589 :param context: How many lines before/after changed lines should be
589 :param context: How many lines before/after changed lines should be
590 shown. Defaults to ``3``.
590 shown. Defaults to ``3``. Due to limitations in Git, if
591 value passed-in is greater than ``2**31-1``
592 (``2147483647``), it will be set to ``2147483647``
593 instead. If negative value is passed-in, it will be set to
594 ``0`` instead.
591 """
595 """
596
597 # Git internally uses a signed long int for storing context
598 # size (number of lines to show before and after the
599 # differences). This can result in integer overflow, so we
600 # ensure the requested context is smaller by one than the
601 # number that would cause the overflow. It is highly unlikely
602 # that a single file will contain that many lines, so this
603 # kind of change should not cause any realistic consequences.
604 overflowed_long_int = 2**31
605
606 if context >= overflowed_long_int:
607 context = overflowed_long_int-1
608
609 # Negative context values make no sense, and will result in
610 # errors. Ensure this does not happen.
611 if context < 0:
612 context = 0
613
592 flags = ['-U%s' % context, '--full-index', '--binary', '-p', '-M', '--abbrev=40']
614 flags = ['-U%s' % context, '--full-index', '--binary', '-p', '-M', '--abbrev=40']
593 if ignore_whitespace:
615 if ignore_whitespace:
594 flags.append('-w')
616 flags.append('-w')
@@ -244,8 +244,15 b' class MercurialRepository(BaseRepository'
244 :param ignore_whitespace: If set to ``True``, would not show whitespace
244 :param ignore_whitespace: If set to ``True``, would not show whitespace
245 changes. Defaults to ``False``.
245 changes. Defaults to ``False``.
246 :param context: How many lines before/after changed lines should be
246 :param context: How many lines before/after changed lines should be
247 shown. Defaults to ``3``.
247 shown. Defaults to ``3``. If negative value is passed-in, it will be
248 set to ``0`` instead.
248 """
249 """
250
251 # Negative context values make no sense, and will result in
252 # errors. Ensure this does not happen.
253 if context < 0:
254 context = 0
255
249 if hasattr(rev1, 'raw_id'):
256 if hasattr(rev1, 'raw_id'):
250 rev1 = getattr(rev1, 'raw_id')
257 rev1 = getattr(rev1, 'raw_id')
251
258
@@ -727,6 +727,46 b' class GitSpecificWithRepoTest(_BackendTe'
727 ['diff', '-U3', '--full-index', '--binary', '-p', '-M', '--abbrev=40',
727 ['diff', '-U3', '--full-index', '--binary', '-p', '-M', '--abbrev=40',
728 self.repo._get_revision(0), self.repo._get_revision(1), '--', 'foo'])
728 self.repo._get_revision(0), self.repo._get_revision(1), '--', 'foo'])
729
729
730 def test_get_diff_does_not_sanitize_valid_context(self):
731 almost_overflowed_long_int = 2**31-1
732
733 self.repo.run_git_command = mock.Mock(return_value=['', ''])
734 self.repo.get_diff(0, 1, 'foo', context=almost_overflowed_long_int)
735 self.repo.run_git_command.assert_called_once_with(
736 ['diff', '-U' + str(almost_overflowed_long_int), '--full-index', '--binary', '-p', '-M', '--abbrev=40',
737 self.repo._get_revision(0), self.repo._get_revision(1), '--', 'foo'])
738
739 def test_get_diff_sanitizes_overflowing_context(self):
740 overflowed_long_int = 2**31
741 sanitized_overflowed_long_int = overflowed_long_int-1
742
743 self.repo.run_git_command = mock.Mock(return_value=['', ''])
744 self.repo.get_diff(0, 1, 'foo', context=overflowed_long_int)
745
746 self.repo.run_git_command.assert_called_once_with(
747 ['diff', '-U' + str(sanitized_overflowed_long_int), '--full-index', '--binary', '-p', '-M', '--abbrev=40',
748 self.repo._get_revision(0), self.repo._get_revision(1), '--', 'foo'])
749
750 def test_get_diff_does_not_sanitize_zero_context(self):
751 zero_context = 0
752
753 self.repo.run_git_command = mock.Mock(return_value=['', ''])
754 self.repo.get_diff(0, 1, 'foo', context=zero_context)
755
756 self.repo.run_git_command.assert_called_once_with(
757 ['diff', '-U' + str(zero_context), '--full-index', '--binary', '-p', '-M', '--abbrev=40',
758 self.repo._get_revision(0), self.repo._get_revision(1), '--', 'foo'])
759
760 def test_get_diff_sanitizes_negative_context(self):
761 negative_context = -10
762
763 self.repo.run_git_command = mock.Mock(return_value=['', ''])
764 self.repo.get_diff(0, 1, 'foo', context=negative_context)
765
766 self.repo.run_git_command.assert_called_once_with(
767 ['diff', '-U0', '--full-index', '--binary', '-p', '-M', '--abbrev=40',
768 self.repo._get_revision(0), self.repo._get_revision(1), '--', 'foo'])
769
730
770
731 class GitRegressionTest(_BackendTestMixin, unittest.TestCase):
771 class GitRegressionTest(_BackendTestMixin, unittest.TestCase):
732 backend_alias = 'git'
772 backend_alias = 'git'
@@ -1,5 +1,8 b''
1
1
2 import os
2 import os
3
4 import mock
5
3 from kallithea.lib.vcs.backends.hg import MercurialRepository, MercurialChangeset
6 from kallithea.lib.vcs.backends.hg import MercurialRepository, MercurialChangeset
4 from kallithea.lib.vcs.exceptions import RepositoryError, VCSError, NodeDoesNotExistError
7 from kallithea.lib.vcs.exceptions import RepositoryError, VCSError, NodeDoesNotExistError
5 from kallithea.lib.vcs.nodes import NodeKind, NodeState
8 from kallithea.lib.vcs.nodes import NodeKind, NodeState
@@ -231,6 +234,23 b' TODO: To be written...'
231 self.assertEqual(node.kind, NodeKind.FILE)
234 self.assertEqual(node.kind, NodeKind.FILE)
232 self.assertEqual(node.content, README)
235 self.assertEqual(node.content, README)
233
236
237 @mock.patch('kallithea.lib.vcs.backends.hg.repository.diffopts')
238 def test_get_diff_does_not_sanitize_zero_context(self, mock_diffopts):
239 zero_context = 0
240
241 self.repo.get_diff(0, 1, 'foo', context=zero_context)
242
243 mock_diffopts.assert_called_once_with(git=True, showfunc=True, ignorews=False, context=zero_context)
244
245 @mock.patch('kallithea.lib.vcs.backends.hg.repository.diffopts')
246 def test_get_diff_sanitizes_negative_context(self, mock_diffopts):
247 negative_context = -10
248 zero_context = 0
249
250 self.repo.get_diff(0, 1, 'foo', context=negative_context)
251
252 mock_diffopts.assert_called_once_with(git=True, showfunc=True, ignorews=False, context=zero_context)
253
234
254
235 class MercurialChangesetTest(unittest.TestCase):
255 class MercurialChangesetTest(unittest.TestCase):
236
256
General Comments 0
You need to be logged in to leave comments. Login now