Git Smudge操作后文件为空求助:分支配置文件处理异常
Let's break down why your team members are getting empty config files, and walk through the fixes step by step.
Core Issue: Your Script Doesn't Follow Git Filter's IO Rules
Git's clean and smudge filters work in a pipe-based system—they don't directly modify files on disk. Here's how they're supposed to function:
- Clean filter: When you run
git add, Git sends the working file's content to the script via standard input (stdin). The script must process this content and output the result to standard output (stdout)—Git stores this output in the repo. - Smudge filter: When you run
git checkout, Git sends the repo's stored content to the script via stdin. The script processes it and outputs the final content to stdout—Git writes this to your working directory file.
Your current script skips this entirely, instead searching for and modifying local files directly. This might work when you run it manually, but when Git calls it during checkout, the script outputs nothing to stdout—so Git writes an empty file to the workspace.
Other Critical Problems
1. Undefined Variables Break Script Execution
Your script uses variables like branche, backup_dir, log_file, util_dir, and branche_dir but never initializes them. You might have set these in your local environment, but your team members don't have them. When the script runs on their machines, it errors out and produces no output, leading to empty files.
2. Absolute Paths Aren't Portable
You've hardcoded the script path as /data/atl_sncftn/utils/util_git/util_lhr/cleanJob.sh in your .gitconfig. It's almost guaranteed this path doesn't exist (or lack execute permissions) on your teammates' machines. Git can't run the script at all, so it creates empty files by default.
3. Git 1.7.1 Is Severely Outdated
Git 1.7.1 is a 2010-era version with known bugs and limited filter support. Older versions handle script IO errors poorly, and some filter behaviors have been fixed in newer releases. Upgrading would eliminate many edge-case issues.
Fixes to Implement
Step 1: Rewrite the Script to Follow Git Filter Standards
Here's a refactored version that uses stdin/stdout correctly, handles variables, and works with Git's filter flow:
#!/bin/bash # Initialize variables branche=$(git rev-parse --abbrev-ref HEAD) util_dir="${UTIL_DIR:-./utils}" source_conf="${branche}_conf.txt" file_type="job.properties" # Handle -d flag for dev configs while getopts "d" opt; do case $opt in d) source_conf="${branche}_dev_conf.txt" ; file_type="job_dev.properties" ;; esac done # Read Git's input into a temp file temp_file=$(mktemp) cat > "$temp_file" # Apply config overrides if the conf file exists if [[ -f "${util_dir}/conf/${source_conf}" ]]; then while IFS='=' read -r key value; do # Skip comments and empty lines [[ "$key" =~ ^#.* || -z "$key" ]] && continue sed -i "s|^$key=.*|$key=$value|g" "$temp_file" done < "${util_dir}/conf/${source_conf}" fi # Output processed content to stdout (Git writes this to the workspace) cat "$temp_file" # Clean up temp file rm -f "$temp_file"
Note: For the clean filter, you'll want reverse logic—replace environment-specific values with placeholders so the repo stores a generic version of the config.
Step 2: Fix Paths and Variable Initialization
- Replace absolute paths with relative paths (e.g.,
./utils/cleanJob.sh) and commit the script to your repo so all team members get it. - Initialize all variables in the script instead of relying on external environment variables.
- Ensure the script has execute permissions:
chmod +x cleanJob.sh
Step 3: Standardize Team Git Configs
Avoid global .gitconfig settings—use the repo's local .git/config instead. Have your team run these commands in the repo:
git config --local filter.LHRJob.clean "./utils/cleanJob.sh" git config --local filter.LHRJob.smudge "./utils/cleanJob.sh" git config --local filter.LHRJobDev.clean "./utils/cleanJob.sh -d" git config --local filter.LHRJobDev.smudge "./utils/cleanJob.sh -d"
Alternatively, commit a .gitconfig template to the repo and have teammates copy it to their local .git/config.
Step 4: Upgrade Git (Highly Recommended)
Git 1.7.1 is way past its end of life. Have your team upgrade to a modern stable version (2.x or later)—this will fix many old filter bugs and improve reliability.
Validation Steps
- Delete your local
job.propertiesandjob_dev.properties, then rungit checkout HEAD -- job.properties job_dev.properties—verify the files have the correct content. - Ask a teammate to pull the updated code, set up the local Git config, and run the same checkout command to confirm their files are no longer empty.
内容的提问来源于stack exchange,提问作者Armand Berges

