【问题标题】:Diff tool filter差异工具过滤器
【发布时间】:2021-07-09 03:04:52
【问题描述】:

如何区分两个文件但忽略注释字符串之间的所有差异。我想在结果差异中查看 cmets,但没有让工具将 cmets 之间的差异视为真正的差异。

文件1.py

# File 1 code
print(“code”)
print(“same code”)
print(“code”) # comment 1

文件2.py

# File 2 code
print(“different code”)
print(“same code”)
print(“code”) # comment 2

当我比较 file1.py 和 file2.py 时,我希望能够忽略 cmets,但仍将它们打印在 diff 中。也许一些命令,如:

diff -y file1.py file2.py -- magicRegex “#.*”

所需的输出可能如下所示:

#File 1 code                  # File 2 code
print(“code”)              |  print(“different code”)
print(“same code”)            print(“same code”)
print(“code”) # comment 1     print(“code”) # comment 2

【问题讨论】:

  • 有几个关于i的建议:stackoverflow.com/questions/7504059/…
  • @Lenna:简单地删除任何#.* 是没有意义的,因为例如,如果你有一行prog "#" x y ,你当然不想删除任何东西, 因为在这种情况下 # 不表示评论。您的正则表达式不会处理这种情况,只是在 # 之后清除所有内容。
  • 您是否对 Meld 等其他工具持开放态度,或者必须是 diff + 其他 bash 工具?我认为使用diff + grep + sed 这样做是可能的,但很困难。我特别认为使用 bash 工具将 cmets 重新插入到 diff -y 输出中同时保持美观是很困难的。
  • 对其他工具完全开放。将更改问题标题以反映这一点。

标签: diff


【解决方案1】:

我今天想了更多。理想情况下,有一个工具可以做到这一点,但如果没有,我认为这可能会起作用,这取决于你编写脚本的价值:

评论保留差异算法:

1 . For file1 and file2, process them and create 2 new files for each:

  i.  A version of each file with the comments removed, (file1.py.nocom).
      Lines containing only a comment would not be removed. Just the comment
      removed. The line numbering would need to stay the same.

  ii. A file containing the locations for all the comments as well as the
      actual comment text. Something like:
      1,1:# File 1 code
      4,15:# comment 1 

2. Do the diff between file1.py.nocom and file1.py.nocom, but without the -y
   flag. This will be easier to parse. Even easier, use the -c flag with a 
   really high value. Hopefully you can get the whole file in the diff
   without any missing "common" lines that way.

3. Go through the output from #2 and add back in the comments using the info
   from 1.ii. I experimented with manually editing the diff from #2 and 
   applying it with vim, but it didn't seem to like one of the "common" lines 
   having a comment change. But there may be some tool that will allow you to 
   view it. Barring that:

4. Use the commented diff output to recreate yourself the -y flag style 
   output. I guess the tricky part will be determining the width of the
   left side and printing out the right column. If on #2 you weren't able
   to get all the common lines into the diff output using the -c flag, then 
   here you'll have to re-add those missing common lines.

以上内容不会(很容易)与文档字符串一起使用,并且可能还有其他我没有想到的情况。我想如果您还需要在文件之间添加/删除注释行,则可能需要对其进行调整。但有我的两分钱。这似乎是可行的,但绝对是一大块工作。

【讨论】:

    【解决方案2】:

    您可以使用 sed 对它们进行预处理。您可以制作一个执行以下操作的包装器:

    sed -e 's/#.*$//' file1.py > file1.stripped
    sed -e 's/#.*$//' file2.py > file2.stripped
    diff -y file1.stripped file2.stripped
    rm file1.stripped file2.stripped
    

    【讨论】:

      猜你喜欢
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 2010-12-27
      • 1970-01-01
      相关资源
      最近更新 更多