#!/usr/bin/env python3
# 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.

import sys as mod_sys
import argparse as mod_argparse
import time as mod_time

import gitutils as mod_gitutils

mod_gitutils.assert_in_git_repository()

parser = mod_argparse.ArgumentParser(
    description='Show list of branches sorted by last commit time')

parser.add_argument('-r', '--remote', action='store_true',
                    default=False, help='Get remote branches')
parser.add_argument('-a', '--all', action='store_true',
                    default=False, help='Get all (local and remote) branches')
parser.add_argument('--no-merged', action='store_true',
                    default=False, help='Only *not* merged branches')
parser.add_argument('--merged', action='store_true',
                    default=False, help='Only merged branches')
parser.add_argument('entries', metavar='entries', type=int, default=1000, nargs='?',
                    help='Number of entries (negative number if you want the last N entries)')

args = parser.parse_args()

now = mod_time.time()

times_and_branches = []

branches = mod_gitutils.get_branches(args.remote, merged=args.merged, no_merged=args.no_merged, all=args.all)
for branch in branches:
    cmd = 'log ' + branch + ' -1 --format=%at --'
    success, result = mod_gitutils.execute_git(cmd, output=False)
    if not success:
        mod_sys.stderr.write(cmd)
        mod_sys.stderr.write(result)

    if (not success) or (len(result.strip()) == 0):
        print('Cannot find the age of %s' % branch)
    else:
        time_diff_seconds = int(now) - int(result)
        time_diff_days = int((float(time_diff_seconds) / (60*60*24)) * 100) / 100.

        times_and_branches.append((time_diff_seconds, '%10s days: %s' % (time_diff_days, branch), ))

times_and_branches.sort()

try:
    entries = int(args.entries)
except:
    entries = 1000

if entries > 0:
    times_and_branches = times_and_branches[:entries]
if entries < 0:
    times_and_branches = times_and_branches[entries:]

for _, branch in times_and_branches:
    print(branch)
