Skip to content

Commit 019e124

Browse files
authored
Auto install git-theta when doing a track as people often forget this step (#232)
* When someone runs git theta track or git theta add, make sure they have git theta installed. * move to loggers
1 parent 79ab4be commit 019e124

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

git_theta/git_utils.py

+27
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
# files. Defined as a variable in case extra functionality ever requires more
3434
# attributes.
3535
THETA_ATTRIBUTES = ("filter", "merge", "diff")
36+
THETA_CONFIG_KEYS = (
37+
"filter.theta.clean=",
38+
"filter.theta.smudge=",
39+
"merge.theta.name=",
40+
"merge.theta.driver=",
41+
"diff.theta.command=",
42+
)
3643

3744

3845
def get_git_repo():
@@ -409,6 +416,26 @@ def is_git_lfs_installed():
409416
return False
410417

411418

419+
def is_git_theta_installed(
420+
repo: Optional = None, git_theta_config_keys: Sequence[str] = THETA_CONFIG_KEYS
421+
) -> bool:
422+
"""Check if git-theta is installed.
423+
424+
By checking `git config --list` we see all configuration options reguardless
425+
of if they setup git-theta up in ~/.gitconfig, ${repo}/.git/config, etc.
426+
427+
Note:
428+
This check requires you to be in the repo, but this is fine for our use
429+
cases.
430+
"""
431+
repo = get_git_repo() if repo is None else repo
432+
config = repo.git.config("--list")
433+
for config_key in git_theta_config_keys:
434+
if config_key not in config:
435+
return False
436+
return True
437+
438+
412439
def make_blob(repo, contents: str, path: str):
413440
contents = contents.encode("utf-8")
414441
mode = 33188 # The mode magic number used for blobs in git python

git_theta/scripts/git_theta.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ def install(args):
120120
"""
121121
# check if git-lfs is installed and abort if not
122122
if not git_utils.is_git_lfs_installed():
123-
print(
124-
f"git-theta depends on git-lfs and it does not appear to be installed. See installation directions at https://github.com/r-three/git-theta/blob/main/README.md#git-lfs-installation"
123+
logger = logging.getLogger("git_theta")
124+
logger.error(
125+
"git-theta depends on git-lfs and it does not appear to be installed. See installation directions at https://github.com/r-three/git-theta/blob/main/README.md#git-lfs-installation"
125126
)
126127
sys.exit(1)
127128

@@ -142,6 +143,12 @@ def track(args):
142143
Track a particular model checkpoint file with git-theta
143144
"""
144145
repo = git_utils.get_git_repo()
146+
if not git_utils.is_git_theta_installed():
147+
logger = logging.getLogger("git_theta")
148+
logger.error(
149+
"You are trying to track a file with git-theta, but git-theta is not installed, please run `git theta install`."
150+
)
151+
sys.exit(1)
145152
model_path = git_utils.get_relative_path_from_root(repo, args.file)
146153

147154
gitattributes_file = git_utils.get_gitattributes_file(repo)
@@ -155,6 +162,12 @@ def track(args):
155162

156163
def add(args, unparsed_args):
157164
repo = git_utils.get_git_repo()
165+
if not git_utils.is_git_theta_installed():
166+
logger = logging.getLogger("git_theta")
167+
logger.error(
168+
"You are trying to add a file using git-theta, but git-theta is not installed, please run `git theta install`."
169+
)
170+
sys.exit(1)
158171
env_vars = {
159172
"GIT_THETA_UPDATE_TYPE": args.update_type,
160173
"GIT_THETA_UPDATE_DATA_PATH": args.update_data,

0 commit comments

Comments
 (0)