# Path: gh/util.py
# def find_git_config():
#     """Attempt to find the git config file for this repository in this
#     directory and it's parent.
#     """
#     original = cur_dir = os.path.abspath(os.curdir)
#     home = os.path.abspath(os.environ.get('HOME', ''))
# 
#     while cur_dir != home:
#         if os.path.isdir('.git') and os.access('.git/config', os.R_OK):
#             os.chdir(original)
#             return os.path.join(cur_dir, '.git', 'config')
#         else:
#             os.chdir(os.pardir)
#             cur_dir = os.path.abspath(os.curdir)
# 
#     os.chdir(original)
#     return ''
# 
# def get_repository_tuple():
#     """Parse the git config file for this repository if it exists.
# 
#     :returns: namedtuple with attributes, owner and repo
#     """
#     # If there are multiple remotes, this could choose the wrong one
#     reg = compile(
#         '(?:https://|git@)github\.com(?::|/)([^\./-]*)/(.*)'
#     )
#     config = find_git_config()
#     r = ()
#     if config:
#         fd = open(config)
#         for line in fd:
#             match = reg.search(line)
#             if match and match.groups():
#                 r = match.groups()
#                 break
# 
#         fd.close()
# 
#     if r and r[1].endswith('.git'):
#         r = (r[0], r[1][:-4])
#     return r
# 
# def wrap(text):
#     if hasattr(wrap, 'tw'):
#         tw = wrap.tw
#     else:
#         tw = TextWrapper(width=72, replace_whitespace=False)
# 
#     paragraphs = [p.replace('\n', ' ') for p in text.split('\n\n')]
#     paragraphs = ['\n'.join(tw.wrap(p)) for p in paragraphs]
#     return '\n\n'.join(paragraphs)


from unittest import TestCase
from gh.util import find_git_config, get_repository_tuple, wrap
import os
import shutil
    def setUp(self):
        self.orig = os.path.abspath(os.curdir)
        os.makedirs('proj/.git/')
        os.makedirs('proj/subdir/subdir2/subdir3')
        with open('proj/.git/config', 'w+') as fd:
            fd.writelines([
                '[core]',
                'bare = false',
                '[remote "origin"]'
                'url = git@github.com:sigmavirus24/Todo.txt-python.git'
            ]
            )
        os.chdir('proj')

    def tearDown(self):
        os.chdir(self.orig)
        shutil.rmtree('proj')

    def test_find_git_config(self):
        dirs = ['./subdir', './subdir/subdir2', './subdir/subdir2/subdir3']
        dirs = [os.path.abspath(d) for d in dirs]
        path = os.path.join(os.path.abspath(self.orig), 'proj', '.git',
                            'config')
        for d in dirs:
            os.chdir(d)
            ret = find_git_config()
            assert path == ret
            assert os.path.abspath(os.curdir) == d

    def test_get_repository_tuple(self):

# Path: gh/util.py
# def find_git_config():
#     """Attempt to find the git config file for this repository in this
#     directory and it's parent.
#     """
#     original = cur_dir = os.path.abspath(os.curdir)
#     home = os.path.abspath(os.environ.get('HOME', ''))
# 
#     while cur_dir != home:
#         if os.path.isdir('.git') and os.access('.git/config', os.R_OK):
#             os.chdir(original)
#             return os.path.join(cur_dir, '.git', 'config')
#         else:
#             os.chdir(os.pardir)
#             cur_dir = os.path.abspath(os.curdir)
# 
#     os.chdir(original)
#     return ''
# 
# def get_repository_tuple():
#     """Parse the git config file for this repository if it exists.
# 
#     :returns: namedtuple with attributes, owner and repo
#     """
#     # If there are multiple remotes, this could choose the wrong one
#     reg = compile(
#         '(?:https://|git@)github\.com(?::|/)([^\./-]*)/(.*)'
#     )
#     config = find_git_config()
#     r = ()
#     if config:
#         fd = open(config)
#         for line in fd:
#             match = reg.search(line)
#             if match and match.groups():
#                 r = match.groups()
#                 break
# 
#         fd.close()
# 
#     if r and r[1].endswith('.git'):
#         r = (r[0], r[1][:-4])
#     return r
# 
# def wrap(text):
#     if hasattr(wrap, 'tw'):
#         tw = wrap.tw
#     else:
#         tw = TextWrapper(width=72, replace_whitespace=False)
# 
#     paragraphs = [p.replace('\n', ' ') for p in text.split('\n\n')]
#     paragraphs = ['\n'.join(tw.wrap(p)) for p in paragraphs]
#     return '\n\n'.join(paragraphs)


# Path: tests/test_util.py
from unittest import TestCase
from gh.util import find_git_config, get_repository_tuple, wrap
import os
import shutil

    def setUp(self):
        self.orig = os.path.abspath(os.curdir)
        os.makedirs('proj/.git/')
        os.makedirs('proj/subdir/subdir2/subdir3')
        with open('proj/.git/config', 'w+') as fd:
            fd.writelines([
                '[core]',
                'bare = false',
                '[remote "origin"]'
                'url = git@github.com:sigmavirus24/Todo.txt-python.git'
            ]
            )
        os.chdir('proj')

    def tearDown(self):
        os.chdir(self.orig)
        shutil.rmtree('proj')

    def test_find_git_config(self):
        dirs = ['./subdir', './subdir/subdir2', './subdir/subdir2/subdir3']
        dirs = [os.path.abspath(d) for d in dirs]
        path = os.path.join(os.path.abspath(self.orig), 'proj', '.git',
                            'config')
        for d in dirs:
            os.chdir(d)
            ret = find_git_config()
            assert path == ret
            assert os.path.abspath(os.curdir) == d

    def test_get_repository_tuple(self):

        ret = get_repository_tuple()