编辑:以下是一种幼稚的“第一种方法”,无法验证为可行的解决方案。
这个问题要复杂得多,我会尝试改进我的方法,但这不是当前的优先事项。
我要解决这个问题的方法是将“唯一”文件或目录软链接到 .local 文件夹中的文件或目录。
第一次创建这个文件夹的结构-在创建其他分支之前在master分支-使用以下命令:
cd ./$(git rev-parse --show-cdup) && mkdir -p .local/$(echo "$USER")/$(git symbolic-ref --short -q HEAD)
此命令将在存储库的根目录中创建树:
.local/<username>/<git branch>.
存储库的“唯一”文件必须移动到此树下的相关目录,并且必须进行软链接。
例如:
独特的文件
./path/to/file/<my_file>
将手动移至:
./.local/<username>/<git branch>/path/to/file/<my_file>
同时将在旧位置手动创建符号链接以链接到新位置。
"./path/to/file/<my_file>" -> "./.local/<username>/<git branch>/path/to/file/<my_file>"
符号链接也可以添加到 .gitignore 文件中:
path/to/file/<my_file>
如果其他分支已经存在,则必须从 master 分支中检出 .local 文件夹,并且必须为当前分支手动重复上述过程。
从现在开始有两个脚本叫做 post-merge 和 post-checkout in:
.git/hooks/post-merge
.git/hooks/post-checkout
如果权限设置为 755,则有望在每次合并后更新符号链接。
在第一次拉取/合并期间,它将创建一个本地环境,其中将复制和符号链接来自原始存储库的所有“唯一”文件。
用户必须删除或相应地安排 .local 子文件夹,然后链接将在需要时由主要使用 sed 的 bash 编写的脚本自动更新。
由于时间限制,我没有彻底测试脚本。它似乎在我的 Fedora 19 环境中工作,如果必须进行更改,我会更新帖子。
我仍然非常欢迎更面向 git 的解决方案。
合并后:
#! /bin/sh
# "origin": Refers to the existing merged files (soft links) information that will be overriden.
# "local": Refers to the local system information that will replace origin information.
#Get local user name to define the repository directory.
local_user=$(echo "$USER")
#Get local branch name to define the branch directory.
local_branch=$(git symbolic-ref --short -q HEAD)
#Go to the root of the repository
cd ./$(git rev-parse --show-cdup)
#Create local environment
local_environment=$(echo './.local/'"$local_user"'/'"$local_branch"'/')
mkdir -p $local_environment
#Grub and manipulate all soft links.
symlink_no=0
for link in $(find -L ./ -xtype l);
do
symlink_no=$[symlink_no+1]
# Grub information from each symlink
source_file=$(ls -la $link | sed -n 's/^.*[0-9]\+:[0-9]\+\s\(.*\)\s->.*$/\1/p')
origin_target_file=$(ls -la $link | sed -n 's/.*>\s\+\(.*\)$/\1/p')
relative_to_root_origin_target_file=$(ls -la $link | sed -n 's/.*>\s.*\(.\/.local.*\)$/\1/p')
find_origin_user='s/.*local\/\([a-zA-Z0-9._-]\+\).*/\1/p'
origin_user=$(echo $origin_target_file | sed -n "$find_origin_user")
find_origin_branch='s/.*local\/'"$origin_user"'\/\([a-zA-Z0-9._-]\+\).*/\1/p'
origin_branch=$(echo $origin_target_file | sed -n "$find_origin_branch")
local_target_file=$(echo $origin_target_file | sed -n 's/local\/'"$origin_user"'\/'"$origin_branch"'/local\/'"$local_user"'\/'"$local_branch"'/p')
relative_to_root_local_target_file=$(echo $relative_to_root_origin_target_file | sed -n 's/local\/'"$origin_user"'\/'"$origin_branch"'/local\/'"$local_user"'\/'"$local_branch"'/p')
remove_file_from_target_path='s/\(.*\)\/.*$/\1/p'
find_local_target_layout=$(echo $relative_to_root_origin_target_file | sed -n "$remove_file_from_target_path" | sed -n 's/.*'"$origin_branch"'\/\(.*$\)/\1/p')
local_environment_path=$(echo "$local_environment""$find_local_target_layout")
#Wording header.
echo ''
echo ''
echo 'Checking for symlink [' "$symlink_no"' ]:' "$source_file"
echo '------------------------------------------------------'
echo ''
# ------------------- Target File Manipulation ---------------------------------
#Check if target file does not exist in local environment.
if [ ! -f "$relative_to_root_local_target_file" ];
then
#If true, verify that the source file exists and copy it.
if [ -f "$relative_to_root_origin_target_file" ];
then
mkdir -p "$local_environment_path"
rsync -va "$relative_to_root_origin_target_file" "$relative_to_root_local_target_file"
echo ''
origin_target_file_exists=1
local_target_file_exists=1
echo 'The source file has been created: ' "$relative_to_root_local_target_file"
else
echo 'The source file does not exist: ' "$relative_to_root_origin_target_file"
origin_target_file_exists=0
local_target_file_exists=0
fi
else
local_target_file_exists=1
echo 'The local file "'"$relative_to_root_local_target_file" '" already exists.'
fi
source_path=$(echo $source_file | sed -n "$remove_file_from_target_path")
source_check_target=$(echo "$source_path"'/'"$local_target_file")
#-------------------- Source File Manipulation ---------------------------------
# If target file exists:
if [[ $local_target_file_exists -eq 1 ]];
then
#Check the source file if it is already linked.
if [ "$local_target_file" == "$origin_target_file" ];
then
#Is it a correct link?
if [ -f $source_check_target ];
then
echo 'symlink "' "$source_file" '->' "$local_target_file" '" already exists and it is correct.'
else
echo '*********** This is a broken link: ' "$source_file"
echo ' **********************'
fi
else
#Delete existing symlink
rm "$source_file"
#Create new symlink according to local environment
ln -s "$local_target_file" "$source_file"
echo 'symlink "' "$source_file" '->' "$local_target_file" '" has been created.'
fi
else
echo '*********** This is a broken link: ' "$source_file"
echo ' **********************'
fi
done
结帐后:
#! /bin/sh
# Start from the repository root.
cd ./$(git rev-parse --show-cdup)
# Delete .pyc files and empty directories.
find . -name "*.pyc" -delete
#find . -type d -empty -delete
#----------------Symlink manipulation---------------------------------------
#Get local user name to define the repository directory.
local_user=$(echo "$USER")
#Get local branch name to define the branch directory.
local_branch=$(git symbolic-ref --short -q HEAD)
#Go to the root of the repository
cd ./$(git rev-parse --show-cdup)
#Grub and manipulate all soft links.
symlink_no=0
for link in $(find -L ./ -xtype l);
do
symlink_no=$[symlink_no+1]
# Grub information from each symlink
source_file=$(ls -la $link | sed -n 's/^.*[0-9]\+:[0-9]\+\s\(.*\)\s->.*$/\1/p')
origin_target_file=$(ls -la $link | sed -n 's/.*>\s\+\(.*\)$/\1/p')
relative_to_root_origin_target_file=$(ls -la $link | sed -n 's/.*>\s.*\(.\/.local.*\)$/\1/p')
find_origin_user='s/.*local\/\([a-zA-Z0-9._-]\+\).*/\1/p'
origin_user=$(echo $origin_target_file | sed -n "$find_origin_user")
find_origin_branch='s/.*local\/'"$origin_user"'\/\([a-zA-Z0-9._-]\+\).*/\1/p'
origin_branch=$(echo $origin_target_file | sed -n "$find_origin_branch")
local_target_file=$(echo $origin_target_file | sed -n 's/local\/'"$origin_user"'\/'"$origin_branch"'/local\/'"$local_user"'\/'"$local_branch"'/p')
relative_to_root_local_target_file=$(echo $relative_to_root_origin_target_file | sed -n 's/local\/'"$origin_user"'\/'"$origin_branch"'/local\/'"$local_user"'\/'"$local_branch"'/p')
remove_file_from_target_path='s/\(.*\)\/.*$/\1/p'
find_local_target_layout=$(echo $relative_to_root_origin_target_file | sed -n "$remove_file_from_target_path" | sed -n 's/.*'"$origin_branch"'\/\(.*$\)/\1/p')
local_environment_path=$(echo "$local_environment""$find_local_target_layout")
source_path=$(echo $source_file | sed -n "$remove_file_from_target_path")
source_check_target=$(echo "$source_path"'/'"$local_target_file")
#Wording header.
echo ''
echo ''
echo 'Checking for symlink [' "$symlink_no"' ]:' "$source_file"
echo '------------------------------------------------------'
echo ''
# ------------------- Target File Manipulation ---------------------------------
#Check if target file does not exist in local environment.
if [ -f "$relative_to_root_local_target_file" ];
then
#Check the source file if it is already linked.
if [ "$local_target_file" == "$origin_target_file" ];
then
#Is it a correct link?
if [ -f $source_check_target ];
then
echo 'symlink "' "$source_file" '->' "$local_target_file" '" already exists and it is correct.'
else
echo '*********** This is a broken link: ' "$source_file"
echo ' **********************'
fi
else
#Delete existing symlink
rm "$source_file"
#Create new symlink according to local environment
ln -s "$local_target_file" "$source_file"
echo 'symlink "' "$source_file" '->' "$local_target_file" '" has been created.'
fi
else
echo 'The source file does not exist: ' "$relative_to_root_local_target_file"
fi
done