对于我的 Swift 项目,我试图识别 project.pbxproj 文件中的更改,这些更改在周围的上下文行中具有特定的单词或正则表达式(不在实际的差异更改中)。
我尝试过使用
git diff -G"<searchterm>"
但这似乎只将正则表达式应用于差异中的更改,而不是周围的上下文行。
也尝试过:
git diff -U# -G"<searchterm>"
但这不会改变可搜索的行。
示例:
目标差异看起来像:
repositoryURL = "[email protected]:myRepo.git"
requirement = {
- branch = main;
- kind = branch;
+ kind = upToNextMajorVersion;
+ minimumVersion = 1.0.0;
};
};
运行
git diff -G"kind"
或 git diff -G"branch"
将返回成功,但 git diff -G"repositoryURL"
不会返回。
我会使用 grep 而不是尝试利用 git 搜索来实现此目的。
类似
git diff | grep -A 5 myRepo
在你的例子中会产生:
repositoryURL = [email protected]:myRepo.git
requirement = {
- branch = main;
- kind = branch;
+ kind = upToNextMajorVersion;
+ minimumVersion = 1.0.0;
对于 grep 中的上下文控制,来自手册页:
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
Places a line containing a group separator (--) between
contiguous groups of matches. With the -o or
--only-matching option, this has no effect and a warning
is given.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines.
Places a line containing a group separator (--) between
contiguous groups of matches. With the -o or
--only-matching option, this has no effect and a warning
is given.
-C NUM, -NUM, --context=NUM
Print NUM lines of output context. Places a line
containing a group separator (--) between contiguous
groups of matches. With the -o or --only-matching option,
this has no effect and a warning is given.