Skip to content

Instantly share code, notes, and snippets.

@noisy
Last active September 15, 2017 04:47
Show Gist options
  • Save noisy/1f30761450310c698fcacc23ea8a6f9d to your computer and use it in GitHub Desktop.
Save noisy/1f30761450310c698fcacc23ea8a6f9d to your computer and use it in GitHub Desktop.
import urllib2
import re
test_diff = """
diff --git a/aaaa.asc b/aaaa.asc
index e6ec784..81c9e30 100644
--- a/aaaa.asc
+++ b/aaaa.asc
-A aaa. B bbb. C ccc.
+A aaa.
+B bbb.
+C ccc.
diff --git a/README.asc b/README.asc
index e6ec784..81c9e30 100644
--- a/README.asc
+++ b/README.asc
@@ -6,19 +6,24 @@ You can find this book online at: http://git-scm.com/book
You can find the current builds on http://git-scm.com/book[] and more information about the builds available at https://progit.org[].
-The other way to generate e-book files is to do so manually with Asciidoctor. If you run the following you _may_ actually get HTML, Epub, Mobi and PDF output files:
+The other way to generate e-book files is to do so manually with Asciidoctor.
+If you run the following you _may_ actually get HTML, Epub, Mobi and PDF output files:
diff --git a/11111.asc b/11111.asc
index e6ec784..81c9e30 100644
--- a/11111.asc
+++ b/11111.asc
-1 111. 2 22 2. 3 3 333 33 3 3.
+1 111.
+2 22 2.
+3 3 333 33 3 3.
"""
def remove_minus_sign_from_removed_line(removed):
"""
'-A aaa. B bbb. C ccc.\n' --> 'A aaa. B bbb. C ccc.\n'
There is always only one removed line per chunk.
"""
assert removed[0] == '-'
return removed[1:]
def remove_plus_sign_from_each_line(added):
"""
'+A aaa.\n+B bbb.\n+C ccc.\n' --> 'A aaa.\nB bbb.\nC ccc.\n'
"""
splitted = []
for line in added.split('\n'):
assert line == '' or line[0] == '+'
splitted.append(line[1:])
return "\n".join(splitted)
def concat_splited_lines(lines):
"""
'A aaa.\nB bbb.\nC ccc.\n' --> 'A aaa. B bbb. C ccc.\n'
"""
return lines.replace('\n', ' ')[:-1] + '\n'
def assert_diff_only_change_newlines(diff):
pattern = r'^(-[^--].*\n)((?:\+[^\+\+].*\n)*)'
for match in re.findall(pattern, diff, flags=re.M):
# print match[0]
# print match[1]
removed = match[0]
added = match[1]
removed = remove_minus_sign_from_removed_line(removed)
added = remove_plus_sign_from_each_line(added)
concated = concat_splited_lines(added)
assert concated == removed
# problems = 0
# if concated != removed:
# problems += 1
#
# print "%d - #####################" % problems
# print ">%s<" % removed
# print ">%s<" % concated
print "No asserts means, that everything is ok :)"
if __name__ == '__main__':
#assert_diff_only_change_newlines(test_diff)
##### get diff from downloaded repository
# from git import Repo
# repo_url = 'git@github.com:noisy/progit2-pl.git'
# branch = 'origin/new-lines'
#
# print "Clonning repo %s ..." % repo_url
# Repo.clone_from(repo_url, '/tmp/new_lines/')
#
# repo = Repo('/tmp/new_lines/')
#
# diff = repo.git.diff(branch + '~1', branch)
##### get diff from direct commit from github
f = urllib2.urlopen("https://github.com/noisy/steem-bluepaper-pl/commit/4037affa78348d178633c8159c83862602aaa1c5.diff")
diff = f.read()
assert_diff_only_change_newlines(diff)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment