# Path: dgitcore/helper.py
# def run(cmd):
#     """
#     Run a shell command
#     """
#     cmd = [pipes.quote(c) for c in cmd]
#     cmd = " ".join(cmd)
#     cmd += "; exit 0"
#     # print("Running {} in {}".format(cmd, os.getcwd()))
#     try:
#         output = subprocess.check_output(cmd,
#                                          stderr=subprocess.STDOUT,
#                                          shell=True)
#     except subprocess.CalledProcessError as e:
#             output = e.output
# 
#     output = output.decode('utf-8')
#     output = output.strip()
#     return output
# 
# class cd:
#     """Context manager for changing the current working directory"""
#     def __init__(self, newPath):
#         self.newPath = os.path.expanduser(newPath)
# 
#     def __enter__(self):
#         self.savedPath = os.getcwd()
#         os.chdir(self.newPath)
# 
#     def __exit__(self, etype, value, traceback):
#         os.chdir(self.savedPath)
# 
# Path: dgitcore/plugins/common.py
# def plugins_get_mgr():
#     """
#     Get the global plugin manager
#     """
#     global pluginmgr
#     return pluginmgr


import os, sys, parse, shutil 
import traceback 
import subprocess, pipes
import tempfile 
import json
import daff
from collections import OrderedDict
from ..helper import run, cd
from ..plugins.common import plugins_get_mgr


def get_change():

    cmd = ["git", "log", "--all", "--branches", "--numstat"]
    output = run(cmd)

    #commit 6a9c9e0db3869df252910dbcdae8cc97fa0291e4
    #Author: Venkata Pingali <pingali@gmail.com>
    #Date:   Fri Feb 26 17:07:50 2016 +0530
    #
    #    1. Simplify the SQL and post-processing scripts
    #
    #5       3       performance/bin/analyze
    #21      20      performance/bin/gather-rawdata.sh
    #


    changes = {}
    h = None
    lines = output.split("\n")
    for l in lines:
        #if l.startswith("Date") or l.startswith("Author"):
        #    continue
        r =  parse.parse("commit {commit}", l)
        if r is not None:
            if h is not None:
                changes[h['abbrev-commit']] = h
            h = {
                'abbrev-commit': r['commit'][:7],

# Path: dgitcore/helper.py
# def run(cmd):
#     """
#     Run a shell command
#     """
#     cmd = [pipes.quote(c) for c in cmd]
#     cmd = " ".join(cmd)
#     cmd += "; exit 0"
#     # print("Running {} in {}".format(cmd, os.getcwd()))
#     try:
#         output = subprocess.check_output(cmd,
#                                          stderr=subprocess.STDOUT,
#                                          shell=True)
#     except subprocess.CalledProcessError as e:
#             output = e.output
# 
#     output = output.decode('utf-8')
#     output = output.strip()
#     return output
# 
# class cd:
#     """Context manager for changing the current working directory"""
#     def __init__(self, newPath):
#         self.newPath = os.path.expanduser(newPath)
# 
#     def __enter__(self):
#         self.savedPath = os.getcwd()
#         os.chdir(self.newPath)
# 
#     def __exit__(self, etype, value, traceback):
#         os.chdir(self.savedPath)
# 
# Path: dgitcore/plugins/common.py
# def plugins_get_mgr():
#     """
#     Get the global plugin manager
#     """
#     global pluginmgr
#     return pluginmgr


# Path: dgitcore/datasets/history.py
import os, sys, parse, shutil 
import traceback 
import subprocess, pipes
import tempfile 
import json
import daff
from collections import OrderedDict
from ..helper import run, cd
from ..plugins.common import plugins_get_mgr



def get_change():

    cmd = ["git", "log", "--all", "--branches", "--numstat"]
    output = run(cmd)

    #commit 6a9c9e0db3869df252910dbcdae8cc97fa0291e4
    #Author: Venkata Pingali <pingali@gmail.com>
    #Date:   Fri Feb 26 17:07:50 2016 +0530
    #
    #    1. Simplify the SQL and post-processing scripts
    #
    #5       3       performance/bin/analyze
    #21      20      performance/bin/gather-rawdata.sh
    #


    changes = {}
    h = None
    lines = output.split("\n")
    for l in lines:
        #if l.startswith("Date") or l.startswith("Author"):
        #    continue
        r =  parse.parse("commit {commit}", l)
        if r is not None:
            if h is not None:
                changes[h['abbrev-commit']] = h
            h = {
                'abbrev-commit': r['commit'][:7],

                'commit': r['commit'],