A Short Story of Pruning Remote Branches

Recently I started to use git in a more professional setting. I have been using it for several years on my own but things change once you start to use it in a team setting. Once in a team setting, branch management becomes something you have to pay attention too.

Yesterday I created a branch that was going to merged back into the master branch and deploy once I was done developing it. A teammate was going to review the branch and deploy, so when I was done with development I pushed the branch to the remote repository for him to pull down. This was when we ran into our first problem.

Seeing New Remote Branches

My teammate ran

git remote -a

This lists remote branches but the remote branch I had just created. Where to did it go?

Git tracks the remote branches locally and so you have to refresh your tracking list (this is important to remember for later). To be able to see the new remote branches he needed to run:

git fetch

Fetch updates the your locally-tracked remote branches. Think of it like a local cache of remote branches. After awhile you need to refresh your latest list.

Removing Old Branches

Now that he was able to see the branch, he reviewed it and merged it into the trunk. Using good branch management, he deleted his local branch and the remote branch for the feature:

// delete local
git branch -d branchname 
// delete remote
git branch :branchname

When I got back to my desk I pulled down the new trunk changes. I expected this would refresh my list of repository’s and I would not see the feature branch my teammate just removed. I was wrong. When I ran git remote -a I found the remote feature branch was still listed.

I was a bit confused, but then remember that git tracks branches locally. After a quick google search I found my answer. To refresh your local remote branch list you need to run the command:

git fetch --prune

Automating

Don’t want to remember to do that every time? While doing the research into remote branches I found that as of git version 1.8.5 you can configure git to do this automatically for you. Here is the configuration:

git config fetch.prune true

You can learn more about it from Alberto Grespan’s blog post Always prune remote-tracking branches

I hope this short story helps you understand how you might end up with to many remote branches in your local repository and how to clean them up.

SQL Server Management Studio (SSMS) over VPN

As with most days, today I learned something new. This is a great tip that was passed onto me by one of my clients. Currently, as a contractor, I work for many different companies. Almost always, I have to do some sort of database work involving Microsoft SQL Server over a VPN.

Generally, I am given access to SQL Server using Windows Authentication based on my client’s domain. Until today I always had to log-in into one of their machines using Remote Desktop in order for SQL Server Management Studio (SSMS) to recognize my identity on the client’s domain.

Logging into remote machines is always a pain: getting/setting permission for remote access to the machine, restricted to a single monitor (RDP doesn’t work well on multiple monitors), Alt-Tab doesn’t work like it should… the little annoyances add up.

But no longer… This simple trick will allow you to use your local instance of SSMS:

Solution

  1. Find your SQL Server Management Studio EXE. Usually located somewhere like: C:\Program Files (x86)\Microsoft SQL Server\[version number]\Tools\Binn\ManagementStudio\Ssms.exe

  2. Create a shortcut to it on the desktop.
  3. Right click and select properties.
  4. Add the following before the Target information: C:\Windows\System32\runas.exe /user:yourclientsdomainname\yourusername /netonly

  5. Add -nosplash after Ssms.exe after the Target information
  6. Your Target should look like: C:\Windows\System32\runas.exe /user:yourclientsdomainname\yourusername /netonly "C:\Program Files (x86)\Microsoft SQL Server\[version number]\Tools\Binn\ManagementStudio\Ssms.exe -nosplash"

  7. Click OK to save your changes.
  8. Make sure you are logged into the VPN for the domain your accessing and launch the shortcut.
  9. You will be prompted for a password at a command prompt.
  10. Enter you password and hit enter (use the client’s domain password).
  11. SSMS will launch. Note: Windows authentication might still show as your computers main domain but if you try to connect to a SQL Server instance in your client’s domain you should connect!

The setup is a few steps but once you are up and running it is really simple to start the correct instance.

One Step Further

I love working on the command line, especially since I started using Cmder. So naturally, I turned the shortcut outlined above into a simple batch script that I dropped into Cmder’s bin folder. This gave me access to call it from the command line.

Create a Batch script named remoteSSMS.bat in the Cmder bin folder and copy/paste the same information into the script as you did for the Shorcut Target information above (changing location of ssms.exe and your domain/username):

C:\Windows\System32\runas.exe /user:yourclientsdomainname\yourusername /netonly "C:\Program Files (x86)\Microsoft SQL Server\[version number]\Tools\Binn\ManagementStudio\Ssms.exe -nosplash"

Now open up Cmder and type remoteSSMS and you will be prompted for a password and SSMS will open!

Hope you find the trick as helpful as I did.

Awaiting the Block

The await keyword in C# is magical. It seems to just work. Throw an await in front of a resource intensive function and slap an async on the method. Do this on all the methods in the chain and it just works. Right?

Although it does just work most of the time there are quite a few gotcha’s. There are many best practices that have been shared to alleviate some of the scenario’s you might run into.

I recently watched an awesome Pluralsight Video by Jon Skeet that covered Asynchronous code in C# 5. I recommend the video (and Pluralsight in general) as it goes into great depth on how async works under the covers, which is out of scope for this article.

One of Async’s best use cases is IO related tasks, such as calling web services. When adding async to a webservice call you will likely see benefits. If, however, you do intensive processing before or after making the web service call you could end up in a blocked state.

Blocked

Take a look at the code below from a click even on a simple WinForms app. What do you think will happen to the responsiveness of the form?

private void noAwaitClick(object sender, EventArgs e)
{
    label1.Text = "starting...";
    long prime = this.FindPrimeNumber(PRIME_TO_FIND);
    label1.Text = prime.ToString();
}

If you guessed that the form will freeze up until the prime number is calculated then you are correct. Using the well known methods to spin off worker threads and then calling invoke on the UI thread is one way to get responsiveness back. This can be a bit tedious and difficult for beginners to understand. With async and await we have a new, simpler way to stay responsive.

Async to the Rescue

Let’s take a look at the code block below. What do you think will happen to the responsiveness of the form this time?

private async void async1Click(object sender, EventArgs e)
{
    label1.Text = "starting...";
    var prime=  await this.primenumberAsync();
    label1.Text = prime.ToString();
}

Did you guess that it would be responsive? Good guess, but this was a trick question and the gotcha I would like to share with you. To understand why this is not going to keep the UI responsive we have see the implementation of the primenumberAsync() and have basic understanding of what is happening when the code is compiled using the await and async keywords.

Let’s see what is happening in the implementation of primenumberAsync():

private async Task<long> primenumberAsync()
{
    long prime = this.FindPrimeNumber(PRIME_TO_FIND);
    return await Task.FromResult(prime);
}

We are simply calling FindPrimeNumber and creating a Task from the result. Now if you are like me, you might be thinking “Doesn’t await and async create new threads when primenumberAsync() is called?”

Under the Covers

To see why the async1Click() code above blocks we have to have a understand of how await and async gets compiled. The key words await and async actually get compiled into a state machine. The compiled state machine is how the asynchronous behavior is achieved. I would recommend watching the pluralsight video or reading an overview of c#’s asyncrounous statemachine article to get a better understanding of the generated state machine.

The key take away here is that the await and async keywords do not create threads. Instead the compiler generates a state machine which, in this case, runs on the same UI thread.

Since the work is not being done on a separate thread and requires intensive processing power, the UI thread gets blocked. You can see the code being executed on the main thread if you download the sample code from the github repository. Run the code, set a break point inside of the FindPrimeNumber routine and use the [thread viewer] (http://msdn.microsoft.com/en-us/library/w15yf86f.aspx).

Unblocked

To create a responsive UI we can leave our call in the button handler the same but re-implement primenumberAsync() to create a new thread:

private async Task<long> primenumberAsync2()
{
    var prime = await Task.Run(() => this.FindPrimeNumber(PRIME_TO_FIND));
    return prime;
}

Conclusion

Await and async are power additions to the C# language but with great power comes great responsibility. In this post we took a look at one scenario where await and async are not magic bullets and a little bit of care needs to be taken to make sure that any extra processing is done a separate thread.

My First Post

I was suppose to take months to create this blog, allowing me to tell everyone I was working on my blog. Months were going to go by writing my own css from scratch and coding the coolest javascript framework, maybe even writing my own blog engine. Instead, after only 2 hours of setup time I am writing my first post. What happen?

The power of building on others work.

Building blocks

The internet is full of things to learn. You could learn about the slap stick or take free course from the top level colleges in the world such as MIT.

It is the openness and social aspect of the internet that is so powerful. We live in a time where we are able to learn and share with people from all around the world. We are mixing ideas and cultures in exciting ways we are only just starting to understand.

By sharing ideas and experiments it allows us to learn from each other and build amazing things. Students from all over the world are able to learn subjects that were not accessible to them just a few years ago… building blocks that might just lead to the next great idea.

The Adjacent Possible

The adjacent possible is a concept developed by Stuart Kauffman. In the book Where Good Ideas Come From, Steven Johnson expands upon this concept. Johnson summarizes where we get our ideas in his TED Talk:

We like to think our breakthrough ideas, you know, are like that $40,000, brand new incubator, state-of-the-art technology, but more often than not, they’re cobbled together from whatever parts that happen to be around nearby.

Today, with the prevalence of the internet, unlimited ideas are always nearby. What are the adjacent possibilities for innovation just around the next corner in a shared and connected world?

I see adjacent possibilities developed in my breakneck industry everyday. From the latest javascript framework to the new languages being developed. We are constantly trying new things and sharing the experiences so we can move forward.

Moving Forward

This is my first step in contributing. I had to overcome many fears to write this first post but leveraging the graciously shared work from others I was enabled to get started right away. My blog may not be innovative or earth shattering but I hope in the near future it helps someone cobble together a great idea.

I am grateful to all people behind the projects that I used to get up and running quickly: