From 136e7e75940bfb8a9816592c4aa64104069af62a Mon Sep 17 00:00:00 2001 From: Scott Lee Date: Tue, 8 Oct 2024 23:30:44 +0000 Subject: [PATCH] [cpplint] add stdin support in stdin Cpplint can already process input from stdin (when the filename is '-'), but `git cl lint` currently lacks this capability. Adding stdin support to `git cl lint` would enable seamless integration with IDEs like Visual Studio. Linters could then operate directly on unsaved code within the editor. Bug: 372288610 Change-Id: Ib2d5a3534ac0c5afcfcac64b708becde41612881 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5917767 Commit-Queue: Scott Lee Reviewed-by: Gavin Mak --- cpplint.py | 7 +++---- git_cl.py | 11 ++++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cpplint.py b/cpplint.py index 6bb6f0568..432f18c36 100755 --- a/cpplint.py +++ b/cpplint.py @@ -6399,10 +6399,9 @@ def ProcessFile(filename, vlevel, extra_check_functions=[]): # If after the split a trailing '\r' is present, it is removed # below. if filename == '-': - lines = codecs.StreamReaderWriter(sys.stdin, - codecs.getreader('utf8'), - codecs.getwriter('utf8'), - 'replace').read().split('\n') + lines = codecs.StreamReaderWriter( + sys.stdin.read().encode('utf8'), codecs.getreader('utf8'), + codecs.getwriter('utf8'), 'replace').decode('utf8').split('\n') else: with codecs.open(filename, 'r', 'utf8', 'replace') as stream: lines = stream.read().split('\n') diff --git a/git_cl.py b/git_cl.py index e057e0456..bce00b911 100755 --- a/git_cl.py +++ b/git_cl.py @@ -4795,7 +4795,10 @@ def FindFilesForLint(options, args): """Returns the base folder and a list of files to lint.""" files = [] cwd = os.getcwd() - if len(args) > 0: + if len(args) == 1 and args[0] == '-': + # the input is from stdin. + files.append(args[0]) + elif len(args) > 0: # If file paths are given in positional args, run the lint tools # against them without using git commands. This allows git_cl.py # runnable against any files even out of git checkouts. @@ -4836,10 +4839,8 @@ def CMDlint(parser, args): """Runs cpplint on the current changelist or given files. positional arguments: - files Files to lint. If omitted in the current git checkout, it - will run cpplint against all the affected files. If it is - not executed in git checkouts, files to lint must be - provided. + files Files to lint. If omitted, lint all the affected files. + If -, lint stdin. """ parser.add_option( '--filter',