Git house cleaning

I use *Git* for several years and there are tons of articles out there about how to create new branch, pull, fetch, merge, conflict resolve, etc…

I looked at how we are doing with branches on current project – holds over 1M of code lines and +10 years of its history – I was surprised that we had almost 2 000 branches (features, patches,….). We don’t follow the standard git flow (yeah I know we should, but some in our team thinks that for  current project it is better how we do it now).  So as you can see there are many branches which needs to be cleaned up and GIT helps a lot with that.

Some Git commands which are useful for cleanup:

Clean of local branches which has been deleted on remote server:

git fetch --prune

list of all branches (local & remote )

git branch --all

List branches which are merged to remote master:

git branch --all --merged origin/master

Result of above command can be deleted

Delete local branch:

git branch -d branch_name

Delete branch from remote “origin” :

git push origin --delete branch_name

Show list of branches which are not merged into remote master branch:

git branch --all --no-merged origin/master

Results from above command are not merged into master. You need to check whether those branches are new feature/patch branches (still in work in progress) or they were not merged because you feature/patch is not needed any more (absolete and can be deleted) or you simply forget to merged them into your master (which should not happen by using tools like “git flow”)

You can scripts to combine of commands together. Let’s say we want to see last commit day and author to find out whether branch is still under development or not.

for branch in `git branch -r --merged origin/master | egrep -v 'HEAD|master'`; do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | sort -r

Similarly you can create scripts, which deletes not used merged branches and many other things you need.


Jenkins on openshift #3 – building repo from github or bitbucket

Next task I wanted to do is to create jenkins on openshift and be able to build projects from github or bitbucket. It sounds simple, but you will face quite a lot of issues. So let’s do it:

Create Jenkins gear

  • this creates gear called “jenkins” using jenkins-1 application and includes ssh wrapper “git-ssh” which helps you to overcome ssh obstacles (openshift forbids to write into .ssh folder) – as you will see we will have to solve this problem several times later as well

Set jenkins slave

jenkins needs other linux machines (called slaves) to use them for building, anyway we don’t have so much free gears, so we will use jenkins itself for it.

Manage Jenkins -> Configure System -> 
# of executors = 1
Labels = put here anything

Generate ssh key

Add public key (id_rsa.pub) to github & bitbucket

search github/ bitbucket how to do it 🙂

Add new Jenkins Job

  • in begining I mentioned that home folder is not writeable, so we are encountering several problems, like ssh can’t write into .ssh , maven can’t write into .m2 folder and so on. The only solution is to point all applications into writeable folder which is $OPENSHIFT_DATA_DIR
  • Because of the problem above, you can’t use standard maven jobs but you have to use “Build a free-style software project”
  • Source Code Management: git: your git url (i.e. git@bitbucket.org:majecek/testtest.git)
  • Build: (add Execute shell): enter:

  • this creates setings.xml file and when runing maven commands – you have to specify where is the settings file
  • check this site for more info
  • next step is to add hooks in github/bitbucket – so after push in repo they will trigger jenkins to create new build – again check it in github/bitbucke or this site

Feed Sonar with data from Jenkins

This was quite a problem. I have to admit that I didn’t finish it, but found solution. Here is another problem, sonar and specially it’s mysql runs on different gear and openshift by default don’t allow any connection between gears nor from outside.

  • install Sonar plugin into jenkins, add URL & jdbc url and all info needed
  • I had to add into Sonar Aditional properties: -DSONAR_USER_HOME=$OPENSHIFT_DATA_DIR
    • this is again, because sonar can’t write into home directory, so you have to point to writeable folder

Now you will face problem, that jenkins can’t connect to MYSQL DB – as said above, this is because gears can’t communicate to each other. There are 2 solutions:

  • set up SONAR as scaled application – scalled applications can communicate to each other
  • setup ssh port forwarding between applications

You can read more here.


How to run SonarQube 4.0 on openshift

I have managed to run latest SonarQube on openshift for free.

Because openshift has bug you can’t just have one-line command to do all setup for you, but I had to separate it into several commands and two git repos.

Bug

you can’t have .openshift folder in repo – so I have to have 2 git repos

  1. git repo with sonar without (.openshift folder)
  2. git repo with .openshift folder  with start & stop commands

How to get SonarQube 4.0 running on openshift

Description

  1. when RedHat fixes the bug, you should be fine, just with line #1
  2. cd into project
  3. add another git repo which holds .openshift folder with start & stop commands
  4. get changes from repo above
  5. pull from origin repo – git was complaining when I didn’t do pull
  6. push into openshift
  7. wait several minutes until sonar gets running

Thanks

Big thanks goes to Rui Rodrigues(@rodriguesrmb) as he managed to solve port binding problems and update java wrapper with new version


google code with git

google code allows to use SVN to access the code and I was wondering whether there is way to access code with git.

  • Import
$ git svn clone --username your-name -s https://your-project.googlecode.com/svn
few other commands
git log
gitk
git diff HEAD^^
  • Developing
git add filename
git rm  filename
git commit -a     => commits to local repository
git push          => sends to server repository
  • Update – get online and check latest changes from google code
git svn rebase
  • Export – sends commits to google code
git svn dcommit       => sends commit
  • Other useful commads:
git branch -a         => shows all branches

You have noticed that svn command connected with git “git svn”. This is as already written google supports SVN, but git can comunicate “SVN” way 🙂

You can read more here. For GIT tutorial check git pages or this tutorial.

You can use “egit” plugin to connect your eclipse to git repository.

So summary time - equivalent svn/git commands - Git for SVN User

svn checkout $SVNREP/trunk -> git svn clone $SVNREP -T trunk -b branches -t tags svn checkout $SVNREP/branches/release-0.3 -> git checkout -b local/release-0.3 release-0.3 svn commit -> git commit -a, or git add then git commit followed by git-svn dcommit svn diff|less, svn commit -> git commit -v svn merge -> git cherry-pick or use "gitk --all" svn status -> git status svn revert, --recursive -> git checkout , git checkout -f or git reset --hard svn diff -> git diff svn update -> git svn rebase --all (see also git stash)