Have you ever triumphantly pushed your final changes to that annoying problem up for review and totally forgot to take your debug out amongst all the excitement? Of course you have!

So have I. In fact, I did it today. Twice.

As a developer, I thought to myself “there must be a better solution to this problem” and then I remembered about the weird bash commands I have to request from friends and previous colleagues whenever I get a new laptop or start a new job — those commands that put my current branch name and dirty status onto my terminal prompt so I can see what’s up at a glance. Those scripts I’ve never really looked at before.

Lets take a look!

So we have a few functions, what do they do?

The “repo” function, simply returns the name of the current git repo. This is a combination of basic Unix command line tools such as head, awk and sed to manipulate the response from the “git remote -v” command.

Next up is “parse_git_dirty”. This uses a similar technique as above, along with “git status” to determine if there are any uncommitted changes in the local repository. If there are any changes, it simply outputs an asterisk.

Finally we have “parse_git_branch” which finds out the current branch and combines it with the previous functions and returns some information. We can then add this information to our command prompt by modifying the “PS1” variable — there is much more detailed information on how to do this right here.

If you put all of the above code into your “.bash_profile” file and open a new terminal, you will end up with a something like this (note, you must be in a git repository!):

The first “darknet” is the name of the current directory I am in. Inside the square braces we have the name of the current repository (darknet) and the current branch (master). If I were to make some changes, I might see something like this:

Notice the asterisk? That means I have uncommitted changes that I need to deal with. Cool, huh?

So. I don’t think this new information to a lot of people, this is quite common — but I mentioned something about detecting rogue debug code earlier, so lets have a look at that.

We will need to create a new function to detect if we have any unwanted debug code in there — I’m writing this form a JavaScript perspective so I am wanting to detect any mention of the “console” object. Here’s my new .bash_profile:

You can see the new function “parse_git_console” function, which simply parses the output of “git diff” to search for any mention of “console.”. If any is found, it outputs a carat. I add this to my parse_git_branch function and now I get the following if I have any rogue debug code:

A nice and easy way to know if I have something in my code that I shouldn’t have. Like I said, this is coming from a JavaScript point of view, but it can easily be modified to look for whatever you want in whatever language you want.

Happy debugging!