使用 git-diffc.sh awk-基于语言的包装器的 1 行解决方案,我围绕 git diff 编写:
git diffc
完成!
git diff 8d4d4fd3b60f200cbbb87f2b352fb097792180b2~2..8d4d4fd3b60f200cbbb87f2b352fb097792180b2~3 的示例输出:
diff --git a/useful_scripts/rg_replace.sh b/useful_scripts/rg_replace.sh
index 74bc5bb..0add69d 100755
--- a/useful_scripts/rg_replace.sh
+++ b/useful_scripts/rg_replace.sh
@@ -2,12 +2,11 @@
# This file is part of eRCaGuy_dotfiles: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles
-# STATUS: functional and ready-to-use
-
+# WORK IN PROGRESS! <===========
# This is a simple wrapper around RipGrep (`rg`) to allow in-place find-and-replace, since the
# `rg --replace` option replaces only the stdout, NOT the contents of the file.
# `man rg` under the `--replace` section states: "Neither this flag nor any other ripgrep
-# flag will modify your files." This wrapper overcomes that limitation.
+# flag will modify your files."
# INSTALLATION INSTRUCTIONS:
# 1. Install RipGrep: https://github.com/BurntSushi/ripgrep#installation
相对于git diffc 8d4d4fd3b60f200cbbb87f2b352fb097792180b2~2..8d4d4fd3b60f200cbbb87f2b352fb097792180b2~3 的示例输出。请注意,仅显示 - 和 + 行,而周围的上下文行消失了,并且所有其他行(例如 diff、index、---、+++ 和 @@)也消失了!:
-# STATUS: functional and ready-to-use
-
+# WORK IN PROGRESS! <===========
-# flag will modify your files." This wrapper overcomes that limitation.
+# flag will modify your files."
git diffc 代表“git diff changes”,意思是:只显示更改 行代码,没有别的。我写的。它不是常规 git 的一部分。
它支持git diff 支持的所有选项和参数,因为它只是git diff 的轻量级包装器。
在此处下载:git-diffc.sh。它是我的eRCaGuy_dotfiles repo 的一部分。
要安装它:
git clone https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles.git
cd eRCaGuy_dotfiles/useful_scripts
mkdir -p ~/bin
ln -si "${PWD}/git-diffc.sh" ~/bin/git-diffc
如果这是您第一次创建或使用~/bin 目录,现在手动注销并立即重新登录,以使 Ubuntu 的默认~/.profile 文件将~/bin 添加到您的@987654356 @ 多变的。如果注销并重新登录不起作用,请将以下几行代码添加到您的 ~/.profile 文件中,然后注销 Ubuntu 并重新登录:
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
就是这样!
用法:同git diff。例如:
git diffc
git diffc -h
git diffc commit1 commit2
git diffc --no-color
# etc.
附加安装说明:
See also the installation section of my other answer about git diffn, which I also wrote, here。除了在这些说明中看到git-diffn 的任何地方,请改用git-diffc。这也包括在wget 命令中。下载和安装git diffc 很简单:只需几个命令。
如何使用awk 只显示+ 和- 行,考虑到git diff 可能输出的任何颜色或文本格式:
下面的这段代码构成了git diffc 包装器。
这里没有一个其他答案(包括my other answer)能100% 正确地做到你想要的。然而,这个答案会。这是一个 1-liner,您可以将其复制并粘贴到您的终端中。 为了便于阅读,我只是将它设置为多行——你可以复制粘贴,所以我不妨让它可读!它依赖于awk 编程语言: p>
git diff --color=always "$@" | awk '
# 1. Match and then skip "--- a/" and "+++ b/" lines
/^(\033\[(([0-9]{1,2};?){1,10})m)?(--- a\/|\+\+\+ b\/)/ {
next
}
# 2. Now print the remaining "+" and "-" lines ONLY! Note: doing step 1 above first was required or
# else those lines would have been matched by this matcher below too since they also begin with
# the "+" and "-" symbols.
/^(\033\[(([0-9]{1,2};?){1,10})m)?[-+]/ {
print $0
}
' | less -RFX
这是它的特点。所有这些特点结合起来,解决了这里所有其他答案的缺点:
-
它处理彩色和非彩色输出。这就是这个正则表达式的作用:^(\033\[(([0-9]{1,2};?){1,10})m)?
-
它处理所有颜色和所有文本格式选项,包括粗体、斜体、删除线等,您可以set in your git config settings。这就是为什么上面的正则表达式中有;? 和{1,10} 的原因:如果它检测到颜色或文本格式代码的开头,它将匹配最多10 个这些组合的ANSI 代码序列。
-
它不包括以@@ 和单词diff 开头的行,就像accepted answer 那样。如果您确实想要这些行(坦率地说,我认为它们很有用:)),请改为这样做:
git diff --unified=0
或
git diff -U0
- 它以与
git diff 完全相同的方式显示输出:在具有可选颜色输出的less 寻呼机中(-R),并且仅当文本大于1 页时(-F),并且当你quit(-X)时,屏幕上保留当前页面的文字。
它还具有强大和易于配置的优点,因为它使用 awk 编程语言。
如果你有兴趣学习awk,这里有一些资源:
-
gawk (GNU awk) 手册:https://www.gnu.org/software/gawk/manual/html_node/index.html#SEC_Contents
- 研究
git diffn 和其中的cmets:https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/blob/master/useful_scripts/git-diffn.sh
- 如果你也想要
git diffn,即带有行号的git diff,请参见此处:Git diff with line numbers (Git log with line numbers)
- 一些 awk "hello world" 和语法测试示例:https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/tree/master/awk
作为奖励,我还将上面的内容包装起来用作git diffc,这意味着“git diff 只显示'c'hanges”。用法与git diff相同;只需使用 git diffc 代替!它支持所有选项。颜色默认为开启。要关闭它,只需使用git diffc --no-color 或git diffc --color=never。详情请见man git diff。
由于我昨晚刚刚完成了git diffn(一个显示git diff 行'数字'的工具),所以写git diffc 是微不足道的。我想我现在最好趁着知识还新鲜的时候去做。