# -*- coding: utf-8 -*-
from logging import getLogger
from git.exc import GitCommandError
from datetime import datetime
import git

logger = getLogger(__name__)


def init_repo(repo_dir, repo_url):
    repo = git.Repo.init(repo_dir)
    if repo.remotes:
        origin = repo.remote('origin')
    else:
        origin = repo.create_remote('origin', repo_url)
    origin.fetch()
    repo.git.reset('--hard', 'origin/master')
    logger.info("Initialized git repository %s in %s", repo_dir, repo_url)


def update_repo(repo_dir):
    repo = git.Repo.init(repo_dir)

    try:
        if repo.commit().hexsha != repo.remote().fetch()[0].commit.hexsha:
            origin = repo.remote()
            origin.fetch()
            repo.git.reset('--hard', 'origin/master')
            return True
        else:
            return False
    except GitCommandError:
        logger.error("Git fetch failed.", exc_info=True, extra={'data': {
            'repo_dir': repo_dir
        }})
    else:
        logger.info("Fetched git repository", extra={'data': {
            'repo_dir': repo_dir
        }})


def get_repo_active_branch(repo_dir):
    """
    :param repo_dir: path of repo
    :type repo_dir: str
    :return: name of currently checked out branch
    :rtype: str
    """
    try:
        sipa_repo = git.Repo(repo_dir)
        return sipa_repo.active_branch.name
    except:
        logger.error("Could not get active branch", extra={'data': {
            'repo_dir': repo_dir}})
        return 'repo problem'


def get_latest_commits(repo_dir, commit_count):
    """
    :param repo_dir: path of repo
    :type repo_dir: str
    :param commit_count: number of commits to return
    :type commit_count: int
    :return: commit information (hash, message, author, date) about
    commit_count last commits
    :rtype: list of dicts
    """
    try:
        sipa_repo = git.Repo(repo_dir)
        commits = list(sipa_repo.iter_commits(get_repo_active_branch(repo_dir),
                                              max_count=commit_count))
        return [{'hexsha': commit.hexsha,
                 'message': commit.message,
                 'author': commit.author,
                 'date': datetime.fromtimestamp(int(commit.committed_date))
                 .strftime('%Y-%m-%dT' '%H:%M'),
                 } for commit in commits]
    except:
        logger.error("Could not get latest commits", extra={'data': {
            'repo_dir': repo_dir}})
        return []

@@ -26,98 +26,181 @@
 rom 
-logging import getLogger%0Afrom git.exc import GitCommandError%0Afrom datetime import datetime
+datetime import datetime%0Afrom flask.ext.babel import format_datetime%0Afrom git.exc import GitCommandError, InvalidGitRepositoryError, CacheError%0Afrom logging import getLogger
 %0Aimp
@@ -1986,13 +1986,8 @@
 s = 
-list(
 sipa
@@ -2080,21 +2080,16 @@
         
-     
 max_coun
@@ -2103,17 +2103,16 @@
 t_count)
-)
 %0A       
@@ -2121,16 +2121,29 @@
 eturn %5B%7B
+%0A            
 'hexsha'
@@ -2171,21 +2171,16 @@
         
-   
-  
 'message
@@ -2193,22 +2193,17 @@
 mit.
-message,%0A     
+summary,%0A
     
@@ -2251,21 +2251,16 @@
     
-     
 'date': 
 date
@@ -2255,16 +2255,32 @@
 'date': 
+format_datetime(
 datetime
@@ -2298,137 +2298,150 @@
 amp(
-int(commit.committed_date))%0A                 .strftime('%25Y-%25m-%25dT' '%25H:%25M'),%0A                 %7D for commit in commits%5D%0A    except
+%0A                commit.committed_date)),%0A        %7D for commit in commits%5D%0A    except (InvalidGitRepositoryError, CacheError, GitCommandError)
 :%0A  
@@ -2446,36 +2446,40 @@
         logger.e
-rror
+xception
 (%22Could not get 

Improve information given in `version`