在预提交钩子中,我想确保没有人(除了 1 人)能够更改 reserved_file.txt
该钩子需要处理当有人执行'git pull'或'git merge some_branch',获取最新的restricted_file.txt但something_else.txt导致冲突的情况。
他解决了冲突并保留了“他们的”restricted_file.txt 版本,执行了“git add”和“git commit”。
此时,pre-commit 钩子被执行,它标识出restricted_file.txt 将被提交。我们希望仅当文件与来自 'git pull' 或 'git merge' 的文件相同时才允许提交(换句话说,如果他保留了“他们的”版本)。
有人知道在解决冲突期间是否可以确定文件是否与“他们的”相同?
相当于以下不存在的命令:
git diff --cached --against_theirs -- restricted_file.txt
Run Code Online (Sandbox Code Playgroud) 我有一个 Git 预提交钩子,它会去除空格并将修改后的文件保留在工作副本中,这样它就不会踩踏像git add -p.
如果我提交多个已更改文件中的一个,并且更正了空格,则我会在工作副本中更改两个文件和一个暂存文件(该文件也在工作副本中,但暂存更改存在空格错误):
vi fileWithBadWS.txt # leave bad whitespace
vi fileWithGoodWS.txt # don't leave bad whitespace
vi unrelatedFile.txt
git add fileWithBadWS.txt fileWithGoodWS.txt
git commit -m "Commited files, one with bad whitespace" # pre-commit hook fails
Run Code Online (Sandbox Code Playgroud)
仓库现在看起来像这样:
On branch master
Changes to be committed:
modified: fileWithBadWS.txt # bad WS
modified: fileWithGoodWS.txt
Changes not staged for commit:
modified: fileWithBadWS.txt # fixed WS
modified: unrelatedFile.txt
Run Code Online (Sandbox Code Playgroud)
我可以用:
git diff去看fileWithBadWS.txt并且unrelatedFile.txtgit diff --cached查看暂存文件fileWithBadWS.txt和 …Szenario:在使用git进行文件提交时,我想在预提交挂钩内检查某条线是否已被修改.因此,我想比较修改后的文件中的部分行与来自存储库的相应行部分.
背景:我想确保要提交的文件具有另一个版本号,而不是存储库中已有的文件.应强制用户为文件提供适当的版本号.在提交时自动增加版本号不是一个选项,因为我们使用多部分版本(1.0.0.0),用户必须修改版本字符串的正确部分...
问题:是否可以在预提交挂钩中访问已修改的文件内容以及存储库文件?
为什么git允许你承诺一个独立的头?有没有可以禁用它的预提交钩子?什么目的?许多新开发人员这样做,我想找到一种方法来禁用它.
我正在使用 GITpre-commit挂钩来查找 README.md 文件 ex 中的字符串模式。{{STRING_PATTERN}},然后将字符串模式更改为所需的文本/代码输出,其中还包含当前分支名称。
这就是问题所在。该pre-commit脚本的工作原理是它找到字符串模式并将其替换为正确的文本/代码,但它似乎在提交更改之前没有这样做......这意味着在我运行git commit -m "Updated README.md"并检查git status自述文件之后.md 文件显示为正在修改,如果我要运行git push origin branch_nameREADME.md 文件,则包含实际的{{STRING_PATTERN}}标识符(不需要),而不是更新的文本(这正是我想要的)。
正如我所说,这是pre-commit代码,请注意,从定位字符串{{STRING_PATTERN}}标识符并使用动态创建的文本/代码 \xe2\x80\x93 更新它的角度来看,这确实有效。并没有真正提交这些更改,这就是问题所在。
#!/usr/bin/env bash\n\n# Echo out text to ensure pre-commit is running\necho "pre-commit working"\n\n# Get the current branch name\nfunction git_branch {\n git rev-parse --abbrev-ref HEAD\n}\n\n# Set branch variable to current branch\nbranch=$(git_branch)\n\n# Define the string/pattern marker to search for\n# This pattern/string serves …Run Code Online (Sandbox Code Playgroud) 我正在按照办公室指南创建钩子并将其添加到预提交检查过程中。我需要创建 3 个文件
.pre-commit-config.yaml
.pre-commit-hooks.yaml
theCheckFile.sh
Run Code Online (Sandbox Code Playgroud)
配置文件配置hooks文件,该文件调用theCheckFils.sh文件来检查我的代码风格。
Q.1我应该把这些文件放在哪里?我目前将它们放入我的项目文件夹中,并编辑 .gitignore 文件以忽略所有它们,有更好的建议吗?或者这样就可以了。
Q.2 pre-commit-config.yaml 文件中需要 rev,我应该在哪里找到此信息,我当前使用的代码 Repo 中没有版本信息,我可以随机创建一个数字吗?
Yelp 检测机密是一个防止机密进入您的代码库的系统。我想安装它提供的预提交钩子。
我以前从未使用过 git hooks,但我看到的所有示例文件.git/hooks/都是 bash,而yelp的自述文件中给出的示例是一个 yaml 文件。
它们链接到pre-commit.com;“用于管理和维护多语言预提交挂钩的框架”。
这是否意味着 yelp pre-commit hook 只能通过先安装pre-commit框架才能使用?
如果是这样,我有点迷失在预提交的使用中。我已经安装了它,我可以调用它。所以在现有的仓库中,我运行了pre-commit install. 但是我现在如何添加 yelp 检测秘密预提交钩子?
当然,我已经查看了pre-commit.com上的文档,但我有点迷失了。欢迎任何提示。
我正在使用这个库来尝试向我的项目添加预提交挂钩。有很多需要考虑的内容,我认为我想要的预提交检查不需要他们大多数示例使用的大范围。我想做的就是运行一个 python 文件。我想要允许或禁止提交取决于该文件是否退出/完成而没有问题(IE 没有引发异常)。我已经做了一个,.pre-commit-config.yaml但我不知道如何让它只运行一个文件。
我基本上希望输入git commit -m "whatever"自动运行python myfile.py<- 并根据其退出代码,允许或阻止提交。知道我的 yaml 应该是什么样子吗?
这是我到目前为止所拥有的:
repos:
- repo: local
hooks:
- id: translation-file-check
name: Check Translation Files Are Aligned
description: This hook ensures that all translation files share the same set of keys and generates a CSV if there are no issues
language: python
entry: "./dir/subdir/myfile.py"
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:.An unexpected error has occurred: OSError: [WinError 193] %1 is not a valid Win32 application
我认为因为它期望这个 .py …
我希望pre-commit在提交我的代码之前运行测试。
该命令python -m unittest discover正在命令行中工作。
D:\project_dir>python -m unittest discover
...
...
...
----------------------------------------------------------------------
Ran 5 tests in 6.743s
OK
Run Code Online (Sandbox Code Playgroud)
但是当我尝试提交时,我得到了
D:\project_dir>git commit -m "fix tests with hook"
run tests................................................................Failed
hookid: tests
usage: python.exe -m unittest discover [-h] [-v] [-q] [--locals] [-f] [-c]
[-b] [-k TESTNAMEPATTERNS] [-s START]
[-p PATTERN] [-t TOP]
python.exe -m unittest discover: error: unrecognized arguments: bigpipe_response/processors_manager.py
usage: python.exe -m unittest discover [-h] [-v] [-q] [--locals] [-f] [-c]
[-b] [-k TESTNAMEPATTERNS] [-s START]
[-p …Run Code Online (Sandbox Code Playgroud) 在我的 Python 项目中,我pytest用作pre-commit挂钩。一些测试创建和删除临时文件,当我运行pytest <test directory>. 但是,当我运行git commit并pre-commit挂钩 triggers 时pytest,某些测试由于FileNotFoundError: [Errno 2] No such file or directory: '<file name>'. 我的印象是,当许多文件已更改且位于暂存区域时,会发生这种情况(对于 1-2 个文件,我没有观察到此问题)。这是我的pytest部分.pre-commit-config.yaml:
- repo: local
hooks:
- id: pytest-check
name: pytest-check
entry: bash -c 'pytest'
language: system
Run Code Online (Sandbox Code Playgroud)
输出如下所示:
pytest-check.............................................................Failed
- hook id: pytest-check
- exit code: 1
tests/utils/test_application.py F [ 91%]
tests/utils/test_image_io.py .FFF......... [100%]
==================================== ERRORS ====================================
_ ERROR at teardown of test_calling_with_nonexisting_parameter[--non_existing …Run Code Online (Sandbox Code Playgroud)