Get gerrit commit with only short hash [duplicate] - gerrit

When working with Gerrit (Code Review), I often need to get a copy of a given patch set for testing or validation purpose. The obvious and easiest way is to download the archive or the patch file through the Gerrit Web interface and manually apply it to my local source.
While the above steps are pretty straightforward and fulfill my needs, in the best world I would like to have the patch set appearing as a commit in my local Git.
I was looking around and didn't find the solution. I found some sparse info that once compiled together gives the following solution.
Say that you want to pull the patch set 2 of the Gerrit change 1222:
Find the remote refs we are interested in:
$ git ls-remote | grep 1220
From http://something.com:8081/MyProject
e2e0212a59240ac5cd7c11220c35542523f44b59 refs/changes/13/713/1
b8c4dceea5eaf1bad711b0ea6938c80ec932726a refs/changes/20/1220/1
6f20c182ec7f54a2aa9e8f6188a0eef1b0790df4 refs/changes/20/1220/2
ed94a98386d224ce3d86004ce99f61220905a077 refs/changes/22/1222/1
Pull the refs:
git pull origin refs/changes/20/1220/2
This will create a Git commit point that you could eventually rebase:
git rebase

This feature is standard in the Gerrit UI.
On the top right of the UI for a patch, click Download, and you will see something like:
When you are navigating the patches you go to the download section and copy the command line command for checking out the patch set, for example like this:
git fetch https://gerrit.googlesource.com/gerrit refs/changes/03/64403/2 && git checkout FETCH_HEAD
Then I normally create a branch with the review number and patchset as name
git checkout -b b64403-2
For here you can work normally and commit your changes or cherry-pick/rebase your changes on this change.
Once the review of r64403 is done your code can be merged or when there is another patchset submitted you will need to do the same thing again.
If you do not see the options to download the option to Checkout or Cherry Pick you need to edit the gerrit.config, something like this:
[download]
scheme = ssh
command = checkout
command = cherry_pick
More details can be found in the Gerrit Documentation
Update:
As barryku correctly points out, in the later version you need to download the downloads-commands plugin. This can be done during the initial setup or by using the following command:
java -jar gerrit-2.11.4.war init -d review_site --batch --install-plugin download-commands

Or you can use the -d option to git-review. For example, assuming you were working the with nova-docker repository and were interested in this change in gerrit:
https://review.openstack.org/#/c/148486/
You could download the latest patchset like this:
git review -d 148486
Or you can use the change id:
git review -d I35729a86e211391f67cc959d19416c9125c6f9eb
You can also request a specific revision of the patch by appending a comma and the patch number. E.g, to get the second revision of that patch:
git review -d 148486,2

I am not 100% sure what your question is. Sounds like you want to easy the workflow or typing. larsks mentioned already git review which is mostly used.
For your case, maybe it helps to download all ref's automatically so you can reference them directly. You can always all specified ref's like with
git fetch origin "+refs/changes/*:refs/remotes/origin/changes/*"
Then you can work locally with the commit id.
A simple git alias or scripting it for all refs can be easily done. An example of such a while loop can be found in the script on https://github.com/saper/gerrit-fetch-all With such a small shell snippet you can easily accomplish to skip one part of the ref id to easier reference them:
Server side: Client side:
refs/changes/13/713/1 refs/head/713/1
refs/changes/20/1220/1 refs/head/1220/1
refs/changes/20/1220/2 refs/head/1220/2
refs/changes/22/1222/1 refs/head/1222/1

As mentioned in the comments, you can just get the right git command from the gerrit GUI. If you really dislike GUIs, or you want to automate it (and for some reason can't use git-review), you can use the gerrit API:
curl -s 'https://<your gerrit server>/r/changes/<change id>?o=CURRENT_REVISION&o=DOWNLOAD_COMMANDS' | tail -n+2 | jq -r '.revisions[.current_revision].fetch["anonymous http"].commands.Pull' | bash -
or
git pull origin `curl -s 'https://<your gerrit server>/r/changes/<change id>?o=CURRENT_REVISION' | tail -n+2 | jq -r '.revisions[.current_revision].ref'`

Related

Puppet Code Manager setup issue with Bitbucket

I have just installed puppet server enterprise and successfully added a few nodes and got some custom modules running also. I am now wanting to move to Code Manager before we get too deep in it.
I have followed the instructions for creating an empty Bitbucket repo here and initializing it with one single file environment.conf on a production branch as described in that link.
I have then followed the steps here to configure Code Manager but when I get to Test the control repository section to test the connection with puppet-code deploy --dry-run I get the following error:
--dry-run implies --all.
--dry-run implies --wait.
Dry-run deploying all environments.
2021/12/21 20:21:12 ERROR - [POST /deploys][500] Errors while collecting a list of environments to deploy (exit code: 1).
"/opt/puppetlabs/puppet/lib/ruby/gems/2.7.0/gems/rugged-0.27.7/lib/rugged/repository.rb:258: warning: Using the last argument as keyword parameters is deprecated\nERROR\t -\u003e Unable to determine current branches for Git source 'puppet' (/etc/puppetlabs/code-staging/environments)\nOriginal exception:\nFailed to authenticate SSH session: Unable to send userauth-publickey request at /opt/puppetlabs/server/data/code-manager/git/git#git.company.com-1234-in-puppet-control-repo.git\n"
I have added the puppet server's SSH pub key to the bitbucket repo's access tokens.
There are a few things in that error message im not fully understanding.
Unable to determine current branches for Git source 'puppet' - What is meant by source 'puppet' - my repo is called puppet-control-repo...?
Failed to authenticate SSH session: Unable to send userauth-publickey request - My puppet master's SSH keys are in the token list for that repo so confused here also.
Any guidance would be appreciated.
UPDATE (13-01-2022):
I can successfully clone on puppet server using command
git clone ssh://git#git.example.com:1234/project/puppet-control-repo.git --config core.sshCommand="ssh -i /etc/puppetlabs/puppetserver/ssh/id-control_repo.rsa"
Note sure why puppet is still returning:
Failed to authenticate SSH session: Unable to send userauth-publickey request
I don't know if you saw the instructions here https://puppet.com/docs/pe/2021.4/control_repo.html#managing_environments_with_a_control_repository but you can run
puppet infrastructure configure
which makes sure the files have right permissions.
I would also test attempting a clone with keys works outside of code deploy
git clone -i /etc/puppetlabs/puppetserver/ssh/id-control_repo.rsa your_gir_url
If this works it may be worth being aware of an issue we experienced on github https://puppet.com/blog/how-githubs-protocol-changes-impact-your-puppet-code-deployments/ which depending on bitbuckets approach to protocal may be having a similar affect.
We are updating docs to recommend the usage of more secure keys ed25519 creating as per the article.
if a manual clone doesnt work it suggests bitbucket doesn't have your public key correctly
Also a more complete debugging command is
runuser -u pe-puppet -- /opt/puppetlabs/puppet/bin/r10k -c /opt/puppetlabs/server/data/code-manager/r10k.yaml deploy environment production --puppetfile --verbose debug2
FOLLOWUP
On investigation we found https://support.puppet.com/hc/en-us/articles/227829007 which showed ssh:// was required at the start of r10k_remote making an example command of ssh://git#bitbucket.org:davidsandilands/control-repo.git
I have requested updates to https://support.puppet.com/hc/en-us/articles/227829007 to highlight this is not a version confined issue and asked for the puppet code manager configuration docs to be updated to reflect this may be required.
I see that you have a .pub file in the ssh directory. I believe it's expecting a private key there.
Also do you have the master class set up to point to your repo inside of Puppet Enterprise web ui?
You'll want to set the following parameters on that class.
code_manager_auto_configure = true
r10k_private_key = $PRIVATE_KEY_IN_SSH_FOLDER_ABSOLUTE_PATH
r10k_remote = Your git URL
The PE Master can be found in Node Groups on the PE Web UI Node Groups -> PE Infrastructure -> PE Master
Thanks to #david-sandilands for helping me resolve this and guiding me to this article via the puppet community slack. Top guy!
EDIT 1:
The solution was documented here: https://support.puppet.com/hc/en-us/articles/227829007-Fix-your-Bitbucket-Stash-Code-Manager-configuration-in-Puppet-Enterprise-2015-3-to-2017-2
However the documentation was out of date as it affected version 2021.4 also.
In short:
r10k_remote = "ssh://git#git.company.com:1234/project/control-repo.git"
Not
r10k_remote = "git#git.company.com:1234/project/control-repo.git"
When working with Bitbucket Server.
EDIT 2:
Puppet have since updated their documentation:
https://puppet.com/docs/pe/2021.5/code_mgr_config.html#code_mgr_enable

How to set up 1-way sync from Bitbucket Cloud to a local folder

This might sound like a stupid question, and the use-case is rather simple, but I haven't been able to find a decent and simple solution.
In short:
I have a Bitbucket repo that I want to have synced to a local folder on my local server.
So whenever there's an upstream change, the most updated version of the file must be copied to the local folder. There is never a push/commit from local-to-cloud, it's merely a 1-way read-only sync.
Thanks in advance for any suggestions! (maybe the solution is so obvious that I don't see it?)
You don't explain what software is running on your local server, but assuming this is some flavor of UNIX/Linux/macOS and you have crontab access, the easiest thing is probably to just schedule a cron job to pull updates.
A command like the following will schedule a git update every 60 seconds, logging the output to a file:
echo '* * * * * cd $HOME/path/to/git/workdir && git pull -q --ff-only >> update-log 2>&1' | crontab
Note 1: This assumes your user currently has an empty crontab on the server, if you don't then you should instead use crontab -e to manually append the directive to your existing crontab.
Note 2: You'll need to ensure your account on the server has permission to access the BitBucket repo without a tty connection (e.g. without SSH agent forwarding), so you might need to fiddle with authentication to set that up (which is beyond the scope of this answer). For a public BitBucket repo, cloning via HTTPS without a user name is probably the simplest approach, since no authentication is required.
Note 3: The first * in the directive above can be adjusted to select a different polling frequency, e.g. 0,15,30,45 for every 15 minutes. If you omit the 2>&1 then you should get an email for any errors (assuming SMTP is configured on the server).
Note 4: The git command embedded above assumes you never rewrite history in the upstream git repo or manually modify the local directory. If either is a possibility, then you might instead want to use git pull -q --rebase or even git fetch && git reset --hard '#{upstream}'

Automatic version in DATETIME format on every "git push" on Heroku or elsewhere

Every time I change my project (whether in PHP, Ruby, Erlang, Javascript) I need that before executing commands routine:
git add. git commit -am "blablabla" and git push heroku master
(or another provider instead of Heroku) I automatically add a version of the project based on the date and time in a project file (for example: layout.hbs or application.html.erb).
I can use also a macro in Sublime Text or something else in Terminal, just I do not know what to use.
I don't want to use a ENV_VAR because I want that date and time WRITTEN in the file.
I'd also like to hear from you if you have solved in other ways, even using environment variables.
Why don't you simply use git ?
You can get the date of your last commit by running git show -s --format=%ci
If you want it to be displayed somewhere in your app, you could run the git commands on the server side :
# add this action in the controller of your choice,
# then you can access those variables in your view
def version
#version_datetime = `git show -s --format=%ci`
#version_tag = `git describe --tags`
end
That's just an example, but it should hopefully help you to get started.

Adding a variant to an existing homebrew formula?

I'd like to modify the dtc formula to build a specialized version of dtc used with the Beaglebone Black (and other device-tree ARM boards). The problem is multi-fold. The dtc formula provides bottled versions, and installed 1.4.0. I want to install a version that comes from newer sources, but not the absolute latest, and applies a patch available elsewhere. I can build this manually on OS X, but I don't know the best way to make it available in homebrew.
I'd like to make it a variant. Either an entirely separate formula, or something within the current formula that can be selected with a command-line parameter. But I'm not well-versed enough in brew to know how best to do this.
I tried creating new formula called "dtc-dyn" but it doesn't like dashes in the name.
I'd also like to make it a bottle, for others' convenience, but that seems more complicated.
I tried just modifying the current dtc and removing all the bottle stuff, then modifying the URL, version, and adding system steps. But some of the operations fail (that seem to work when run from the original build script):
def install
git_sha = "f6dbc6ca9618391e4f30c415a0a09b7af35f7647"
system "git checkout master -f"
system "git pull"
system "git checkout #{git_sha} -b #{git_sha}-build"
system "git pull --no-edit https://github.com/pantoniou/dtc dt-overlays5"
system "make clean"
system "make all"
end
Sadly, this doesn't work:
$ brew install dtc
==> Cloning git://git.kernel.org/pub/scm/utils/dtc/dtc.git
Updating /Library/Caches/Homebrew/dtc--git
==> Checking out branch master
==> git checkout master -f
==> git pull
==> git checkout f6dbc6ca9618391e4f30c415a0a09b7af35f7647 -b f6dbc6ca9618391e4f30c415a0a09b7af35f7647-build
2015-09-21 00:22:37 -0700
git checkout f6dbc6ca9618391e4f30c415a0a09b7af35f7647 -b f6dbc6ca9618391e4f30c415a0a09b7af35f7647-build
fatal: reference is not a tree: f6dbc6ca9618391e4f30c415a0a09b7af35f7647
READ THIS: https://git.io/brew-troubleshooting
The original script can be found here.
Anyway, what's the best way to approach this? I think some kind of with-option, based on some of the example scripts, but I'm not sure how to avoid the bottle business. Thanks!
Second Approach
Okay, after more doc reading and experimenting, I'm trying this:
class Dtc < Formula
desc "Device tree compiler"
homepage "http://www.devicetree.org/"
url "https://mirrors.kernel.org/debian/pool/main/d/device-tree-compiler/device-tree-compiler_1.4.0+dfsg.orig.tar.gz"
mirror "https://mirrors.ocf.berkeley.edu/debian/pool/main/d/device-tree-compiler/device-tree-compiler_1.4.0+dfsg.orig.tar.gz"
sha256 "f5f9a1aea478ee6dbcece8907fd4551058fe72fc2c2a7be972e3d0b7eec4fa43"
version "1.4.0"
option "with-symbols", "Add symbols/fixup support (-# option)."
bottle…
if build.with? "symbols"
url "http://git.kernel.org/pub/scm/utils/dtc/dtc.git", :revision => "f6dbc6ca9618391e4f30c415a0a09b7af35f7647"
system "git", "pull", "--no-edit", "https://github.com/pantoniou/dtc", "dt-overlays5"
end
def install
system "make"
system "make", "DESTDIR=#{prefix}", "PREFIX=", "install"
mv lib/"libfdt.dylib.1", lib/"libfdt.1.dylib"
end
end
Unfortunately, the git pull fails because it does that before cloning the git repo, and it's not in the right directory. I first considered patch do…, but it wasn't clear how to apply the patch via git pull. I tried this:
if build.with? "symbols"
url "http://git.kernel.org/pub/scm/utils/dtc/dtc.git", :revision => "f6dbc6ca9618391e4f30c415a0a09b7af35f7647"
patch do
system "git", "pull", "--no-edit", "https://github.com/pantoniou/dtc", "dt-overlays5"
end
end
But it still tries to do the git pull before cloning the repo and cding to it.

Is there any way to recover my application in ruby on rails if I have a copy at the git repository?

I have stupidly named a new web_app the same name in my ruby on rails directory.
And now I tried to look back the "blog" in the browser, all is gone.
Now, I have a copy of the application in git repository.
Is there anyway I can pull it down to the command prompt in my computer?
Hope someone can advise me how to.
I have so far tried git pull origin master and the rails command prompts only said master -> Fetch Head
But, still nothing shows up in the browser when I type localhost:3000 etc..
Thanks.
You should be able to clone it.
Run the git clone command.
git clone (the url to your remote repository)
If you go to your repository on the right hand side of the page you will find the url to clone.

Resources