Git预推钩子在整体更改中拒绝添加字符串 [英] Git pre-push hook to reject string addition in overall changes

查看:44
本文介绍了Git预推钩子在整体更改中拒绝添加字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当整体差异引入特定的单词/句子/字符串时,您如何编写一个拒绝推送的预推钩子?

激发用例:

  1. 提交TODO笔记(标记任务要在推送之前完成)
  2. 添加git可以防止我在推送时忘记说出这些笔记
  1. committing TODO notes (marking tasks to complete before pushing)
  2. letting git prevent me from forgetting to address said notes when pushing


注释:

  1. 我见过很多不同的问题和答案,但是没有一个接近.快速尝试的方法在某些情况下会失败(例如).
  2. 请注意,"overall diff"表示只要在随后的提交中删除字符串,就允许添加TODO的提交(这样,整个 delta 不包含它)
  3. 主要困难是找到可以传递给git diff的范围,该范围在所有情况下均有效.
  4. 仅阻止添加TODO,应允许在差异中删除
  5. 以前包含TODO的行的
  6. 修改对其进行了维护,这意味着添加TODO(即使与移除同时进行),因此应予以阻止(合理的解释:没有客观的方法来区分所引入的TODO是否为与被删除的相同).
  7. 这样的钩子应该处理所有有效的 pushhes ,仅检查与新提交范围相对应的增量(例如,push --delete中没有要检查的内容).需要考虑的一些特殊情况:
    • 新分支
    • 已删除的分支机构
    • 重命名分支机构
    • 分支出除master以外的东西(所以没有merge-base origin/master)
    • 拆分/合并
    • 标签
    • 推力
  1. I have seen a lot of different questions and answers but none come close. The ones that try quickly fail on edge cases (e.g. this and this).
  2. notice that "overall diff" means that commits adding TODO would be allowed as long as the string was removed in subsequent commits being pushed (such that the overall delta did not contain it)
  3. the main difficulty is finding a range to pass to git diff that works in all cases.
  4. only additions of TODO are to be blocked, removals should be allowed in the diff
  5. modifications of lines previously containing TODO that maintain it, imply a TODO addition (even if simultaneous with a removal) and should therefore be blocked (rationale: no objective way to distinguish whether the introduced TODO is the same as the removed one).
  6. such a hook should cope with all valid pushes, checking only deltas corresponding to ranges of new commits (e.g. nothing to check in push --delete). Some particular cases to consider:
    • new branches
    • deleted branches
    • renamed branches
    • branching off of something other than master (so no merge-base origin/master)
    • splits/merges
    • tags
    • force pushes

推荐答案

可以尝试以下操作.

#!/usr/bin/env ruby

branchName = `git rev-parse --abbrev-ref HEAD`.strip
log = `git reflog show --no-abbrev #{branchName} --format='\%h'`.split("\n")
range = "#{log.last}..#{log.first}".gsub("'", "")

`git diff --name-only --diff-filter=ACMR #{range}`.each_line do |file|
  file = file.chomp
  command = "git show #{log.first}:#{file}".gsub("'", "")
  content = `#{command}`

  if ( content =~ /TODO/ )
    puts "'#{file}' contains TODO"
    exit 1
  end
end

exit 0

这将在任何新的,添加的,修改的或重命名的文件的内容中搜索单词TODO.如果找到单词,它将退出并输出包含匹配正则表达式的文件的名称.

This will search the content of any new, added, modified, or renamed files for the word TODO. If it finds the word it will exit and output the name of the file that contains the matching regex.

这将适用于新的分支机构,但是如果您重新设置分支机构的基础确实存在问题,因为在这种情况下,它可能会引入其他人的更改.

This will work for new branches, but it does have issues if you rebase your branch as in that case it may pull in other people's changes.

这篇关于Git预推钩子在整体更改中拒绝添加字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆