Skip to content

Commit 873fde4

Browse files
committed
--show-unchanged option, closes #9
1 parent 410bc22 commit 873fde4

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
lines changed

Diff for: README.md

+11
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ The `--key=id` option means that the `id` column should be treated as the unique
5252

5353
The tool will automatically detect if your files are comma- or tab-separated. You can over-ride this automatic detection and force the tool to use a specific format using `--format=tsv` or `--format=csv`.
5454

55+
Use `--show-unchanged` to include full details of the unchanged rows in the diff output:
56+
57+
% csv-diff one.csv two.csv --key=id --show-unchanged
58+
1 row changed
59+
60+
id: 1
61+
age: "4" => "5"
62+
63+
Unchanged:
64+
name: "Cleo"
65+
5566
You can use the `--json` option to get a machine-readable difference:
5667

5768
$ csv-diff one.csv two.csv --key=id --json

Diff for: csv_diff/__init__.py

+22-10
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def load_csv(fp, key=None, dialect=None):
2424
return {keyfn(r): r for r in rows}
2525

2626

27-
def compare(previous, current):
27+
def compare(previous, current, show_unchanged=False):
2828
result = {
2929
"added": [],
3030
"removed": [],
@@ -59,19 +59,24 @@ def compare(previous, current):
5959
for id in changed:
6060
d = list(diff(previous[id], current[id], ignore=ignore_columns))
6161
if d:
62-
result["changed"].append(
63-
{
64-
"key": id,
65-
"changes": {
66-
field: [prev_value, current_value]
67-
for _, field, (prev_value, current_value) in d
68-
},
62+
changes = {
63+
"key": id,
64+
"changes": {
65+
field: [prev_value, current_value]
66+
for _, field, (prev_value, current_value) in d
67+
},
68+
}
69+
if show_unchanged:
70+
changes["unchanged"] = {
71+
field: value
72+
for field, value in previous[id].items()
73+
if field not in changes["changes"] and field != "id"
6974
}
70-
)
75+
result["changed"].append(changes)
7176
return result
7277

7378

74-
def human_text(result, key=None, singular=None, plural=None):
79+
def human_text(result, key=None, singular=None, plural=None, show_unchanged=False):
7580
singular = singular or "row"
7681
plural = plural or "rows"
7782
title = []
@@ -116,6 +121,13 @@ def human_text(result, key=None, singular=None, plural=None):
116121
)
117122
block.append("")
118123
change_blocks.append("\n".join(block))
124+
if details.get("unchanged"):
125+
block = []
126+
block.append(" Unchanged:")
127+
for field, value in details["unchanged"].items():
128+
block.append(' {}: "{}"'.format(field, value))
129+
block.append("")
130+
change_blocks.append("\n".join(block))
119131
summary.append("\n".join(change_blocks))
120132
if result["added"]:
121133
fragment = "{} {} added".format(

Diff for: csv_diff/cli.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@
3737
default=None,
3838
help="Plural word to use, e.g. 'trees' for '2 trees'",
3939
)
40-
def cli(previous, current, key, format, json, singular, plural):
40+
@click.option(
41+
"--show-unchanged",
42+
is_flag=True,
43+
help="Show unchanged fields for rows with at least one change",
44+
)
45+
def cli(previous, current, key, format, json, singular, plural, show_unchanged):
4146
"Diff two CSV files"
4247
dialect = {
4348
"csv": "excel",
@@ -49,7 +54,7 @@ def load(filename):
4954
open(filename, newline=""), key=key, dialect=dialect.get(format)
5055
)
5156

52-
diff = compare(load(previous), load(current))
57+
diff = compare(load(previous), load(current), show_unchanged)
5358
if json:
5459
print(std_json.dumps(diff, indent=4))
5560
else:

Diff for: tests/test_human_text.py

+22
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ def test_row_changed():
2121
)
2222

2323

24+
def test_row_changed_show_unchanged():
25+
diff = compare(
26+
load_csv(io.StringIO(ONE), key="id"),
27+
load_csv(io.StringIO(TWO), key="id"),
28+
show_unchanged=True,
29+
)
30+
assert (
31+
dedent(
32+
"""
33+
1 row changed
34+
35+
id: 1
36+
age: "4" => "5"
37+
38+
Unchanged:
39+
name: "Cleo"
40+
"""
41+
).strip()
42+
== human_text(diff, "id")
43+
)
44+
45+
2446
def test_row_added():
2547
diff = compare(
2648
load_csv(io.StringIO(THREE), key="id"), load_csv(io.StringIO(TWO), key="id")

0 commit comments

Comments
 (0)