# Path: ellen/utils/git.py
# PYGIT2_OBJ_TYPE = {
#     GIT_OBJ_COMMIT: 'commit',
#     GIT_OBJ_BLOB: 'blob',
#     GIT_OBJ_TREE: 'tree',
#     GIT_OBJ_TAG: 'tag',
# }
# 
# Path: ellen/utils/text.py
# def trunc_utf8(string, num, etc="..."):
#     """truncate a utf-8 string, show as num chars.
#     arg: string, a utf-8 encoding string; num, look like num chars
#     return: a utf-8 string
#     """
#     try:
#         gb = string.decode("utf8", "ignore")
#     except UnicodeEncodeError:  # Already decoded
#         gb = string
#     gb = gb.encode("gb18030", "ignore")
#     if num >= len(gb):
#         return string
#     if etc:
#         etc_len = len(etc.decode("utf8", "ignore").encode("gb18030", "ignore"))
#         trunc_idx = num - etc_len
#     else:
#         trunc_idx = num
#     ret = gb[:trunc_idx].decode("gb18030", "ignore").encode("utf8")
#     if etc:
#         ret += etc
#     return ret


import re
import sys
import mime
import magic
from datetime import datetime
from ellen.utils.git import PYGIT2_OBJ_TYPE
from ellen.utils.text import trunc_utf8
    from pygit2 import GIT_MERGE_ANALYSIS_FASTFORWARD, GIT_MERGE_ANALYSIS_UP_TO_DATE


def format_obj(obj, repository):
    func_name = 'format_' + PYGIT2_OBJ_TYPE[obj.type]
    this = sys.modules[__name__]
    formatter = getattr(this, func_name)
    return formatter(obj, repository)


def format_commit(commit, repository):
    d = {}
    d['type'] = 'commit'
    # FIXME: use parents
    try:
        d['parent'] = [str(p.id) for p in commit.parents] if commit.parents else []
        d['parents'] = [str(p.id) for p in commit.parents] if commit.parents else []
    except KeyError:
        # FIXME: pygit2 commit.parents
        d['parent'] = []
        d['parents'] = []
    d['tree'] = str(commit.tree.id)
    d['committer'] = _format_pygit2_signature(commit.committer)
    d['author'] = _format_pygit2_signature(commit.author)
    d['email'] = commit.author.email  # FIXME
    d['commit'] = str(commit.id)  # FIXME
    d['message'], _, d['body'] = commit.message.strip().partition('\n\n')
    d['sha'] = str(commit.id)
    return d



# Path: ellen/utils/git.py
# PYGIT2_OBJ_TYPE = {
#     GIT_OBJ_COMMIT: 'commit',
#     GIT_OBJ_BLOB: 'blob',
#     GIT_OBJ_TREE: 'tree',
#     GIT_OBJ_TAG: 'tag',
# }
# 
# Path: ellen/utils/text.py
# def trunc_utf8(string, num, etc="..."):
#     """truncate a utf-8 string, show as num chars.
#     arg: string, a utf-8 encoding string; num, look like num chars
#     return: a utf-8 string
#     """
#     try:
#         gb = string.decode("utf8", "ignore")
#     except UnicodeEncodeError:  # Already decoded
#         gb = string
#     gb = gb.encode("gb18030", "ignore")
#     if num >= len(gb):
#         return string
#     if etc:
#         etc_len = len(etc.decode("utf8", "ignore").encode("gb18030", "ignore"))
#         trunc_idx = num - etc_len
#     else:
#         trunc_idx = num
#     ret = gb[:trunc_idx].decode("gb18030", "ignore").encode("utf8")
#     if etc:
#         ret += etc
#     return ret


# Path: ellen/utils/format.py
import re
import sys
import mime
import magic
from datetime import datetime
from ellen.utils.git import PYGIT2_OBJ_TYPE
from ellen.utils.text import trunc_utf8
    from pygit2 import GIT_MERGE_ANALYSIS_FASTFORWARD, GIT_MERGE_ANALYSIS_UP_TO_DATE



def format_obj(obj, repository):
    func_name = 'format_' + PYGIT2_OBJ_TYPE[obj.type]
    this = sys.modules[__name__]
    formatter = getattr(this, func_name)
    return formatter(obj, repository)


def format_commit(commit, repository):
    d = {}
    d['type'] = 'commit'
    # FIXME: use parents
    try:
        d['parent'] = [str(p.id) for p in commit.parents] if commit.parents else []
        d['parents'] = [str(p.id) for p in commit.parents] if commit.parents else []
    except KeyError:
        # FIXME: pygit2 commit.parents
        d['parent'] = []
        d['parents'] = []
    d['tree'] = str(commit.tree.id)
    d['committer'] = _format_pygit2_signature(commit.committer)
    d['author'] = _format_pygit2_signature(commit.author)
    d['email'] = commit.author.email  # FIXME
    d['commit'] = str(commit.id)  # FIXME
    d['message'], _, d['body'] = commit.message.strip().partition('\n\n')
    d['sha'] = str(commit.id)
    return d



def format_tree(tree, repository):