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 revision
Previous revision
git_hooks [2017/10/17 08:01] admingit_hooks [2017/12/20 02:34] (current) admin
Line 3: Line 3:
 ===== pre-receive Hook (Server-Side) ===== ===== pre-receive Hook (Server-Side) =====
  
-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 is send to the server. When data is send to the server old and new revs will be compared depending on the changed files. If the changed files contain a file named <code>.do_not_merge__WHAT-EVER-YOUR-BRANCH-IS</code> the hook will check if the branch you are pushing to **is not equal** to **WHAT-EVER-YOUR-BRANCH-IS** and will refuse to merge.
-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/+
  
-Make sure the file ist executable using 'chmod +x pre-receive'+In case **WHAT-EVER-YOUR-BRANCH-IS** is equal to the branch you try to push to the merge will not be refuse. This is because we want to be able to create the .do_not_merge file where necessary.
  
-<code bash pre-receive> +==== Setup ====
-#!/bin/sh+
  
-read oldrev newrev refname+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/''
  
-## EXAMPLE COMMAND: git log --oneline --merges 1bcca583df49bab1388a64730d1647785b3e9b8d..84da95ea03859df818ae23370f83cc80b6ce3972 +Make sure the file ist executable using ''chmod +x pre-receive''
-################## git log --oneline --merges OLDREV..NEWREV+
  
-merges=`git log --oneline --merges $oldrev..$newrev`+==== How it works ====
  
-echo ----------$("git log --oneline --merges $oldrev..$newrev")+The branch you do not want to be merged in any other branch create a file named <code>.do_not_merge__THE-BRANCH-NAME</code> 
 +Make sure **THE-BRANCH-NAME** its exactly the same name as the Branch.
  
-readarray -t y <<< "$merges"+Given you have the branch ''develop'' and want to avoid merge into any other branch, simply create a file named <code>.do_not_merge__develop</code>
  
-for i in "${y[@]}" +==== Expected output in CLI ====
-do +
-    echo -e "☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️\e[31m" +
-    echo -e "G'Day Mate, what the fuck are you doing?!\nPlease do not merge 'develop' in any other branch \e[0m" +
-    echo -e "☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️" +
-    exit 1 +
-done +
-</code>+
  
-===== pre-commit Hook =====+{{::hook-pre-receive-acoid-merge.png?800|}} 
 +==== The Hook ====
  
-Executed when running "git commit"+<code ruby pre-receive> 
 +#!/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']
  
-<code bash pre-commit> +require 'net/http' 
-#!/bin/sh+require 'json' 
 +require 'cgi'
  
-LOCATION: MagentoRoot/app/etc/local.xml +################### 
-mageConfig='../../var/www/clients/web/app/etc/local.xml'+# 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']
  
-USE MAGNTOs ROOT DIRECTORY +Read given hashes 
-sqlStorage='/root/hook/mysql_structure.sql'+hash refs.split(') 
 +hash_from = hash[0] 
 +hash_to = hash[1] 
 +@branch = hash[2]
  
-WARNING: cat IS CALLED FOR EACH VARIABLE - TODO: IMPROVE LOGIC/SYNTAX +################# 
-host=$(cat ${mageConfig| sed -n '/<connection>/,/<\/connection>/p' | sed -n -e 's/.*<host>\(.*\)<\/host>.*/\1/p' | sed -n -e 's/.*CDATA\[\(.*\)\]\].*/\1/p'+# Colorize output 
-username=$(cat ${mageConfig| sed -n '/<connection>/,/<\/connection>/p' | sed -n -e 's/.*<username>\(.*\)<\/username>.*/\1/p' | sed -n -e 's/.*CDATA\[\(.*\)\]\].*/\1/p') +################# 
-password=$(cat ${mageConfig} | sed -n '/<connection>/,/<\/connection>/p' | sed -n -e 's/.*<password>\(.*\)<\/password>.*/\1/p' | sed -n -e 's/.*CDATA\[\(.*\)\]\].*/\1/p'+class String 
-dbname=$(cat ${mageConfig| sed -n '/<connection>/,/<\/connection>/p' | sed -n -e 's/.*<dbname>\(.*\)<\/dbname>.*/\1/p' | sed -n -e 's/.*CDATA\[\(.*\)\]\].*/\1/p')+    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
  
-echo '' +########################## 
-echo '--> EXPORT SQL STRUCTURE <--+# Pull a Chuck Norris Joke 
-echo $host +########################## 
-echo $username +def get_joke(author = ''
-echo $password +    begin 
-echo $dbname +        plain_uri = 'http://api.icndb.com/jokes/random        
-echo '--> END EXPORT SQL STRUCTURE <--' +        url = URI.parse(plain_uri) 
-echo ''+        req = Net::HTTP::Get.new(url.to_s) 
 +         
 +        res = Net::HTTP.start(url.host, url.port) {|http| 
 +            http.request(req) 
 +        } 
 +        joke = CGI.unescapeHTML(JSON.parse(res.body)['value']['joke']) 
 +     
 +    rescue StandardError 
 +    joke = "Chuck Norris never sleeps? Well, even Chuck Norris needs a power nap every once in a while ..." 
 +    end   
 +   
 +    return joke 
 +end 
 + 
 +# 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/
 + 
 +################################################## 
 +# 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 
 + 
 +###################################### 
 +# Decide whether to ban or not to ban 
 +###################################### 
 +do_not_merge.each do |file|     
 +    if (file[/^.do_not_merge__/]) 
 +        if(is_not_origin_branch(file)) 
 +            puts "☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️" 
 +            puts get_joke().blue 
 +            puts "" 
 +            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 
 +            puts "" 
 +            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" 
 +            puts "\n☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️  ☠️" 
 +            exit 1 
 +        end 
 +    end 
 +end                                                                                                                                                                </code> 
 + 
 +==== ToDo ==== 
 + 
 +  * Find a way to make hook output look pretty in Gitlab {{::gitlab-hook-pre-receive-avoid-merge.png?800|}}
  
-# EXPORT SQL STRUCTURE 
-mysqldump -d -h $host -u $username -p$password $dbname > $sqlStorage 
-</code> 
git_hooks.1508220065.txt.gz · Last modified: 2017/10/17 08:01 by admin