66import sys
77
88from git_dummy .settings import settings
9+ from git_dummy .util import (
10+ is_dir_exist ,
11+ is_git_dir ,
12+ is_inside_git_dir ,
13+ is_valid_folder_name ,
14+ )
915
1016
1117app = typer .Typer (context_settings = {"help_option_names" : ["-h" , "--help" ]})
@@ -16,6 +22,7 @@ def main(
1622 name : str = typer .Option (
1723 settings .name ,
1824 help = "Name of the dummy repo" ,
25+ callback = is_valid_folder_name ,
1926 ),
2027 git_dir : pathlib .Path = typer .Option (
2128 settings .git_dir ,
@@ -45,6 +52,10 @@ def main(
4552 settings .constant_sha ,
4653 help = "Use constant values for commit author, email, and commit date parameters to yield consistent sha1 values across git-dummy runs" ,
4754 ),
55+ allow_nested : bool = typer .Option (
56+ settings .allow_nested ,
57+ help = "Allow dummy repo creation within an existing Git repo, as long as it's not at the level of an existing .git/ folder" ,
58+ ),
4859):
4960 settings .name = name
5061 settings .commits = commits
@@ -53,28 +64,25 @@ def main(
5364 settings .merge = merge
5465 settings .no_subdir = no_subdir
5566 settings .constant_sha = constant_sha
67+ settings .allow_nested = allow_nested
5668
5769 settings .git_dir = os .path .expanduser (git_dir )
5870 if not settings .no_subdir :
5971 settings .git_dir = os .path .join (settings .git_dir , settings .name )
6072
61- try :
62- git .Repo (settings .git_dir , search_parent_directories = True )
73+ if is_git_dir (settings .git_dir ):
74+ print (f"git-dummy error: Git repository already exists at { settings .git_dir } " )
75+ sys .exit (1 )
76+
77+ if not settings .allow_nested and is_inside_git_dir (settings .git_dir ):
6378 print (
64- f"git-dummy error: Git repository already exists at { settings .git_dir } or parent"
79+ f"git-dummy error: Git repository already exists at { settings .git_dir } or parent: use --allow-nested flag to override "
6580 )
6681 sys .exit (1 )
67- except (git .exc .InvalidGitRepositoryError , git .exc .NoSuchPathError ):
68- try :
69- git .Repo (pathlib .Path ().cwd (), search_parent_directories = True )
70- print (
71- f"git-dummy error: Git repository already exists at { settings .git_dir } or parent"
72- )
73- sys .exit (1 )
74- except git .exc .InvalidGitRepositoryError :
75- print (
76- f"git-dummy: Generating dummy Git repo at { settings .git_dir } with { settings .branches } branch(es) and { settings .commits } commit(s)."
77- )
82+
83+ print (
84+ f"git-dummy: Generating dummy Git repo at { settings .git_dir } with { settings .branches } branch(es) and { settings .commits } commit(s)."
85+ )
7886
7987 repo = git .Repo .init (settings .git_dir , initial_branch = "main" )
8088
0 commit comments