From 2d9ad0e5f50689c0b941f24d156a7ea4c0d60c50 Mon Sep 17 00:00:00 2001 From: Hleb Shauchenka Date: Sun, 19 Oct 2025 13:34:31 +0200 Subject: [PATCH] sysutils/git-backup: add force push option (#4981) --- sysutils/git-backup/Makefile | 2 +- .../opnsense/mvc/app/library/OPNsense/Backup/Git.php | 10 +++++++++- .../mvc/app/models/OPNsense/Backup/GitSettings.xml | 4 ++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/sysutils/git-backup/Makefile b/sysutils/git-backup/Makefile index 9c31c558f..17eb65ad5 100644 --- a/sysutils/git-backup/Makefile +++ b/sysutils/git-backup/Makefile @@ -1,6 +1,6 @@ PLUGIN_NAME= git-backup PLUGIN_VERSION= 1.0 -PLUGIN_REVISION= 3 +PLUGIN_REVISION= 4 PLUGIN_COMMENT= Track config changes using git PLUGIN_DEPENDS= git PLUGIN_MAINTAINER= ad@opnsense.org diff --git a/sysutils/git-backup/src/opnsense/mvc/app/library/OPNsense/Backup/Git.php b/sysutils/git-backup/src/opnsense/mvc/app/library/OPNsense/Backup/Git.php index a109a1adc..77fbfc640 100644 --- a/sysutils/git-backup/src/opnsense/mvc/app/library/OPNsense/Backup/Git.php +++ b/sysutils/git-backup/src/opnsense/mvc/app/library/OPNsense/Backup/Git.php @@ -66,6 +66,13 @@ class Git extends Base implements IBackupProvider "help" => gettext("Target branch to push to."), "value" => null ], + [ + "name" => "force_push", + "type" => "checkbox", + "label" => gettext("Force Push"), + "help" => gettext("When enabled, force push to origin if diverged (e.g., after restoring from an earlier backup). Use with caution as this overwrites remote history."), + "value" => null + ], [ "name" => "privkey", "type" => "passwordarea", @@ -161,8 +168,9 @@ class Git extends Base implements IBackupProvider } exec("cd {$targetdir} && {$git} remote remove origin"); exec("cd {$targetdir} && {$git} remote add origin " . escapeshellarg($url)); + $force_flag = (string)$mdl->force_push === "1" ? "--force-with-lease " : ""; $pushtxt = shell_exec( - "(cd {$targetdir} && {$git} push origin " . escapeshellarg("master:{$mdl->branch}") . + "(cd {$targetdir} && {$git} push {$force_flag}origin " . escapeshellarg("master:{$mdl->branch}") . " && echo '__exit_ok__') 2>&1" ); if (strpos($pushtxt, '__exit_ok__')) { diff --git a/sysutils/git-backup/src/opnsense/mvc/app/models/OPNsense/Backup/GitSettings.xml b/sysutils/git-backup/src/opnsense/mvc/app/models/OPNsense/Backup/GitSettings.xml index 08fafa161..29a41d0c3 100644 --- a/sysutils/git-backup/src/opnsense/mvc/app/models/OPNsense/Backup/GitSettings.xml +++ b/sysutils/git-backup/src/opnsense/mvc/app/models/OPNsense/Backup/GitSettings.xml @@ -33,6 +33,10 @@ master Y + + 0 + Y + N