This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
git_hooks [2014/04/03 17:24] – created admin | git_hooks [2017/12/20 01:34] (current) – admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Git Hooks ====== | ====== Git Hooks ====== | ||
- | <code bash> | + | ===== pre-receive Hook (Server-Side) ===== |
- | #!/bin/sh | + | |
- | # LOCATION: MagentoRoot/ | + | 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 < |
- | mageConfig=' | + | |
- | # USE MAGNTOs ROOT DIRECTORY | + | 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. |
- | sqlStorage='/ | + | |
- | # WARNING: cat IS CALLED FOR EACH VARIABLE - TODO: IMPROVE LOGIC/ | + | ==== Setup ==== |
- | host=$(cat ${mageConfig} | sed -n '/< | + | |
- | username=$(cat ${mageConfig} | sed -n '/< | + | |
- | password=$(cat ${mageConfig} | sed -n '/< | + | |
- | dbname=$(cat ${mageConfig} | sed -n '/< | + | |
- | echo '' | + | For Gitlab Omnibus installations place '' |
- | echo ' | + | ''/ |
- | echo $host | + | |
- | echo $username | + | Make sure the file ist executable using '' |
- | echo $password | + | |
- | echo $dbname | + | ==== How it works ==== |
- | echo '--> END Tagwork< | + | |
- | echo '' | + | 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. | ||
+ | |||
+ | Given you have the branch '' | ||
+ | |||
+ | ==== Expected output in CLI ==== | ||
+ | |||
+ | {{:: | ||
+ | ==== 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 | ||
- | </ |