Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
git diffに対して行数出力
#!/bin/sh
# Ref: Using git diff, how can I get added and modified lines numbers? - Stack Overflow -
# http://stackoverflow.com/questions/8259851/using-git-diff-how-can-i-get-added-and-modified-lines-numbers
path=
line=
while read; do
esc=$'\033'
if [[ $REPLY =~ ---\ (a/)?.* ]]; then
continue
elif [[ $REPLY =~ \+\+\+\ (b/)?([^[:blank:]]+).* ]]; then
path=${BASH_REMATCH[2]}
elif [[ $REPLY =~ @@\ -[0-9]+(,[0-9]+)?\ \+([0-9]+)(,[0-9]+)?\ @@.* ]]; then
line=${BASH_REMATCH[2]}
elif [[ $REPLY =~ ^($esc\[[0-9;]+m)*([\ +-]) ]]; then
echo "$path:$line:$REPLY"
if [[ ${BASH_REMATCH[2]} != - ]]; then
((line++))
fi
fi
done
@KenshoFujisaki

This comment has been minimized.

Copy link
Owner Author

KenshoFujisaki commented Apr 21, 2015

下記のように利用します.

 git diff -U100000 | diff-lines.sh
 git diff -U100000 | diff-lines.sh | grep -E "^.*\:[0-9]+\:\+"
@KenshoFujisaki

This comment has been minimized.

Copy link
Owner Author

KenshoFujisaki commented Apr 25, 2015

shellによって正規表現マッチが異なる(ShellScript - シェルスクリプトでの正規表現マッチ - Qiita)ので注意
例)
if [[ $REPLY =~ '---\ (a/)?.*' ]]; then
if [[ $REPLY =~ ---\ (a/)?.* ]]; then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.