Skip to content

Commit

Permalink
[lit] Implement -I option for builtin "diff" command to ignore change…
Browse files Browse the repository at this point in the history
…s where all lines match RE

Reviewed By: pengfei

Differential Revision: https://reviews.llvm.org/D147301
  • Loading branch information
yubingex007-a11y committed Apr 4, 2023
1 parent 9b3529b commit 22068a3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion llvm/utils/lit/lit/builtin_commands/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io
import locale
import os
import re
import sys

import util
Expand All @@ -13,6 +14,8 @@ class DiffFlags():
def __init__(self):
self.ignore_all_space = False
self.ignore_space_change = False
self.ignore_matching_lines = False
self.ignore_matching_lines_regex = ""
self.unified_diff = False
self.num_context_lines = 3
self.recursive_diff = False
Expand Down Expand Up @@ -95,6 +98,8 @@ def compose2(f, g):
f = compose2(ignoreAllSpaceOrSpaceChange, f)

for idx, lines in enumerate(filelines):
if flags.ignore_matching_lines:
lines = filter(lambda x: not re.match(r"{}".format(flags.ignore_matching_lines_regex), x), lines)
filelines[idx]= [f(line) for line in lines]

func = difflib.unified_diff if flags.unified_diff else difflib.context_diff
Expand Down Expand Up @@ -190,7 +195,7 @@ def main(argv):
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
args = argv[1:]
try:
opts, args = getopt.gnu_getopt(args, "wbuU:r", ["strip-trailing-cr"])
opts, args = getopt.gnu_getopt(args, "wbuI:U:r", ["strip-trailing-cr"])
except getopt.GetoptError as err:
sys.stderr.write("Unsupported: 'diff': %s\n" % str(err))
sys.exit(1)
Expand All @@ -214,6 +219,9 @@ def main(argv):
sys.stderr.write("Error: invalid '-U' argument: {}\n"
.format(a))
sys.exit(1)
elif o == "-I":
flags.ignore_matching_lines = True
flags.ignore_matching_lines_regex = a
elif o == "-r":
flags.recursive_diff = True
elif o == "--strip-trailing-cr":
Expand Down
11 changes: 11 additions & 0 deletions llvm/utils/lit/tests/Inputs/shtest-shell/diff-I.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Check diff ("diff -I") which is aimed to ignore changes where all lines match RE.

# RUN: echo 'foo' > %t.0
# RUN: echo 'bar1' >> %t.0
# RUN: echo 'foo' >> %t.0

# RUN: echo 'foo' > %t.1
# RUN: echo 'bar2' >> %t.1
# RUN: echo 'foo' >> %t.1

# RUN: diff -I "bar*" %t.0 %t.1

0 comments on commit 22068a3

Please sign in to comment.