Article directory
I. Introduction
-
reason
While working, in the hot update iteration branch of the project, the code of a certain category A of the main project was modified, and the hot update project also used the latest category A code of the main project.
Then during hot update, the latest hot update project will be imported into the player's mobile phone as a dll, but the main project code on the player's mobile phone does not existThe latest modified code of Category A. To put it simply: the latest hot update code does not match the main project code on the player's mobile phone, so the hot update will fail and an error will occur.
This will lead to a delay in the server opening time and will be criticized. . .
-
Purpose of this document
In case it happens again next time, I need a non-Master branch (hot update branch) to remind me when submitting the main project code The function of waiting for manual confirmation before submitting
2. Set PreCommit of a project
(1) Steps
-
Enter the .git/hooks directory
-
Create a new pre-commit file, note there is no suffix
-
Edit pre-commit file
#!/bin/sh # 获取当前工作目录 repo_path=$(pwd) # 获取当前分支名称 current_branch=$(git branch --show-current) # 如果当前分支不是master分支,执行检测逻辑 if [ "$current_branch" != "master" ]; then # 获取待提交的文件列表 files_to_commit=$(git diff --cached --name-only) # 初始化变量 main_detected=0 # 检查每个待提交文件的目录,查找名为Main的文件夹 for file_path in $files_to_commit; do if [[ $file_path == *"/Main/"* ]]; then echo "$file_path, 包含Main主工程的修改. 请修改它再提交" main_detected=1 fi done # 如果检测到Main文件夹,阻止提交 if [ $main_detected -eq 1 ]; then exit 1 fi fi # CodeBy liujianjie # 如果没有检测到Main文件夹,允许提交 exit 0
-
The mostimportant step: make the pre-commit file effective
-
In the folder of the pre-commit file, that is, under .git/hooks
-
Right click on the blank space and click git base here
-
Enter: chmod +x pre-commit and press Enter
-
(2) Test effect
-
test case
Main folder exists in the code path to be submitted
-
Go to the current folder that has the .git folder
-
Add and commit normally and you can see the effect.
3. Set PreCommit for multiple projects
-
illustrate
A project needs to set precommit in the project's .git/hooks
But if there are multiple projects, you need to set precommit in the hooks folder of each project, which will be very troublesome, so you need to set a unified precommit.
-
step
-
Create a new folder somewhere and copy the precommit hooks folder that has been set up
-
And follow the previous steps to set pre-commit to take effect.
-
Open cmd.exe and make this global folder serve as hooks for all git projects.
git config --global core.hooksPath G:\GitHook\hooks
-
-
Test sourceTree submission effect
4. FAQ
(1) If you have to submit the main project code of the hot update branch, how to submit it normally?
-
Explain the situation
After setting Precommit, as long as the submission path contains Main, the submission will be blocked
How to make this submission successful? -
Open the global hook or the pre-commit file under the current project's hook
You need to manually change sys.exit(1) to sys.exit(0)
will allow this submission to be committed successfully. Remember, after the commit is successful, will be modified back to sys.exit(1)
(2) How to solve the problem if there is a false alarm?
-
Explain the situation
If the submitted file path exists in the Main folder, but is not the Main path of the main project code, but Resource/Main or MyFloder/ How to solve the problem that files in the Main folder are blocked from submission
-
Open the global hook or the pre-commit file under the current project's hook
Change ‘/Main/' to the path of the main project in the specific project, such as: ‘Scripts/Main’
5. Related explanation Hooks links
https://zhuanlan.zhihu.com/p/521707440