This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| git_hooks [2017/10/17 06:01] – admin | git_hooks [2017/12/20 01: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. |
| - | For Gitlab Omnibus installations place ' | + | |
| - | / | + | |
| - | 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 |
| - | <code bash pre-receive> | + | ==== Setup ==== |
| - | #!/bin/sh | + | |
| - | read oldrev newrev refname | + | For Gitlab Omnibus installations place '' |
| + | ''/ | ||
| - | ## EXAMPLE COMMAND: git log --oneline --merges 1bcca583df49bab1388a64730d1647785b3e9b8d..84da95ea03859df818ae23370f83cc80b6ce3972 | + | Make sure the file ist executable using '' |
| - | ################## | + | |
| - | merges=`git log --oneline --merges $oldrev..$newrev` | + | ==== How it works ==== |
| - | echo ----------$(" | + | The branch you do not want to be merged in any other branch create a file named < |
| + | Make sure **THE-BRANCH-NAME** its exactly the same name as the Branch. | ||
| - | readarray -t y <<< " | + | Given you have the branch '' |
| - | for i in " | + | ==== Expected output |
| - | do | + | |
| - | echo -e " | + | |
| - | echo -e " | + | |
| - | echo -e " | + | |
| - | exit 1 | + | |
| - | done | + | |
| - | </ | + | |
| - | ===== pre-commit | + | {{:: |
| + | ==== The Hook ==== | ||
| - | Executed when running "git commit" | + | <code ruby pre-receive> |
| + | # | ||
| + | # Fix the PATH so that gitlab-shell can find git-upload-pack and friends. | ||
| + | ENV[' | ||
| - | <code bash pre-commit> | + | require 'net/http' |
| - | #!/bin/sh | + | require ' |
| + | require ' | ||
| - | # LOCATION: MagentoRoot/ | + | ################### |
| - | mageConfig='../ | + | # General variables |
| + | ################### | ||
| + | refs = $stdin.read | ||
| + | key_id | ||
| + | proto = $stdin | ||
| + | protocol = ENV.delete(' | ||
| + | repo_path = Dir.pwd | ||
| + | gl_repository = ENV[' | ||
| - | # USE MAGNTOs ROOT DIRECTORY | + | # Read given hashes |
| - | sqlStorage='/ | + | hash = refs.split(' |
| + | hash_from = hash[0] | ||
| + | hash_to = hash[1] | ||
| + | @branch = hash[2] | ||
| - | # WARNING: cat IS CALLED FOR EACH VARIABLE - TODO: IMPROVE LOGIC/ | + | ################# |
| - | host=$(cat ${mageConfig} | sed -n '/< | + | # Colorize output |
| - | username=$(cat ${mageConfig} | sed -n '/< | + | ################# |
| - | password=$(cat ${mageConfig} | sed -n '/< | + | class String |
| - | dbname=$(cat ${mageConfig} | sed -n '/< | + | def red; " |
| + | def green; | ||
| + | def blue; " | ||
| + | end | ||
| - | echo '' | + | ########################## |
| - | echo '--> EXPORT SQL STRUCTURE <--' | + | # Pull a Chuck Norris Joke |
| - | echo $host | + | ########################## |
| - | echo $username | + | def get_joke(author = '' |
| - | echo $password | + | begin |
| - | echo $dbname | + | |
| - | echo '--> END - EXPORT SQL STRUCTURE <--' | + | url = URI.parse(plain_uri) |
| - | echo '' | + | req = Net:: |
| + | |||
| + | res = Net:: | ||
| + | | ||
| + | } | ||
| + | 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 - check if commits contain file named '.do_not_merge' | ||
| + | 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 | ||
| - | </ | ||