Fix: Resolve relative path issue in backup script git operations

- Convert backup file path to absolute path before git add/commit
- Fixes 'pathspec did not match any files' error when using ../
- Use absolute_backup_path consistently in commit_to_git()
- Tested successfully: backups now commit to git without errors
This commit is contained in:
Kenji Morishige
2026-04-22 14:44:19 -05:00
parent c8fe188cca
commit 2f0a95674c

View File

@@ -212,15 +212,17 @@ validate_config() {
commit_to_git() {
log_info "Committing backup to git repository..."
local relative_path
relative_path=$(git -C "${REPO_ROOT}" ls-files --full-name "${BACKUP_FILE}" 2>/dev/null || echo "${BACKUP_FILE}")
# Convert to absolute path to avoid issues with relative paths containing ../
local absolute_backup_path
absolute_backup_path="$(cd "$(dirname "${BACKUP_FILE}")" && pwd)/$(basename "${BACKUP_FILE}")"
if [[ -z "${relative_path}" ]]; then
relative_path="${BACKUP_FILE}"
if [[ ! -f "${absolute_backup_path}" ]]; then
log_error "Backup file not found at: ${absolute_backup_path}"
return 1
fi
# Add the backup file to git
if git -C "${REPO_ROOT}" add "${BACKUP_FILE}"; then
# Add the backup file to git using absolute path
if git -C "${REPO_ROOT}" add "${absolute_backup_path}"; then
log_success "Added backup to git staging area"
else
log_error "Failed to add backup to git"
@@ -234,9 +236,9 @@ commit_to_git() {
# Get configuration summary from XML
local vlan_count
vlan_count=$(grep -c '<vlan>' "${BACKUP_FILE}" 2>/dev/null || echo "unknown")
vlan_count=$(grep -c '<vlan>' "${absolute_backup_path}" 2>/dev/null || echo "0")
local rule_count
rule_count=$(grep -c '<rule>' "${BACKUP_FILE}" 2>/dev/null || echo "unknown")
rule_count=$(grep -c '<rule>' "${absolute_backup_path}" 2>/dev/null || echo "0")
# Enhanced commit message with details
local detailed_msg="${commit_msg}
@@ -247,7 +249,7 @@ Contains:
- Backup timestamp: ${TIMESTAMP}
File: $(basename "${BACKUP_FILE}")
SHA256: $(shasum -a 256 "${BACKUP_FILE}" | awk '{print $1}')
SHA256: $(shasum -a 256 "${absolute_backup_path}" | awk '{print $1}')
"
# Commit the backup