This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| git_hooks [2017/11/28 20:13] – 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 0 | + | |
| - | else | + | |
| - | # IF MERGE EXISTS | + | |
| - | for i in " | + | |
| - | do | + | |
| - | REGEX_MERGE_DEVELOP=" | + | Given you have the branch |
| - | + | ||
| - | if [[ $COMMIT =~ $REGEX_MERGE_DEVELOP ]]; | + | |
| - | then | + | |
| - | + | ||
| - | echo -e " | + | |
| - | echo -e " | + | |
| - | echo -e " | + | |
| - | echo -e " | + | |
| - | echo -e "" | + | |
| - | + | ||
| - | for i in " | + | |
| - | do | + | |
| - | echo -e " | + | |
| - | done | + | |
| - | + | ||
| - | echo -e "" | + | |
| - | echo -e " | + | |
| - | + | ||
| - | exit 1 | + | |
| - | else | + | |
| - | exit 0 | + | |
| - | fi | + | |
| - | done | + | |
| - | fi | + | |
| - | </ | + | |
| + | ==== Expected output in CLI ==== | ||
| - | Another Version | + | {{:: |
| + | ==== The Hook ==== | ||
| - | < | + | < |
| - | #!/bin/sh | + | #!/ |
| + | # Fix the PATH so that gitlab-shell can find git-upload-pack and friends. | ||
| + | ENV[' | ||
| - | read oldrev newrev refname | + | require ' |
| + | require ' | ||
| + | require ' | ||
| + | |||
| + | ################### | ||
| + | # General variables | ||
| + | ################### | ||
| + | refs = $stdin.read | ||
| + | key_id = ENV.delete(' | ||
| + | proto = $stdin | ||
| + | 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 | ||
| - | #### http://api.icndb.com/jokes/random | + | # Get changed files between hashes - check if commits contain file named ' |
| + | changed_files = `git diff --name-only --stat | ||
| + | do_not_merge = changed_files.split(/\n/) | ||
| - | merges=`git diff --name-only --stat $oldrev..$newrev` | + | ################################################## |
| + | # Check if this is the .do_not_merge__ORIGIN Branch | ||
| + | ################################################## | ||
| + | def is_not_origin_branch(origin_branch) | ||
| + | current_branch | ||
| + | origin_branch = origin_branch.split(/ | ||
| + | return current_branch != origin_branch | ||
| + | end | ||
| - | for changed_file in `echo $merges`; | + | ###################################### |
| + | # Decide whether to ban or not to ban | ||
| + | ###################################### | ||
| + | do_not_merge.each | ||
| + | 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 </ | ||
| - | REGEX_MERGE_DEVELOP=" | + | ==== ToDo ==== |
| - | # If $changed_file matches regex $REGEX_MERGE_DEVELOP | + | * Find a way to make hook output look pretty in Gitlab {{::gitlab-hook-pre-receive-avoid-merge.png?800|}} |
| - | if [[ $changed_file =~ $REGEX_MERGE_DEVELOP ]]; | + | |
| - | then | + | |
| - | + | ||
| - | # MERGE_PREVENT_BRANCH=$(echo $changed_file | tr " | + | |
| - | MERGE_PREVENT_BRANCH=$(echo $changed_file | tr " | + | |
| - | MERGE_TO_BRANCH=$(echo $refname | tr "/" | + | |
| - | + | ||
| - | if [[ " | + | |
| - | then | + | |
| - | + | ||
| - | echo -e " | + | |
| - | echo -e " | + | |
| - | echo -e " | + | |
| - | echo -e "This file prevents merging: $changed_file \e[0m" | + | |
| - | echo -e "FIX IT: git reset HEAD $changed_file" | + | |
| - | echo -e "" | + | |
| - | echo -e " | + | |
| - | exit 1 | + | |
| - | fi | + | |
| - | else | + | |
| - | exit 0 | + | |
| - | fi | + | |
| - | done | + | |
| - | </ | ||