Last active
June 5, 2024 06:57
-
-
Save KenshoFujisaki/169326eb2fdde5182153 to your computer and use it in GitHub Desktop.
git diffに対して行数出力
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 | |
# 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 |
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
下記のように利用します.