|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +# ******************************************************************************* |
| 4 | +# Copyright 2024 Arm Limited and affiliates. |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# ******************************************************************************* |
| 19 | + |
| 20 | +import argparse |
| 21 | + |
| 22 | + |
| 23 | +# * Ensuring the scopes end in colon and same level scopes are comma delimited. |
| 24 | +# TODO: Limit scopes to an acceptable list of tags. |
| 25 | +def __scopeCheck(msg: str): |
| 26 | + firstLine = (msg.partition("\n")[0]).strip() |
| 27 | + |
| 28 | + if not ":" in firstLine: |
| 29 | + raise ValueError( |
| 30 | + f"Please see contribution guidelines. First line must contain a scope ending in a colon. Got: {firstLine}" |
| 31 | + ) |
| 32 | + |
| 33 | + # The last element of the split is the title, which we don't care about. Remove it. |
| 34 | + scopesArray = firstLine.split(":")[:-1] |
| 35 | + print("---") |
| 36 | + print(f"Scopes: {scopesArray}") |
| 37 | + |
| 38 | + for scopes in scopesArray: |
| 39 | + print("---") |
| 40 | + print(f"Same-level scope: {scopes}") |
| 41 | + numWords = len(scopes.split()) |
| 42 | + numCommas = scopes.count(",") |
| 43 | + print(f"Number of words in scope: {numWords}") |
| 44 | + print(f"Number of commas in scope: {numCommas}") |
| 45 | + |
| 46 | + if numWords != numCommas + 1: |
| 47 | + raise ValueError( |
| 48 | + f"Please see contribution guidelines. Same-level scopes must be seperated by a comma. If this is true then words == commas + 1." |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +# * Ensuring a character limit for the first line. |
| 53 | +def __numCharacterCheck(msg: str): |
| 54 | + summary = msg.partition("\n")[0] |
| 55 | + msgSummaryLen = len(summary) |
| 56 | + if msgSummaryLen >= 72: |
| 57 | + raise ValueError( |
| 58 | + f"Please see contribution guidelines. Message summary must be less than 72. Got: {msgSummaryLen}" |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +def main(): |
| 63 | + parser = argparse.ArgumentParser() |
| 64 | + parser.add_argument("msg", help="Commit message to check.") |
| 65 | + args = parser.parse_args() |
| 66 | + msg: str = args.msg |
| 67 | + print(f"msg: {msg}") |
| 68 | + __numCharacterCheck(msg) |
| 69 | + __scopeCheck(msg) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + main() |
0 commit comments