This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
git_hooks [2017/10/18 18:52] – 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 wusel | + | For Gitlab Omnibus installations place '' |
+ | ''/ | ||
- | merges=`git log --oneline --merges $oldrev..$newrev` | + | Make sure the file ist executable using '' |
- | readarray -t COMMIT <<< | + | ==== How it works ==== |
- | if [ -z " | + | The branch you do not want to be merged in any other branch create a file named < |
- | # IF ARRAY EMPTY | + | Make sure **THE-BRANCH-NAME** its exactly the same name as the Branch. |
- | echo ">>>>>" | + | |
- | exit 1 | + | |
- | else | + | |
- | # IF MERGE EXISTS | + | |
- | for i in " | + | |
- | do | + | |
- | REGEX_MERGE_DEVELOP=" | + | 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[' | ||
+ | |||
+ | require ' | ||
+ | require ' | ||
+ | require ' | ||
+ | |||
+ | ################### | ||
+ | # General variables | ||
+ | ################### | ||
+ | refs = $stdin.read | ||
+ | key_id = ENV.delete(' | ||
+ | proto = $stdin | ||
+ | protocol = ENV.delete(' | ||
+ | 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; "\033[31m# | ||
+ | def green; | ||
+ | def blue; " | ||
+ | end | ||
+ | |||
+ | ########################## | ||
+ | # Pull a Chuck Norris Joke | ||
+ | ########################## | ||
+ | def get_joke(author = '' | ||
+ | begin | ||
+ | plain_uri = ' | ||
+ | url = URI.parse(plain_uri) | ||
+ | req = Net:: | ||
| | ||
- | | + | |
- | | + | http.request(req) |
- | | + | } |
- | echo -e " | + | joke = CGI.unescapeHTML(JSON.parse(res.body)[' |
- | echo -e " | + | |
- | echo -e "Please do not merge ' | + | rescue StandardError |
- | echo -e "Review | + | joke = "Chuck Norris never sleeps? Well, even Chuck Norris needs a power nap every once in a while ..." |
- | echo -e "" | + | end |
- | + | ||
- | | + | return joke |
- | | + | end |
- | echo -e " | + | |
- | | + | # Get changed files between hashes - check if commits contain file named ' |
- | + | changed_files = `git diff --name-only --stat # | |
- | echo -e "" | + | do_not_merge = changed_files.split(/ |
- | | + | |
- | + | ################################################## | |
- | exit 1 | + | # Check if this is the .do_not_merge__ORIGIN Branch |
- | | + | ################################################## |
- | exit 0 | + | def is_not_origin_branch(origin_branch) |
- | fi | + | current_branch = @branch.split(/ |
- | | + | origin_branch = origin_branch.split(/ |
- | fi | + | return current_branch != origin_branch |
- | </ | + | end |
+ | |||
+ | ###################################### | ||
+ | # Decide whether to ban or not to ban | ||
+ | ###################################### | ||
+ | do_not_merge.each do |file| | ||
+ | if (file[/ | ||
+ | | ||
+ | | ||
+ | puts get_joke().blue | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | exit 1 | ||
+ | | ||
+ | | ||
+ | end | ||
+ | |||
+ | ==== ToDo ==== | ||
+ | |||
+ | * Find a way to make hook output look pretty in Gitlab {{:: |