diff --git a/README.md b/README.md index f84f827..a64aa9c 100644 --- a/README.md +++ b/README.md @@ -207,8 +207,8 @@ journalctl -u shop -n 5 --no-pager Каталог клона — там, где лежат `package.json` и `scripts/` (часто `/opt/shop` или `/opt/shop/shop10`): ```bash -cd /opt/shop/shop10 # ваш путь к репозиторию -git config --global --add safe.directory "$(pwd)" +cd /opt/shop/shop10 +bash scripts/git-sync.sh # если detached HEAD или ошибка git pull bash scripts/server-update.sh ``` diff --git a/scripts/git-sync.sh b/scripts/git-sync.sh new file mode 100644 index 0000000..7c3b3f2 --- /dev/null +++ b/scripts/git-sync.sh @@ -0,0 +1,38 @@ +#!/bin/bash +# Синхронизация с origin/main (исправляет detached HEAD) +set -euo pipefail + +source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/shop-root.sh" + +if [ ! -d .git ]; then + echo "Ошибка: в $SHOP_ROOT нет .git — нужен полный clone:" + echo " git clone https://git.evilfox.cc/test/shop10.git /opt/shop/shop10" + exit 1 +fi + +git config --global --add safe.directory "$SHOP_ROOT" 2>/dev/null || true + +echo "=== git sync: $SHOP_ROOT ===" +git fetch origin + +if git symbolic-ref -q HEAD >/dev/null 2>&1; then + BRANCH=$(git branch --show-current) + echo "Текущая ветка: ${BRANCH:-?}" +else + echo "Состояние: detached HEAD ($(git rev-parse --short HEAD))" + BRANCH="" +fi + +if [ "$BRANCH" != "main" ]; then + if git show-ref --verify --quiet refs/remotes/origin/main; then + git checkout -B main origin/main + elif git show-ref --verify --quiet refs/heads/main; then + git checkout main + else + echo "Ветка main не найдена на origin" + exit 1 + fi +fi + +git pull origin main +echo "OK: $(git log -1 --oneline)" diff --git a/scripts/server-update.sh b/scripts/server-update.sh index 475b1b9..6153907 100644 --- a/scripts/server-update.sh +++ b/scripts/server-update.sh @@ -1,17 +1,23 @@ #!/bin/bash # Обновление на сервере: git pull, npm, restart shop -# Запуск из любого места: # bash /opt/shop/shop10/scripts/server-update.sh -# cd /opt/shop/shop10 && bash scripts/server-update.sh set -euo pipefail +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # shellcheck source=shop-root.sh -source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/shop-root.sh" +source "$SCRIPT_DIR/shop-root.sh" echo "=== Shop update: $SHOP_ROOT ===" -git config --global --add safe.directory "$SHOP_ROOT" 2>/dev/null || true -git pull +if [ ! -d .git ]; then + echo "Ошибка: $SHOP_ROOT не git-репозиторий (нет .git)." + echo "Если код только в /opt/shop/shop10 без git — переустановите:" + echo " mv /opt/shop/shop10 /opt/shop/shop10.bak" + echo " git clone https://git.evilfox.cc/test/shop10.git /opt/shop/shop10" + exit 1 +fi + +bash "$SCRIPT_DIR/git-sync.sh" npm install --omit=dev diff --git a/scripts/shop-root.sh b/scripts/shop-root.sh index 6a578c6..3c03840 100644 --- a/scripts/shop-root.sh +++ b/scripts/shop-root.sh @@ -1,31 +1,31 @@ #!/bin/bash -# Каталог репозитория (где package.json). Переопределение: SHOP_ROOT=/path +# Каталог репозитория (package.json + по возможности .git) +# Переопределение: SHOP_ROOT=/opt/shop/shop10 _resolve_shop_root() { - if [ -n "${SHOP_ROOT:-}" ] && [ -f "${SHOP_ROOT}/package.json" ]; then - return 0 - fi - local here - here="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" - if [ -f "${here}/package.json" ]; then - SHOP_ROOT="$here" - return 0 - fi - if [ -f /opt/shop/shop10/package.json ]; then - SHOP_ROOT=/opt/shop/shop10 - return 0 - fi - if [ -f /opt/shop/package.json ]; then - SHOP_ROOT=/opt/shop - return 0 - fi + local d + for d in \ + "${SHOP_ROOT:-}" \ + "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" \ + "/opt/shop/shop10" \ + "/opt/shop"; do + [ -z "$d" ] && continue + [ -f "${d}/package.json" ] && [ -d "${d}/.git" ] && SHOP_ROOT="$d" && return 0 + done + for d in \ + "${SHOP_ROOT:-}" \ + "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" \ + "/opt/shop/shop10" \ + "/opt/shop"; do + [ -z "$d" ] && continue + [ -f "${d}/package.json" ] && SHOP_ROOT="$d" && return 0 + done return 1 } if ! _resolve_shop_root; then echo "Ошибка: не найден каталог Shop (нет package.json)." - echo "Перейдите в каталог клона и запустите:" - echo " bash scripts/server-update.sh" - echo "Или задайте: SHOP_ROOT=/opt/shop/shop10 bash scripts/server-update.sh" + echo " git clone https://git.evilfox.cc/test/shop10.git /opt/shop/shop10" + echo " SHOP_ROOT=/opt/shop/shop10 bash scripts/server-update.sh" exit 1 fi diff --git a/wiki/Troubleshooting.md b/wiki/Troubleshooting.md index 8437a8c..ec5c77a 100644 --- a/wiki/Troubleshooting.md +++ b/wiki/Troubleshooting.md @@ -30,6 +30,28 @@ sudo bash scripts/quick-deploy-ubuntu.sh bash /opt/shop/shop10/scripts/server-update.sh ``` +## git: You are not currently on a branch / not a git repository + +Клон на теге (detached HEAD) или обновление из `/opt/shop` вместо `/opt/shop/shop10`: + +```bash +cd /opt/shop/shop10 +git status +bash scripts/git-sync.sh +bash scripts/server-update.sh +``` + +Если `fatal: not a git repository` в `/opt/shop/shop10`: + +```bash +ls -la /opt/shop/shop10/.git +# нет .git — заново: +mv /opt/shop/shop10 /opt/shop/shop10.bak.$(date +%s) +git clone https://git.evilfox.cc/test/shop10.git /opt/shop/shop10 +cp /opt/shop/shop10.bak.*/.env /opt/shop/shop10/.env 2>/dev/null || cp /opt/shop/shop10/.env.example /opt/shop/shop10/.env +bash /opt/shop/shop10/scripts/server-update.sh +``` + ## scripts/server-update.sh: No such file or directory Вы не в каталоге репозитория или клон в подпапке (например `/opt/shop/shop10`):