#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright 2013 Tomo Krajina
#
# 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.

"""
Show a relation between two git commits/tags/branches.
"""

import pdb

import os as mod_os
import sys as mod_sys

import gitutils as mod_gitutils

mod_gitutils.assert_in_git_repository()

branches = mod_sys.argv[1:]

if not branches:
    print 'No branches specified'
    mod_sys.exit(1)

if len(branches) == 1:
    branches = ['HEAD'] + branches

if len(branches) != 2:
    print 'Two branches expected, got %s' % branches
    mod_sys.exit(1)

def get_git_sha1(branch_name):
    success, sha1 = mod_gitutils.execute_git('log -1 %s --format=%%H' % branch_name,
                                             output=False)
    if not success:
        print 'Invalid branch %s' % branch_name
        mod_sys.exit(1)
    return sha1.strip()

def print_log(commit_1, commit_2):
    success, log = mod_gitutils.execute_git(
            'log %s..%s --format=%%x20%%x20%%x20%%h%%x20%%Cgreen%%an%%Creset%%x20\"%%Cred%%s%%Creset\",%%x20%%ar' % (commit_1, commit_2),
            output=False)
    if not success:
        print 'Error retrieving log %s..%s' % (commit_1, commit_2)
        mod_sys.exit(1)
    result = log.rstrip()

    lines = result.split('\n')

    print '%s commits from \033[1;34m%s\033[1;m to \033[1;34m%s\033[1;m:' % (len(lines), commit_1, commit_2)

    if len(lines) < 30:
        print result
        return

    first = lines[:10]
    last = lines[-10:]

    lines = first + ['   ...%s more commits...' % (len(lines) - len(first) - len(last))] + last

    print '\n'.join(lines)

branch_1 = branches[0]
branch_2 = branches[1]

branch_1_sha1 = get_git_sha1(branch_1)
branch_2_sha1 = get_git_sha1(branch_2)

success, merge_base = mod_gitutils.execute_git('merge-base %s %s' % (branch_1, branch_2),
                                               output=False)
merge_base = merge_base.strip()
if not success:
    print 'Can\'t find merge base for %s and %s' % (branch_1, branch_2)
    mod_sys.exit(1)

elif merge_base == branch_1_sha1 and merge_base == branch_2_sha1:
    print '\033[1;34m%s\033[1;m \033[1;31mEQUALS\033[1;m \033[1;34m%s\033[1;m' % (branch_1, branch_2)
elif merge_base == branch_1_sha1:
    print '\033[1;34m%s\033[1;m is \033[1;31mBEHIND\033[1;m \033[1;34m%s\033[1;m' % (branch_1, branch_2)
    print
    print_log(branch_1, branch_2)
elif merge_base == branch_2_sha1:
    print '\033[1;34m%s\033[1;m is \033[1;31mAHEAD\033[1;m of \033[1;34m%s\033[1;m' % (branch_1, branch_2)
    print
    print_log(branch_2, branch_1)
else:
    print '\033[1;34m%s\033[1;m and %s \033[1;31mDIVERGED\033[1;m, common point is \033[1;34m%s\033[1;m' % (branch_1, branch_2, merge_base)
    print
    print_log(merge_base, branch_1)
    print
    print_log(merge_base, branch_2)
