From: Bobby R. Bruce Date: Thu, 28 Jan 2021 05:33:15 +0000 (-0800) Subject: util,python: Add check to ensure files are utf-8 in pre-commit X-Git-Tag: develop-gem5-snapshot~168 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=58ccc6287e95c597a14d359c0fb703774d02eb82;p=gem5.git util,python: Add check to ensure files are utf-8 in pre-commit The `file_from_index` function throws a UnicodeDecodeError if a modified file targetted for style-checking (i.e. source-code) cannot be decoded using `.decode("utf-8")`. This check throws an error informing the user a submitted file must be utf-8 encoded if this case arises. Change-Id: I2361017f2e7413ed60f897d2301f2e4c7995dd76 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/40015 Reviewed-by: Jason Lowe-Power Maintainer: Jason Lowe-Power Tested-by: kokoro --- diff --git a/util/git-pre-commit.py b/util/git-pre-commit.py index bf13f3bce..82fcf3900 100755 --- a/util/git-pre-commit.py +++ b/util/git-pre-commit.py @@ -76,8 +76,16 @@ for status, fname in git.status(filter="MA", cached=True): else: regions = all_regions - # Show they appropriate object and dump it to a file - status = git.file_from_index(fname) + # Show the appropriate object and dump it to a file + try: + status = git.file_from_index(fname) + except UnicodeDecodeError: + print("Decoding '" + fname + + "' throws a UnicodeDecodeError.", file=sys.stderr) + print("Please check '" + fname + + "' exclusively uses utf-8 character encoding.", file=sys.stderr) + sys.exit(1) + f = TemporaryFile() f.write(status.encode('utf-8'))