Skip to content

Instantly share code, notes, and snippets.

@edblack1
Forked from cagerton/no_push_f_master.rb
Created March 9, 2019 06:33
Show Gist options
  • Select an option

  • Save edblack1/00050b9d66bfab1532c47bb8f2af4df1 to your computer and use it in GitHub Desktop.

Select an option

Save edblack1/00050b9d66bfab1532c47bb8f2af4df1 to your computer and use it in GitHub Desktop.

Revisions

  1. @cagerton cagerton revised this gist Sep 23, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion no_push_f_master.rb
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,9 @@
    #!/usr/bin/env ruby

    # pre-receive hook to block 'push -f' on master
    # For Phabricator (remember to chmod +x):
    # For Phabricator, save this as:
    # $REPO/hooks/pre-receive-phabricator.d/no_push_f_master.rb
    # Remember to chmod +x
    # Ref: http://git-scm.com/book/en/Customizing-Git-An-Example-Git-Enforced-Policy

    args = STDIN.readline.chomp.split(" ")
  2. @cagerton cagerton renamed this gist Sep 23, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @cagerton cagerton created this gist Sep 23, 2014.
    42 changes: 42 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    #!/usr/bin/env ruby

    # pre-receive hook to block 'push -f' on master
    # For Phabricator (remember to chmod +x):
    # $REPO/hooks/pre-receive-phabricator.d/no_push_f_master.rb
    # Ref: http://git-scm.com/book/en/Customizing-Git-An-Example-Git-Enforced-Policy

    args = STDIN.readline.chomp.split(" ")
    $oldrev, $newrev, $refspec = args

    # paranoid input check:
    SHA_RE = /^[\da-f]{40}$/i
    if $oldrev !~ SHA_RE or $newrev !~ SHA_RE then
    puts "invalid input?"
    exit 1
    end

    # enforces fast-forward only pushes:
    def check_fast_forward
    missed_refs = `git rev-list #{$newrev}..#{$oldrev}`
    missed_ref_count = missed_refs.split("\n").size
    if missed_ref_count > 0
    puts "[POLICY] Cannot push a non fast-forward reference"
    puts "[POLICY] missed ref count: #{missed_ref_count}"
    exit 1
    end
    end

    # don't allow deletes
    def check_delete
    if /0{40}/ =~ $newrev then
    puts "[POLICY] delete rejected"
    exit 1
    end
    end

    # enforcing on master
    if $refspec == "refs/heads/master"
    puts "[POLICY:master]"
    check_delete
    check_fast_forward
    end