utils: make check-style.py compatible with git

This commit is contained in:
Parth Pratim (GCI 2018)
2020-03-05 23:26:52 +05:30
committed by Mohit P. Tahiliani
parent a9491c47ae
commit 503e330c1a

View File

@@ -10,9 +10,17 @@ import shutil
import difflib
import re
def hg_modified_files():
files = os.popen ('hg st -nma')
return [filename.strip() for filename in files]
def git_modified_files():
files = os.popen('git diff --name-only')
process = subprocess.Popen(["git","rev-parse","--show-toplevel"],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
(root_dir, error) = process.communicate()
if isinstance(root_dir, bytes):
root_dir=root_dir.decode("utf-8")
files_changed = [item.strip() for item in files.readlines()]
files_changed = [item for item in files_changed if item.endswith('.h') or item.endswith('.cc')]
return [root_dir[: -1] + "/" + filename.strip () for filename in files_changed]
def copy_file(filename):
[tmp,pathname] = tempfile.mkstemp()
@@ -513,16 +521,6 @@ def indent_files(files, diff=False, debug=False, level=0, inplace=False):
return False
return True
def run_as_hg_hook(ui, repo, **kwargs):
# hack to work around mercurial < 1.3 bug
from mercurial import lock, error
lock.LockError = error.LockError
# actually do the work
files = hg_modified_files()
if not indent_files(files, inplace=False):
return True
return False
def run_as_main():
parser = optparse.OptionParser()
parser.add_option('--debug', action='store_true', dest='debug', default=False,
@@ -531,11 +529,8 @@ def run_as_main():
help="Level of style conformance: higher levels include all lower levels. "
"level=0: re-indent only. level=1: add extra spaces. level=2: insert extra newlines and "
"extra braces around single-line statements. level=3: remove all trailing spaces")
parser.add_option('--check-hg-hook', action='store_true', dest='hg_hook', default=False,
help='Get the list of files to check from mercurial\'s list of modified '
'and added files and assume that the script runs as a pretxncommit mercurial hook')
parser.add_option('--check-hg', action='store_true', dest='hg', default=False,
help="Get the list of files to check from mercurial\'s list of modified and added files")
parser.add_option('--check-git', action='store_true', dest='git', default=False,
help="Get the list of files to check from Git\'s list of modified and added files")
parser.add_option('-f', '--check-file', action='store', dest='file', default='',
help="Check a single file")
parser.add_option('--diff', action='store_true', dest='diff', default=False,
@@ -544,33 +539,34 @@ def run_as_main():
help="Indent the input files in-place")
(options,args) = parser.parse_args()
debug = options.debug
if options.hg_hook:
files = hg_modified_files()
if not indent_files(files, debug=options.debug,
level=options.level,
inplace=False):
sys.exit(1)
elif options.hg:
files = hg_modified_files()
indent_files(files, diff=options.diff,
debug=options.debug,
level=options.level,
inplace=options.in_place)
style_is_correct = False;
if options.git:
files = git_modified_files()
style_is_correct = indent_files(files,
diff=options.diff,
debug=options.debug,
level=options.level,
inplace=options.in_place)
elif options.file != '':
file = options.file
if not os.path.exists(file) or \
not os.path.isfile(file):
print('file %s does not exist' % file)
sys.exit(1)
indent_files([file], diff=options.diff,
debug=options.debug,
level=options.level,
inplace=options.in_place)
style_is_correct = indent_files([file],
diff=options.diff,
debug=options.debug,
level=options.level,
inplace=options.in_place)
if not style_is_correct:
sys.exit(1)
sys.exit(0)
if __name__ == '__main__':
# try:
try:
run_as_main()
# except Exception, e:
# sys.stderr.write(str(e) + '\n')
# sys.exit(1)
except Exception as e:
sys.stderr.write(str(e) + '\n')
sys.exit(1)