6
6
import sys
7
7
8
8
from 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
+ )
9
15
10
16
11
17
app = typer .Typer (context_settings = {"help_option_names" : ["-h" , "--help" ]})
@@ -16,6 +22,7 @@ def main(
16
22
name : str = typer .Option (
17
23
settings .name ,
18
24
help = "Name of the dummy repo" ,
25
+ callback = is_valid_folder_name ,
19
26
),
20
27
git_dir : pathlib .Path = typer .Option (
21
28
settings .git_dir ,
@@ -45,6 +52,10 @@ def main(
45
52
settings .constant_sha ,
46
53
help = "Use constant values for commit author, email, and commit date parameters to yield consistent sha1 values across git-dummy runs" ,
47
54
),
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
+ ),
48
59
):
49
60
settings .name = name
50
61
settings .commits = commits
@@ -53,28 +64,25 @@ def main(
53
64
settings .merge = merge
54
65
settings .no_subdir = no_subdir
55
66
settings .constant_sha = constant_sha
67
+ settings .allow_nested = allow_nested
56
68
57
69
settings .git_dir = os .path .expanduser (git_dir )
58
70
if not settings .no_subdir :
59
71
settings .git_dir = os .path .join (settings .git_dir , settings .name )
60
72
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 ):
63
78
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 "
65
80
)
66
81
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
+ )
78
86
79
87
repo = git .Repo .init (settings .git_dir , initial_branch = "main" )
80
88
0 commit comments