如何执行忽略所有注释的差异? [英] How can I perform a diff that ignores all comments?

查看:72
本文介绍了如何执行忽略所有注释的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的代码库,该代码库是从原始项目中派生出来的,我正在尝试查找与原始项目的所有差异。许多文件编辑包含注释掉的调试代码和其他注释。在Ubuntu下称为Meld的GUI差异/合并工具可以忽略注释,但只能忽略单行注释。

I have a large codebase that was forked from the original project and I'm trying to track down all the differences from the original. A lot of the file edits consist of commented out debugging code and other miscellaneous comments. The GUI diff/merge tool called Meld under Ubuntu can ignore comments, but only single line comments.

还有其他简便的方法可以只查找非注释差异,使用GUI工具还是linux命令行工具?如果有所作为,代码是PHP和Javascript的混合,所以我主要是想忽略 // / * * /

Is there any other convenient way of finding only the non-comment diffs, either using a GUI tool or linux command line tools? In case it makes a difference, the code is a mixture of PHP and Javascript, so I'm primarily interested in ignoring //, /* */ and #.

推荐答案

到使用视觉差异,您可以尝试合并 DiffMerge

To use visual diff, you can try Meld or DiffMerge.

其规则集和选项可提供自定义行为。

Its rulesets and options provide for customized behavior.

从命令行角度来看,可以使用- diff 的ignore-matching-lines = RE 选项,例如:

From the command-line perspective, you can use --ignore-matching-lines=RE option for diff, for example:

diff -d -I '^#' -I '^ #' file1 file2

请注意regex必须匹配两个文件中的相应行,并且匹配大块中每个更改的行才能正常工作,否则仍会显示差异。

Please note that the regex has to match the corresponding line in both files and it matches every changed line in the hunk in order to work, otherwise it'll still show the difference.

使用单引号保护模式F rom shell展开并转义保留正则表达式的字符(例如

Use single quotes to protect pattern from shell expanding and to escape the regex-reserved characters (e.g. brackets).

我们可以阅读 diffutils 手册:

We can read in diffutils manual:


但是, -I 仅在大块中每条更改的行(每次插入和每次删除)都匹配正则表达式时,才忽略包含正则表达式的行的插入或删除。

However, -I only ignores the insertion or deletion of lines that contain the regular expression if every changed line in the hunk (every insertion and every deletion) matches the regular expression.

换句话说,对于每个不可忽略的更改, diff 会在其附近打印出完整的更改集,包括可忽略的更改。通过使用多个 -I 选项,可以为要忽略的行指定多个正则表达式。 diff 尝试将每行与每个正则表达式匹配,从给定的最后一个开始。

In other words, for each non-ignorable change, diff prints the complete set of changes in its vicinity, including the ignorable ones. You can specify more than one regular expression for lines to ignore by using more than one -I option. diff tries to match each line against each regular expression, starting with the last one given.

在此处armel 也很好地解释了此行为。

This behavior is also well explained by armel here.

另请参见:

  • How to diff files ignoring comments (lines starting with #)?

或者,检查其他差异应用程序,例如:

Alternatively, check other diff apps, for example:

  • for macOS: Code compare and merge tools
  • for Windows: 3-way merge tools for Windows

这篇关于如何执行忽略所有注释的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆