Skip to content

Commit

Permalink
Partial fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
starcraftman committed May 30, 2018
1 parent 6e401d9 commit bd26fbb
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
30 changes: 28 additions & 2 deletions gitstatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@
SYM_PREHASH = os.environ.get('ZSH_THEME_GIT_PROMPT_HASH_PREFIX', ':')


# TODO: Naiev fix, consider work trees too.
def find_git_root():
"""
Find the nearest enclosing git root.
Condition: Will be called from within a git respository.
Returns: The git project root.
Raises: IOError - Could not find the directory.
"""
working_d = os.getcwd()
while working_d != '/':
git_d = os.path.join(working_d, '.git')
if os.path.exists(git_d):
return git_d
working_d = os.path.dirname(working_d)

raise IOError("No git dir in folder hierarchy.")


def parse_stats(lines):
"""
Computes and returns the following _numbers_ describing the current state.
Expand Down Expand Up @@ -87,7 +107,7 @@ def parse_branch(branch):
upstream = SYM_NOUPSTREAM
local = 1
if 'no branch' in branch:
with open('.git/HEAD') as fin:
with open(os.path.join(GIT_D, 'HEAD')) as fin:
branch = SYM_PREHASH + fin.read().strip()[:7]
local = 0
elif '...' in branch:
Expand All @@ -110,7 +130,7 @@ def current_git_status(lines):
stats = parse_stats(lines[1:])

try:
with open('.git/logs/refs/stash') as fin:
with open(os.path.join(GIT_D, 'logs', 'refs', 'stash')) as fin:
stashed = len(fin.readlines())
except IOError:
stashed = 0
Expand Down Expand Up @@ -146,5 +166,11 @@ def main():
sys.stdout.flush()


try:
GIT_D = find_git_root()
except IOError:
pass


if __name__ == "__main__":
main()
70 changes: 70 additions & 0 deletions test_gitstatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,55 @@ def git_repo_remote_diverged():
os.chdir(cwd)


@pytest.yield_fixture(scope="function")
def git_repo_find_git_root():
"""
Create a fake git repo with the following properties:
- 1 commit
- nested folders called, first/second/third
"""
cwd = os.getcwd()
folder = tempfile.mkdtemp()
cmds = [
"git init",
"git config user.email '[email protected]'",
"git config user.name 'Your Name'",
"first:A single line",
"git add first",
"git commit -m 'first commit'",
]
try:
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
# os.makedirs(folder)
subs = os.path.join(folder, 'd_one', 'd_two', 'd_three')
print(subs)
os.makedirs(subs)
os.chdir(folder)

for cmd in cmds:
if re.match(r'\S+:', cmd):
assert len(cmd.split(":")) == 2
fname, text = cmd.split(":")
with open(os.path.join(folder, fname), 'a') as fout:
fout.write(text + '\n')
else:
with open(os.devnull, 'w') as devnull:
subprocess.check_call(shlex.split(cmd),
stdout=devnull, stderr=subprocess.STDOUT)

yield

finally:
try:
shutil.rmtree(folder)
except (OSError, IOError):
pass
os.chdir(cwd)


def test_branch_fatal():
""" Simple string to suppress doc warning. """
cwd = os.getcwd()
Expand Down Expand Up @@ -597,3 +646,24 @@ def test_main_stdin(git_repo_parse_stats):
finput.seek(0)
out = subprocess.check_output(['python', GIT_STATUS], stdin=finput)
assert out.decode('utf-8', errors='ignore') == 'master 0 0 3 0 1 2 1 0'


def test_find_git_root(git_repo_find_git_root):
expect = os.path.join(os.getcwd(), '.git')
sub_d = os.path.join(os.getcwd(), 'd_one', 'd_two', 'd_three')
assert os.path.isdir(sub_d)
os.chdir(sub_d)
assert gitstatus.find_git_root() == expect


def test_find_git_root_fail():
try:
temp_d = tempfile.mkdtemp()
cwd = os.getcwd()
os.chdir(temp_d)

with pytest.raises(IOError):
gitstatus.find_git_root()
finally:
os.chdir(cwd)
shutil.rmtree(temp_d)

0 comments on commit bd26fbb

Please sign in to comment.