Skip to content

Commit

Permalink
Do not fail when a diff contains deleted or added files.
Browse files Browse the repository at this point in the history
Bump version to 1.3.1

Fixes #11
  • Loading branch information
Dan Ellis committed Jul 10, 2015
1 parent fdeb715 commit 8040b41
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ to make it work for more environments.
Changelog
---------

### 1.3.1 ###

`atk-git-diff` now does not fail when a diff contains deleted or added files.

### 1.3.0 ###

`atk-git-diff` added.
Expand Down
2 changes: 2 additions & 0 deletions ansible_toolkit/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class MalformedGitDiff(Exception):
pass
31 changes: 27 additions & 4 deletions ansible_toolkit/git_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from ansible.utils.vault import VaultLib

from exceptions import MalformedGitDiff
from utils import get_vault_password, green, red, cyan, intense


Expand All @@ -31,8 +32,19 @@ def get_old_filename(diff_part):
"""
Returns the filename for the original file that was changed in a diff part.
"""
r = re.compile(r'^--- a/(.*)', re.MULTILINE)
return r.search(diff_part).groups()[0]
regexps = (
# e.g. "+++ a/foo/bar"
r'^--- a/(.*)',
# e.g. "+++ /dev/null"
r'^\-\-\- (.*)',
)
for regexp in regexps:
r = re.compile(regexp, re.MULTILINE)
match = r.search(diff_part)
if match is not None:
return match.groups()[0]
raise MalformedGitDiff("No old filename in diff part found. "
"Examined diff part: {}".format(diff_part))


def get_old_contents(sha, filename):
Expand All @@ -43,8 +55,19 @@ def get_new_filename(diff_part):
"""
Returns the filename for the updated file in a diff part.
"""
r = re.compile(r'^\+\+\+ b/(.*)', re.MULTILINE)
return r.search(diff_part).groups()[0]
regexps = (
# e.g. "+++ b/foo/bar"
r'^\+\+\+ b/(.*)',
# e.g. "+++ /dev/null"
r'^\+\+\+ (.*)',
)
for regexp in regexps:
r = re.compile(regexp, re.MULTILINE)
match = r.search(diff_part)
if match is not None:
return match.groups()[0]
raise MalformedGitDiff("No new filename in diff part found. "
"Examined diff part: {}".format(diff_part))


def get_new_contents(filename):
Expand Down
40 changes: 40 additions & 0 deletions ansible_toolkit/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest

from ansible_toolkit import git_diff
from ansible_toolkit.exceptions import MalformedGitDiff


VAULT_PASSWORD = "foo"
Expand Down Expand Up @@ -60,6 +61,24 @@
+65393432336536653066303736336632356364306533643131656461316332353138316239336137
+3038396139303439356236343161396331353332326232626566"""

DELETED_FILE_DIFF = """
diff --git a/foo b/foo
deleted file mode 100644
index 257cc56..0000000
--- a/foo
+++ /dev/null
@@ -1 +0,0 @@
-foo"""

ADDED_FILE_DIFF = """
diff --git a/bar b/bar
new file mode 100644
index 0000000..5716ca5
--- /dev/null
+++ b/bar
@@ -0,0 +1 @@
+bar"""


class TestGitDiff(unittest.TestCase):

Expand All @@ -77,6 +96,27 @@ def test_get_old_filename(self):
old_filename = git_diff.get_old_filename(parts[0])
self.assertEqual(old_filename, 'group_vars/foo')

def test_get_old_filename_for_added_file(self):
parts = git_diff.get_parts(ADDED_FILE_DIFF)
old_filename = git_diff.get_old_filename(parts[0])
self.assertEqual(old_filename, '/dev/null')

def test_missing_old_filename_raises_exception(self):
self.assertRaises(MalformedGitDiff, git_diff.get_old_filename, '')

def test_get_new_filename(self):
parts = git_diff.get_parts(SAMPLE_DIFF)
new_filename = git_diff.get_new_filename(parts[0])
self.assertEqual(new_filename, 'group_vars/foo')

def test_get_new_filename_for_deleted_file(self):
parts = git_diff.get_parts(DELETED_FILE_DIFF)
new_filename = git_diff.get_new_filename(parts[0])
self.assertEqual(new_filename, '/dev/null')

def test_missing_new_filename_raises_exception(self):
self.assertRaises(MalformedGitDiff, git_diff.get_new_filename, '')

def test_decrypt_diff(self):

# Monkey-patch
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


setup(name='ansible-toolkit',
version='1.3.0',
version='1.3.1',
description='The missing Ansible tools',
url='http://github.com/dellis23/ansible-toolkit',
author='Daniel Ellis',
Expand Down

0 comments on commit 8040b41

Please sign in to comment.