fredrik.eriksson

Coffee and a keyboard

Cast hack

This is a pretty useful lite hack.

public static class Extensions
{
    public static T Cast<T>(this object obj)
    {
        return (T)Convert.ChangeType(obj, typeof(T));
    }
}

And it can be used like this:

[Test]
public void Cast_stringNumber_ReturnsNumber()
{
    var i = "123".Cast<int>();
    Assert.That(i == 123);
}

[Test]
public void Cast_dateString_ReturnDateTime()
{
    var d = "2011-07-23".Cast<DateTime>();
    Assert.That(d == new DateTime(2011, 07, 23));
}

Cherry picking in TFS

Cherry picking is the name used in git for picking one or more changesets from one branch and applying them to another (in Mercurial it’s called transplant).

Problem:

You are maintaining two version of a product e.g. 7.1 and 7.2. A customer finds a problem in your 7.2 release and reports a bug. After fixing the bug in the mainline codebase (in changeset 1234) another customer finds the same problem in the 7.1 version. What is the easiest way to pick the changes done in 7.2 and move them to 7.1.

Solution:

In the TFS cherry picking is accomplished by doing a so called baseless merge between the source and target branch from the command line. To pick the changeset 1234 from the 7.2 branch and apply it to the destination branch 7.1 we will use the bellow command:

tf merge /baseless /recursive /v:C1234~C1234 <source_path> <destination_path>

Where source_path is the directory on disk where you have checked out the 7.2 branch code. And destination_path is where the 7.1 branch is checked out.

/v:C1234~C1234 is used to specify that we want to merge only the 1234 changeset. If we instead had written /v:C1234 we would have gotten all the changes up to and including 1234. So ~ is used to limit the range from 1234 to 1234 resulting in only that changeset.

Note: The tf command is available through the Visual Studio Command Prompt and for more information on tf merge visit the MSDN page.