git diff with color words

I had these files with very very long lines of text on each line. It was paragraphs worth of words but not word wrapped. These files were stored in a git repository. Now and then, someone would change a word. Diff with line based output was difficult to find what actually changed.

I figured there must be a way to see just the words. A little searching around the docs and I found it.

> git diff --word-diff=color

Perfect. I still got ling scrolling lines though. You can override that with your $GIT_PAGER variable or I just piped the output manually for a one time fix:

> git diff --word-diff=color | less -r

Much easier to see the changes!

Dual Master Git Repositories

One of the nice things about git, is the ability to work in a distributed manor. Instead of having to have a central repository for your source code, you can create a copy of your repository and do work, while sharing the changes, on any number of machines. Often, when a few developers share code, they still use git with a central repository. There isn’t anything wrong with this, but sometimes, I find myself wanting to set something like this when I only want to share code between a couple computers where I am the only developer. In this case, I don’t need 3 copies of the code. I want to be able to push or pull changes between each of the machines without having to push to a 3rd repository.

So, you might try the following steps:

# on the 1st machine
> git init .
> # make some code changes
> git add ...
> git commit
# 2nd machine
> git clone <1st machine ref>
> # make some changes
> git commit
> git push # here is the problem

When you push from the 2nd machine to the 1st, the HEAD on the 1st machine is updated. The problem is that your working copy is NOT updated. To fix this, there are two solutions.

Mediocre, recover from doing the push solution:

# warning, the following will destroy any local changes you had made.
> git reset --hard HEAD

If you had made changes on the 1st machine and forgot to commit them, you’d need to stash them, check out to a branch, or whatever.

Better way, avoid the reset all together

# on the 2nd machine
> git push origin master:refs/heads/tmp_branch_name
# on the 1st machine
> git merge tmp_branch_name
> git branch -d tmp_branch_name

Another solution would be to add a remote on the 1st machine that points to the 2nd machine. Then each machine can just pull from the other rather than doing any pushes at all.
Have fun.

Fixing the code highlighter for WordPress

Many of my posts have some sort of code, shell script, or other syntax highlighted portion. I’ve loved the code highligher plugin for wordpress. The version I’ve been using (1.8) doesn’t seem to have had any updates for quite some time. The only reason that bothers me in the slightest is that there is a syntax highlighting bug in bash commenting. Since the highlighting for the plugin is done with Geshi, You can just download the latest Geshi and overwrite the plugin geshi folder.

> cd /your/wp/plugins/code-highligher
> wget
> # untar geshi files to the current directory
> # note that the archive file has a top level geshi directory that the plugin file doesn't have.
> # now, if I did this right, my bash comments can contain 'strings' and reserved words df, ls, etc

An Open Source Library for Writing Firmware on the Cypress FX2 with SDCC

After playing around with the Cypress FX2 and SDCC for the past while, I’ve developed a library of utilities that make some of the common tasks for writing firmware and performing certain functions a little easier. I’m lucky to work for a company that approves of the open source initiative and believes that it is beneficial to give back as well as receive from a wider audience of developers. That being said, I’ve created a git repository with my library:

Here is fx2lib on github:

You can clone it with git like this:

> git clone git:// fx2lib

Here is a short list of some of the things you can do with this library:

  1. Read/Write data on the i2c bus
  2. Handle USB and GPIF interrupts
  3. Read/Write data on to a serial console
  4. Handle the common USB vendor commands
  5. Implement your own vendor commands
  6. Program the GPIF

Hope you enjoy!

Update 12/15/08: Added project home page at Sourceforge: fx2lib home.

Sharing git branches

I’ve been learning git lately. Here are a few tips for sharing branches I’ve collected during the past few weeks.

Create a branch

In git, branches are stored on your local machine. Even the commonly named “master”, is just a branch on your local machine. Master is simply set up to track a branch on a remote machine by default. To create a new local branch, you can do one of two things. Create the branch and then check it out, or do a checkout with a branch switch.

# using the branch tag
$ git branch newbranch
$ git checkout newbranch
# or just do it with checkout first
$ git checkout -b newbranch
# you can look at your local branches
$ git branch

Pushing a branch to the server

This is only applicable for using git with a central server to track changes. You can of course, use the same method to push a branch to another machine that isn’t being used as a central server (like another development machine) but I haven’t done that as much.

$ git push origin newbranch
# You can look at the new branch on the server
$ git branch -r

Checkout a remote branch

If someone else has pushed a branch to the server, you can check it out on your machine.

$ git checkout -b newbranch origin/newbranch

NOTE There is a –track switch on the checkout command you might need to add. For me, it has been doing that by default though.

NOTE After you push a branch to a server, your local branch isn’t set up to track changes on that branch. There may be another way to accomplish getting that set up, but I just check out the branch again.

$ git push origin newbranch
$ git checkout -b newbranch_renamed origin/newbranch

You could of course, delete “newbranch” and then re-checkout the branch to the same name. It’s important to realize that the names don’t matter really. Newbranch can be named differently on the local and remote machine.

Deleting branches

To delete a branch, just use the -d switch on branch.

$ git checkout master # or another branch
# do your merging before you do the delete 
$ git branch -d newbranch
# you can delete the remote branch after you have deleted the local one like this:
# the syntax is slightly confusing, but it works
$ git push origin :newbranch 

Formatting Source Code with WordPress

Sometimes you get used to doing things one way and you forget to take a step back now and then and see if there isn’t something you’re missing. In my case, I was formatting source code on this blog by putting html non-breaking spaces and &amp;lt; codes for angle brackets etc. Ouch. What was I thinking.

Here is a much better way to do it. Use the CodeHighLigher plugin. All you have to do after enabling the plugin, is place your source code in a pre tag. Add the lang attribute to the pre tag and you’ll end up with code formatted in a number of languages. The usage section of the plugin document has all the details and languages. I’ll not add that again here. Here is an example though:

int main(int argc, char* argv[]) {
 cout << "Hello Code Formatter Plugin" << endl;
< /pre>

You can include code that would otherwise be formatted by the plugin by putting a space between the end angle bracket and the forward slash for the pre tag. The plugin then outputs the ending pre tag correctly for formatting.

Here is the above code formatted by the plugin:

int main(int argc, char* argv[]) {
 cout << "Hello Code Formatter Plugin" << endl;

Again, I'm not sure why I didn't find this sooner.