"""GitHandler module - Provides GitHandler with shortcuts for handling git repository access."""
import os
import dulwich
import re

from io import StringIO

from dulwich import porcelain
from util.loghandler import log
from bottypes.invalid_command import InvalidCommand


class GitHandler():
    """Handles commit for multiple files."""

    def __init__(self, repo_path):
        try:
            self.repo_path = repo_path

            self.repo = porcelain.open_repo(repo_path)
        except Exception:
            log.exception("GitHandler::__init__()")
            raise InvalidCommand("Opening repo failed: Please check your log")

    def add_file(self, data, filename):
        """Add a file to the commit."""
        try:
            full_filename = os.path.join(self.repo_path, filename)

            with open(full_filename, "w") as f:
                f.write(data)

            porcelain.add(self.repo, full_filename)
        except Exception:
            # Anonymizing exceptions
            log.exception("GitHandler::add_file()")
            raise InvalidCommand("Adding file failed: Please check your log files...")

    def commit(self, commit_message):
        """Commit the current changeset."""
        try:
            porcelain.commit(self.repo, bytes(commit_message, "utf-8"))
        except Exception:
            # Anonymizing exceptions
            log.exception("GitHandler::commit()")
            raise InvalidCommand("Comitting file failed: Please check your log files...")

    def push(self, repo_user, repo_pass, repo_remote, repo_branch):
        """Push the current commit to git."""
        try:
            porcelain.push(self.repo, "https://{}:{}@{}".format(repo_user,
                                                                repo_pass, repo_remote), bytes(repo_branch, "utf-8"))
        except dulwich.errors.GitProtocolError:
            raise InvalidCommand(
                "Upload file failed: GitProtocolError - Check your username and password in the git configuration...")
        except KeyError:
            raise InvalidCommand("Upload file failed: KeyError - Check your git configuration for missing keys...")
        except TypeError:
            raise InvalidCommand("Upload file failed: TypeError - Did you forget to create a git configuration?")
        except Exception:
            log.exception("GitHandler::push()")
            raise InvalidCommand("Upload file failed: Unknown - Please check your log files...")

    def get_version(self):
        last_log = StringIO()

        # Get current branch
        current_branch = self.repo.refs.follow(b"HEAD")[0][1].decode().split("/")[-1]

        # Get last commit
        porcelain.log(self.repo, outstream=last_log, max_entries=1)

        commit_match = re.search('commit: (.+?)\n', last_log.getvalue())
        commit = commit_match.group(1) if commit_match else ""

        commit_match = re.search("\n\n(.+?)\Z", last_log.getvalue(), flags=re.DOTALL)
        commit_info = commit_match.group(1).strip() if commit_match else ""

        return "I'm running commit `{}` of branch `{}`\n\n```{}```".format(commit, current_branch, commit_info)

@@ -2763,63 +2763,224 @@
 it_m
-atch = re.search('commit: (.+?)%5Cn', last_log.getvalue()
+sg = last_log.getvalue()%0A%0A        commit_match = re.search('commit: (.+?)%5Cn', commit_msg)%0A        commit = commit_match.group(1) if commit_match else %22%22%0A%0A        commit_match = re.search('Date: (.+?)%5Cn%5Cn', commit_msg
 )%0A  
@@ -2983,32 +2983,37 @@
 )%0A        commit
+_date
  = commit_match.
@@ -3012,32 +3012,40 @@
 t_match.group(1)
+.strip()
  if commit_match
@@ -3102,35 +3102,26 @@
 +?)%5CZ%22, 
-last_log.getvalue()
+commit_msg
 , flags=
@@ -3266,16 +3266,20 @@
 %60%7B%7D%60%5Cn%5Cn
+*%7B%7D*
 %60%60%60%7B%7D%60%60%60
@@ -3310,16 +3310,29 @@
 _branch,
+ commit_date,
  commit_

Add date to commit version