Git的Hooks机制

news/2025/2/12 3:56:35/

参考文章:详解如何在项目中使用git Hooks(husky、yorkie)

git hooks钩子

git hooks是一些自定义的脚本,用于控制git工作的流程,分为客户端钩子和服务端钩子。

 ~/work/step-time/ [master+*] ll .git/hooks 
total 128
-rwxr-xr-x  1 admin  staff   478B  8 19 18:52 applypatch-msg.sample
-rwxr-xr-x  1 admin  staff   896B  8 19 18:52 commit-msg.sample
-rwxr-xr-x  1 admin  staff   4.5K  8 19 18:52 fsmonitor-watchman.sample
-rwxr-xr-x  1 admin  staff   189B  8 19 18:52 post-update.sample
-rwxr-xr-x  1 admin  staff   424B  8 19 18:52 pre-applypatch.sample
-rwxr-xr-x  1 admin  staff   1.6K 11 12 17:42 pre-commit
-rwxr-xr-x  1 admin  staff   1.6K 11 12 17:00 pre-commit.sample.bck
-rwxr-xr-x  1 admin  staff   416B  8 19 18:52 pre-merge-commit.sample
-rwxr-xr-x  1 admin  staff   1.3K  8 19 18:52 pre-push.sample
-rwxr-xr-x  1 admin  staff   4.8K  8 19 18:52 pre-rebase.sample
-rwxr-xr-x  1 admin  staff   544B  8 19 18:52 pre-receive.sample
-rwxr-xr-x  1 admin  staff   1.5K  8 19 18:52 prepare-commit-msg.sample
-rwxr-xr-x  1 admin  staff   2.7K  8 19 18:52 push-to-checkout.sample
-rwxr-xr-x  1 admin  staff   3.6K  8 19 18:52 update.sample
 ~/work/step-time/ [master+*] 
 ~/work/step-time/ [master+*] 
 ~/work/step-time/ [master+*] 

.git.hooks目录下的就是勾子,想要运行她们只需要去除.sample后缀,并且把文件改成可执行的就可以了。

pre-commit.sample介绍

#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".# 获取提交的文件名
if git rev-parse --verify HEAD >/dev/null 2>&1
thenagainst=HEAD
else# Initial commit: diff against an empty tree objectagainst=$(git hash-object -t tree /dev/null)
fi# If you want to allow non-ASCII filenames set this variable to true.
allownonascii=$(git config --type=bool hooks.allownonascii)# Redirect output to stderr.
exec 1>&2# Cross platform projects tend to avoid non-ASCII filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&# Note that the use of brackets around a tr range is ok here, (it's# even required, for portability to Solaris 10's /usr/bin/tr), since# the square bracket bytes happen to fall in the designated range.## 获取改动的文件名,使用tr删除ascii表的0到126的字符,## 也就是计算non-ASCII 的字符数量test $(git diff --cached --name-only --diff-filter=A -z $against |LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
thencat <<\EOF
Error: Attempt to add a non-ASCII file name.This can cause problems if you want to work with people on other platforms.To be portable it is advisable to rename the file.If you know what you are doing you can disable this check using:git config hooks.allownonascii true
EOFexit 1
fi# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

http://www.ppmy.cn/news/1212675.html

相关文章

【进度条tqdm】python项目中告别丑陋的print伪进度条

在Python开发中&#xff0c;进度条是提高用户体验和可视化操作的重要组成部分。本文将为你介绍热门的Python进度条库tqdm&#xff0c;详细解释常见的使用场景&方法&#xff0c;同时提供代码示例。 1. tqdm库 tqdm是一个简单易用且功能强大的进度条库。首先&#xff0c;你…

【Java】Java8 Function 和 Consumer 接口的使用场景

文章目录 前言1. Function 示例2. Function 介绍3. Consumer 示例4. Consumer 介绍5. Function 和 Consumer 接口的使用场景后记 前言 在 《精通Java8》一书中有讲过 Java8的函数式接口可以简化设计模式的实施&#xff0c;这里记录一下Function 和 Consumer 的使用场景。 1. …

Kotlin基础——接口和类

接口 使用 : 表示继承关系&#xff0c;只能继承一个类&#xff0c;但可以实现多个接口override修饰符表示重写可以有默认方法&#xff0c;若父类的默认方法冲突&#xff0c;则需要子类重写&#xff0c;使用super<XXX>.xxx()调用某一父类方法 interface Focusable {fun …

深入理解Java中的OutOfMemoryError(OOM)异常

导言&#xff1a; 在Java开发中&#xff0c;我们经常会遇到程序抛出OutOfMemoryError异常的情况&#xff0c;这意味着程序在运行时无法继续分配所需的内存。这篇博客将深入探讨Java中的OOM异常&#xff0c;包括异常的种类、常见的引起OOM的原因以及如何诊断和处理这些问题。 1…

css sprite 的优缺点,使用方法和示例

CSS Sprite是一种网页图片应用处理方式。 CSS Sprite的原理是将一个网页或者一个模块所用到的零碎的icon整合拼接到一张大图里&#xff0c;再把这张大图作为背景图放入到网页中&#xff0c;当访问该页面时&#xff0c;加载的图片就不会像以前那样一幅一幅地慢慢显示出来了。 …

Angular 使用教程——基本语法和双向数据绑定

Angular 是一个应用设计框架与开发平台&#xff0c;旨在创建高效而精致的单页面应用 Angular 是一个基于 TypeScript 构建的开发平台。它包括&#xff1a;一个基于组件的框架&#xff0c;用于构建可伸缩的 Web 应用&#xff0c;一组完美集成的库&#xff0c;涵盖各种功能&…

LeetCode110. Balanced Binary Tree

文章目录 一、题目二、题解 一、题目 Given a binary tree, determine if it is height-balanced . Example 1: Input: root [3,9,20,null,null,15,7] Output: true Example 2: Input: root [1,2,2,3,3,null,null,4,4] Output: false Example 3: Input: root [] Output…

AI:80-基于深度学习的医学图像分割与病变识别

🚀 本文选自专栏:人工智能领域200例教程专栏 从基础到实践,深入学习。无论你是初学者还是经验丰富的老手,对于本专栏案例和项目实践都有参考学习意义。 ✨✨✨ 每一个案例都附带有在本地跑过的代码,详细讲解供大家学习,希望可以帮到大家。欢迎订阅支持,正在不断更新中,…