mirror of
https://github.com/jbranchaud/til
synced 2026-01-11 19:18:01 +00:00
Add Diff Two Files In Unified Format as a Unix TIL
This commit is contained in:
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
||||
|
||||
_1723 TILs and counting..._
|
||||
_1724 TILs and counting..._
|
||||
|
||||
See some of the other learning resources I work on:
|
||||
|
||||
@@ -1641,6 +1641,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Curling For Headers](unix/curling-for-headers.md)
|
||||
- [Curling With Basic Auth Credentials](unix/curling-with-basic-auth-credentials.md)
|
||||
- [Determine ipv4 And ipv6 Public IP Addresses](unix/determine-ipv4-and-ipv6-public-ip-addresses.md)
|
||||
- [Diff Two Files In Unified Format](unix/diff-two-files-in-unified-format.md)
|
||||
- [Different Ways To Generate A v4 UUID](unix/different-ways-to-generate-a-v4-uuid.md)
|
||||
- [Display All The Terminal Colors](unix/display-all-the-terminal-colors.md)
|
||||
- [Display Free Disk Space](unix/display-free-disk-space.md)
|
||||
|
||||
78
unix/diff-two-files-in-unified-format.md
Normal file
78
unix/diff-two-files-in-unified-format.md
Normal file
@@ -0,0 +1,78 @@
|
||||
# Diff Two Files In Unified Format
|
||||
|
||||
The `diff` command is a standalone utility that can be used to get the
|
||||
difference between two files. It is similar to what you might expect when
|
||||
running `git diff` which compares two different versions of the same file. The
|
||||
`diff` command predates `git` and its unified format is what became the standard
|
||||
that `git` uses for its own diff implementation.
|
||||
|
||||
Running `diff` with two files as is gives output like the following:
|
||||
|
||||
```bash
|
||||
❯ diff startup.sh startup2.sh
|
||||
10,13c10,14
|
||||
< declare -A SESSIONS=(
|
||||
< ["TIL"]="$HOME/dev/jbranchaud/til:setup_til"
|
||||
< ["PLP"]="$HOME/dev/jbranchaud/pool-league-pro:"
|
||||
< ["client-app"]="$HOME/dev/client/client-app:"
|
||||
---
|
||||
> # Sessions will be created in the order listed here
|
||||
> SESSIONS=(
|
||||
> "TIL:$HOME/dev/jbranchaud/til:setup_til"
|
||||
> "PLP:$HOME/dev/jbranchaud/pool-league-pro:"
|
||||
> "client-app:$HOME/dev/client/client-app:"
|
||||
73,74c74,75
|
||||
< for session_name in TIL PLP client-app; do
|
||||
< IFS=':' read -r directory setup_function <<<"${SESSIONS[$session_name]}"
|
||||
---
|
||||
> for session_config in "${SESSIONS[@]}"; do
|
||||
> IFS=':' read -r session_name directory setup_function <<<"$session_config"
|
||||
```
|
||||
|
||||
That's readable at a glance, but the unified format (with the `-u` flag) can
|
||||
provide more context:
|
||||
|
||||
```bash
|
||||
❯ diff -u startup.sh startup2.sh
|
||||
--- startup.sh 2026-01-10 12:46:52
|
||||
+++ startup2.sh 2026-01-10 12:48:00
|
||||
@@ -7,10 +7,11 @@
|
||||
|
||||
# Session configurations
|
||||
# Format: "session_name:directory:setup_function"
|
||||
-declare -A SESSIONS=(
|
||||
- ["TIL"]="$HOME/dev/jbranchaud/til:setup_til"
|
||||
- ["PLP"]="$HOME/dev/jbranchaud/pool-league-pro:"
|
||||
- ["client-app"]="$HOME/dev/client/client-app:"
|
||||
+# Sessions will be created in the order listed here
|
||||
+SESSIONS=(
|
||||
+ "TIL:$HOME/dev/jbranchaud/til:setup_til"
|
||||
+ "PLP:$HOME/dev/jbranchaud/pool-league-pro:"
|
||||
+ "client-app:$HOME/dev/client/client-app:"
|
||||
)
|
||||
|
||||
# Setup function for TIL session
|
||||
@@ -70,8 +71,8 @@
|
||||
echo ""
|
||||
|
||||
# Create sessions in order
|
||||
- for session_name in TIL PLP client-app; do
|
||||
- IFS=':' read -r directory setup_function <<<"${SESSIONS[$session_name]}"
|
||||
+ for session_config in "${SESSIONS[@]}"; do
|
||||
+ IFS=':' read -r session_name directory setup_function <<<"$session_config"
|
||||
create_session "$session_name" "$directory" "$setup_function"
|
||||
done
|
||||
```
|
||||
|
||||
Here we get additional context like surrounding lines and file name details.
|
||||
|
||||
While this is useful on its own, it also has the added benefit of making the
|
||||
output compatible with other tools we may already be using. For instance, I'm
|
||||
already using [delta](https://github.com/dandavison/delta) as my [git pager](https://github.com/jbranchaud/dotfiles/blob/main/gitconfig#L51) and [git differ](https://github.com/jbranchaud/dotfiles/blob/main/gitconfig#L139).
|
||||
|
||||
With the unified format, I can pipe the output directly to `delta` to get a
|
||||
better view of the diff that is colorized and includes syntax highlighting.
|
||||
|
||||
```bash
|
||||
❯ diff -u startup.sh startup2.sh | delta
|
||||
```
|
||||
Reference in New Issue
Block a user