Files
heretek-openclaw-core/scripts/patch-create.sh
T
John Doe 762f51b890 docs: Create Heretek fork documentation and patch management system
- Add HERETEK_FORK.md with fork strategy and upstream sync workflow
- Add CHANGELOG_HERETEK.md tracking all Heretek-specific changes
- Create patches/ directory with README documentation
- Generate Phase 1 patch files:
  - a2a-protocol-infrastructure.patch
  - agent-lifecycle-steward-primary.patch
  - approval-system-liberation.patch
- Add patch management scripts:
  - patch-apply.sh - Apply all patches from .patchestoo
  - patch-create.sh - Create new patches from diffs
  - patch-status.sh - Show patch application status
  - upstream-sync.sh - Sync with upstream repository
- Add .patchestoo file listing patches in order
- Update package.json with patch-related npm scripts
- Add postinstall hook for automatic patch application

Phase 1 fixes include:
- A2A Protocol infrastructure (Redis messaging, Gateway, WebSocket bridge)
- Agent lifecycle improvements (auto-registration, heartbeat, /agent-status)
- Approval system liberation (auto-apply patches, approval bypass)
2026-04-01 12:53:16 -04:00

190 lines
5.9 KiB
Bash
Executable File

#!/bin/bash
# ==============================================================================
# Heretek OpenClaw Core - Patch Create Script
# ==============================================================================
# Creates a new patch file from the diff between modified files and upstream.
#
# Usage:
# ./scripts/patch-create.sh <patch-name> <file-path> [file-path...]
# ./scripts/patch-create.sh <patch-name> --all
#
# Examples:
# ./scripts/patch-create.sh my-fix gateway/openclaw-gateway.js
# ./scripts/patch-create.sh multi-file-fix file1.js file2.js file3.js
# ./scripts/patch-create.sh all-changes --all
# ==============================================================================
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(dirname "$SCRIPT_DIR")"
PATCHES_DIR="$ROOT_DIR/patches"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Check arguments
if [ $# -lt 2 ]; then
echo -e "${RED}Usage: $0 <patch-name> <file-path> [file-path...]${NC}"
echo " $0 <patch-name> --all"
echo ""
echo "Examples:"
echo " $0 my-fix gateway/openclaw-gateway.js"
echo " $0 multi-file-fix file1.js file2.js"
echo " $0 all-changes --all"
exit 1
fi
PATCH_NAME="$1"
shift
# Validate patch name (alphanumeric, hyphens, underscores only)
if ! [[ "$PATCH_NAME" =~ ^[a-zA-Z0-9_-]+$ ]]; then
echo -e "${RED}Error: Patch name must contain only alphanumeric characters, hyphens, and underscores${NC}"
exit 1
fi
PATCH_FILE="$PATCHES_DIR/${PATCH_NAME}.patch"
# Ensure patches directory exists
mkdir -p "$PATCHES_DIR"
echo -e "${BLUE}=============================================================================="
echo -e "Heretek OpenClaw Core - Patch Create"
echo -e "==============================================================================${NC}"
echo ""
echo -e "Patch name: ${YELLOW}$PATCH_NAME${NC}"
echo -e "Output file: ${YELLOW}$PATCH_FILE${NC}"
echo ""
# Generate patch header
cat > "$PATCH_FILE" << EOF
---
# ${PATCH_NAME}
# Heretek OpenClaw Core
# Date: $(date -u +"%Y-%m-%d")
#
# Generated by scripts/patch-create.sh
---
EOF
# Handle --all flag
if [ "$1" = "--all" ]; then
echo -e "${BLUE}Detecting all modified files...${NC}"
# Check if we're in a git repository
if git rev-parse --git-dir > /dev/null 2>&1; then
# Get list of modified files
MODIFIED_FILES=$(git diff --name-only HEAD)
if [ -z "$MODIFIED_FILES" ]; then
echo -e "${YELLOW}No modified files detected${NC}"
exit 0
fi
echo -e "${GREEN}Found modified files:${NC}"
echo "$MODIFIED_FILES" | while read -r file; do
echo " - $file"
done
echo ""
# Generate diff for all modified files
echo -e "${BLUE}Generating patch for all modified files...${NC}"
git diff HEAD >> "$PATCH_FILE"
else
echo -e "${RED}Error: Not in a git repository. Cannot detect modified files.${NC}"
echo "Please specify files explicitly or initialize git."
exit 1
fi
else
# Process specified files
FILES=("$@")
echo -e "${BLUE}Files to include in patch:${NC}"
for file in "${FILES[@]}"; do
echo " - $file"
if [ ! -f "$ROOT_DIR/$file" ]; then
echo -e "${YELLOW} Warning: File not found (will be marked as new file if in diff)${NC}"
fi
done
echo ""
# Generate diff for specified files
echo -e "${BLUE}Generating patch...${NC}"
for file in "${FILES[@]}"; do
FILE_PATH="$ROOT_DIR/$file"
if git rev-parse --git-dir > /dev/null 2>&1; then
# Use git diff if in git repo
if git diff HEAD -- "$file" >> "$PATCH_FILE" 2>/dev/null; then
echo -e "${GREEN}✓ Added: $file${NC}"
else
# File might be new (not tracked)
echo -e "${YELLOW}! New or untracked file: $file${NC}"
fi
else
# Not in git repo - create a simple diff format
if [ -f "$FILE_PATH" ]; then
echo "diff --git a/$file b/$file" >> "$PATCH_FILE"
echo "new file mode 100644" >> "$PATCH_FILE"
echo "--- /dev/null" >> "$PATCH_FILE"
echo "+++ b/$file" >> "$PATCH_FILE"
# Add file content with + prefix
while IFS= read -r line || [ -n "$line" ]; do
echo "+$line" >> "$PATCH_FILE"
done < "$FILE_PATH"
echo -e "${GREEN}✓ Added: $file (as new file)${NC}"
else
echo -e "${RED}✗ File not found: $file${NC}"
fi
fi
done
fi
# Check if patch file has content beyond header
PATCH_CONTENT=$(tail -n +8 "$PATCH_FILE" | wc -l)
if [ "$PATCH_CONTENT" -eq 0 ]; then
echo -e "${YELLOW}Warning: No changes detected in patch${NC}"
echo "The patch file may be empty or contain only the header."
rm -f "$PATCH_FILE"
exit 1
fi
echo ""
echo -e "${BLUE}=============================================================================="
echo -e "Patch Created Successfully"
echo -e "==============================================================================${NC}"
echo ""
echo -e "Output: ${GREEN}$PATCH_FILE${NC}"
echo -e "Size: $(wc -c < "$PATCH_FILE") bytes"
echo -e "Lines: $(wc -l < "$PATCH_FILE")"
echo ""
# Add to .patchestoo if not already present
PATCHLIST_FILE="$ROOT_DIR/.patchestoo"
if ! grep -q "$PATCH_NAME" "$PATCHLIST_FILE" 2>/dev/null; then
echo "patches/${PATCH_NAME}.patch" >> "$PATCHLIST_FILE"
echo -e "${GREEN}✓ Added to .patchestoo${NC}"
fi
echo ""
echo -e "${BLUE}To apply this patch:${NC}"
echo " ./scripts/patch-apply.sh"
echo ""
echo -e "${BLUE}To view this patch:${NC}"
echo " cat $PATCH_FILE"
echo ""
exit 0