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

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

    Please add image description

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

    image-20231102095710716

  • 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

    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-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

    image-20231103204806329

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

    image-20231103204831686

    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

    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 link

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