Git sets PreCommit to submit the main project code in case of hot iteration - Windows environment

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

    Please add image description

  • Create a new pre-commit file, note there is no suffix

    image-20231102095710716

  • 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

    1. In the folder of the pre-commit file, that is, under .git/hooks

      image-20231102100121162

    2. Right click on the blank space and click git base here

      image-20231102101339292

    3. Enter: chmod +x pre-commit and press Enter

      Please add image description

(2) Test effect

  • test case

    Main folder exists in the code path to be submitted

    image-20231102100002343

  • Go to the current folder that has the .git folder

    Please add image description

  • Add and commit normally and you can see the effect.

    Please add image description

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

    1. Create a new folder somewhere and copy the precommit hooks folder that has been set up

      image-20231102101945685

    2. And follow the previous steps to set pre-commit to take effect.

      image-20231102101423955

    3. Open cmd.exe and make this global folder serve as hooks for all git projects.

      Please add image description

      git config --global core.hooksPath G:\GitHook\hooks
      
  • Test sourceTree submission effect

    image-20231102102345214

    image-20231102102407478

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

    image-20231103204806329

    You need to manually change sys.exit(1) to sys.exit(0)

    image-20231103204831686

    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

    image-20231103204927239

    Change ‘/Main/' to the path of the main project in the specific project, such as: ‘Scripts/Main’

    image-20231103204948327

5. Related explanation Hooks links

https://zhuanlan.zhihu.com/p/521707440

Guess you like

Origin blog.csdn.net/qq_34060370/article/details/134211120