Skip to content

Worktree support #238

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

Open
wants to merge 3 commits 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
17 changes: 16 additions & 1 deletion lib/fontv/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import os

import git.repo.fun


def dir_exists(dirpath):
"""Tests for existence of a directory on the string filepath"""
Expand Down Expand Up @@ -39,13 +41,26 @@ def get_git_root_path(filepath):
:raises: IOError if unable to detect the root of the git repository through this path traversal
"""

if "GIT_DIR" in os.environ:
dot_git = os.path.abspath(os.environ["GIT_DIR"])
if git.repo.fun.is_git_dir(dot_git):
return os.path.dirname(dot_git)

# begin by defining directory that contains font as the git root needle
gitroot_path = os.path.dirname(os.path.abspath(filepath))

# search up to five directories above for the git repo root
for _ in range(6):
if dir_exists(os.path.join(gitroot_path, ".git")):
dot_git = os.path.join(gitroot_path, ".git")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before you assume that .git exists at the root of a repository, first check $GIT_DIR from the environment. The defaults are obviously in common use but exceptions are not so uncommon that it is smart to just ignore them. Unfortunately anywhere you are running font-v might not actually by inside a shell environment where the Git dir has been setup for the benefit of other tools, but it's better than nothing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not mistaken, this is more of a gripe with the original implementation, rather than the logic I added for worktree support?

I can add a check for $GIT_DIR as a fast path before the for loop if you'd like? I can validate it with is_git_dir()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, just something I happened to notice when reviewing this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some logic for this and a corresponding test

if git.repo.fun.is_git_dir(dot_git):
return gitroot_path
elif file_exists(dot_git):
# Returns something like ".../font-v/.git/worktrees/work-worktrees-work"
worktree_path = git.repo.fun.find_worktree_git_dir(dot_git)
if worktree_path:
# ".../font-v/.git/worktrees/work-worktrees-work" -> ".../font-v"
return worktree_path.split("/.git/worktrees/", 2)[0]

gitroot_path = os.path.dirname(gitroot_path)

raise IOError(
Expand Down
34 changes: 34 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-

import os
import subprocess

import pytest

Expand Down Expand Up @@ -56,12 +57,45 @@ def test_utilities_get_gitrootpath_function_returns_proper_path_three_levels_up(
assert os.path.isdir(gitdir_path) is True


@pytest.fixture
def worktree_fixture():
subprocess.check_call(
["git", "worktree", "add", "--detach", "/tmp/font-v", "HEAD^"]
)
yield
subprocess.call(
[
"git",
"worktree",
"remove",
"/tmp/font-v",
]
)


def test_utilities_get_gitrootpath_function_returns_proper_path_from_worktree(
worktree_fixture,
):
filepath = "/tmp/font-v/README.md"
gitdir_path = get_git_root_path(filepath)
assert os.path.basename(gitdir_path) == "font-v"
assert os.path.isdir(gitdir_path) is True


def test_utilities_get_gitrootpath_function_raises_ioerror_six_levels_up():
with pytest.raises(IOError):
filepath = "tests/testfiles/deepdir/deepdir2/deepdir3/deepdir4/test.txt"
get_git_root_path(filepath)


def test_utilities_get_gitrootpath_uses_git_dir_env_var():
os.environ["GIT_DIR"] = os.path.abspath(".git")
filepath = "/this/isnt/even/a/path/but/it/doesnt/matter"
gitdir_path = get_git_root_path(filepath)
assert os.path.basename(gitdir_path) == "font-v"
assert os.path.isdir(gitdir_path) is True


def test_utilities_is_font_ttf():
assert is_font("Test-Regular.ttf") is True

Expand Down