How to get ALL hashes that are being committed in a pre-receive hook?

前端 未结 2 434
花落未央
花落未央 2021-01-03 10:34

I am writing a pre-receive hook for Git. This is the one where if multiple commits are pushed, and any one of them fail, then the whole push fails. Which is what I want.

相关标签:
2条回答
  • 2021-01-03 10:55
    while read old new ref; do 
        [[ $new = *[^0]* ]] && news="$news $new"
    done
    git rev-list $news --not --all
    

    This will avoid things like fastforwards over previously-pushed commits triggering wasted revalidation of unchanged content.

    0 讨论(0)
  • 2021-01-03 11:06

    You can use a pre-receive hook and still list all pushed commits.
    See this answer which includes:

    chomp(my @commits = `git rev-list $old..$new`);
    if ($?) {
      warn "git rev-list $old..$new failed\n";
      ++$errors, next;
    }
    
    foreach my $sha1 (@commits) {
      // validate some policy
    }
    

    As commented by torek, this is only for the master branch.

    You can deal with multiple branches:

    #!/bin/bash
    while read oldrev newrev refname
    do
        branch=$(git rev-parse --symbolic --abbrev-ref $refname)
        if [ "master" == "$branch" ]; then
            # Do something
        fi
    done
    
    0 讨论(0)
提交回复
热议问题