| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# This script is used to produce git context diffs |
| 4 |
|
| 5 |
# Supplied parameters: |
| 6 |
# $1 $2 $3 $4 $5 $6 $7 |
| 7 |
# path old-file old-hash old-mode new-file new-hash new-mode |
| 8 |
# 'path' is the git-tree-relative path of the file being diff'ed |
| 9 |
|
| 10 |
=comment |
| 11 |
|
| 12 |
This info is copied from the old wiki page on Working with git: |
| 13 |
|
| 14 |
Context diffs with Git |
| 15 |
|
| 16 |
Copy git-external-diff into libexec/git-core/ of your git installation |
| 17 |
and configure git to use that wrapper with: |
| 18 |
|
| 19 |
git config [--global] diff.external git-external-diff |
| 20 |
|
| 21 |
--global makes the configuration global for your user - otherwise it is |
| 22 |
just configured for the current repository. |
| 23 |
|
| 24 |
For every command which displays diffs in some way you can use the |
| 25 |
parameter "--[no-]-ext-diff" to enable respectively disable using the |
| 26 |
external diff command. |
| 27 |
|
| 28 |
For the git diff command --ext-diff is enabled by default - for any |
| 29 |
other command like git log -p or git format-patch it is not! |
| 30 |
|
| 31 |
This method should work on all platforms supported by git. |
| 32 |
|
| 33 |
If you do not want to configure the external wrapper permanently or you |
| 34 |
want to overwrite it you can also invoke git like: |
| 35 |
|
| 36 |
export GIT_EXTERNAL_DIFF=git-external-diff |
| 37 |
git diff --[no-]ext-diff |
| 38 |
|
| 39 |
Alternatively, configure a git alias in ~/.gitconfig or .git/config: |
| 40 |
|
| 41 |
[alias] |
| 42 |
cdiff = !GIT_EXTERNAL_DIFF=git-context-diff git diff |
| 43 |
=cut |
| 44 |
|
| 45 |
old_hash="$3" |
| 46 |
new_hash=$(git hash-object "$5") |
| 47 |
|
| 48 |
# no change? |
| 49 |
[ "$old_hash" = "$new_hash" ] && exit 0 |
| 50 |
|
| 51 |
[ "$DIFF_OPTS" = "" ] && DIFF_OPTS='-pcd' |
| 52 |
|
| 53 |
echo "diff --git a/$1 b/$1" |
| 54 |
echo "new file mode $7" |
| 55 |
echo "index ${old_hash:0:7}..${new_hash:0:7}" |
| 56 |
|
| 57 |
diff --label a/"$1" --label b/"$1" $DIFF_OPTS "$2" "$5" |
| 58 |
|
| 59 |
exit 0 |