Добавлена возможность инициализации репозитория с перезаписью существующего через флаг force

This commit is contained in:
Alexander Zhirov 2025-05-27 00:12:43 +03:00
parent 35a7b26a4a
commit db9a6be9f4
Signed by: alexander
GPG key ID: C8D8BE544A27C511
2 changed files with 14 additions and 4 deletions

View file

@ -15,7 +15,12 @@ int main(string[] args)
} }
auto argumets = new Program(programName, snagVersion) auto argumets = new Program(programName, snagVersion)
.add(new Command("init", "Initializing the repository for storing snapshots")) .add(new Command("init", "Initializing the repository for storing snapshots")
.add(new Flag("f", "force", "Initializing a repository with overwriting the existing one")
.name("force")
.optional
)
)
.add(new Command("status", "Checking the status of tracked files")) .add(new Command("status", "Checking the status of tracked files"))
.add(new Command("diff", "Show changed data")) .add(new Command("diff", "Show changed data"))
.add(new Command("import", "Import snapshot from a tar.gz archive") .add(new Command("import", "Import snapshot from a tar.gz archive")
@ -117,7 +122,7 @@ int main(string[] args)
try { try {
argumets argumets
.on("init", init => .on("init", init =>
snag.initialize() snag.initialize(init.flag("force"))
) )
.on("diff", diff => .on("diff", diff =>
snag.diff() snag.diff()

View file

@ -44,13 +44,18 @@ class Snag {
return result; return result;
} }
void initialize() { void initialize(bool force) {
auto result = execute(_baseCommand ~ ["rev-parse", "--git-dir"]); auto result = execute(_baseCommand ~ ["rev-parse", "--git-dir"]);
!result.status && !force && !result.status &&
throw new SnagException( throw new SnagException(
"The Git repository has already been initialized: " "The Git repository has already been initialized: "
~ result.output.strip('\n') ~ result.output.strip('\n')
); );
force && _config.git.exists
&& _config.git.isDir
&& _config.git.rmdirRecurse;
git( git(
["init", "--initial-branch=default"], ["init", "--initial-branch=default"],
"A Git repository initialization error occurred" "A Git repository initialization error occurred"