https://blog.csdn.net/TAlice/article/details/117533318
#!/bin/bash
diff-lines() {
local path=
local pathOld=
local line=
while read; do
esc=$'\033'
if [[ $REPLY =~ ---\ (a/)?.* ]]; then
continue
elif [[ $REPLY =~ \+\+\+\ (b/)?([^[:blank:]$esc]+).* ]]; 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
if [ "$path" != "$pathOld" ]; then
echo -e "\n\n$path:$line:$REPLY" #不同文件间的修改用换行隔开
else
echo "$path:$line:$REPLY"
fi
if [[ ${BASH_REMATCH[2]} != - ]]; then
((line++))
fi
pathOld=$path
fi
done
}
if [ "$1" != "" ]; then #可查看单个文件的差异(方法:git-diff-lines xxx.c)
git diff -U0 --color=always -- "$1" | diff-lines
else
git diff -U0 --color=always | diff-lines
fi
0 条评论