Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better repository directory detection #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/git-goggles
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import os

from gitgoggles.codereview import get_status, complete_review, start_review, update_branches
from gitgoggles.progress import enable_progress
from gitgoggles.git import Repository

def handle_command():
if not os.path.exists('.git'):
if not Repository().in_repo():
print 'Not within a git repository.'
sys.exit(1)

Expand Down
9 changes: 8 additions & 1 deletion gitgoggles/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class Tag(Ref):

class Repository(object):
def __init__(self, path=None):
self.path = os.path.abspath(path or os.path.curdir)
self.path = os.path.realpath(path or os.path.curdir)
# Hack, make configurable
self.master = 'master'
master_sha = self.shell('git', 'log', '-1', '--pretty=format:%H', self.master).split
Expand Down Expand Up @@ -202,6 +202,13 @@ def configs(self):
return dict([ x.partition('=')[0::2] for x in self.shell('git', 'config', '--list').split ])
configs = property(memoize(configs))

def in_repo(self):
"""Is the current directory in a git repo and is it our repo?"""
rc = self.shell('git', 'rev-parse', '--git-dir').returncode
if rc == 0 and os.path.realpath(os.getcwd()).startswith(self.path):
return True
return False

def fetch(self):
log.info('Fetching updates.')
self.shell('git', 'remote', 'update', '--prune')
Expand Down
Empty file added gitgoggles/tests/__init__.py
Empty file.
61 changes: 61 additions & 0 deletions gitgoggles/tests/test_in_repo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python

import unittest
import os
import tempfile
import shutil

from ..git import Repository


class TestInRepo(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
os.chdir(self.tmpdir)

def tearDown(self):
shutil.rmtree(self.tmpdir)

def test_in_repo_true_top(self):
"""test that in_repo() returns True when in a git repo"""
os.chdir(self.tmpdir)
repo = Repository(self.tmpdir)
repo.shell("git", "init")

self.assertTrue(repo.in_repo())

def test_in_repo_true_subdir(self):
"""test that in_repo() returns True when in a git repo subdir"""
os.chdir(self.tmpdir)
repo = Repository(self.tmpdir)
repo.shell("git", "init")

subdir = os.path.join(self.tmpdir, "subdir")
os.mkdir(subdir)
os.chdir(subdir)

self.assertTrue(repo.in_repo())

def test_in_repo_false_another_repo(self):
"""
test that in_repo() returns False when in a git repo that isnt
ours
"""
repo1_dir = os.path.join(self.tmpdir, "repo1")
os.mkdir(repo1_dir)
repo2_dir = os.path.join(self.tmpdir, "repo2")
os.mkdir(repo2_dir)

os.chdir(repo1_dir)
repo1 = Repository(repo1_dir)
repo1.shell("git", "init")

os.chdir(repo2_dir)
repo2 = Repository(repo2_dir)
repo2.shell("git", "init")

self.assertFalse(repo1.in_repo())

def test_in_repo_false(self):
"""test that in_repo() returns False when not in a git repo"""
self.assertFalse(Repository(self.tmpdir).in_repo())