Removing find_git_repo

This commit is contained in:
Qubasa
2023-10-25 19:23:28 +02:00
parent 8e8b6530b0
commit a628934351
11 changed files with 68 additions and 75 deletions

View File

@@ -1,6 +1,7 @@
# !/usr/bin/env python3
import argparse
import json
import logging
import os
import re
import shlex
@@ -17,6 +18,8 @@ from clan_cli.types import FlakeName
script_dir = Path(__file__).parent
log = logging.getLogger(__name__)
# nixos option type description to python type
def map_type(type: str) -> Any:
@@ -287,6 +290,7 @@ def set_option(
current_config = json.load(f)
else:
current_config = {}
# merge and save the new config file
new_config = merge(current_config, result)
settings_file.parent.mkdir(parents=True, exist_ok=True)
@@ -295,7 +299,11 @@ def set_option(
print(file=f) # add newline at the end of the file to make git happy
if settings_file.resolve().is_relative_to(specific_flake_dir(flake_name)):
commit_file(settings_file, commit_message=f"Set option {option_description}")
commit_file(
settings_file,
repo_dir=specific_flake_dir(flake_name),
commit_message=f"Set option {option_description}",
)
# takes a (sub)parser and configures it

View File

@@ -12,7 +12,7 @@ from clan_cli.dirs import (
specific_flake_dir,
specific_machine_dir,
)
from clan_cli.git import commit_file, find_git_repo_root
from clan_cli.git import commit_file
from clan_cli.nix import nix_eval
from ..types import FlakeName
@@ -82,7 +82,7 @@ def set_config_for_machine(
settings_path.parent.mkdir(parents=True, exist_ok=True)
with open(settings_path, "w") as f:
json.dump(config, f)
repo_dir = find_git_repo_root()
repo_dir = specific_flake_dir(flake_name)
if repo_dir is not None:
commit_file(settings_path, repo_dir)

View File

@@ -2,7 +2,6 @@ import logging
import os
import sys
from pathlib import Path
from typing import Optional
from .errors import ClanError
from .types import FlakeName
@@ -10,27 +9,27 @@ from .types import FlakeName
log = logging.getLogger(__name__)
def _get_clan_flake_toplevel() -> Path:
return find_toplevel([".clan-flake", ".git", ".hg", ".svn", "flake.nix"])
# def _get_clan_flake_toplevel() -> Path:
# return find_toplevel([".clan-flake", ".git", ".hg", ".svn", "flake.nix"])
def find_git_repo_root() -> Optional[Path]:
try:
return find_toplevel([".git"])
except ClanError:
return None
# def find_git_repo_root() -> Optional[Path]:
# try:
# return find_toplevel([".git"])
# except ClanError:
# return None
def find_toplevel(top_level_files: list[str]) -> Path:
"""Returns the path to the toplevel of the clan flake"""
for project_file in top_level_files:
initial_path = Path(os.getcwd())
path = Path(initial_path)
while path.parent != path:
if (path / project_file).exists():
return path
path = path.parent
raise ClanError("Could not find clan flake toplevel directory")
# def find_toplevel(top_level_files: list[str]) -> Path:
# """Returns the path to the toplevel of the clan flake"""
# for project_file in top_level_files:
# initial_path = Path(os.getcwd())
# path = Path(initial_path)
# while path.parent != path:
# if (path / project_file).exists():
# return path
# path = path.parent
# raise ClanError("Could not find clan flake toplevel directory")
def user_config_dir() -> Path:

View File

@@ -3,7 +3,7 @@ import subprocess
from pathlib import Path
from typing import Optional
from clan_cli.dirs import find_git_repo_root
# from clan_cli.dirs import find_git_repo_root
from clan_cli.errors import ClanError
from clan_cli.nix import nix_shell
@@ -11,13 +11,9 @@ from clan_cli.nix import nix_shell
# generic vcs agnostic commit function
def commit_file(
file_path: Path,
repo_dir: Optional[Path] = None,
repo_dir: Path,
commit_message: Optional[str] = None,
) -> None:
if repo_dir is None:
repo_dir = find_git_repo_root()
if repo_dir is None:
return
# check that the file is in the git repository and exists
if not Path(file_path).resolve().is_relative_to(repo_dir.resolve()):
raise ClanError(f"File {file_path} is not in the git repository {repo_dir}")