Tuesday, July 15, 2014

puzzling difference between zsh and bash

Accidentally found a little difference between zsh and bash, which seems a bit annoying.

Suppose there is a simple script.

for file in `hadoop classpath | tr ':' ' ' | sort | uniq`; do echo $file; done

and the original output of `hadoop classpath`  looks like this:

zsh %> hadoop classpath  
/usr/local/hadoop/etc/hadoop:/usr/local/hadoop/share/hadoop/common/lib/*:/usr/local/hadoop/share/hadoop/common/*:/usr/local/hadoop/share/hadoop/hdfs:/usr/local/hadoop/share/hadoop/hdfs/lib/*:/usr/local/hadoop/share/hadoop/hdfs/*:/usr/local/hadoop/share/hadoop/yarn/lib/*:/usr/local/hadoop/share/hadoop/yarn/*:/usr/local/hadoop/share/hadoop/mapreduce/lib/*:/usr/local/hadoop/share/hadoop/mapreduce/*:/usr/local/hadoop/contrib/capacity-scheduler/*.jar

If I run the above in Bourne shell, the results will be like this (which will give the unique list of all jars contained in the specified classpath):

bash-4.1$  for file in `hadoop classpath | tr ':' ' ' | sort | uniq`; do echo $file; done
/usr/local/hadoop/etc/hadoop
/usr/local/hadoop/share/hadoop/common/lib/activation-1.1.jar
/usr/local/hadoop/share/hadoop/common/lib/asm-3.2.jar
/usr/local/hadoop/share/hadoop/common/lib/avro-1.7.4.jar
/usr/local/hadoop/share/hadoop/common/lib/commons-beanutils-1.7.0.jar
/usr/local/hadoop/share/hadoop/common/lib/commons-beanutils-core-1.8.0.jar
/usr/local/hadoop/share/hadoop/common/lib/commons-cli-1.2.jar
/usr/local/hadoop/share/hadoop/common/lib/commons-codec-1.4.jar
...

In zsh, however, I still get:

zsh %> for file in `hadoop classpath | tr ':' ' ' | sort | uniq`; do echo $file; done        
/usr/local/hadoop/etc/hadoop
/usr/local/hadoop/share/hadoop/common/lib/*
/usr/local/hadoop/share/hadoop/common/*
/usr/local/hadoop/share/hadoop/hdfs
/usr/local/hadoop/share/hadoop/hdfs/lib/*
/usr/local/hadoop/share/hadoop/hdfs/*
/usr/local/hadoop/share/hadoop/yarn/lib/*
/usr/local/hadoop/share/hadoop/yarn/*
/usr/local/hadoop/share/hadoop/mapreduce/lib/*
/usr/local/hadoop/share/hadoop/mapreduce/*
/usr/local/hadoop/contrib/capacity-scheduler/*.jar


This was quite unexpected to find out. Now I am wondering how to make it work in zsh.
PS I was curious enough to ask in SO about this.



Saturday, July 12, 2014

How to mock a non-declared checked exception with Mockito

Little useful mocking hack, which costed me quite some time to find:

to throw checked exception from Scala method (which usually don't declare exceptions, which disappoints frameworks like Mockito), use answer:

mockedObject.doSomething(any[Whatever]).answers{ _ => throw new CheckedException("ouch!") } 

where CheckedException is not an instance of RuntimeException. Not because RuntimeException cannot be thrown that way (it can), but because there is a simpler method for those:

mockedObject.doSomething(any[Whatever]) throws new RuntimeException("ouch!")

So if you need to throw e.g. SQLException, method 1 will work, whereas method 2 will compile but complain during the test that such type of exception does not correspond to the signature. For something like ArithmeticException, both methods will work.

Tuesday, May 6, 2014

Sorting remote Git branches in reverse commit order

Small, but useful Git + Perl command:


for k in `git branch -r | perl -pe 's/^..(.*?)( ->.*)?$/\1/'`; do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset %aN" $k -- | head -n 1`\\t$k; done | sort -r

It will sort all remote Git branches in reverse order by commit date. The result looks like this:

2014-05-06 10:18:29 +0200 33 minutes ago origin/master
2014-05-06 10:18:29 +0200 33 minutes ago origin/HEAD
2014-05-05 19:10:50 +0200 16 hours ago origin/the-hot-branch
...
2014-05-01 18:33:08 +0200 5 days ago origin/less-recent-branch
...
2014-01-27 15:44:27 +0100 3 months ago origin/totally-forgotten branch


Found on SO who quoted it from there.

One useful modification: also show the last committer:

for k in `git branch -r | perl -pe 's/^..(.*?)( ->.*)?$/\1/'`; do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset %aN" $k -- | head -n 1`\\t$k; done | sort -r

And don't forget to call first:

git fetch -p

to prune away the zombie branch references.

Then you can easily grep for someone's stale branches :)