Article directory
- I. Introduction
- 2. Set PreCommit of a project
-
- (1) Steps
- (2) Test effect
- 3. Set PreCommit for multiple projects
- 4. FAQ
-
- (1) If you have to submit the main project code of the hot update branch, how to submit it normally?
- (2) How to solve the problem if there is a false alarm?
- 5. Related explanation Hooks links
1. Preface
-
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 the hot update, the latest hot update project is imported into the player’s mobile phone as a dll, but the latest modified Class A code does not exist in the main project code on the player’s mobile phone. 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
To prevent it from happening again next time, I need a function that can remind me when submitting the main project code in the non-Master branch (hot update branch) and wait 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 # Get the current working directory repo_path=$(pwd) # Get the current branch name current_branch=$(git branch --show-current) # If the current branch is not the master branch, execute the detection logic if [ "$current_branch" != "master" ]; then # Get the list of files to be submitted files_to_commit=$(git diff --cached --name-only) # Initialize variables main_detected=0 # Check the directory of each file to be submitted and look for a folder named Main for file_path in $files_to_commit; do if [[ $file_path == *"/Main/"* ]]; then echo "$file_path, contains modifications to the Main project. Please modify it before submitting" main_detected=1 fi done # If Main folder is detected, prevent submission if [ $main_detected -eq 1 ]; then exit 1 fi fi #CodeBy liujianjie # If the Main folder is not detected, allow submission exit 0
-
The most important 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-change branch, how to submit it normally
-
Explain the situation
After setting up Precommit, as long as the submitted 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)
This will allow this submission to be committed successfully. Remember to modify back to sys.exit(1) after the commit is successful.
(2) How to solve the problem if there is a false alarm
-
Explain the situation
What should I do if the submitted file path exists in the Main folder, but is not the Main path of the main project code, but the file in the Resource/Main or MyFloder/Main folder is 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 link
https://zhuanlan.zhihu.com/p/521707440