Gerrit server hook to validate the commit message - gerrit

I'm using Gerrit-2.16.17 as a code review tool and need to configure server side hooks to validate the commit message when a commit is received by Gerrit for code review to (refs/for/branch).
We need to follow the below custom commit message. If suppose the below contents are not present in our commit message. Gerrit automatically need to be rejected before a review request is gets created.
Feature Ver:
Bug-ID:
Task:
Test result:
According to this Document to validate incoming commits, On the server side $GERRIT_SITE/hooks/commit-received custom hook needs to be created. Is there any pre-script is available to validate our commit message? Need help to configure/enforce this hook on server side.

Here an example of commit-received hook:
#!/usr/bin/env bash
echo $#
echo Project: $2
echo Refname: $4
echo Uploader: $6
echo Uploader-username: $8
if [[ $2 = "test" ]]
then
exit 0
else
exit -1
fi
Following an example of successful push on the test repository:
➜ test git:(master) git push origin HEAD:refs/for/master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Writing objects: 100% (3/3), 290 bytes | 290.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Processing changes: refs: 1, updated: 1, done
remote: commit b2f0e7d: --project test --refname refs/heads/master --uploader Administrator <admin#example.com> --uploader-username admin --oldrev 9d4768e749448cbf10c9e4a1297f4ac206ea9e84 --newrev b2f0e7d920f35719cfa4a9bb5c5d2d54d6dad768 --cmdref refs/for/master
remote: Project: test
remote: Refname: refs/heads/master
remote: Uploader: Administrator <admin#example.com>
remote: Uploader-username: admin
remote:
remote: SUCCESS
remote:
remote: http://localhost:8080/c/test/+/41 Testina
remote:
To ssh://localhost:29418/test
* [new reference] HEAD -> refs/for/master
And a failing push on the shouldfail repository:
➜ shouldfail git:(master) git push origin HEAD:refs/for/master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Writing objects: 100% (3/3), 274 bytes | 274.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Processing changes: refs: 1, done
To ssh://localhost:29418/shouldfail
! [remote rejected] HEAD -> refs/for/master (commit 9addd6c: --project shouldfail --refname refs/heads/master --uploader Administrator <admin#example.com> --uploader-username admin --oldrev 6fcf316d435687e9138f4d63e228bdeafd40daae --newrev 9addd6c796fce09d187d90251b32b152530e8a4b --cmdref refs/for/master
Project: shouldfail
Refname: refs/heads/master
Uploader: Administrator <admin#example.com>
Uploader-username: admin)
error: failed to push some refs to 'ssh://localhost:29418/shouldfail'
I wrote the example in bash, but you can use any scripting language.
Note the everything you will print in the hook server-side, will b displayed client-side.

Related

Error: Error cloning remote repo 'origin`

I'm facing the same issue as mentioned here
ERROR: Error cloning remote repo 'origin'
I'm getting this issue when it is triggered as a downstream job (triggered from another job / Upstream). I'm still be able to make it work by rebuilding the build / manually triggering the build but I don't understand how.
Also Tried giving Git tool chain explicitly in the configuration, but this doesn't have any effect on the build (resolving the above issue)
PFB Code Block:
12:43:55 > /usr/bin/git fetch --tags --force --progress -- <ssh git repo link> +refs/heads/*:refs/remotes/origin/* # timeout=10
12:45:11 ERROR: Error cloning remote repo 'origin'
12:45:11 hudson.plugins.git.GitException: Command "/usr/bin/git fetch --tags --force --progress -- <ssh git repo link> +refs/heads/*:refs/remotes/origin/*" returned status code 128:
12:45:11 stdout:
12:45:11 stderr: remote: Counting objects: 12277, done
remote: Compressing objects: 1% (47/4605)
remote: Compressing objects: 2% (93/4605)
.
.
12:45:11 Receiving objects: 100% (12277/12277), 1012.00 KiB | 525.00 KiB/s
Receiving objects: 100% (12277/12277), 1.55 MiB | 731.00 KiB/s, done.
12:45:11 fatal: unable to open .git/objects/pack/tmp_pack_9xXtjN: No such file or directory
12:45:11 fatal: index-pack failed

git migration error: [remote rejected] master -> master (cannot combine normal pushes and magic pushes)

I would like to migrate a repo from server-A to server-B, here is my procedure:
"git clone --mirror" a repo, named test from server-A
$ git clone --mirror ssh://my.name#10.58.65.50:29418/test
Cloning into bare repository 'test.git'...
remote: Counting objects: 1836, done
remote: Finding sources: 100% (1829/1829)
remote: Total 2758 (delta 377), reused 2664 (delta 377)
Receiving objects: 100% (2758/2758), 1.28 MiB | 0 bytes/s, done.
Resolving deltas: 100% (949/949), done.
Checking connectivity... done.
Create an empty repo (test) on server-B
"git push --mirror" the repo (test) to server-B
git push was failed, and the error log is listed as below:
$ git push --mirror ssh://my.name#10.58.65.51:29418/test
Counting objects: 2661, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (1713/1713), done.
Writing objects: 100% (2661/2661), 1.25 MiB | 0 bytes/s, done.
Total 2661 (delta 921), reused 2661 (delta 921)
remote: Resolving deltas: 100% (921/921)
remote: Processing changes: refs: 352, done
To ssh://my.name#10.58.65.51:29418/test
! [remote rejected] master -> master (cannot combine normal pushes and magic pushes)
! [remote rejected] refs/meta/config -> refs/meta/config (cannot combine normal pushes and magic pushes)
! [remote rejected] refs/changes/00/12100/1 -> refs/changes/00/12100/1 (cannot combine normal pushes and magic pushes)
error: failed to push some refs to 'ssh://my.name#10.58.65.51:29418/test'
I have no idea of this error and how could I solve that?
Thanks!
1-Clone the empty repository from server B( make sure to clone it with commit msg)
2-Copy all your files from the 'A' server repository (except the .git file) to your empty 'B' server repository.
3-In your 'B' server repository run git add .
then git commit -m 'your commit message'
And then lastly git push origin HEAD:refs/for/master'

Gerrit access controls delete branch or tag by project owner

In gerrit version 2.16, does a user who is a member of the Project Owners group (i.e., who has been granted Owner permission) automatically have the privileges/rights to delete a branch or tag?
The gerrit access control guide https://gerrit-review.googlesource.com/Documentation/access-control.html (currently showing version 3.1.2) and the SO answer
As using gerrit, how to remove remotes tag? both state that only users with Push + Force permission on reference refs/tags/* can delete a tag.
I checked my repo and the All-Projects parent. My repo grants me Owner permission. I do not have Delete Reference permissions. All-Projects grants owners on namespace refs/tags/* ALLOW for Allow pushing (but not force pushing). The transcript below shows I can delete tags, which seems like it contradicts the doc.
% git tag "delete-me"
% git tag
1.3.0
delete-me
% git push origin delete-me
Enumerating objects: 89, done.
Counting objects: 100% (89/89), done.
Delta compression using up to 8 threads
Compressing objects: 100% (31/31), done.
Writing objects: 100% (51/51), 5.23 KiB | 1.74 MiB/s, done.
Total 51 (delta 20), reused 0 (delta 0)
remote: Resolving deltas: 100% (20/20)
remote: Processing changes: refs: 1, done
To ssh://gerrit.obfuscated-forgive-me.org:29418/my/project.git
* [new tag] delete-me -> delete-me
% git tag -d delete-me
Deleted tag 'delete-me' (was 85013e4)
% git push origin :delete-me
remote: Processing changes: refs: 1, done
To ssh://gerrit.obfuscated-forgive-me.org:29418/my/project.git
- [deleted] delete-me
The access guide gives hints about blocking tag deletes, but doesn't really make it clear: https://gerrit-review.googlesource.com/Documentation/access-control.html#_make_sure_no_one_can_update_or_delete_a_tag
Update 1 - Results of testing with an annotated tag:
% git tag -a "delete-me-annotate"
..enter annotation comment in editor, save, exit..
% git tag
1.3.0
delete-me-annotate
% git push origin delete-me-annotate
Enumerating objects: 90, done.
Counting objects: 100% (90/90), done.
Delta compression using up to 8 threads
Compressing objects: 100% (32/32), done.
Writing objects: 100% (52/52), 5.38 KiB | 1.79 MiB/s, done.
Total 52 (delta 20), reused 0 (delta 0)
remote: Resolving deltas: 100% (20/20)
remote: Processing changes: refs: 1, done
To ssh://gerrit.obfuscated-forgive-me.org:29418/my/project.git
* [new tag] delete-me-annotate -> delete-me-annotate
% git tag -d delete-me-annotate
Deleted tag 'delete-me-annotate' (was c854646)
% git push origin :delete-me-annotate
remote: Processing changes: refs: 1, done
To ssh://gerrit.obfuscated-forgive-me.org:29418/my/project.git
- [deleted] delete-me-annotate

Push to gerrit command error prohibited by Gerrit

I'd like to push my code to Gerrit. I added these lines in my ~/.gitconfig
[remote "origin"]
url = ssh://hxx#www.xxxx.com:29418/project/cloud
fetch = +refs/heads/*:refs/remotes/origin/*
push = refs/heads/*:refs/for/*
When I push in my source tree, I got error:
'git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree push -v origin refs/heads/master:refs/heads/refs/for/master
Pushing to ssh://hxx#www.xxxx.com:29418/project/cloud
remote:
remote: Processing changes: refs: 1
remote: Processing changes: refs: 1, done
To ssh://hxx#www.xxxx.com:29418/project/cloud
! [remote rejected] master -> refs/for/master (prohibited by Gerrit)
error: failed to push some refs to
ssh://hxx#www.xxxx.com:29418/project/cloud
Pushing to ssh://hxx#www.xxxx.com:29418/project/cloud
remote:
remote: Processing changes: refs: 1
remote: Processing changes: refs: 1, done
To ssh://hxx#www.xxxx.com:29418/project/cloud! [remote rejected] master ->
refs/for/master (prohibited by Gerrit)
error: failed to push some refs to ssh://hxx#www.xxxx.com:29418/project/cloud
Completed with errors, see above
It seems my sourcetree send push command twice. also the command is incorrect:
git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree push -v origin refs/heads/master:refs/heads/refs/for/master
It seems the correct command should be:
git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree push -v origin master:refs/for/master
where is my mistake in my configuration?
Thanks in advance.
Don't worry, "refs/heads/master" and "master" points to the same commit. This is not the issue. Looking to the "To ssh://hxx#www.xxxx.com:29418/project/cloud! [remote rejected] master ->refs/for/master (prohibited by Gerrit)" error message, it seems that you do not have permission in Gerrit to create changes on this repository. Check your permissions.

Development Log file exceeds GitHub's file size limit, even after deleting file

I tried to commit some changes in my app, and received an error that the development log was too big at 512MB. I deleted the development log file and tried again, and the same error showed up with a log size of 103.2MB. I also tried rake log:clear with the same error.
Apparently the development log file is getting rewritten. I have never used the logs and would probably not miss them...is there a way to commit to git and not rewrite the development log?
2 files changed, 0 insertions(+), 1096498 deletions(-)
rewrite log/development.log (100%)
rewrite log/production.log (100%)
[master]~/Projects/schoolsapi: git push origin master
Username:
Password:
Counting objects: 26, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (17/17), 6.90 MiB | 322 KiB/s, done.
Total 17 (delta 7), reused 0 (delta 0)
remote: Error code: 026c4e06d174bf5b0e51c754dc9459b0
remote: warning: Error GH413: Large files detected.
remote: warning: See http://git.io/iEPt8g for more information.
remote: error: File log/development.log is 103.32 MB; this exceeds GitHub's file size limit of 100 MB
Update after trying suggestions from answers 1 and 2 below:
The problem still exists. I've removed the log file from the git repo, and my local machine, inserted the .gitignore file and updated development.rb with the config logger bit. The last two lines below show that the development.log file does not exist in git or my local machine.
master]~/Projects/schoolsapi: git add .
[master]~/Projects/schoolsapi: git commit -m"Tried config logger per apnea diving"
[master b83b259] Tried config logger per apnea diving
2 files changed, 4 insertions(+), 0 deletions(-)
create mode 100644 .gitignore
[master]~/Projects/schoolsapi: git push origin master
Username:
Password:
Counting objects: 38, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (23/23), done.
Writing objects: 100% (26/26), 6.90 MiB | 525 KiB/s, done.
Total 26 (delta 12), reused 0 (delta 0)
remote: Error code: e69d138ee720f7bcb8112e0e7ec03470
remote: warning: Error GH413: Large files detected.
remote: warning: See http://git.io/iEPt8g for more information.
remote: error: File log/development.log is 103.32 MB; this exceeds GitHub's file size limit of 100 MB
[master]~/Projects/schoolsapi: rm log/development.log
rm: log/development.log: No such file or directory
[master]~/Projects/schoolsapi: git rm log/development.log
fatal: pathspec 'log/development.log' did not match any files
[master]~/Projects/schoolsapi:
UPDATE
I had earlier commits which still had the log/development.log file. Using this code provided by the selected answer below (huge thanks to this person), the problem was fixed with one small caveat:
git filter-branch --index-filter 'git rm --cached --ignore-unmatch log/development.log' --tag-name-filter cat -- --all
The caveat is that I had to use git push origin +master to override git's automatic rejection of non-fast-forward-updates. I was comfortable doing this because I am the only person working on this app. See this question:
Git non-fast-forward rejected
It seems you have earlier added/checked-in your development.log file into the git repo.
You need to remove it, and make a commit.
git rm log/development.log
git commit -m "removed log file"
In general, you should put your log directory into your .gitignore file
echo log >> .gitignore
And to completely remove all the log files (in case others were added)
git rm -r --cached log
git commit -m "removed log file"
Github has recently started enforcing a 100MB limit for maximum file sizes. https://help.github.com/articles/working-with-large-files
Edit:
It seems you have previous commits which weren't pushed to github locally.
Try running
git filter-branch --index-filter 'git rm --cached --ignore-unmatch log/development.log' --tag-name-filter cat -- --all
Side answer to prevent the file to expand on your own disk, simply log to STDOUT in development.rb:
config.logger = Logger.new(STDOUT)
You'll keep logs on your server page, but the file won't be populated anymore.

Resources