Skip to content

Instantly share code, notes, and snippets.

@cubxxw
Last active August 15, 2023 04:53
Show Gist options
  • Save cubxxw/126b72104ac0b0ca484c9db09c3e5694 to your computer and use it in GitHub Desktop.
Save cubxxw/126b72104ac0b0ca484c9db09c3e5694 to your computer and use it in GitHub Desktop.
自己的工作流总结,githook 设计和配置,以及 Makefile githook 支持

社区不规范怎么办

作为 OpenIM 社区首席运营官,对整个社区的 communtiy 以及 GitHub 配置仓库 进行了全面的配置。并且对整个 OpenIM 的 MakefileCICD 流,以及整个 OpenIM 使用的日志包 、错误码、协同流、贡献者文档以及 社区文档 进行架构和设计。

在这个时候总会有一些问题,即使你觉得自己的 贡献者文档 写的很牛逼了,很全面了,但是依旧很少有人愿意花心思去按照你写的规范去学习。这对我打造顶级的开源社区是一个非常大的阻碍,于是就有了今天的这个文档,我会将它记录在 GitHub Gists 上,提供拉取和使用的说明、链接,并且定期的维护它。

首先,我提供克隆的链接:

git clone https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694

使用方式: 我们提供了两种的使用方式,一种就是,很多项目都已经集成在 make 中,只要触发 make, 就会自动的触发 hook 脚本。当然,第一次使用的时候需要下载二进制 go-gitlint 并且放入到 _output/tools 目录下。 安装的方式是 make tools.verify.go-gitlint

Note: ⚠️ 注意,可能出现国内用户无法下载问题,我们提供了 cos 快捷下载方式,下载链接:

wget -P _output/tools/ https://openim-1306374445.cos.ap-guangzhou.myqcloud.com/openim/tools/go-gitlint

将二进制下载到 _output/tools/ 下面

如何设计

首先是针对基础的功能,那就是我们熟知的 commit 信息和 push 信息。

  • 我们可以对 commit 信息的格式进行设置
  • 我们可以对 push 的大小进行设置
  • 我们提供了 actions 的功能
  • 我们提供了 Makefile 标记和清除 Hook 的能力

我之前在学习 git 的时候写了一篇很全的笔记,并且分享在 GitHub 上面,在这个 🤖 链接 上可以学习到 git 很多高级用法。

这篇文章讲解了 git 有哪些规范,寻找合适的 CICD 流:

统一格式:

统一格式:git commit -m 'type(scope): 描述(#issue)'

我们在提交的时候带上邮箱信息 -s 来签证,这是一个很好的习惯。

git commit -s -m "..."

git commit艺术:

<类型>[可选 范围]: <描述>
[可选 正文]
[可选 脚注]

git commit提交类型可以是如下:

  1. feat:新功能(feature)
  2. fix:修补bug
  3. docs:文档(documentation)
  4. style: 格式(不影响代码运行的变动)
  5. refactor:重构(即不是新增功能,也不是修改bug的代码变动)
  6. test:增加测试
  7. chore:构建过程或辅助工具的变动
  8. perf:性能优化
  9. revert:回滚
  10. build:构建
  11. ci:持续集成

⚠️ 使用这个 Git hook 应该注意什么?

  • 这个 git hook 会在你不小心上传大文件的时候,阻止你,并且警告你,这是不可逆转的
  • 这个 git hook 会在你没有办法控制提交格式的时候,让你很痛苦。

分支规范

pre-commit 脚本会自动检查分支是否符合规范,新建的 branch 名要符合 Git Flow 工作流中的分支命名规则。否则,在 git commit 阶段,会因为 branch 不规范导致 commit 失败。

分支命名规则如下: image

Note: 当你使用 make 触发自动化脚本命令后,在项目根目录下执行 git commit 命令,git 会自动执行pre-commit脚本,该脚本会检查当前 branch 的名字是否符合规范。 make 的原理是通过 cp 的方式将 scripts/githooks/ 目录下的脚本复制到 .git/hooks/ 目录下,这样设计的原因是:git 不会提交 .git/hooks 目录下的 githooks 脚本,所以我们需要通过以下手段,确保开发者 clone 仓库之后,仍然能安装我们指定的 githooks 脚本到 .git/hooks 目录:

如何使用

你可以选择两种方式来使用 Git hook:

  1. 直接作为 .git/hooks文件夹中的 pre-commit 钩子。
  2. 使用 Husky 更新您的 package.json

Husky 使得 git hook 变的更加容易和方便:

"husky": {
    "hooks": {
      "pre-commit": "sh ./some-path/pre-commit-prevent-large-files.sh"
    }
}

这个钩子我是为 OpenIM 社区项目设计的,但是转念一想,反正都做成很方便和高端的 Github Gist 了,为何不用 Gist 的特性来对 Git Hook 做一个记录和保存,后期提供维护和下载,也提供了一个交流平台,这是非常 perfect 的。

所以我第一步,利用了 GitHub Gist 提供了一个下载的通道,这样以后有别的项目也可以直接一键使用脚本来拉取到本地。

不仅仅如此,我针对提交设计了一个脚本,我们使用脚本可以一键并且规范的 push 到远程仓库。

curl -L https://gist.githubusercontent.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694/raw/gitsync.sh |sh

默认限制为每个文件最大5MB。如果你觉得你的提交是一个特例,你可以使用以下命令覆盖这个限制:

GIT_FILE_SIZE_LIMIT=50000000 git commit -m "test: this commit is allowed file sizes up to 50MB"
# OR
git commit --no-verify

我不关心任何特定的文件,只是限制整个提交本身,这至少应该使开发人员的事情两次之前,他们可能会作出决定 git commit --no-verify

可复用脚本一键安装:

curl -L https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694/raw/install.sh |bash

Windows 平台下解决问题

如果没有 make:

在 Windows 下面 makefile 可能没办法正常使用,其实虽然使用报错,但是在 windows 下面依旧触发了 copy 的机制,就是将 ./scripts/githoooks/* 下面的内容 copy 到 .git/hooks/* 目录下。 如果并没有 copy 的源目录 ./scripts/githoooks/*,那么可以 clone 下面的 commit-msg, pre-commit, 以及 pre-push 的文件到 .git/hooks/* 目录下。

Windows 平台没办法使用 make tools 安装 go-gitlint:

我们的 git hooks 的 pre-commit 机制依靠 go-gitlint 来实现,在 Linux 下直接使用 make tools.verify.go-gitlint 来安装。但是可能这个命令在 Windows 没有 bash 的环境没办法执行。报错如下:

PS C:\Users\Administrator\GolandProjects\chat-dev> git commit -a -s -m "fix:reconstruct chat service"
OpenIM : Running local OpenIM pre-commit hook.
OpenIM : Running the OpenIM commit-msg hook.
.git/hooks/commit-msg: line 62: ./_output/tools/go-gitlint: No such file or directory
OpenIM : ./_output/tools/go-gitlint not found. Please run 'make tools' OR 'make tools.verify.go-gitlint' make verto install it.
OpenIM : Please fix your commit message to match kubecub coding standards
OpenIM : https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694#file-githook-md

我们可以这样解决这个问题:

从 GitHub release 直接下载:

Note: 如果你的仓库根目录暂时没有 _output/tools/, 你可以创建这个目录。

下载后的目录结构如下:

# tree _output/
_output/
├── bin
├── tmp
└── tools
    └── git-chglog

Contents

Note 点击右上角 Raw 进入脚本文件

可复用的安装脚本

#!/bin/sh
set -e
echo "Starting install script..."

SET_GIT_TEMPLATE_DIR=false
EXISTING_TEMPLATE=$(git config --global init.templateDir || echo "")
if [ -z "$EXISTING_TEMPLATE" ]; then
  echo "Creating a new global git template dir at ~/.git_template"
  mkdir ~/.git_template
  EXISTING_TEMPLATE="$(cd ~; pwd -P)/.git_template"
  SET_GIT_TEMPLATE_DIR=true
else
  EXISTING_TEMPLATE="$(eval cd $(dirname "$EXISTING_TEMPLATE"); pwd -P)/$(basename "$EXISTING_TEMPLATE")"
  echo "Using existing git template dir: $EXISTING_TEMPLATE"
fi

HOOKS_DIR="$EXISTING_TEMPLATE/hooks"
PRECOMMIT_HOOK="$HOOKS_DIR/pre-commit"
echo "Creating hooks dir if it doesn't already exist: $HOOKS_DIR"
mkdir -p "$HOOKS_DIR"
if [ -f "$PRECOMMIT_HOOK" ]; then
  echo "Cannot install hook as it's already defined: '$PRECOMMIT_HOOK'" >&2
  exit 1
fi

echo "Downloading the hook to $PRECOMMIT_HOOK"
curl -L https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694/raw/pre-commit -o "$PRECOMMIT_HOOK" 2> /dev/null
curl -L https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694/raw/commit-msg -o "$PRECOMMIT_HOOK" 2> /dev/null
curl -L https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694/raw/pre-push -o "$PRECOMMIT_HOOK" 2> /dev/null

echo "Making it executable"
chmod +x "$PRECOMMIT_HOOK"

if [ "$SET_GIT_TEMPLATE_DIR" = true ]; then
  echo "Defining ~/.git_template as the global git template dir"
  git config --global init.templateDir '~/.git_template'
fi

echo -e "\nDone! Any future git repo created in this user profile will contain the hook\n"


##################################################
GIT_PATH=$(git rev-parse --show-toplevel)
echo "===> Copying hooks to $GIT_PATH/.git/hooks/"
mv ~/.git_template/hooks/* $GIT_PATH/.git/hooks/

git commit 设置

pre-commit 文件:

#!/usr/bin/env bash

# Copyright © 2023 OpenIMSDK.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ==============================================================================
# This is a pre-commit hook that ensures attempts to commit files that are
# are larger than $limit to your _local_ repo fail, with a helpful error message.

# You can override the default limit of 2MB by supplying the environment variable:
# GIT_FILE_SIZE_LIMIT=50000000 git commit -m "test: this commit is allowed file sizes up to 50MB"
#
# ==============================================================================


YELLOW="\e[93m"
GREEN="\e[32m"
RED="\e[31m"
ENDCOLOR="\e[0m"

printMessage() {
   printf "${YELLOW}OpenIM : $1${ENDCOLOR}\n"
}

printSuccess() {
   printf "${GREEN}OpenIM : $1${ENDCOLOR}\n"
}

printError() {
   printf "${RED}OpenIM : $1${ENDCOLOR}\n"
}

printMessage "Running local OpenIM pre-commit hook."

# flutter format .
# https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694
# TODO! GIT_FILE_SIZE_LIMIT=50000000 git commit -m "test: this commit is allowed file sizes up to 50MB"
# Maximum file size limit in bytes
limit=${GIT_FILE_SIZE_LIMIT:-2000000} # Default 2MB
limitInMB=$(( $limit / 1000000 ))

function file_too_large(){
	filename=$0
	filesize=$(( $1 / 2**20 ))

	cat <<HEREDOC

	File $filename is $filesize MB, which is larger than github's maximum
        file size (2 MB). We will not be able to push this file to GitHub.
	Commit aborted

HEREDOC
    git status

}

# Move to the repo root so git files paths make sense
repo_root=$( git rev-parse --show-toplevel )
cd $repo_root

empty_tree=$( git hash-object -t tree /dev/null )

if git rev-parse --verify HEAD > /dev/null 2>&1
then
	against=HEAD
else
	against="$empty_tree"
fi

# Set split so that for loop below can handle spaces in file names by splitting on line breaks
IFS='
'

shouldFail=false
for file in $( git diff-index --cached --name-only $against ); do
	file_size=$(([ ! -f $file ] && echo 0) || (ls -la $file | awk '{ print $5 }'))
	if [ "$file_size" -gt  "$limit" ]; then
	    printError "File $file is $(( $file_size / 10**6 )) MB, which is larger than our configured limit of $limitInMB MB"
        shouldFail=true
	fi
done

if $shouldFail
then
    printMessage "If you really need to commit this file, you can override the size limit by setting the GIT_FILE_SIZE_LIMIT environment variable, e.g. GIT_FILE_SIZE_LIMIT=42000000 for 42MB. Or, commit with the --no-verify switch to skip the check entirely."
	  printError "Commit aborted"
    exit 1;
fi

commit-msg

#!/usr/bin/env bash

# Copyright © 2023 OpenIMSDK.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ==============================================================================
#
# Store this file as .git/hooks/commit-msg in your repository in order to
# enforce checking for proper commit message format before actual commits.
# You may need to make the script executable by 'chmod +x .git/hooks/commit-msg'.

# commit-msg use go-gitlint tool, install go-gitlint via `go get github.com/llorllale/go-gitlint/cmd/go-gitlint`
# go-gitlint --msg-file="$1"

# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message.  The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit.  The hook is allowed to edit the commit message file.

YELLOW="\e[93m"
GREEN="\e[32m"
RED="\e[31m"
ENDCOLOR="\e[0m"

printMessage() {
   printf "${YELLOW}OpenIM : $1${ENDCOLOR}\n"
}

printSuccess() {
   printf "${GREEN}OpenIM : $1${ENDCOLOR}\n"
}

printError() {
   printf "${RED}OpenIM : $1${ENDCOLOR}\n"
}

printMessage "Running the OpenIM commit-msg hook."

# This example catches duplicate Signed-off-by lines.

test "" = "$(grep '^Signed-off-by: ' "$1" |
	 sort | uniq -c | sed -e '/^[ 	]*1[ 	]/d')" || {
	echo >&2 Duplicate Signed-off-by lines.
	exit 1
}

./tools/go-gitlint \
	 --msg-file=$1 \
	 --subject-regex="^(build|chore|ci|docs|feat|feature|fix|perf|refactor|revert|style|test)(.*)?:\s?.*" \
    --subject-maxlen=150 \
    --subject-minlen=10 \
    --body-regex=".*" \
    --max-parents=1

if [ $? -ne 0 ]
then
    printError "Please fix your commit message to match OpenIM coding standards"
    printError "https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694"
    exit 1
fi

push

#!/usr/bin/env bash

YELLOW="\e[93m"
GREEN="\e[32m"
RED="\e[31m"
ENDCOLOR="\e[0m"

printMessage() {
   printf "${YELLOW}OpenIM : $1${ENDCOLOR}\n"
}

printSuccess() {
   printf "${GREEN}OpenIM : $1${ENDCOLOR}\n"
}

printError() {
   printf "${RED}OpenIM : $1${ENDCOLOR}\n"
}

printMessage "Running local OpenIM pre-push hook."

if [[ `git status --porcelain` ]]; then
  printError "This script needs to run against committed code only. Please commit or stash you changes."
  exit 1
fi

#
#printMessage "Running the Flutter analyzer"
#flutter analyze
#
#if [ $? -ne 0 ]; then
#  printError "Flutter analyzer error"
#  exit 1
#fi
#
#printMessage "Finished running the Flutter analyzer"

actions

git-warning.yml 文件:

# This workflow will check for large files being added in PRs
# and label the PR if one is found that exceeds the configured limit.
#
# For more information, see: https://github.com/marketplace/actions/lfs-warning

name: Large file size warning

on: 
  pull_request:
    # Ignore some files to avoid consuming Actions minutes unnecessarily 
    # for file types we're fairly confident we'll never need to worry about
    paths-ignore: 
      - '**.config'
      - '**.cs'
      - '**.cshtml'
      - '**.cs'
      - '**.csproj'
      - '**.cmd'
      - '**.dockerignore'
      - '**.gitignore'
      - '**.graphql'
      - '**.jsx?'
      - '**.md'
      - '**.props'
      - '**.ps1'
      - '**.scss'
      - '**.sh'
      - '**.sln'
      - '**.tsx?'
      - '**.yml'
      - '**/appsettings.*.json'
      - '**/Dockerfile'
      
jobs:
  run-check:
    runs-on: ubuntu-latest
    steps:
    - uses: actionsdesk/lfs-warning@v2.0
      with:
        #token: ${{ secrets.GITHUB_TOKEN }} # Optional
        filesizelimit: '5242880' # 5MB

嵌入到 Makefile 后,整个逻辑就更简单了:

# Copy githook scripts when execute makefile
COPY_GITHOOK:=$(shell cp -f script/githooks/* .git/hooks/; chmod +x .git/hooks/*)
#!/usr/bin/env bash
# Copyright © 2023 OpenIMSDK.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ==============================================================================
#
# Store this file as .git/hooks/commit-msg in your repository in order to
# enforce checking for proper commit message format before actual commits.
# You may need to make the script executable by 'chmod +x .git/hooks/commit-msg'.
# commit-msg use go-gitlint tool, install go-gitlint via `go get github.com/llorllale/go-gitlint/cmd/go-gitlint`
# go-gitlint --msg-file="$1"
# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message. The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit. The hook is allowed to edit the commit message file.
YELLOW="\e[93m"
GREEN="\e[32m"
RED="\e[31m"
ENDCOLOR="\e[0m"
printMessage() {
printf "${YELLOW}OpenIM : $1${ENDCOLOR}\n"
}
printSuccess() {
printf "${GREEN}OpenIM : $1${ENDCOLOR}\n"
}
printError() {
printf "${RED}OpenIM : $1${ENDCOLOR}\n"
}
printMessage "Running the OpenIM commit-msg hook."
# This example catches duplicate Signed-off-by lines.
test "" = "$(grep '^Signed-off-by: ' "$1" |
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
echo >&2 Duplicate Signed-off-by lines.
exit 1
}
./tools/go-gitlint \
--msg-file=$1 \
--subject-regex="^(build|chore|ci|docs|feat|feature|fix|perf|refactor|revert|style|test)(.*)?:\s?.*" \
--subject-maxlen=150 \
--subject-minlen=10 \
--body-regex=".*" \
--max-parents=1
if [ $? -ne 0 ]
then
printError "Please fix your commit message to match OpenIM coding standards"
printError "https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694"
exit 1
fi
### !/bin/bash
# @Description: git sync
# @Author: xiongxinwei 3293172751nss@gmail.com
# @LastEditTime: 2022-10-26 18:27:35
# @FilePath: \coded:\其他\测试文件夹\gitsync.sh
# Copyright (c) 2022 by xiongxinwei 3293172751nss@gmail.com, All Rights Reserved. @blog: http://nsddd.top
# 1. git add .
# 2. git commit -m "update"
# 3. git push
# 4. git pull
# Linux下可以直接使用crontab -e命令来编辑定时任务,Windows下可以使用计划任务来实现定时任务的功能。
# crontab -e
# 0 0 * * * /bin/bash /home/xxx/gitsync.sh
# 不推荐使用pull,因为pull会把本地的修改覆盖掉,如果本地有修改,应该先commit,然后再pull
# windows下面没有bash,所以需要使用git-bash来执行脚本
# 脚本代码在线浏览
# http://xxw.nsddd.top/code?p=%2Fgitsync.sh&id=AN16H9
# Linux一键下载
# wget -c -d -O gitsync.sh https://sm.nsddd.top/uploads/2022/10/27/Y0iHb6ix_gitsync.sh?attname=gitsync.sh && echo "gitsync.sh" >> .gitignore && sh gitsync.sh && chmod 777 gitsync.sh \
###
is_change=$(git status -s) # 判断是否有文件改动
# 当前分支
branch=$(git symbolic-ref --short -q HEAD)
# remark
if [ -n "$1" ]; then
guser=$1
else
# git.user.name
guser="$(git config user.name) update"
fi
if [ 0 -lt ${#is_change} ]; then # 有文件改动
echo "监测到程序改动,程序将会自动帮您同步⚡ 请稍等...(The program will automatically help you synchronize ⚡ Please wait...)"
echo " "
echo "Myblog: http://nsddd.top"
echo
echo "脚本地址在线浏览🧷: https://sm.nsddd.top/uploads/2022/10/26/2iCzooCq_gitsync.sh"
is_change=$(git status -s) # 判断是否有文件改动
# 输出git信息
git status && git remote -v && git branch -a
echo "是否选择提交类型(默认选择yes)?(Y/N)"
# 输入
read -p "=> " choice
# 判断
if [ $choice == "n" ] || [ $choice == "N" ]; then
echo "您选择了不输入提交类型,这将会省略commit的<类型>参数"
else
echo "⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️"
echo "1: feat: 新功能(feature)"
echo "2: fix: 修补bug"
echo "3: docs: 文档(documentation)"
echo "4: style: 格式(不影响代码运行的变动)"
echo "5: refactor: 重构(即不是新增功能,也不是修改bug的代码变动)"
echo "6: test: 增加测试"
echo "7: chore: 构建过程或辅助工具的变动"
echo "8: revert: 回滚"
echo "9: perf: 性能优化"
echo "⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️"
feat="feat"
fix="fix"
docs="docs"
style="style"
refactor="refactor"
test="test"
chore="chore"
revert="revert"
perf="perf"
echo "请输入提交类型(前面的序号): "
read -p "=> " type
echo "请输入对应的说明"
read -p "=> " desc
if [ $type == "1" ]; then
type=feat
type2=$feat
elif [ $type == "2" ]; then
type=fix
type2=$fix
elif [ $type == "3" ]; then
type=docs
type2=$docs
elif [ $type == "4" ]; then
type=style
type2=$style
elif [ $type == "5" ]; then
type=refactor
type2=$refactor
elif [ $type == "6" ]; then
type=test
type2=$test
elif [ $type == "7" ]; then
type=chore
type2=$chore
elif [ $type == "8" ]; then
type=revert
type2=$revert
elif [ $type == "9" ]; then
type=perf
type2=$perf
else
echo "输入错误,将会使用默认提交类型"
type=feat
type=$feat
fi
echo "<$type>您输入的提交类型为 💡$type2 对应的说明为 💡$desc"
fi
git add .
git commit -s -m "$type($desc): $guser" # -s 用于签名, -m 用于备注
# pull
result=$(git pull origin $branch)
tmp=$(echo $result | grep "fix conflicts")
if [ "$tmp" != "" ]
then
echo "(ノ=Д=)ノ┻━┻ 合并冲突, 请手动解决后提交"
else
# 推送并且保证推送成功
git push origin $branch
fi
# 判断推动是否成功
if [ $? -eq 0 ]; then
echo "⚠️ 推送成功"
else
echo "⚠️ 推送失败"
fi
else # 没有文件改动
echo "本地没有改变, 正在从远程仓库同步代码. 请耐心等待➡️➡️➡️ ...";
result=$(git pull origin $branch)
tmp=$(echo $result | grep "fix conflicts")
if [[ "$tmp" != "" ]]
then
echo "(ノ=Д=)ノ┻━┻ 合并冲突, 请手动解决后提交"
fi
fi
# 打印一条log日志
git log -q -1 --use-mailmap --source --mailmap # -q 用于简化输出, -1 用于显示最近1条, --use-mailmap 用于显示作者名, --source 用于显示提交类型, --mailmap 用于显示作者邮箱
#!/bin/sh
set -e
echo "Starting install script..."
SET_GIT_TEMPLATE_DIR=false
EXISTING_TEMPLATE=$(git config --global init.templateDir || echo "")
if [ -z "$EXISTING_TEMPLATE" ]; then
echo "Creating a new global git template dir at ~/.git_template"
mkdir ~/.git_template
EXISTING_TEMPLATE="$(cd ~; pwd -P)/.git_template"
SET_GIT_TEMPLATE_DIR=true
else
EXISTING_TEMPLATE="$(eval cd $(dirname "$EXISTING_TEMPLATE"); pwd -P)/$(basename "$EXISTING_TEMPLATE")"
echo "Using existing git template dir: $EXISTING_TEMPLATE"
fi
HOOKS_DIR="$EXISTING_TEMPLATE/hooks"
PRECOMMIT_HOOK="$HOOKS_DIR/pre-commit"
echo "Creating hooks dir if it doesn't already exist: $HOOKS_DIR"
mkdir -p "$HOOKS_DIR"
if [ -f "$PRECOMMIT_HOOK" ]; then
echo "Cannot install hook as it's already defined: '$PRECOMMIT_HOOK'" >&2
exit 1
fi
echo "Downloading the hook to $PRECOMMIT_HOOK"
curl -L https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694/raw/pre-commit -o "$PRECOMMIT_HOOK" 2> /dev/null
curl -L https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694/raw/commit-msg -o "$PRECOMMIT_HOOK" 2> /dev/null
curl -L https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694/raw/pre-push -o "$PRECOMMIT_HOOK" 2> /dev/null
echo "Making it executable"
chmod +x "$PRECOMMIT_HOOK"
if [ "$SET_GIT_TEMPLATE_DIR" = true ]; then
echo "Defining ~/.git_template as the global git template dir"
git config --global init.templateDir '~/.git_template'
fi
echo -e "\nDone! Any future git repo created in this user profile will contain the hook\n"
##################################################
GIT_PATH=$(git rev-parse --show-toplevel)
echo "===> Copying hooks to $GIT_PATH/.git/hooks/"
mv ~/.git_template/hooks/* $GIT_PATH/.git/hooks/
# Copy githook scripts when execute makefile
# TODO! GIT_FILE_SIZE_LIMIT=42000000 git commit -m "This commit is allowed file sizes up to 42MB"
COPY_GITHOOK:=$(shell cp -f script/githooks/* .git/hooks/; chmod +x .git/hooks/*)
#!/usr/bin/env bash
# Copyright © 2023 KubeCub & Xinwei Xiong(cubxxw). All rights reserved.
#
# Licensed under the MIT License (the "License");
# you may not use this file except in compliance with the License.
# ==============================================================================
# This is a pre-commit hook that ensures attempts to commit files that are
# are larger than $limit to your _local_ repo fail, with a helpful error message.
# You can override the default limit of 2MB by supplying the environment variable:
# GIT_FILE_SIZE_LIMIT=50000000 git commit -m "test: this commit is allowed file sizes up to 50MB"
#
# ==============================================================================
LC_ALL=C
local_branch="$(git rev-parse --abbrev-ref HEAD)"
valid_branch_regex="^(master|develop)$|(feature|release|hotfix)\/[a-z0-9._-]+$|^HEAD$"
YELLOW="\e[93m"
GREEN="\e[32m"
RED="\e[31m"
ENDCOLOR="\e[0m"
printMessage() {
printf "${YELLOW}kubecub : $1${ENDCOLOR}\n"
}
printSuccess() {
printf "${GREEN}kubecub : $1${ENDCOLOR}\n"
}
printError() {
printf "${RED}kubecub : $1${ENDCOLOR}\n"
}
printMessage "Running local kubecub pre-commit hook."
# flutter format .
# https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694#file-githook-md
# TODO! GIT_FILE_SIZE_LIMIT=50000000 git commit -m "test: this commit is allowed file sizes up to 50MB"
# Maximum file size limit in bytes
limit=${GIT_FILE_SIZE_LIMIT:-2000000} # Default 2MB
limitInMB=$(( $limit / 1000000 ))
function file_too_large(){
filename=$0
filesize=$(( $1 / 2**20 ))
cat <<HEREDOC
File $filename is $filesize MB, which is larger than github's maximum
file size (2 MB). We will not be able to push this file to GitHub.
Commit aborted
HEREDOC
git status
}
# Move to the repo root so git files paths make sense
repo_root=$( git rev-parse --show-toplevel )
cd $repo_root
empty_tree=$( git hash-object -t tree /dev/null )
if git rev-parse --verify HEAD > /dev/null 2>&1
then
against=HEAD
else
against="$empty_tree"
fi
# Set split so that for loop below can handle spaces in file names by splitting on line breaks
IFS='
'
shouldFail=false
for file in $( git diff-index --cached --name-only $against ); do
file_size=$(([ ! -f $file ] && echo 0) || (ls -la $file | awk '{ print $5 }'))
if [ "$file_size" -gt "$limit" ]; then
printError "File $file is $(( $file_size / 10**6 )) MB, which is larger than our configured limit of $limitInMB MB"
shouldFail=true
fi
done
if $shouldFail
then
printMessage "If you really need to commit this file, you can override the size limit by setting the GIT_FILE_SIZE_LIMIT environment variable, e.g. GIT_FILE_SIZE_LIMIT=42000000 for 42MB. Or, commit with the --no-verify switch to skip the check entirely."
printError "Commit aborted"
exit 1;
fi
if [[ ! $local_branch =~ $valid_branch_regex ]]
then
printError "There is something wrong with your branch name. Branch names in this project must adhere to this contract: $valid_branch_regex.
Your commit will be rejected. You should rename your branch to a valid name and try again."
printError "For more on this, read on: https://gist.github.com/cubxxw/126b72104ac0b0ca484c9db09c3e5694"
exit 1
fi
#!/usr/bin/env bash
YELLOW="\e[93m"
GREEN="\e[32m"
RED="\e[31m"
ENDCOLOR="\e[0m"
printMessage() {
printf "${YELLOW}OpenIM : $1${ENDCOLOR}\n"
}
printSuccess() {
printf "${GREEN}OpenIM : $1${ENDCOLOR}\n"
}
printError() {
printf "${RED}OpenIM : $1${ENDCOLOR}\n"
}
printMessage "Running local OpenIM pre-push hook."
if [[ `git status --porcelain` ]]; then
printError "This script needs to run against committed code only. Please commit or stash you changes."
exit 1
fi
#
#printMessage "Running the Flutter analyzer"
#flutter analyze
#
#if [ $? -ne 0 ]; then
# printError "Flutter analyzer error"
# exit 1
#fi
#
#printMessage "Finished running the Flutter analyzer"
@qidian99
Copy link

开发新 feature,修 bug 的分支没必要搞pre-commit hook,dx下降严重

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment