Git Graphical Tools

I was just wandering around to look for some git graphical tools when i came a post on Stackoverflow(See below for the links). That post list some nice graphical tool(showing all branches, commit, tags, merging), I tried creating a somewhat similar project to that shown in the post just to test all that tool, here that are:

gitx

This is gitx which i think is only for Mac OSX

qgit

qgit

gitg

gitg

gitk

gitk

SmartGit

I posted this post on LinkedIn and I was pointed towards a very nice tool, SmartGit. I downloaded it and tried it with this scenario, thats the result

Smartgit

Bare Git

On bare git with the command git log --graph --full-history --all --color --pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"
baregit

info from Stackoverflow:
1. http://stackoverflow.com/questions/3509776/which-git-tool-generated-this-tree-view
2. http://stackoverflow.com/questions/1838873/visualizing-branch-topology-in-git

Java “choice factory”

This is suitable for students MAY BE. Sometimes, in programs that we have to write in academia, we just need to display a list of choices and then get a number representing the choice. Having manually to write the same code is not intelligent. Isn’t it?

I was doing the same shit right now. Then, i created a simple “factory” (Don’t get religious on this, its not a real factory) to display the choices:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class ChoiceFactory {
	public static int getChoice(String choices[]){
		String mainChoice = null; 
		Scanner in = new Scanner(System.in);
		do {
			if (mainChoice != null) 
                            System.out.println("Please enter a 'decent' choice !!");
			int choiceNum = 1;
			for (String currentChoice:choices){
				System.out.println(choiceNum+". "+currentChoice);
				choiceNum ++;
			}
			System.out.print("Enter Choice:");
			mainChoice = in.next();
		} while (!mainChoice.matches("^[1-"+choices.length+"]*$"));
		return Integer.valueOf(mainChoice).intValue();
	}
}

In case you see any improvement or error, please just leave a comment, I’ll correct it right away.

Create Inline array in Java

In Java, sometimes, we want to create to pass an array to as an argument without explicitly creating a variable to hold a reference to the array. We can do this like:

Instead of

String choices [] = {"Create a Plate","Delete a Plate","View Plates","Get Receipt"};
ChoiceFactory.getChoice(choices);

We can just do:

ChoiceFactory.getChoice(new String[] {"Create a Plate","Delete a Plate","View Plates","Get Receipt"});

Similarly, we can do the same for a list,

List acceptedTypes = new ArrayList(){{add("Kingdom");add("Class");add("Order");add("Phylum");add("Species");add("Genus");add("Family");}};

Simple :)

Extract Audio from webm file linux

You are on linux and there is no software to retrieve audio from a webm file, no worry, open source is here :)

if you are on ubuntu, run sudo apt-get install ffmpeg libavcodec-extra-52, on Red Hat distribution, run
yum install ffmpeg

After successfully installing it, you can do a man ffmpeg to see the command and argument description.

To convert a simple file, just do

ffmpeg -i videoFile.webm -ab 320k -ar 44100 audioFile.mp3

Facebook offline_access is offline now

Okk, “moment de verite”, Facebook offline_access extended permission has been on 5th December 2012. Why? I don’t know but if I ask myself, I think its mainly because of security or abusive use.

So, what happens to those who are currently using Facebook offline_access extended permission?

<h2>How is it now?</h2>
Though there is no longer offline access, there is the capability to extend short lived session. Facebook automatically renews short lived session I think every 1 to 2 hours but if you want to use the access token for longer period, ensure that you renew it. So, yeah, for now, to confirm, long-lived session are for 60 days while short-lived one are for 1-2 hours.

for those who were not using the extended permission offline_access, you have no changes to make to your application. But application using offline_access will have to add some chunks of code for new users. If you were using offline_access extended permission, this will no longer be asked in the application login dialog box when the user uses your application. For new users, you’ll get a short-lived token which you will use to extend to get a long-lived one. Now, if you try to extend a token which already a long-lived one, you may get the same token or a token with a longer expiration time.

To get a long-lived session, just make a call on this URL with the required parameters replaced,

https://graph.facebook.com/oauth/access_token?

client_id=APP_ID&
client_secret=APP_SECRET&
grant_type=fb_exchange_token&
fb_exchange_token=EXISTING_ACCESS_TOKEN

So, it make sense for you to have a short-lived token before submitting the above request

Further information goes here:)
1. http://developers.facebook.com/roadmap/offline-access-removal/
2. http://developers.facebook.com/docs/howtos/login/extending-tokens/
3. http://stackoverflow.com/questions/10487049/facebook-long-lived-and-short-lived-access-tokens-and-their-expirancy-after-off

Reload bash in Linux

I was a fool, ya I was. When I newly started with Linux, each and every time I was changing my bashrc, I was restarting my PC just to reload the new bash setting, crazy naa. In fact, you can do this in a very easy way:

just run this command:

source .bashrc

and it will do the job

Openshift Console in terminal

Needs an openshift console to control your openshift application, just install rubygem-rhc, i’m using fedora 17, I just installed it with yum. First, do a yum provides to check the version available from repository and then you install it

1. yum provides rubygem-rhc

2. yum install rubygem-rhc-0.91.12-3.fc17.noarch

The openshift console is simply your terminal and some commands to control the application from it.

Now that you have installed, you can take control of your openshift account from your terminal:

Openshift console command

rhc-app ( | cartridge cartridge-action | --help) [args]

in your terminal, just run the command rhc-app, you’ll get the syntax of the command including all the command, cartridge-action and arguments,

Now suppose you want to restart your application,

its as simple as

rhc-app restart -a applicationName -l usernameOpenShift

if everything goes fine, it should output a success
RESULT:
Success

There are some links where you can find more information:

1. https://openshift.redhat.com/community/get-started
2. https://openshift.redhat.com/community/kb/kb-e1067-running-rhc-commands-without-re-entering-password

Broadcom on fedora 17 due to missing firmware

Getting Firmware missing on broadcom fedora 17

broadcom fedora 17.So, I’ve just installed Fedora 17, no great changes in UI, but it’s nice. Now, as always, the problem I get is to set up wireless on laptop(I love my old Dell Inspiron 1545) which uses a broadcom 4312. In case, you are having the same issue, you can follow the procedures below or follow the links found at the end of this post.

First, ensure you have b43-fwcutter,

yum install b43-fwcutter

Now, perform the following:

1 export FIRMWARE_INSTALL_DIR="/lib/firmware"
2 wget http://www.lwfinger.com/b43-firmware/broadcom-wl-5.100.138.tar.bz2
3 tar xjf broadcom-wl-5.100.138.tar.bz2
4 sudo b43-fwcutter -w "$FIRMWARE_INSTALL_DIR" broadcom-wl-5.100.138/linux/wl_apsta.o

The above information has been found in the links below:

1.http://linuxwireless.org/en/users/Drivers/b43#If_you_are_using_the_b43_driver_from_3.2_kernel_or_newer
2.http://ask.fedoraproject.org/question/2053/wireless-broadcom-4312

RESTful Web Services, return an Id or a URL?

In the RESTful world, there is a discussion about whether to return an id or a URL. Some RESTful Web Services returns identifiers while others complete URL for resources. But which one to use? Ok, suppose that our Web Service returns JSON encoded data and that a User can takes several books on loan.

Now, suppose a GET /user/1/books returns book ids on loan for user 1 as {41,56,67,12,89,23,56}.

So, the discussion is whether to return {41,56,67,12,89,23,56} or {"http://baseurl.com/book/41","http://baseurl.com/book/56",
"http://baseurl.com/book/67","http://baseurl.com/book/12",
"http://baseurl.com/book/89","http://baseurl.com/book/23",
"http://baseurl.com/book/56"}

After reading several articles and blogs, I’ve found that numerous developers prefer the second one because the baseurl can be changed without affecting the application. However, I completely disagree with this because even with the first one, the base url can be made dynamic. Also, with the second one, unnecessary data are sent back with is cumbersome.

But, I prefer a different solution. Suppose you have a REST interface running at http://example.com/v1, I find it better to expose the base URL itself. Say that a GET http://example.com/baseURL returns http://example.com/v1. Then, you can just get the base URL once that application first start up, and put it in a global variable so that it can be accessed everywhere throughout the application.

This is a solution which I though on the fly after reading several posts advising the use of complete URL. It may be wrong but one thing for sure, returning completing URL for a resource may good but not the best.

Git

History

So, it’s always nice to know a bit of history. What happened was that the Linux Kernel was being maintained using bitkeeper free of charge. However, later Bitkeeper free of charge license terminated. So, Linus Torvalds decided to create Git to maintain the Linux Kernel Development.

Differences with other VCSs

Git is a Version Control System. There are many other VCSs but what’s good with it is that it was initially designed to maintain the Linux Source code which is huge. By that, you can understand that it’s going to consider lots of aspects such as size, bandwidth optimization and many others into consideration.

There are several differences between Git and other VCSs is that Git stores whole snapshots of the project rather than only deltas. Also, files are checksumed using SHA-1 Hash before they are stored and referenced using the checksum.

Now, a file in Git can be three states:

  1. Modified
  2. Staged
  3. Committed

It’s very simple. Whenever, we create/modify a file, we need to explicitly tell Git (Staging Process) to include the file in the next version. Now, to create this “next version”, we simply commit.

Configuration Files

Git has 3 level of configuration:

  1. System Wide Configuration
  2. User Specific Configuration
  3. Project Specific Configurations

What’s more interesting is that the configuration are overridden. So, User Specific configuration overrides System Wide Configurations and Project Specific configurations overrides User Specific configurations.

To view configurations, you can simply use this command:
git config --[system|global|local] -l

1. System Wide Configurations
Global git configurations are stored in the /etc/gitconfig file. To add/modify to this file, simply pass —system as an argument
git config --system user.name "Noorani Bakerally"

2. User Specific Configuration
All user specific configurations are stored in a .gitconfig file. This file is found in the home folder of users. So, to display it, simply run cat ~/.gitconfig.To add/modify to this file, simply pass – -global as an argument
git config --global user.name "Noorani Bakerally"

3. Project Specific Configuration
So, project specific configurations overrides all configurations. So, add/modify these project specific configurations, simply ensure that in the git repository of the project and do git config user.name "noorani Bakerally"

Git Objects

There are 4 types of object:

  1. blog: Consider it as a file in a git repository
  2. tree: A directory which can contain other trees or blobs
  3. commit: A specific point in time containing meta information and a pointer to the previous commit
  4. tag:A milestone in the life of the project. e.g. A new version

Also, all object have 3 attributes, a type(1 of the above 4), a size and its content.

Git basics

To create a git repository
mkdir new_git_repository
cd new_git_repository
git init new_git_repository

To clone an existing git repository
git clone <URL_Existing_git_repository>

To view status of files from the last commit
git status

File tracking
To manage your files, git track your file so that it can identify and changes to it. This is not automatic as you can avoid tracking some files but not adding adding or simple creating pattern in a gitignore file(we’ll see that later)

To explicitly tell git to track a file, do

git add <filename>

Creating/managing commit
git commit -a -m "the commit message", -a:all, -m:message
A commit is like a milestone in the life of a project. It’s like saving the project state in the project life time. A commit is represented in git as a 40 character SHA-1. Anything that is committed in git can be obtained again. At any point in time, you can return back to a specific commit using the reset.

git reset --hard <commit SHA1 Code>

The above command is dangerous. Suppose currently you have some modifications pending (not yet committed), if you run it, you lose all your modifications. Remember, anything committed can be regained. The negation is also true.

Suppose that you have created a commit and have forgotten to include some files. Therefore, just after the commit, perform the necessary modifications and add the files. Then do a

git commit --amend

To undo a commit,

git reset --[soft|hard] HEAD^
soft leaves all unchanges while hard removes all changes and returns back to the previous commit(HEAD^ refers to the previous commit).

Revert and Undo is different. Revert returns to a previous commit and commit this return. To undo a commit using revert,

git revert HEAD

To revert to a previous commit if you know the commit code:

git revert <SHA-1 Code of commit>

You can also revert by going back in history without entering the hashcode:
# move the branch pointer back to the previous HEAD
git reset --soft HEAD@{1}

Just by changing the number in the {}, you can revert back

Sometimes you need to know where you are:
To view the location of the current head,
cat .git/HEAD

this outputs the reference of the current head, ref: refs/heads/master

now to view the current head, simply perform a cat on the file:

cat .git/refs/heads/master

To get the difference between two branches:

git diff –name-status –color branch1..branch2

e.g. git diff –name-status –color master..development

To change the name of the branch you are currently are:

git branch -m newname

File Operations within a git repository
Like in any other folder, in a git repository, you can perform file operations like creating,removing,editing or moving file. For all file operations, you need to explicitly tell git about it. For the “create” operation, consider the file tracking above.

References:

  • http://www.codinginahurry.com/2011/02/05/three-levels-of-git-config/
  • http://linuxmanpages.net/manpages/fedora13/man1/git-config.1.html
  • http://git-scm.com/book
  • http://www.gitguys.com/topics/head-where-are-we-where-were-we/