Working with your git diffs
diffs are annoying to work with, here’s some tricks:
Ignore whitespace
git diff -w
A recent usage of -w turned a 447 line diff into a 102 line diff, that’s 22% the size.
Filter Diff
filterdiff is a CLI tool to…filter…diffs. It’s mostly good at including/ignoring files, less for the content of the diff
# only show diffs of tsx files
git diff | filterdiff -i '*.tsx'
diff-to-json
From the json-toolkit, it leverages unidiff. This won’t simplify the diff, but it does turn it into json so you can write custom jq scripts to parse the diff.
$ git show | diff-to-json | jq
[
{
"patch_info": [
"commit 0ca9e2407271e2d7c30530427e57c5f7c58b3256\n",
"Author: Tyler Adams <coppero1237@gmail.com>\n",
"Date: Sun Apr 23 01:41:33 2023 -0400\n",
"\n",
" Added installation instructions for JSON Toolkit.\n",
"\n",
"diff --git a/README.md b/README.md\n",
"index 2c7f586..b00eb1b 100644\n"
],
"source_file": "a/README.md",
"source_timestamp": null,
"target_file": "b/README.md",
"target_timestamp": null,
"is_binary_file": false,
"is_rename": false,
"hunks": [
{
"source_start": 25,
"source_length": 6,
"target_start": 25,
"target_length": 13,
"section_header": "JSON-to-Delimited Converter: Output JSON data as delimited strings.",
"_added": null,
"_removed": null,
"source": [
" \n",
" With the JSON Toolkit, you'll save time and effort on a variety of data processing tasks, and you'll be able to focus on what matters most - making sense of your data.\n",
" \n",
" ## Prerequisites\n",
" \n",
" * Bash\n"
],
"target": [
" \n",
" With the JSON Toolkit, you'll save time and effort on a variety of data processing tasks, and you'll be able to focus on what matters most - making sense of your data.\n",
" \n",
"+# Installing\n",
"+```\n",
"+sudo add-apt-repository ppa:code-faster/ppa\n",
"+sudo apt update\n",
"+sudo apt install json-toolkit\n",
"+```\n",
"+# Developing\n",
" ## Prerequisites\n",
" \n",
" * Bash\n"
]
}
]
}
]
Python + unidiff
Or you can use the unidiff package directly in a python script and manipulate in python, although personally I recommend converting it to a dict and processing the dict instead of dealing with unidiff’s large number of custom classes.