Created
November 29, 2012 10:55
-
-
Save knu/4168189 to your computer and use it in GitHub Desktop.
git-word-diff-auto - offers a missing --word-diff="auto"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# git-word-diff-auto - offers a missing --word-diff="auto" | |
# | |
# Copyright (c) 2012 Akinori MUSHA | |
# | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions | |
# are met: | |
# 1. Redistributions of source code must retain the above copyright | |
# notice, this list of conditions and the following disclaimer. | |
# 2. Redistributions in binary form must reproduce the above copyright | |
# notice, this list of conditions and the following disclaimer in the | |
# documentation and/or other materials provided with the distribution. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
# SUCH DAMAGE. | |
# | |
usage () { | |
cat <<EOF >&2 | |
usage: $0 <git-subcommand> <args>.. | |
`basename $0` runs a given subcommand with a --word-diff option | |
enabling colorization if stdout is a tty. It can be used to build an | |
alias. | |
e.g. | |
git config --global alias.wdi 'word-diff-auto diff' | |
git config --global alias.wdc 'word-diff-auto diff --cached' | |
The following configuration variables are recognized. | |
color.wordDiff.old | |
The color used for highlighting removed words. | |
(default: "red reverse") | |
color.wordDiff.new | |
The color used for highlighting added words. | |
(default: "green reverse") | |
EOF | |
} | |
main () { | |
local opt OPTIND=1 | |
while getopts '' opt; do | |
: | |
done | |
shift $(($OPTIND - 1)) | |
if [ $# -eq 0 ]; then | |
usage | |
exit 1 | |
fi | |
local command="$1" slot color; shift | |
if git config --get-colorbool color.diff; then | |
set -- "$command" --word-diff=color "$@" | |
for slot in old new; do | |
eval "color=\`word_color $slot\`" | |
set -- -c color.diff.$slot="$color" "$@" | |
done | |
else | |
set -- "$command" --word-diff "$@" | |
fi | |
exec git "$@" | |
} | |
word_color () { | |
local slot="$1" color | |
case "$slot" in | |
old) default="red reverse" ;; | |
new) default="green reverse" ;; | |
*) return ;; | |
esac | |
color="$(git config --get diff.wordDiff.$slot)" | |
echo "${color:-$default}" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment