我想清理一个具有一年提交量(总共大约 4000 次)的存储库的历史记录。
-
一位贡献者一直不同意格式化标准,并多次更改 Prettier 配置文件,或者根本不使用 Prettier。结果,git 的历史就像一场外观变化的拉锯战,差异巨大,而真正的变化很难找到。
-
前端目录的名称在某个时候被重命名。我们从该目录中加载项目,这使得从 VSCode 访问早期的 git 历史记录变得很麻烦。
-
在某个时候添加了 TypeScript 转译器,生成file.js
and file.js.map
对于每一个file.ts
整个项目。这些文件生成的不一致(有时它们末尾有特定的注释,有时没有),这增加了 git 历史记录中的噪音。
我的暂定计划是重新调整一切:
我将保留存储库的备份以防万一,然后重新设置基准,在每次提交时执行以下操作:
- 应用一致的 Prettier 设置;
- 如有必要,重命名前端目录;
- 删除所有不需要的
file.js
and file.js.map
files.
然后我们的团队将转移到新的存储库。
具体来说:
GIT_SEQUENCE_EDIT=cat git rebase
--strategy recursive --strategy-option theirs --rebase-merges \
--exec '../cleanup.sh && git add . && git commit --amend --no-edit --no-verify --allow-empty' \
e709bcd1
where e709bcd1
是一个很好的起点,使用脚本cleanup.sh
:
#! /usr/bin/env zsh
setopt nullglob
echo $(git rev-parse HEAD) > commit.log
# If both directories exist, assume old_front_end is the real one,
# so delete new_front_end to allow us to rename old_front_end.
# (Otherwise, `mv` will move the one directory into the other.)
if [[ -d "old_front_end" ]] && [[ -d "new_front_end" ]]; then
rm -rf new_front_end
fi
# Rename old_front_end if necessary
if [[ -d "old_front_end" ]] && [[ ! -d "new_front_end" ]]; then
mv old_front_end new_front_end
fi
if [[ -d "new_front_end" ]]; then
# Clean up JS files
for file in "new_front_end/src/**/*.ts"; do
[[ ! -e $file ]] && continue # skip following if no such file
rm "${file%.*}.js"
rm "${file%.*}.js.map"
done
# Apply consistent Prettier settings
prettier --config ~/external_source_of_truth/.prettierrc -w "new_front_end/src/**/*.{js,ts,svelte,gql,css,scss}" || true
fi
问题:
- 我没有太多变基或编写 shell 脚本的经验。这是一个合理的计划吗?会带来不幸的后果吗?
- 我尝试运行该脚本,但它经常因合并冲突而卡住。看来我总是可以通过这样做来解决冲突
git add . && git rebase --continue
,但我不想这样做数百次。我可以自动化这个吗?
我使用解决了这个问题git filter-repo
以及相关工具lint-history
:
重命名前端目录:
git filter-repo --path-rename old_front_end/:new_front_end/
清理JS文件:
git filter-repo --force --filename-callback '
if filename.endswith(b".js"):
ts_file = filename[:-3] + b".ts"
if os.path.isfile(ts_file.decode("utf-8")):
return None
if filename.endswith(b".js.map"):
ts_file = filename[:-7] + b".ts"
if os.path.isfile(ts_file.decode("utf-8")):
return None
return filename
'
Prettier
lint-history --relevant '
return filename.endswith(b".ts") or filename.endswith(b".js")
or filename.endswith(b".svelte") or filename.endswith(b".css")
or filename.endswith(b".scss") or filename.endswith(b".gql")
' --filenames-important maybe-prettier
我需要编写一个名为的帮助程序脚本maybe-prettier
因为某些提交有带有未解析的合并符号的错误文件(<<<<<<<< HEAD
等等)。 Prettier 无法格式化这些文件并因错误退出,从而停止了进程filter-repo
。为了解决这个问题,maybe-prettier
首先检查文件是否能够格式化;它总是成功退出。
也许更漂亮
#! /usr/bin/env zsh
# Prettier gives three exit codes:
# 0 file is good
# 1 needs formatting
# 2 error
prettier --config ~/external_source_of_truth/.prettierrc -c $1
if [[ $? = 1 ]]; then
prettier --config ~/external_source_of_truth/.prettierrc -w $1
fi
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)