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

from ellen.utils.process import git_with_repo
from ellen.utils.format import format_merge_result, format_index


# FIXME: param _raise ??
def merge(repository, ref, msg='automerge', commit_msg='',
          no_ff=False, _raise=True, _env=None):
    # msg, commit_msg makes multiline commit message
    git = git_with_repo(repository)
    git_merge = git.bake('merge', ref, no_ff=no_ff)
    if msg:
        git_merge = git_merge.bake(m=msg)
    if commit_msg:
        git_merge = git_merge.bake(m=commit_msg)
    return git_merge(env=_env)

def merge_tree(repository, ours, theirs):
    theirs = repository.revparse_single(theirs)
    theirs_tree = theirs.tree
    ours = repository.revparse_single(ours)
    ours_tree = ours.tree
    merge_base_oid = repository.merge_base(str(theirs.id),
                                       str(ours.id))
    merge_base_tree = repository.get(str(merge_base_oid)).tree
    index = ours_tree.merge(theirs_tree, merge_base_tree)
    return format_index(index)

def merge_head(repository, ref):
    target = repository.revparse_single(ref)
    oid = target.id
    merge_result = repository.merge(oid)
    return format_merge_result(merge_result)

def merge_commits(repository, ours, theirs):
    theirs = repository.revparse_single(theirs)
    ours = repository.revparse_single(ours)
    merge_index = repository.merge_commits(ours, theirs)
    return format_index(merge_index)

@@ -580,16 +580,17 @@
 =_env)%0A%0A
+%0A
 def merg
@@ -869,16 +869,20 @@
         
+    
 str(ours
@@ -945,24 +945,67 @@
 e_oid)).tree
+ %5C%0A        if merge_base_oid else ours_tree
 %0A    index =
@@ -1083,16 +1083,17 @@
 index)%0A%0A
+%0A
 def merg
@@ -1223,16 +1223,16 @@
 ge(oid)%0A
-
     retu
@@ -1269,16 +1269,17 @@
 esult)%0A%0A
+%0A
 def merg

Fix merge if no merge_base.