#! {{sys_executable}}
########
# Copyright (c) 2016 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
############

import re
import subprocess
import sys

CURRENT_BRANCH_CMD = 'git symbolic-ref --short HEAD'
JIRA_ISSUE_REGEX = '(CFY-\d+)-.*?'

output_message_path = sys.argv[1]
branch_name = subprocess.check_output(
    CURRENT_BRANCH_CMD.split(' ')).decode('utf-8')

match = re.match(JIRA_ISSUE_REGEX, branch_name)
if match:
    jira_issue = match.group(1)
    with open(output_message_path) as f:
        current_message = f.read()
    if not current_message.startswith(jira_issue):
        with open(output_message_path, 'w') as f:
            f.write('{0} {1}'.format(jira_issue, current_message))
