1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Make diff header lines more explicit about what the diff is about

This gives a more readable output when producing diff for a task
with multiple items (with_items).
This commit is contained in:
Stoned Elipot 2013-02-26 16:53:59 +01:00
parent 1b8c9bab35
commit 8816dc7ca7
3 changed files with 12 additions and 2 deletions

View file

@ -126,6 +126,7 @@ class ActionModule(object):
dest_contents = base64.b64decode(dest_contents)
else:
raise Exception("unknown encoding, failed: %s" % dest_result.result)
diff['before_header'] = destination
diff['before'] = dest_contents
src = open(source)
@ -137,6 +138,7 @@ class ActionModule(object):
diff['src_larger'] = utils.MAX_FILE_SIZE_FOR_DIFF
else:
src.seek(0)
diff['after_header'] = source
diff['after'] = src.read()
return diff

View file

@ -104,7 +104,7 @@ class ActionModule(object):
module_args = "%s src=%s dest=%s" % (module_args, xfered, dest)
if self.runner.check:
return ReturnData(conn=conn, comm_ok=True, result=dict(changed=True), diff=dict(before=dest_contents, after=resultant))
return ReturnData(conn=conn, comm_ok=True, result=dict(changed=True), diff=dict(before_header=dest, after_header=source, before=dest_contents, after=resultant))
else:
res = self.runner._execute_module(conn, tmp, 'copy', module_args, inject=inject)
res.diff = dict(before=dest_contents, after=resultant)

View file

@ -639,7 +639,15 @@ def get_diff(diff):
if 'src_larger' in diff:
ret.append("diff skipped: source file size is greater than %d\n" % diff['src_larger'])
if 'before' in diff and 'after' in diff:
differ = difflib.unified_diff(diff['before'].splitlines(True), diff['after'].splitlines(True), 'before', 'after', '', '', 10)
if 'before_header' in diff:
before_header = "before: %s" % diff['before_header']
else:
before_header = 'before'
if 'after_header' in diff:
after_header = "after: %s" % diff['after_header']
else:
after_header = 'after'
differ = difflib.unified_diff(diff['before'].splitlines(True), diff['after'].splitlines(True), before_header, after_header, '', '', 10)
for line in list(differ):
ret.append(line)
return "".join(ret)