From b242dc303151af72ca33c80980b80264f2c60c8a Mon Sep 17 00:00:00 2001 From: giveen Date: Wed, 14 Jan 2026 16:03:21 -0700 Subject: [PATCH] scripts: backup untracked dir and safely add subtree when forced --- scripts/add_metasploit_subtree.sh | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/scripts/add_metasploit_subtree.sh b/scripts/add_metasploit_subtree.sh index ba08656..82592f4 100644 --- a/scripts/add_metasploit_subtree.sh +++ b/scripts/add_metasploit_subtree.sh @@ -43,14 +43,29 @@ if [ -d "${PREFIX}" ]; then echo "Or run manually: git subtree pull --prefix=\"${PREFIX}\" ${REPO_URL} ${BRANCH} --squash" fi else - # Directory exists but not tracked by git; attempt to add subtree into it instead - echo "Directory ${PREFIX} exists but is not tracked in git; adding subtree into it..." - mkdir -p "$(dirname "${PREFIX}")" - if git subtree add --prefix="${PREFIX}" "${REPO_URL}" "${BRANCH}" --squash; then - echo "MetasploitMCP subtree added under ${PREFIX}." + # Directory exists but not tracked by git. + echo "Directory ${PREFIX} exists but is not tracked in git." + if [ "${FORCE_SUBTREE_PULL:-false}" = "true" ]; then + echo "FORCE_SUBTREE_PULL=true: backing up existing directory and attempting to add subtree..." + BACKUP="${PREFIX}.backup.$(date +%s)" + mv "${PREFIX}" "${BACKUP}" || { echo "Failed to move ${PREFIX} to ${BACKUP}" >&2; exit 1; } + # Ensure parent exists after move + mkdir -p "$(dirname "${PREFIX}")" + if git subtree add --prefix="${PREFIX}" "${REPO_URL}" "${BRANCH}" --squash; then + echo "MetasploitMCP subtree added under ${PREFIX}." + echo "Removing backup ${BACKUP}." + rm -rf "${BACKUP}" + else + echo "Failed to add subtree from ${REPO_URL}. Restoring backup." >&2 + rm -rf "${PREFIX}" || true + mv "${BACKUP}" "${PREFIX}" || { echo "Failed to restore ${BACKUP} to ${PREFIX}" >&2; exit 1; } + exit 1 + fi else - echo "Failed to add subtree from ${REPO_URL}." >&2 - echo "If this is unexpected, remove or rename ${PREFIX} and retry, or override the repo with METASPLOIT_SUBTREE_REPO." >&2 + echo "To add the subtree into the existing directory, either remove/rename ${PREFIX} and retry," + echo "or run with FORCE_SUBTREE_PULL=true to back up and add:" + echo " FORCE_SUBTREE_PULL=true bash scripts/add_metasploit_subtree.sh" + echo "Or override the repo with METASPLOIT_SUBTREE_REPO to use a different source." exit 1 fi fi