This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
git_hooks [2014/04/03 17:36] – admin | git_hooks [2017/12/20 01:34] (current) – admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Git Hooks ====== | ====== Git Hooks ====== | ||
- | ===== pre-commit | + | ===== pre-receive |
- | Executed | + | This is a hook i use along with Gitlab. Its global and will be triggerd for all repositories |
- | <code bash pre-commit> | + | 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. |
- | #!/bin/sh | + | |
- | # LOCATION: MagentoRoot/ | + | ==== Setup ==== |
- | mageConfig=' | + | |
- | # USE MAGNTOs ROOT DIRECTORY | + | For Gitlab Omnibus installations place '' |
- | sqlStorage='/root/hook/mysql_structure.sql' | + | ''/opt/gitlab/embedded/ |
- | # WARNING: cat IS CALLED FOR EACH VARIABLE - TODO: IMPROVE LOGIC/ | + | Make sure the file ist executable using '' |
- | host=$(cat ${mageConfig} | sed -n '/< | + | |
- | username=$(cat ${mageConfig} | sed -n '/< | + | |
- | password=$(cat ${mageConfig} | sed -n '/< | + | |
- | dbname=$(cat ${mageConfig} | sed -n '/< | + | |
- | echo '' | + | ==== How it works ==== |
- | echo '--> EXPORT SQL STRUCTURE | + | |
- | echo $host | + | The branch you do not want to be merged in any other branch create a file named < |
- | echo $username | + | Make sure **THE-BRANCH-NAME** its exactly the same name as the Branch. |
- | echo $password | + | |
- | echo $dbname | + | Given you have the branch |
- | echo '--> END - EXPORT SQL STRUCTURE <--' | + | |
- | echo '' | + | ==== Expected output in CLI ==== |
+ | |||
+ | {{::hook-pre-receive-acoid-merge.png? | ||
+ | ==== The Hook ==== | ||
+ | |||
+ | <code ruby pre-receive> | ||
+ | # | ||
+ | # Fix the PATH so that gitlab-shell can find git-upload-pack and friends. | ||
+ | ENV['PATH'] = '/ | ||
+ | |||
+ | require ' | ||
+ | require ' | ||
+ | require ' | ||
+ | |||
+ | ################### | ||
+ | # General variables | ||
+ | ################### | ||
+ | refs = $stdin.read | ||
+ | key_id = ENV.delete(' | ||
+ | proto = $stdin | ||
+ | protocol = ENV.delete('GL_PROTOCOL' | ||
+ | repo_path = Dir.pwd | ||
+ | gl_repository = ENV[' | ||
+ | |||
+ | # Read given hashes | ||
+ | hash = refs.split(' | ||
+ | hash_from = hash[0] | ||
+ | hash_to = hash[1] | ||
+ | @branch = hash[2] | ||
+ | |||
+ | ################# | ||
+ | # Colorize output | ||
+ | ################# | ||
+ | class String | ||
+ | def red; " | ||
+ | def green; | ||
+ | def blue; " | ||
+ | end | ||
+ | |||
+ | ########################## | ||
+ | # Pull a Chuck Norris Joke | ||
+ | ########################## | ||
+ | def get_joke(author = '' | ||
+ | begin | ||
+ | plain_uri = ' | ||
+ | url = URI.parse(plain_uri) | ||
+ | req = Net:: | ||
+ | |||
+ | res = Net:: | ||
+ | http.request(req) | ||
+ | } | ||
+ | joke = CGI.unescapeHTML(JSON.parse(res.body)[' | ||
+ | |||
+ | 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 | ||
+ | changed_files = `git diff --name-only --stat # | ||
+ | do_not_merge = changed_files.split(/ | ||
+ | |||
+ | ################################################## | ||
+ | # Check if this is the .do_not_merge__ORIGIN Branch | ||
+ | ################################################## | ||
+ | def is_not_origin_branch(origin_branch) | ||
+ | current_branch = @branch.split(/ | ||
+ | origin_branch = origin_branch.split(/ | ||
+ | return current_branch != origin_branch | ||
+ | end | ||
+ | |||
+ | ###################################### | ||
+ | # Decide whether to ban or not to ban | ||
+ | ###################################### | ||
+ | do_not_merge.each do |file| | ||
+ | if (file[/ | ||
+ | if(is_not_origin_branch(file)) | ||
+ | puts " | ||
+ | puts get_joke().blue | ||
+ | puts "" | ||
+ | puts "The file #{file} stops you from pushing your changes to remote." | ||
+ | puts "You don't want to commit your changes done in DO_NOT_MERGE Branch to master or any other Branch." | ||
+ | puts "" | ||
+ | puts "To reset your branch use 'git reset --hard origin/# | ||
+ | puts " | ||
+ | exit 1 | ||
+ | end | ||
+ | end | ||
+ | end </ | ||
+ | |||
+ | ==== ToDo ==== | ||
+ | |||
+ | * Find a way to make hook output look pretty in Gitlab {{:: | ||
- | # EXPORT SQL STRUCTURE | ||
- | mysqldump -d -h $host -u $username -p$password $dbname > $sqlStorage | ||
- | </ |