Skip to content

Commit e801fa2

Browse files
committed
feat: Add git repository root path normalization for code blocks
Introduce path normalization logic to handle absolute file paths relative to the git repository root. This enhances file handling for markdown code block extraction and application, particularly for scripts operating across different repository contexts. Key improvements: - Add `get_git_root()` function to detect git repository root - Implement `normalize_path()` function to resolve absolute paths - Provide verbose warnings when not in a git repository - Improve path resolution for code block file targets
1 parent c858914 commit e801fa2

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

apply-md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ CONFIRM=false
1515
ONLY_FILES=""
1616
IGNORE_FILES=""
1717

18+
# Function to get git repository root
19+
get_git_root() {
20+
git rev-parse --show-toplevel 2>/dev/null
21+
}
22+
1823
# Parse arguments
1924
while [[ "$#" -gt 0 ]]; do
2025
case $1 in
@@ -49,6 +54,12 @@ while [[ "$#" -gt 0 ]]; do
4954
esac
5055
done
5156

57+
# Get git root if available
58+
GIT_ROOT=$(get_git_root)
59+
if [ -z "$GIT_ROOT" ] && [ "$VERBOSE" = true ]; then
60+
echo "Warning: Not in a git repository, absolute paths will be treated as system paths"
61+
fi
62+
5263
# Create backup directory if needed
5364
if [ "$BACKUP" = true ] && [ ! -d "$BACKUP_DIR" ]; then
5465
if [ "$DRY_RUN" = false ]; then
@@ -72,6 +83,18 @@ cleanup() {
7283
}
7384
trap cleanup EXIT
7485

86+
# Function to normalize path relative to git root
87+
normalize_path() {
88+
local path="$1"
89+
if [ -n "$GIT_ROOT" ] && [[ "$path" == /* ]]; then
90+
# Remove leading slash and resolve relative to git root
91+
local rel_path="${path#/}"
92+
echo "$GIT_ROOT/$rel_path"
93+
else
94+
echo "$path"
95+
fi
96+
}
97+
7598
# Extract code blocks and their file names
7699
extract_blocks() {
77100
local current_file=""
@@ -81,12 +104,13 @@ extract_blocks() {
81104
echo "$MARKDOWN" | while IFS= read -r line; do
82105
if [[ "$line" =~ ^[[:space:]]*\`\`\`([a-zA-Z0-9\+\-]+)[[:space:]]+(.+) ]]; then
83106
current_file="${BASH_REMATCH[2]}"
107+
normalized_file=$(normalize_path "$current_file")
84108
in_block=true
85109
temp_file="$TEMP_DIR/$current_file"
86110
mkdir -p "$(dirname "$temp_file")"
87111
: > "$temp_file" # Create empty file
88112
if [ "$VERBOSE" = true ]; then
89-
echo "Starting code block for file: $current_file"
113+
echo "Starting code block for file: $current_file (normalized: $normalized_file)"
90114
fi
91115
continue
92116
fi
@@ -137,7 +161,8 @@ apply_block() {
137161
fi
138162

139163
# Normalize file path
140-
file=$(realpath -m "$file" 2>/dev/null || echo "$file")
164+
local normalized_file=$(normalize_path "$file")
165+
file="$normalized_file"
141166

142167
if [ ! -f "$file" ]; then
143168
if [ "$CREATE_MISSING" = true ]; then
@@ -223,6 +248,9 @@ if [ "$VERBOSE" = true ]; then
223248
if [ "$DRY_RUN" = true ]; then
224249
echo "Running in DRY RUN mode (no changes will be applied)"
225250
fi
251+
if [ -n "$GIT_ROOT" ]; then
252+
echo "Git repository root detected at: $GIT_ROOT"
253+
fi
226254
fi
227255

228256
extract_blocks

0 commit comments

Comments
 (0)