# Path: kibitzr/storage.py
# class PageHistory(object):
#     """
#     Single file changes history using git
#     """
#     STORAGE_DIR = "pages"
# 
#     def __init__(self, conf, storage_dir=None, style=None):
#         self.storage_dir = storage_dir or self.STORAGE_DIR
#         self.cwd = os.path.join(
#             self.storage_dir,
#             normalize_filename(conf['name']),
#         )
#         self.target = os.path.join(self.cwd, "content")
#         self.git = sh.Command('git').bake(
#             '--no-pager',
#             _cwd=self.cwd,
#         )
#         self.ensure_repo_exists()
#         if conf.get('url'):
#             self.commit_msg = u"{name} at {url}".format(
#                 name=conf['name'],
#                 url=conf.get('url'),
#             )
#         else:
#             self.commit_msg = conf['name']
#         self.reporter = ChangesReporter(
#             self.git,
#             self.commit_msg,
#             style,
#         )
# 
#     def report_changes(self, content):
#         """
#         1) Write changes in file,
#         2) Commit changes in git
#         3.1) If something changed, return tuple(True, changes)
#         3.2) If nothing changed, return tuple(False, None)
# 
#         If style is "verbose", return changes in human-friendly format,
#         else use unified diff
#         """
#         self.write(content)
#         if self.commit():
#             return True, self.reporter.report()
#         else:
#             return False, None
# 
#     def write(self, content):
#         """Save content on disk"""
#         with io.open(self.target, 'w', encoding='utf-8') as fp:
#             fp.write(content)
#             if not content.endswith(u'\n'):
#                 fp.write(u'\n')
# 
#     def commit(self):
#         """git commit and return whether there were changes"""
#         self.git.add('-A', '.')
#         try:
#             self.git.commit('-m', self.commit_msg)
#             return True
#         except sh.ErrorReturnCode_1:
#             return False
# 
#     def ensure_repo_exists(self):
#         """Create git repo if one does not exist yet"""
#         if not os.path.isdir(self.cwd):
#             os.makedirs(self.cwd)
#         if not os.path.isdir(os.path.join(self.cwd, ".git")):
#             self.git.init()
#             self.git.config("user.email", "you@example.com")
#             self.git.config("user.name", "Your Name")
# 
#     @staticmethod
#     def clean():
#         """Remove storage dir (delete all git repos)"""
#         shutil.rmtree(PageHistory.STORAGE_DIR)


import contextlib
import tempfile
import shutil
from kibitzr.storage import PageHistory


@contextlib.contextmanager
def history(style=None):
    storage_dir = tempfile.mkdtemp()

# Path: kibitzr/storage.py
# class PageHistory(object):
#     """
#     Single file changes history using git
#     """
#     STORAGE_DIR = "pages"
# 
#     def __init__(self, conf, storage_dir=None, style=None):
#         self.storage_dir = storage_dir or self.STORAGE_DIR
#         self.cwd = os.path.join(
#             self.storage_dir,
#             normalize_filename(conf['name']),
#         )
#         self.target = os.path.join(self.cwd, "content")
#         self.git = sh.Command('git').bake(
#             '--no-pager',
#             _cwd=self.cwd,
#         )
#         self.ensure_repo_exists()
#         if conf.get('url'):
#             self.commit_msg = u"{name} at {url}".format(
#                 name=conf['name'],
#                 url=conf.get('url'),
#             )
#         else:
#             self.commit_msg = conf['name']
#         self.reporter = ChangesReporter(
#             self.git,
#             self.commit_msg,
#             style,
#         )
# 
#     def report_changes(self, content):
#         """
#         1) Write changes in file,
#         2) Commit changes in git
#         3.1) If something changed, return tuple(True, changes)
#         3.2) If nothing changed, return tuple(False, None)
# 
#         If style is "verbose", return changes in human-friendly format,
#         else use unified diff
#         """
#         self.write(content)
#         if self.commit():
#             return True, self.reporter.report()
#         else:
#             return False, None
# 
#     def write(self, content):
#         """Save content on disk"""
#         with io.open(self.target, 'w', encoding='utf-8') as fp:
#             fp.write(content)
#             if not content.endswith(u'\n'):
#                 fp.write(u'\n')
# 
#     def commit(self):
#         """git commit and return whether there were changes"""
#         self.git.add('-A', '.')
#         try:
#             self.git.commit('-m', self.commit_msg)
#             return True
#         except sh.ErrorReturnCode_1:
#             return False
# 
#     def ensure_repo_exists(self):
#         """Create git repo if one does not exist yet"""
#         if not os.path.isdir(self.cwd):
#             os.makedirs(self.cwd)
#         if not os.path.isdir(os.path.join(self.cwd, ".git")):
#             self.git.init()
#             self.git.config("user.email", "you@example.com")
#             self.git.config("user.name", "Your Name")
# 
#     @staticmethod
#     def clean():
#         """Remove storage dir (delete all git repos)"""
#         shutil.rmtree(PageHistory.STORAGE_DIR)


# Path: tests/unit/transforms/test_changes.py
import contextlib
import tempfile
import shutil
from kibitzr.storage import PageHistory



@contextlib.contextmanager
def history(style=None):
    storage_dir = tempfile.mkdtemp()

    page_history = PageHistory(