是否可以对git diff
输出进行操作,以防我想编写脚本并编辑实际的新/更新的代码行?
说我有档案' foo'由git跟踪
FOO:
line 1
line 2
line 3
line 4
我编辑它(在第3行添加'(编辑)')和
git diff foo
给了我
workspace@workspace:~/gitRepo/$ git diff foo
diff --git a/foo b/foo
index 9c2a709..30fb870 100644
--- a/foo
+++ b/foo
@@ -1,4 +1,4 @@
line 1
line 2
-line 3
+line 3 (edit)
line 4
我希望能够运行编辑foo的git diff | scriptThatAddsBarToNewStrings
cat foo
将呈现
line 1
line 2
line 3 (edit)bar
line 4
这可以想象吗?
答案 0 :(得分:0)
是的,它可以。您可以将任何git命令的输出传递给任何shell命令(grep,sort等等)并根据需要对其进行处理。并且没有什么可以阻止您将此输出作为输入传递给shell脚本。
答案 1 :(得分:0)
此脚本将为每个修改和添加的行追加一个字符串(说话:以+
开头的行。)
如果您需要任何修改,请与我们联系。
#!/bin/sh
# Change this string to whatever you want appended to a modified line
appending="bar"
write_file() {
echo -ne "$content" > "$file"
}
skip_start=true
content=""
while read line; do
if $skip_start; then
# Check for the "starting line"
[[ "$line" == @@* ]] && skip_start=false
# Look for the file name
if [[ "$line" == +++* ]]; then
file=$(echo "$line" | sed -e 's/^+++ b\/\(.*\)$/\1/')
fi
continue
fi
# Check if the diff for the current file ended
if [[ "$line" == diff\ --git* ]]; then
write_file
# Reset variables
skip_start=true
content=""
continue
fi
# Process the diff
first_char=${line:0:1}
if [[ "#$first_char#" == '# #' || "$first_char" == '-' ]]; then
continue
elif [[ "$first_char" == '+' ]]; then
line="${line:1}$appending"
fi
# Prevent a newline at the beginning
if [[ -n "$content" ]]; then
content+="\n"
fi
content+="$line"
done
write_file