This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
git_hooks [2017/11/28 21:13] – admin | git_hooks [2017/12/20 02:31] – [How it works] 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 protocol | + | require ' |
+ | require ' | ||
+ | require ' | ||
- | #### http://api.icndb.com/ | + | ################### |
+ | # General variables | ||
+ | ################### | ||
+ | refs = $stdin.read | ||
+ | key_id = ENV.delete(' | ||
+ | proto = $stdin | ||
+ | protocol = ENV.delete(' | ||
+ | repo_path = Dir.pwd | ||
+ | gl_repository = ENV[' | ||
- | merges=`git diff --name-only --stat $oldrev..$newrev` | + | # 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 | ||
- | for changed_file in `echo $merges`; do | + | # Get changed files between hashes - check if commits contain file named ' |
+ | changed_files = `git diff --name-only --stat # | ||
+ | do_not_merge = changed_files.split(/ | ||
- | REGEX_MERGE_DEVELOP="^.do_not_merge" | + | ################################################## |
+ | # 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 | ||
- | | + | ###################################### |
- | if [[ $changed_file =~ $REGEX_MERGE_DEVELOP ]]; | + | # Decide whether to ban or not to ban |
- | then | + | ###################################### |
- | + | do_not_merge.each do |file| | |
- | | + | |
- | | + | if(is_not_origin_branch(file)) |
- | | + | |
- | + | puts get_joke().blue | |
- | | + | puts "" |
- | | + | |
- | + | puts "You don't want to commit your changes done in DO_NOT_MERGE Branch to master or any other Branch.".red | |
- | echo -e " | + | |
- | echo -e "\e[31m" | + | |
- | echo -e "Please do not merge '$MERGE_PREVENT_BRANCH' | + | |
- | echo -e "This file prevents merging: $changed_file \e[0m" | + | exit 1 |
- | echo -e "FIX IT: git reset HEAD $changed_file" | + | |
- | echo -e "" | + | end |
- | echo -e " | + | end </ |
- | exit 1 | + | |
- | fi | + | |
- | | + | |
- | exit 0 | + | |
- | fi | + | |
- | done | + | |
- | </ |