In this post I will share a git pre-commit hook I created for aiding with code beautification.

In Linux, I work in two different coding styles: User space and Kernel.

The Linux kernel has a very specific coding style and every commit to kernel must adhere to that style.
The Linux kernel provides a very neat script named checkpatch.pl that can be used to check if your patch adheres to the kernel style. It also works on complete files.

Usually, before I prepare an a patch for pushing upstream, I run the checkpatch script and fix all the errors (well, not all off them, 80 columns ? really ??)

Git has an option of providing a scripts that can run before or after certain workflow stages.
For instance, if you ever worked with Gerrit, you probably used Gerrit’s commit-msg hook provided to generate a unique Change-Id token embedded in the commit message.

One hook that is particularly interesting in this case is the pre-commit hook. As one might speculate, this scripts runs just before the commit action by Git and has access to Git objects.

The hook I’ve written, basically runs on all the file in the commit and runs astyle (Artistic style) against each of them.

astyle if a very cool command line tool that works on various platforms. It runs against a set rules describing how to do spacing, indentation and various other beautification stuff.
It then rewrites the original file with the fixed style.

Hi there.
This is the last post in the series of a local AOSP gerrit server setup.
In this post, We’ll finalize the configuration of Gerrit, add new branches for development and see how to maintain the setup.

In the last post, we’ve finished creating the project in Gerrit.
When we develop locally, we should work on our own branches. Basically, you choose a branch/tag that you want to use as a baseline and start working from there.
As we know, in Android, there are too many projects to do it by hand for each project.
Let’s see how we can do it for all of them in one command.

How to create a baseline

For that, we’ll use the following Gerrit command line: gerrit create-branch.
As we did before, we’ll apply this command inside a repo forall command, so our Gerrit command will be applied to all projects.

First, we need to decide on the baseline. For each release, Google releases a build code associated with a manifest branch. We’ll need that tag. build code / manifest branch pairs can be taken from here.

For this tutorial, we’ll take the following Lollipop build LMY48B, which suits Nexus 5 (Which I own).

According to google, the associated branch for it is: android-5.1.1_r3

Let us first init an empty repository with that branch, and of course, we’ll do it from our server.
repo init -u ssh://ramon@localhost:29418/android/platform/manifest -b android-5.1.1_r3

Great, don’t just do repo sync yet. we want to examine the manifest first.
Open the file .repo/manifests/default.xml

You should see the following lines in the beginning:
<remote name="aosp"
fetch=".." />
<default revision="refs/tags/android-5.1.1_r3"
remote="aosp"
sync-j="4" />

The default revision tells which tag to checkout for each project.

Let’s take the android-5.1.1_r3 tag and branch out of this tag to our own branch. This will be our baseline and we could push our changes directly to that branch.

You should see sometimes the error message “fatal: invalid revision “android-5.1.1_r3“”. This is ok, it happens on project which don’t have the tag. This project are not included in the manifest we set our baseline on.

Now, when the new branches are let’s create our own manifest. We’ll go back to the place we initialized the repo.

This is a git repository, it’s actually a project, so let’s create our own branch.
git checkout -b my_manifest_branch

How to set Gerrit as a system service

Our setup can’t be finished until we know for certain that after reboot our gerrit server will start again.

To achieve that, we need to add Gerrit as a system service. I’ll show here how to do it in Ubuntu server (Debian like), for other dists like please find another tutorial online.
Basically, gerrit can be easily be installed by chkconfig. problem is that chkconfig is no longer supported in Ubuntu, which now uses upstart for it’s init process.
The right way, is to write a new Gerrit config for upstart and place it in /etc/init
Couldn’t find a it on the net, so I need to write it myself.
When it will be ready, I’ll post it in the blog.

Gerrit Code Review is not shipped with Bouncy Castle Crypto SSL v151
If available, Gerrit can take advantage of features
in the library, but will also function without it.
Download and install it now [Y/n]?
Downloading http://www.bouncycastle.org/download/bcpkix-jdk15on-151.jar … OK
Checksum bcpkix-jdk15on-151.jar OK
Generating SSH host key … rsa… dsa… done

Allright. We’ve got Gerrit running now.
A thing worth mentioning, the first user that logs into Gerrit becomes an admin. so make sure to login first with the administrator user.

Gerrit can work with several authentication models. I chose OpenID for this tutorial.
If you don’t have OpenID server on your machine, you can use external service such as Yahoo for instance. (Lately Google stopped supporting OpenID)

Let’s get started. open up a browser and browse to Gerrit site.

In the upper right corner you should see Register and Login links.
Register first using OpenID and log in.

Now, let’s set our SSH keys so we can work from command line. we’re doing it as the Admin user for the Ubuntu Server, not as the Gerrit user.

The former will be used for administration, it will have permissions to review, merge, delete, etc.
The latter will have only view and submit change-set permission.

On the menu, press on People –> Create New Group
Enter in group name “android-admin” (Without the quotes)

Do this again for “android”

Now, let’s create a parent project for all the AOSP tree. All configuration we apply to that project will be inherited to the children projects. if we skip this step, all AOSP projects will inherit from the default “All-Projects” project, which is too global.

On the Gerrit GUI, Press on Project -> Create New Project.
Fill in the following:

Project Name: Android
Rights Inherit From: All-Projects

Tick the “Only Serve As Parent For Other Projects” and press on “Create Project” button.

First command will create all the projects in gerrit. it should be rather quick.\
The Second command will change the parent project for all the projects we’ve just created.
The Third command will actually push all the code into the repositories, this should take a while…

In next step, which will take some time, we’re going to push all of git tags and objects to the projects we’ve created. It may fail, but if it is, you can run it again until it succeeds. I found that increasing the Virtual machine RAM solved the issue for me.

Okay.
That’s it for this time, in the next part I’ll show you how to maintain the gerrit server. how to add it as a startup service, how to update the mirror and push new releases of Google in to it.
Cy’a

In this tutorial I’ll explain how to set a local gerrit server that hosts Android source code.

After completing the tutorial you’ll have a fully runnable local AOSP mirror with local Gerrit server.

First thing first. we need a Linux server. if you don’t have one available, you can use a pre-installed Ubuntu 15.04 Server I created – here.
Actually, this tutorial was tested on this machine, so you’re unlikely to get in trouble if you use that.

This tutorial assumes that package management is apt-get. If you’re using other, please do the appropriate adjustments.

Gerrit Prerequisites:

1. Java JDK > 1.7
2. Git
3. SSH server
4. DB

I choose to work with mySQL, put you can also work with various other DB software. consult the Gerrit documentation here.