【问题标题】:skip pre-commit hook conditionally from within commit-msg hook从 commit-msg 钩子中有条件地跳过预提交钩子
【发布时间】:2020-12-11 10:30:36
【问题描述】:

我有一个 precommit 钩子,可确保在任何提交之前始终使用 devel 重新设置主题分支。然而,有时在我的工作中,我想提交我未完成的工作,而不是把它藏起来以防万一,然后在恢复之前重新设置。对于此类工作保存提交,提交消息必须完全相同,例如“工作保存提交”。我希望 commit-msg 钩子检查这种情况,如果它是一个工作保存提交,则跳过 precommit 钩子。 precommit 钩子一直在监听环境变量'override_hooks',所以我试图在 commit-msg 中改变它,但这没有用。我知道我可以手动设置 override_hooks 但我希望我不必每次都这样做。如有任何帮助,我将不胜感激。

【问题讨论】:

    标签: git githooks pre-commit-hook


    【解决方案1】:

    prepare-commit-msg 钩子在 pre-commit 钩子之后运行,所以这不起作用。

    你最好设置一个别名:

    # call 'git commits with
    # -n to skip all pre-commit hooks
    # -m msg to provide the commit message from the command line
    git config alias.worksave 'commit -n -m "works save commit"'
    
    # you can now type :
    git worksave
    

    【讨论】:

    • 谢谢。这是一个很好的解决方法。但是,由于它特定于命令行提交,我使用这个想法来简单地处理 commit-msg 钩子中的分支比较。
    【解决方案2】:

    您可以改用pre-commit 调用的commit-msg 挂钩。我的钩子使用了太多的内部结构,但我的想法是读取$1(提交消息文件)并相应地执行检查以确定退出代码和/或消息。

    我当前实现此功能的commit-msg 有许多自包含的依赖项,并且是用R 编写的,但我会将其粘贴到此处以防万一。

    #!/usr/local/bin/Rscript
    
    ## check if commit msg has 'resetme' or 'worksave' (a save-work commit)
    ## if not, check and ensure 'devel' branch is not ahead
    
    .stop <- function(msg) {
        stop('\n\n', msg, '\n\n', call. = FALSE)
    }
    
    ## source utilities from GitHub
    ## ------------------------------------------------------------------------ ##
    source('https://raw.githubusercontent.com/ajabadi/Altools/master/R/get_package_version.R')
    ## ------------------------------------------------------------------------ ##
    source('https://raw.githubusercontent.com/ajabadi/Altools/master/R/bump_up_version.R')
    
    ## get commit message
    commit_message <- commandArgs(trailingOnly = TRUE)
    commit_message <- paste0(readLines(commit_message), collapse = ' ')
    
    ## ascertain if it was a worksave commit
    its_a_save_work_commit <- grepl('^resetme', tolower(commit_message)) | grepl('^worksave', tolower(commit_message))
    
    
    ## exit with 1 if devel is ahead of topic and it's not a worksave commit
    ## exit with 0 otherwise
    currBranch <- current_branch()
    if (isFALSE(its_a_save_work_commit)) {
        ## remind to rebase devel for topic branches if necessary
        if (!any(currBranch %in% c('master', 'devel'))) {
            devel_is_ahead <- length(system(sprintf('git log %s..devel', currBranch), intern = TRUE)) > 0
            if (devel_is_ahead) {
                .stop('devel is ahead by some changes, rebase or start the commit message with either "worksave" or "resetme"\n')
            }
    }
    TRUE
    }
    
    

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 1970-01-01
      • 2013-02-17
      • 2013-10-16
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      • 2019-07-19
      • 2013-12-15
      相关资源
      最近更新 更多