#!/usr/bin/python

# This file is part of release-path
#
#Copyright (c) 2014 Amplify Education
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.

import sys
import logging
from git import GitCommandError
from release_branch_manager import init_script, find_product_branches, find_upstream_branch, save_failure_info, InvalidBranchException
from release_path import deep_merge

def main():
    (args, config, repo) = init_script()

    # these file names should either be in the config file or in the command-line
    # but for now they're hard-coded so I can get the thing running
    project_list_filename = "project_list"
    email_list_filename = "email_recipients"

    source_type = args.source_branch_type
    downstream_branches = find_product_branches(
        repo, remote=args.remote, branchtype=source_type)
    logging.debug("Found product branches %s for type %s"
        % (downstream_branches, source_type))
    failed_branches = []
    for product_branch in downstream_branches:
        try:
            upstream_branch = find_upstream_branch(repo, product_branch)
            if upstream_branch:
                print "Merging from %s into %s" % (product_branch, upstream_branch)
                deep_merge(repo, product_branch, upstream_branch)
            else:
                print "No upstream branch found for %s" % product_branch
        except GitCommandError, e: # "as" requires python 2.6
            logging.error(e)
            # NOTE: this is currently useless because deep_merge does a hard-reset
            # before raising an exception, effectively making it impossible to
            # diagnose problems.  This would be nice to fix.
            failed_branches.append( (upstream_branch, repo.git.diff()) )
        except InvalidBranchException, e: # "as" requires python 2.6
            failed_branches.append((upstream_branch, "Error finding upstream branch"))
    if failed_branches:
        save_failure_info(failed_branches, config,
            email_list_filename, project_list_filename)
    else:
        logging.debug("All merges succeeded!")
    return 0

if __name__ == "__main__":
    sys.exit(main())
