Skip to content

Instantly share code, notes, and snippets.

@KenshoFujisaki
Last active January 24, 2020 03:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KenshoFujisaki/169326eb2fdde5182153 to your computer and use it in GitHub Desktop.
Save KenshoFujisaki/169326eb2fdde5182153 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

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

@KenshoFujisaki
Copy link
Author

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