User Tools

Site Tools


git_hooks

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
git_hooks [2017/11/28 21:13] admingit_hooks [2017/12/20 01:46] admin
Line 4: Line 4:
  
 This is a hook i use along with Gitlab. Its global and will be triggerd for all repositories when data ist send to the server. This is a hook i use along with Gitlab. Its global and will be triggerd for all repositories when data ist send to the server.
-For Gitlab Omnibus installations place 'pre-receive' file in this Folder (if it does not exist, create it) +For Gitlab Omnibus installations place ''pre-receive'' file in this Folder (if it does not exist, create it) 
-  /opt/gitlab/embedded/service/gitlab-shell/hooks/pre-receive.d/+''/opt/gitlab/embedded/service/gitlab-shell/hooks/pre-receive.d/''
  
-Make sure the file ist executable using 'chmod +x pre-receive'+Make sure the file ist executable using ''chmod +x pre-receive''
  
-<code bash pre-receive> +<code ruby pre-receive> 
-#!/bin/sh+#!/opt/gitlab/embedded/bin/ruby --disable-gems 
 +# Fix the PATH so that gitlab-shell can find git-upload-pack and friends. 
 +ENV['PATH'] = '/opt/gitlab/bin:/opt/gitlab/embedded/bin:' + ENV['PATH']
  
-read oldrev newrev refname wusel+require 'net/http' 
 +require 'json' 
 +require 'cgi'
  
-merges=`git log --oneline --merges $oldrev..$newrev`+################### 
 +# General variables 
 +################### 
 +refs = $stdin.read 
 +key_id = ENV.delete('GL_ID'
 +proto = $stdin 
 +protocol = ENV.delete('GL_PROTOCOL'
 +repo_path = Dir.pwd 
 +gl_repository = ENV['GL_REPOSITORY']
  
-readarray -t COMMIT <<< "$merges"+# Read given hashes 
 +hash = refs.split(' ') 
 +hash_from = hash[0] 
 +hash_to = hash[1] 
 +@branch = hash[2]
  
-if [ -z "$COMMIT" ]; then +puts @branch
-    # IF ARRAY EMPTY +
-    # echo ">>>>>" $COMMIT "<<<<<" +
-    exit 0 +
-else +
-    # IF MERGE EXISTS +
-    for i in "$COMMIT" +
-    do+
  
-        REGEX_MERGE_DEVELOP="\bMerge.*?.branch.*?.develop\b"+################# 
 +# Colorize output 
 +################# 
 +class String 
 +    def red;            "\033[31m#{self}\033[0m" end 
 +    def green;          "\033[32m#{self}\033[0m" end 
 +    def blue;           "\033[34m#{self}\033[0m" end 
 +end 
 + 
 +########################## 
 +# Pull a Chuck Norris Joke 
 +########################## 
 +def get_joke(author = ''
 +    begin 
 +        plain_uri = 'http://api.icndb.com/jokes/random'         
 +        url = URI.parse(plain_uri) 
 +        req = Net::HTTP::Get.new(url.to_s)
                  
-        if [[ $COMMIT =~ $REGEX_MERGE_DEVELOP ]]; +        res Net::HTTP.start(url.host, url.port) {|http| 
-        then +            http.request(req) 
-             +        } 
-            echo -e "☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️" +        joke = CGI.unescapeHTML(JSON.parse(res.body)['value']['joke']) 
-         echo -e "\e[31m" +     
-        echo -e "Please do not merge 'developinto any other branch \e[0m" +    rescue StandardError 
-         echo -e "Review your commit:" +     joke = "Chuck Norris never sleeps? Well, even Chuck Norris needs a power nap every once in a while ...
-         echo -e "" +    end   
-              +   
-            for i in "$COMMIT+    return joke 
-            do         +end
-            echo -e "  $COMMIT" +
-            done +
-             +
-        echo -e "" +
-            echo -e "☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️" +
-         +
-        exit 1 +
-        else +
-            exit 0 +
-        fi +
-    done +
-fi +
-</code> +
- +
- +
-Another Version  +
- +
-<code bash> +
-#!/bin/sh +
- +
-read oldrev newrev refname protocol +
- +
-#### http://api.icndb.com/jokes/random+
  
-merges=`git diff --name-only --stat $oldrev..$newrev`+# Get changed files between hashes - check if commits contain file named '.do_not_merge' 
 +changed_files = `git diff --name-only --stat #{hash_from}..#{hash_to}`.chop 
 +do_not_merge = changed_files.split(/\n/)
  
-for changed_file in `echo $merges`; do        +puts changed_files
  
-        REGEX_MERGE_DEVELOP="^.do_not_merge"+################################################## 
 +# Check if this is the .do_not_merge__ORIGIN Branch 
 +################################################## 
 +def is_not_origin_branch(origin_branch) 
 +    current_branch @branch.split(/\//).last 
 +    origin_branch = origin_branch.split(/__/).last 
 +    return current_branch != origin_branch 
 +end
  
-        If $changed_file matches regex $REGEX_MERGE_DEVELOP +###################################### 
-        if [[ $changed_file =~ $REGEX_MERGE_DEVELOP ]]; +# Decide whether to ban or not to ban 
-        then +###################################### 
-                                     +do_not_merge.each do |file    
-            MERGE_PREVENT_BRANCH=$(echo $changed_file | tr "_into_" "\n") +    if (file[/^.do_not_merge__/]
-            MERGE_PREVENT_BRANCH=$(echo $changed_file tr "_" "\n" tail -1) +        if(is_not_origin_branch(file)) 
-            MERGE_TO_BRANCH=$(echo $refname | tr "/" "\n" | tail -1+            puts "☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️" 
-             +            puts get_joke().blue 
-            if [[ "$MERGE_PREVENT_BRANCH" -eq "$MERGE_TO_BRANCH" ]]; +            puts "" 
-            then +            puts "The file #{file} stops you from pushing your changes to remote.".red 
-                 +            puts "You don't want to commit your changes done in DO_NOT_MERGE Branch to master or any other Branch.".red 
-                echo -e "☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️" +            puts "" 
-             echo -e "\e[31m+            puts "To reset your branch use 'git reset --hard origin/#{@branch.split(/\//).last}'. This will reset your Branch to the state of the origin/remote branch
-             echo -e "Please do not merge '$MERGE_PREVENT_BRANCH' into any other branch +            puts "\n☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️" 
-             echo -e "This file prevents merging: $changed_file \e[0m+            exit 1 
-             echo -e "FIX IT: git reset HEAD $changed_file" +        end 
-            echo -e "+    end 
-                echo -e "☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️" +end                                                                                                                                                                </code>
-                exit 1 +
-            fi +
-        else +
-            exit 0 +
-        fi +
-done+
  
-</code> 
git_hooks.txt · Last modified: 2017/12/20 02:34 by admin