Skip to content

Commit fce706a

Browse files
feat: add sandbox reset with config preservation
• Add --preserve-config option to sandbox reset • Create reset-sandbox.sh helper script for in-sandbox resets • Add sandbox directory detection for running from within sandbox • Automatically update reset script during sandbox reset • Improve reset workflow with preserve/remove config options
1 parent 018f332 commit fce706a

File tree

2 files changed

+139
-14
lines changed

2 files changed

+139
-14
lines changed

scripts/labcommitr-sandbox.sh

Lines changed: 101 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,76 @@ find_existing_sandbox() {
4444
return 1
4545
}
4646

47+
# Function to detect if we're in a sandbox directory
48+
detect_sandbox_dir() {
49+
local current_dir="$(pwd)"
50+
local abs_current_dir="$(cd "$current_dir" && pwd)"
51+
52+
# Check if current directory is within .sandbox/
53+
if [[ "$abs_current_dir" == *"/.sandbox/"* ]]; then
54+
# Find the .sandbox base directory
55+
local sandbox_base="${abs_current_dir%/.sandbox/*}/.sandbox"
56+
57+
# Check if .sandbox directory exists
58+
if [ ! -d "$sandbox_base" ]; then
59+
return 1
60+
fi
61+
62+
# Get the path after .sandbox/
63+
local relative_path="${abs_current_dir#${sandbox_base}/}"
64+
65+
# Extract the first directory name (sandbox name)
66+
local sandbox_name="${relative_path%%/*}"
67+
68+
# Construct full sandbox path
69+
local sandbox_dir="$sandbox_base/$sandbox_name"
70+
71+
# Verify it's actually a git repository (sandbox marker)
72+
if [ -d "$sandbox_dir/.git" ]; then
73+
echo "$sandbox_dir"
74+
return 0
75+
fi
76+
fi
77+
return 1
78+
}
79+
4780
# Function to display usage
4881
show_usage() {
4982
echo "Usage: $0 [OPTIONS]"
5083
echo ""
5184
echo "Options:"
52-
echo " --reset Quick reset: reset git state without full recreation"
53-
echo " --clean Remove sandbox directory entirely"
54-
echo " --no-config Create sandbox without copying config (start from scratch)"
55-
echo " --help Show this help message"
85+
echo " --reset Quick reset: reset git state without full recreation"
86+
echo " --preserve-config Preserve .labcommitr.config.yaml during reset (use with --reset)"
87+
echo " --clean Remove sandbox directory entirely"
88+
echo " --no-config Create sandbox without copying config (start from scratch)"
89+
echo " --help Show this help message"
5690
echo ""
5791
echo "Examples:"
58-
echo " $0 # Create or recreate sandbox (with config if available)"
59-
echo " $0 --no-config # Create sandbox without config (run 'lab init' yourself)"
60-
echo " $0 --reset # Quick reset (faster, keeps repo structure)"
61-
echo " $0 --clean # Remove sandbox completely"
92+
echo " $0 # Create or recreate sandbox (with config if available)"
93+
echo " $0 --no-config # Create sandbox without config (run 'lab init' yourself)"
94+
echo " $0 --reset # Quick reset (faster, keeps repo structure)"
95+
echo " $0 --reset --preserve-config # Quick reset, preserve config file"
96+
echo " $0 --clean # Remove sandbox completely"
97+
echo ""
98+
echo "Note: Can be run from within sandbox directory or project root"
6299
}
63100

64101
# Parse arguments
65102
RESET_MODE=false
66103
CLEAN_MODE=false
67104
NO_CONFIG=false
105+
PRESERVE_CONFIG=false
68106

69107
while [[ $# -gt 0 ]]; do
70108
case $1 in
71109
--reset)
72110
RESET_MODE=true
73111
shift
74112
;;
113+
--preserve-config)
114+
PRESERVE_CONFIG=true
115+
shift
116+
;;
75117
--clean)
76118
CLEAN_MODE=true
77119
shift
@@ -112,8 +154,16 @@ fi
112154

113155
# Handle reset mode
114156
if [ "$RESET_MODE" = true ]; then
115-
SANDBOX_DIR=$(find_existing_sandbox)
116-
if [ -z "$SANDBOX_DIR" ]; then
157+
# Try to detect if we're in a sandbox directory first
158+
DETECTED_SANDBOX=$(detect_sandbox_dir)
159+
if [ -n "$DETECTED_SANDBOX" ] && [ -d "$DETECTED_SANDBOX" ]; then
160+
SANDBOX_DIR="$DETECTED_SANDBOX"
161+
else
162+
# Fall back to finding existing sandbox from project root
163+
SANDBOX_DIR=$(find_existing_sandbox)
164+
fi
165+
166+
if [ -z "$SANDBOX_DIR" ] || [ ! -d "$SANDBOX_DIR" ]; then
117167
echo -e "${RED}Error: No existing sandbox found. Run without --reset to create one.${NC}"
118168
exit 1
119169
fi
@@ -125,10 +175,25 @@ if [ "$RESET_MODE" = true ]; then
125175
echo -e "${GREEN}${NC} Resetting sandbox: $SANDBOX_DIR"
126176
cd "$SANDBOX_DIR"
127177

178+
# Preserve config if requested
179+
CONFIG_BACKUP=""
180+
if [ "$PRESERVE_CONFIG" = true ] && [ -f ".labcommitr.config.yaml" ]; then
181+
CONFIG_BACKUP=$(mktemp)
182+
cp ".labcommitr.config.yaml" "$CONFIG_BACKUP"
183+
echo -e "${GREEN}${NC} Preserving config file..."
184+
fi
185+
128186
# Reset git state
129187
git reset --hard HEAD 2>/dev/null || true
130188
git clean -fd 2>/dev/null || true
131189

190+
# Restore config if preserved
191+
if [ -n "$CONFIG_BACKUP" ] && [ -f "$CONFIG_BACKUP" ]; then
192+
cp "$CONFIG_BACKUP" ".labcommitr.config.yaml"
193+
rm "$CONFIG_BACKUP"
194+
echo -e "${GREEN}${NC} Config file restored"
195+
fi
196+
132197
# Ensure directories exist (they might have been removed by git clean)
133198
mkdir -p src docs lib utils
134199

@@ -201,6 +266,13 @@ EOF
201266
echo "# Pre-staged file" > pre-staged.ts
202267
git add pre-staged.ts
203268

269+
# Always ensure reset script is present and up-to-date
270+
if [ -f "$SCRIPT_DIR/reset-sandbox.sh" ]; then
271+
cp "$SCRIPT_DIR/reset-sandbox.sh" "reset-sandbox.sh"
272+
chmod +x "reset-sandbox.sh"
273+
echo -e "${GREEN}${NC} Reset script updated"
274+
fi
275+
204276
echo ""
205277
echo -e "${GREEN}${NC} Sandbox reset complete!"
206278
echo ""
@@ -210,6 +282,10 @@ EOF
210282
echo " cd $SANDBOX_DIR"
211283
echo " node $PROJECT_ROOT/dist/index.js commit"
212284
echo ""
285+
echo -e "${YELLOW}To reset again (from within sandbox):${NC}"
286+
echo " bash $SCRIPT_DIR/labcommitr-sandbox.sh --reset"
287+
echo " bash $SCRIPT_DIR/labcommitr-sandbox.sh --reset --preserve-config"
288+
echo ""
213289
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
214290
exit 0
215291
fi
@@ -254,6 +330,12 @@ else
254330
echo -e "${YELLOW}⚠ Sandbox created without config. Run 'lab init' to set up configuration.${NC}"
255331
fi
256332

333+
# Copy reset script for convenience
334+
if [ -f "$SCRIPT_DIR/reset-sandbox.sh" ]; then
335+
cp "$SCRIPT_DIR/reset-sandbox.sh" "$SANDBOX_DIR/reset-sandbox.sh"
336+
chmod +x "$SANDBOX_DIR/reset-sandbox.sh"
337+
fi
338+
257339
# Create initial commit
258340
echo -e "${GREEN}${NC} Creating initial commit structure..."
259341
cat > README.md << 'EOF'
@@ -457,14 +539,19 @@ echo -e "${YELLOW}To test:${NC}"
457539
echo " cd $SANDBOX_DIR"
458540
echo " node $PROJECT_ROOT/dist/index.js commit"
459541
echo ""
460-
echo -e "${YELLOW}To reset (quick):${NC}"
461-
echo " bash $SCRIPT_DIR/labcommitr-sandbox.sh --reset"
542+
echo -e "${YELLOW}To reset (from within sandbox):${NC}"
543+
echo " bash reset-sandbox.sh # Reset, remove config"
544+
echo " bash reset-sandbox.sh --preserve-config # Reset, keep config"
545+
echo ""
546+
echo -e "${YELLOW}To reset (from project root):${NC}"
547+
echo " pnpm run test:sandbox:reset # Reset, remove config"
548+
echo " bash $SCRIPT_DIR/labcommitr-sandbox.sh --reset --preserve-config # Reset, keep config"
462549
echo ""
463550
echo -e "${YELLOW}To reset (full recreation):${NC}"
464-
echo " bash $SCRIPT_DIR/labcommitr-sandbox.sh"
551+
echo " pnpm run test:sandbox"
465552
echo ""
466553
echo -e "${YELLOW}To clean up:${NC}"
467-
echo " bash $SCRIPT_DIR/labcommitr-sandbox.sh --clean"
554+
echo " pnpm run test:sandbox:clean"
468555
echo ""
469556
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
470557

scripts/reset-sandbox.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
3+
# Quick reset script for sandbox repositories
4+
# Can be run from within a sandbox directory
5+
# Usage: bash reset-sandbox.sh [--preserve-config]
6+
7+
# Check if we're in a sandbox directory
8+
CURRENT_DIR="$(pwd)"
9+
if [[ "$CURRENT_DIR" != *"/.sandbox/"* ]]; then
10+
echo "Error: This script must be run from within a sandbox directory"
11+
echo "Current directory: $CURRENT_DIR"
12+
echo ""
13+
echo "To reset from project root, use:"
14+
echo " pnpm run test:sandbox:reset"
15+
exit 1
16+
fi
17+
18+
# Find the project root by looking for .sandbox parent
19+
# Current dir is something like: /path/to/project/.sandbox/atom
20+
# We need to go up to find the project root
21+
SANDBOX_DIR="$CURRENT_DIR"
22+
PROJECT_ROOT="${SANDBOX_DIR%/.sandbox/*}"
23+
SCRIPT_DIR="$PROJECT_ROOT/scripts"
24+
25+
# Verify the script exists
26+
if [ ! -f "$SCRIPT_DIR/labcommitr-sandbox.sh" ]; then
27+
echo "Error: Could not find labcommitr-sandbox.sh script"
28+
echo "Expected at: $SCRIPT_DIR/labcommitr-sandbox.sh"
29+
exit 1
30+
fi
31+
32+
# Call the main sandbox script with reset flag
33+
if [ "$1" = "--preserve-config" ]; then
34+
bash "$SCRIPT_DIR/labcommitr-sandbox.sh" --reset --preserve-config
35+
else
36+
bash "$SCRIPT_DIR/labcommitr-sandbox.sh" --reset
37+
fi
38+

0 commit comments

Comments
 (0)