# 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

            # Skip the metadata file
            if c['path'].endswith('datapackage.json'): 
                continue 

            # Find a handler for this kind of file...
            handler = None 
            for r in representations: 
                if r.can_process(path): 
                    handler = r 
                    break 
            
            if handler is None: 
                continue 

            # print(path, "being handled by", handler)

            v1_hex = prev['commit']
            v2_hex = curr['commit']

            temp1 = tempfile.mkdtemp(prefix="dgit-diff-") 
            
            try: 
                for h in [v1_hex, v2_hex]: 
                    filename = '{}/{}/checkout.tar'.format(temp1, h)
                    try:
                        os.makedirs(os.path.dirname(filename))
                    except:
                        pass 
                    extractcmd = ['git', 'archive', '-o', filename, h, path]

# 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


            # Skip the metadata file
            if c['path'].endswith('datapackage.json'): 
                continue 

            # Find a handler for this kind of file...
            handler = None 
            for r in representations: 
                if r.can_process(path): 
                    handler = r 
                    break 
            
            if handler is None: 
                continue 

            # print(path, "being handled by", handler)

            v1_hex = prev['commit']
            v2_hex = curr['commit']

            temp1 = tempfile.mkdtemp(prefix="dgit-diff-") 
            
            try: 
                for h in [v1_hex, v2_hex]: 
                    filename = '{}/{}/checkout.tar'.format(temp1, h)
                    try:
                        os.makedirs(os.path.dirname(filename))
                    except:
                        pass 
                    extractcmd = ['git', 'archive', '-o', filename, h, path]

                    output = run(extractcmd)