1#!/usr/bin/python 2 3import re 4import subprocess 5import sys 6 7# Looks for a string of the form [aosp/branch-name] 8AOSP_BRANCH_REGEX = "\[aosp/[^\]]+\]" 9 10AOSP_COMMIT_TAG_REGEX = "AOSP:" 11AOSP_COMMIT_LINK_REGEX = "aosp/\d+" 12AOSP_INFEASIBLE_REGEX = "Infeasible[ ]?\S+" 13 14ERROR_MESSAGE = """ 15The source of truth for this project is AOSP. If you are uploading something to 16a non-AOSP branch first, please provide a link in your commit message to the 17corresponding patch in AOSP. The link should be formatted as follows: 18 19 AOSP: aosp/<patch number> 20 21If it's infeasible for the change to be included in AOSP (for example, if a 22change contains confidential or security-sensitive information), please state 23that it's infeasible and provide reasoning as follows: 24 25 AOSP: Infeasible <your reasoning here> 26 27If you need to cherry-pick your change from an internal branch to AOSP before 28uploading, you can do so locally by adding the internal branch as a remote in 29AOSP: 30 git remote add goog-master /path/to/your/remote/branch/.git 31starting a new branch in AOSP: 32 repo start <your-branch-name>-cp 33then fetching and cherry-picking the change: 34 git fetch goog-master your-branch-name && git cherry-pick FETCH_HEAD 35""" 36 37def main(): 38 if _is_in_aosp(): 39 sys.exit(0) 40 41 commit_msg = subprocess.check_output(["git", "show", 42 sys.argv[1], "--no-notes"]) 43 for commit_line in commit_msg.splitlines(): 44 # Some lines in the commit message will be given to us as bytes 45 commit_line_str = str(commit_line) 46 if re.search(AOSP_COMMIT_TAG_REGEX, str(commit_line_str), re.IGNORECASE): 47 _check_aosp_message(commit_line_str) 48 49 print(ERROR_MESSAGE) 50 # Print the warning, but do not fail the presubmit check. 51 sys.exit(77) 52 53def _is_in_aosp(): 54 branch_info = subprocess.check_output(["git", "branch", "-vv"]) 55 return re.search(AOSP_BRANCH_REGEX, str(branch_info)) is not None 56 57def _check_aosp_message(aosp_line): 58 if re.search(AOSP_COMMIT_LINK_REGEX, aosp_line): 59 sys.exit(0) 60 61 if re.search(AOSP_INFEASIBLE_REGEX, aosp_line, re.IGNORECASE): 62 sys.exit(0) 63 64 print(ERROR_MESSAGE) 65 # Print the warning, but do not fail the presubmit check. 66 sys.exit(77) 67 68if __name__ == '__main__': 69 main() 70