Выполнение pre и post команд

This commit is contained in:
Alexander Zhirov 2025-05-28 02:28:33 +03:00
parent 5f746c33b7
commit a2131ec574
Signed by: alexander
GPG key ID: C8D8BE544A27C511

View file

@ -61,6 +61,32 @@ class Snag {
return formatted && fullStatus.length < 8 ? fullStatus ~ "\t" : fullStatus; return formatted && fullStatus.length < 8 ? fullStatus ~ "\t" : fullStatus;
} }
void executePreSnag() {
_config.presnag().each!((command) {
auto result = executeShell(command);
result.status && throw new SnagException(
"%s:\n\t%s\n\n%s".format(
"An error occurred during presnag-command execution",
command,
result.output
)
);
});
}
void executePostSnag() {
_config.postsnag().each!((command) {
auto result = executeShell(command);
result.status && throw new SnagException(
"%s:\n\t%s\n\n%s".format(
"An error occurred during postsnag-command execution",
command,
result.output
)
);
});
}
void initialize(bool force) { void initialize(bool force) {
auto result = execute(_baseCommand ~ ["rev-parse", "--git-dir"]); auto result = execute(_baseCommand ~ ["rev-parse", "--git-dir"]);
!force && !result.status && !force && !result.status &&
@ -127,13 +153,18 @@ class Snag {
email.length && (environment["GIT_AUTHOR_EMAIL"] = email); email.length && (environment["GIT_AUTHOR_EMAIL"] = email);
string message = comment.length ? comment : "Standard snapshot creation"; string message = comment.length ? comment : "Standard snapshot creation";
string newSnapshot;
result = execute(_baseCommand ~ ["rev-parse", "--short", "HEAD"]); result = execute(_baseCommand ~ ["rev-parse", "--short", "HEAD"]);
if (result.status == 128) { if (result.status == 128) {
// Если это самый первый коммит после инициализации репозитория // Если это самый первый коммит после инициализации репозитория
git(["add", "."], "Failed to prepare files for archiving"); git(["add", "."], "Failed to prepare files for archiving");
git(["commit", "-m"] ~ message, "Failed to create a backup"); git(["commit", "-m"] ~ message, "Failed to create a snapshot");
writeln("Backup was created successfully"); newSnapshot = git(
["rev-parse", "--short", "HEAD"],
"Failed to retrieve current snapshot information"
).output.strip('\n');
writeln("Snapshot was created successfully: ", newSnapshot);
return; return;
} else if (result.status != 0) } else if (result.status != 0)
throw new SnagException( throw new SnagException(
@ -157,8 +188,6 @@ class Snag {
"Failed to get the commit list between HEAD and " ~ currentBranch "Failed to get the commit list between HEAD and " ~ currentBranch
); );
string newSnapshot;
// Если список существует // Если список существует
if (result.output.length) { if (result.output.length) {
// Если коммит не является последним, то необходимо ответвление // Если коммит не является последним, то необходимо ответвление
@ -173,7 +202,7 @@ class Snag {
); );
git( git(
["commit", "-m"] ~ message, ["commit", "-m"] ~ message,
"Failed to create a backup" "Failed to create a snapshot"
); );
newSnapshot = git( newSnapshot = git(
["rev-parse", "--short", "HEAD"], ["rev-parse", "--short", "HEAD"],
@ -187,7 +216,7 @@ class Snag {
); );
git( git(
["commit", "-m"] ~ message, ["commit", "-m"] ~ message,
"Failed to create a backup" "Failed to create a snapshot"
); );
newSnapshot = git( newSnapshot = git(
["rev-parse", "--short", "HEAD"], ["rev-parse", "--short", "HEAD"],
@ -202,7 +231,7 @@ class Snag {
"Issue with including the commit into the branch " ~ currentBranch "Issue with including the commit into the branch " ~ currentBranch
); );
} }
writeln("Backup was created successfully: ", newSnapshot); writeln("Snapshot was created successfully: ", newSnapshot);
} }
void list(bool comment, bool author, bool email) { void list(bool comment, bool author, bool email) {