fix: handle non-git installations in update mode

- Added fallback to re-initialize git repository if .git directory missing
- Automatically detects current version from package.json
- Attempts to match to correct release branch
- Falls back to main branch if version detection fails
- Prevents update failures for legacy installations
This commit is contained in:
Muhammad Ibrahim
2025-10-28 19:21:11 +00:00
parent f6d23e45b2
commit dae536e96b

View File

@@ -2918,11 +2918,37 @@ update_installation() {
print_info "Installation directory: $instance_dir"
print_info "Service name: $service_name"
# Verify it's a git repository
# Verify it's a git repository, if not, initialize it
if [ ! -d "$instance_dir/.git" ]; then
print_error "Installation directory is not a git repository"
print_error "Cannot perform git-based update"
exit 1
print_warning "Installation directory is not a git repository"
print_info "Attempting to re-initialize as git repository..."
cd "$instance_dir" || exit 1
# Initialize git repository
git init
git remote add origin https://github.com/PatchMon/PatchMon.git
# Fetch all branches
git fetch origin
# Try to determine current version from package.json or default to main
local current_branch="main"
if [ -f "$instance_dir/backend/package.json" ]; then
local pkg_version=$(grep '"version"' "$instance_dir/backend/package.json" | head -1 | sed 's/.*"version": "\(.*\)".*/\1/')
if [ -n "$pkg_version" ]; then
# Check if there's a release branch for this version
if git ls-remote --heads origin | grep -q "release/$(echo $pkg_version | sed 's/\./-/g')"; then
current_branch="release/$(echo $pkg_version | sed 's/\./-/g')"
fi
fi
fi
# Reset to the determined branch
git reset --hard "origin/$current_branch"
git checkout -B "$current_branch" "origin/$current_branch"
print_success "Repository initialized successfully"
fi
# Add git safe.directory to avoid ownership issues when running as root