10

第一个问题......甚至可以用 git 完成这个吗?:)

我想要的是这样的:

有时,出于自己的调试目的,我将代码中的一个变量切换为true( )。localMode = true;但这不应该被提交。我应该只提交变量设置为false. 当然,有时我会忘记做出这种改变。如果我要提交“错误”代码,git 是否有可能以某种方式停止或警告我?

UPD:
感谢您的帮助!我最终得到了以下 shell 脚本:

#!/bin/bash
git diff --cached --name-only | while read FILE; do
if [[ $(echo "$FILE" | grep -E "^.+main\-controller\.js$") ]]; then
    content=$(<"$FILE")
    if [[ $(echo "$content" | grep -E "rootScope\.localMode = true") ]]; then   
        echo -e "\e[1;31m\tCommit contains localMode set to true.\e[0m" >&2
        exit 1
    fi
fi
done
4

4 回答 4

5

下面的链接呢?

http://wadmiraal.net/lore/2014/07/14/how-git-hooks-made-me-a-better-and-more-lovable-developer/

我认为您可以添加一些正则表达式来检测localMode = true链接处的示例代码。

于 2014-11-18T11:08:12.377 回答
5

是的,您可以使用pre-commit挂钩。

pre-commit只需在“.git/hooks”文件夹中放置一个名为(不带扩展名)的 shell 脚本,其中包含检查变量的逻辑以及:

  • 将其更改为false并继续提交或
  • 打印一条消息,告诉用户手动更正该值,并以非零代码退出以中止提交

hooks 文件夹应该包含一些示例,例如“pre-commit.sample”,您可能会觉得这些示例很有帮助。

文档

钩子首先pre-commit运行,甚至在您输入提交消息之前。它用于检查即将提交的快照、查看您是否忘记了某些内容、确保测试运行或检查您需要在代码中检查的任何内容。从这个钩子中退出非零会中止提交,尽管您可以使用 git commit --no-verify 绕过它。您可以检查代码样式(运行 lint 或类似的东西)、检查尾随空格(默认钩子正是这样做的)或检查有关新方法的适当文档。

于 2014-11-18T11:00:32.763 回答
2

这是为想要找到以前解决方案的替代品的人的更新。

现在有很多资源可以使用 git pre-commit hook 检查内容。

最“有名”的大概是https://pre-commit.com/

Git 钩子有时难以维护和共享(即使 git 2.9 引入了core.hooksPath使其更容易的配置)。

我主要使用 JavaScript,我发现了一个名为Husky的流行模块,它通过项目管理可共享的钩子。我喜欢它,因为它通过我的package.json.

我还试图找到一个补充模块来在提交之前检查我的内容,但我发现没有什么令人满意的。我想要类似于这样的共享配置(in package.json)的东西:

"precommit-checks": [
  {
    "filter": "\\.js$",
    "nonBlocking": "true",
    "message": "You’ve got leftover `console.log`",
    "regex": "console\\.log"
  },
  {
    "message": "You’ve got leftover conflict markers",
    "regex": "/^[<>|=]{4,}/m"
  },
  {
    "message": "You have unfinished devs",
    "nonBlocking": "true",
    "regex": "(?:FIXME|TODO)"
  }
]

我终于做了自己的:git-precommit-checks。如果你愿意,你可以尝试一下,否则你仍然可以寻找替代品(尤其是如果你不使用 JS)。

如果您仍想使用 bash 脚本检查您的内容,您可以尝试更通用的方法并循环遍历应该停止提交的搜索模式数组。

#! /bin/bash
# If you encounter any error like `declare: -A: invalid option`
# then you'll have to upgrade bash version to v4.
# For Mac OS, see http://clubmate.fi/upgrade-to-bash-4-in-mac-os-x/

# Hash using its key as a search Regex, and its value as associated error message
declare -A PATTERNS;
PATTERNS['^[<>|=]{4,}']="You've got leftover conflict markers";
PATTERNS['focus:\s*true']="You've got a focused spec";

# Declare empty errors array
declare -a errors;

# Loop over staged files and check for any specific pattern listed in PATTERNS keys
# Filter only added (A), copied (C), modified (M) files
for file in $(git diff --staged --name-only --diff-filter=ACM --no-color --unified=0); do
  for elem in ${!PATTERNS[*]} ; do
    { git show :0:"$file" | grep -Eq ${elem}; } || continue;
    errors+=("${PATTERNS[${elem}]} in ${file}…");
  done
done

# Print errors
# author=$(git config --get user.name)
for error in "${errors[@]}"; do
  echo -e "\033[1;31m${error}\033[0m"
  # Mac OS only: use auditable speech
  # which -s say && say -v Samantha -r 250 "$author $error"
done

# If there is any error, then stop commit creation
if ! [ ${#errors[@]} -eq 0 ]; then
  exit 1
fi
于 2019-01-31T09:11:52.453 回答
0

这是我根据较早的答案的看法:

#! /bin/bash
#
# This is a git hook. It exits with non-zero status and thus aborts
# a running `git commit` if the processed files contain undesirable PATTERNS.
#
# To enable this hook, rename this file to .git/hooks/pre-commit and run:
#   chmod a+x .git/hooks/pre-commit
# 
# To make it global:
#   git config --global core.hooksPath ~/git-central-hooks
#
# Source: https://stackoverflow.com/questions/26992576/how-to-make-a-git-pre-commit-code-check
# 
# Known problem:
# If you encounter error `declare: -A: invalid option` or similar upgrade bash 
# version to v4. For Mac OS, see http://clubmate.fi/upgrade-to-bash-4-in-mac-os-x/
#

# Declare empty arrays
declare -A PATTERNS
declare -a errors

# Customize it:  ['your grep pattern'] ===> "Error message when found"
PATTERNS['^[<>|=]{4,}']="You've got leftover CONFLICT markers"
PATTERNS['FIXME']="You've got FIXME hanging (consider changing to TODO)"

while read file 
do
  for elem in ${!PATTERNS[*]}
  do
    if git show :0:"$file" | grep -Eq "$elem"
    then
        errors+=( "${PATTERNS[${elem}]} in file '$file'" )
    fi
  done
# The post-loop expression generates only filenames added (A) or modified (M)
done < <( git diff --staged --name-only --diff-filter=AM --no-color --unified=0 )

# Print errors
for error in "${errors[@]}"
do
  echo -e "\033[1;31m${error}\033[0m"
  # Mac OS only: use auditable speech
  # author=$(git config --get user.name)
  # which -s say && say -v Samantha -r 250 "$author $error"
done

# Fail if there is an error
if [[ ${#errors[@]} -ne 0 ]]
then
  exit 1
fi
于 2020-02-07T13:59:44.330 回答