Git Cheat Sheet 24 February 2014 at 15:58

We're switching to git (on github.com) at the moment, and I really wanted to use it properly instead of just relying on the GIU clients. I decided to learn it using the command-line, and I found this excellent tutorial online which really helped me understand some of the fundamentals: http://www.sbf5.com/~cduan/technical/git/.

This article contains my basic cheat sheet for Git, which more or less follows the general workflow when using a git repo.

git initInitialises a new git repository in the current folder.
git clone https://myrepo.comClones and initialises a remote git repository locally in the current folder - adds a remote repository reference named "origin".
git logView log changes.
git add .Recursively adds all changes to the repository.
git commit --dry-runSee what changes will be committed before actually running git commit.
git commit -m "My message"Commits changes to the repository.
git branchGet a list of local branches, with a star next to the current head.
git branch branch-name baseCreates a new branch based on an existing branch e.g. git branch test-branch HEAD.
git checkout branch-nameSwitches to a new branch and updates the local folder with the files from that branch.
git fetch originRetrieves remote changes and updates remote heads.
git pull originPulls all remote changes (origin can be replaced with a URL, for example).
git pull --rebase originPulls all remote changes but baselines them BEFORE your local changes, so your changes move on top of what everybody else has already contributed.
git push origin HEADPushes all changes back to the repository origin.

Creating tickets in Trac with XmlRpc and C# .NET 19 December 2013 at 15:00

Programmatically creating tickets in a Trac system using the recommended approach of XmlRpc is actually fairly straight-forward. The documentation and various sources were confusing to me at first, so I thought I would document the steps I went through to set it up. For me, I had a basic Trac instance on Windows set up, with the AccountManagerPlugin enabled.

  1. Go here: http://trac-hacks.org/wiki/XmlRpcPlugin.
  2. From the command-line, run the easy_install option that suits you, I ran easy_install -Z -U http://trac-hacks.org/svn/xmlrpcplugin/trunk
  3. As per the link above, enable the plugin in trac.ini, and if you're using the AccountManagerPlugin, make sure you follow the instructions.
  4. Fire up Trac - you should now have the XmlRpcPlugin running in Trac.
  5. Go to the Admin section in Trac, and to Permissions. You will need to add the XML_RPC permission to the user account that requires it - in my case I added it to my own login for testing purposes:
  6. If you browse to your Trac instance /rpc (e.g. http://localhost:8000/rpc) you will see the full XmlRpc API, with all possible methods to implement.
  7. Create a new Visual Studio project, and implement the methods you want. I just implemented two methods, CreateTicket and AddAttachment as follows (according to the example show here):
        public struct TicketAttributes
        {
        }
    
        [XmlRpcUrl("http://localhost:8000/login/xmlrpc")]
        public interface TracXmlRpcProxy : IXmlRpcProxy
        {
    
            [XmlRpcMethod("ticket.create")]
            int CreateTicket(string summary, string description, TicketAttributes attributes, bool notify, DateTime when);
    
            [XmlRpcMethod("ticket.putAttachment")]
            string AddAttachment(int ticketId, string filename, string description, byte[] data, bool replace = true);
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                TracXmlRpcProxy proxy;
    
                // Fill these in appropriately
                string user = "matt";
                string password = "password!";
    
                /// Create an instance of the Trac interface
                proxy = XmlRpcProxyGen.Create();
    
                // If desired, point this to your URL. If you do not do this,
                // it will use the one specified in the service declaration.
                // proxy.Url = "https://trac-rules.org/xmlrpc";
    
                // Attach your credentials
                proxy.Credentials = new System.Net.NetworkCredential(user, password);
    
                TicketAttributes attr;
                //attr.comment = "This is the comment that goes with the new page";
                int ticketId = proxy.CreateTicket("Xml Rpc Test", "This is an XML RPC Test", attr, false, DateTime.Now);
    
                byte[] fileData = File.ReadAllBytes("C:\Temp\test.jpg");
                proxy.KeepAlive = false;
                proxy.AddAttachment(ticketId, "test.jpg", "Test description", fileData, true);
    
                Console.WriteLine("Ticket ID: " + ticketId);
            }
        }
    
    
    

    One thing to note with the above is the proxy.KeepAlive = false: this HAS to be set before sending the attachment otherwise you get the following error: "The server committed a protocol violation. Section=ResponseStatusLine".

Jenkins Active Directory Authentication with CCTray 01 November 2013 at 15:29

Our Jenkins build server is only accessed internally, so there was never any real reason to put authentication on it. However, I recently added QA Deployments to the build server and we'd like to implement production releases too, so I figured now was a good time to add Active Directory authentication.

Adding authentication was easy: Add the Active Directory plugin, activate it using Global Security, and add the users who you want to be able to login. That took about 2 minutes. The problem for me was that we use CCTray for local build monitoring, and CCTray was now longer working.

I found a solution fairly quickly, but it's implementation wasn't completely straight-forward, so I thought I would document it here. This assumes that you have CCTray 1.8.0 or above installed.

  1. Visit https://github.com/csnate/cctray-jenkins-transport, and download the zip file. Build the project (I built in Release mode), and then copy all the files compiled into the bin folder into an "extensions" folder.
  2. Close down CCTray, and copy the "extensions" folder into your local CCTray folder (usually located at C:Program Files (x86)CCTray)
  3. Run CCTray
  4. Open the Settings... view off CCTray, and add a new server as follows:
    • Click "Using a transport extension"
    • Select "JenkinsTransport" from the drop-down box
    • Click Configure Extension
    • Server: Your server - eg. http://myserver:8080
    • Username: Your current network user name
    • Password: Your current network password

Powershell : Recursively deleting folders 25 October 2013 at 14:55
I haven't posted for ages as I am not doing any coding at the moment, but this little snippet that I used to know in my head completely eluded me today. I thought I'd post it here for when that happens again: this will delete ALL .svn folders in C:\Temp, recursively.
gci C:\Temp -include .svn -Recurse -Force | Remove-Item -Recurse -Force
Cryptext version 1.0.0 06 August 2013 at 19:54
I finally got around to adding Cryptext to my site. This is a simple, single-file utilty that I use to encrypt my personal data. The software uses a fairly strong encrypting algorithm to encrypt the data with a key of your choosing per file. I'm no security expert, but I think it's pretty secure in it's implementation. Please feel free to use and abuse.
Feed subscriptions April 2013 10 April 2013 at 08:53

With the shutting down of Google Reader, I've had to think about how I want to receive my favourite blog posts and podcasts. I've looked at all sorts of alternatives - Feedly, ifttt, but none of them has really done it for me. I've finally resorted to the technology I used 5 years ago - Microsoft Outlook. It actually works really well, but it's not easily portable and available everywhere I go so I thought I'd at least document my current list so I can easily set them up when I find a provider that works for me.

Blog Posts

Podcasts

Update: Subsequent to this article, I reverted to feedly, which I'm actually really enjoying now, despite my initial misgivings.

Customising Console2 05 April 2013 at 08:08

Every time I get a new machine, I always have to set up Console2 again, and because I have the memory of a goldfish, I always forget how. So this time around, I thought I would blog the minor changes I make.

Add Visual Studio Command Prompt

The first thing I like to do is make sure I can open tabs to the relevant Visual Studio command prompts. Go to Edit...Settings...Tabs, and then set up a new tab as per the screenshot below (you can copy and paste the actual location of your command prompt by viewing the properties of your visual studio command prompt shortcut):

I also like to set up a custom icon for the prompt, but that is obviously optional.

Add shortcuts to your context menu

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOTDirectoryshellConsole2]
@="Open Console2"
[HKEY_CLASSES_ROOTDirectoryshellConsole2command]
@="C:\\Matt\Dropbox\\Utilities\\Console2\\Console.exe -d %1" 
[HKEY_CLASSES_ROOTDirectoryBackgroundshellConsole2]
@="Open Console2"
[HKEY_CLASSES_ROOTDirectoryBackgroundshellConsole2command]
@="C:\\Matt\\Dropbox\\Utilities\\Console2\\Console.exe -d  \"%V\"" 

Create a console2.reg file with the above as contents, making sure you change the paths to correctly point to your console2.exe file. This will give you two context menu additions:

  • When you right-click a folder, you will have an option to open a console at that folder location
  • When you right-click inside a directory, you will have an option to open a console at that directory location

Using SQL Server's BCP Import Utility 06 February 2013 at 17:10

I had an issue today trying to get data from a client where we had access to their database via their VPN, but couldn't do a straight SQL Server transfer. I opted to use the bcp utility to export the table to a 0.75GB file, which I was then able to zip up, download, and then import again to our local database, once again using bcp. I had never used to the tool before, but it was really easy to use, and I thought I would document it here for my future reference.

Export

I needed a filtered subset of all the data in a table, so I opted to use a query for the import.

bcp "SELECT  FROM [dbo].[DataTable] WHERE [Description] like '%example%'" queryout "C:\Temp\MyData.txt" -c -S 10.1.0.18 -d ClientDb -b 1000 -U MyUser -P MyPassword
This statement executes a query against the "ClientDb" database, using the user name "MyUser" and password "MyPassword" on the server 10.1.0.18, executed in batches of 1000. This is then all exported into a file called "MyData.txt".

Import

Import was straight-forward, I just wanted to dump all the data into a local database.

bcp Temp_Table in "C:\Temp\MyData.txt" -c -T -E -S MyServer -d LocalDb -b 100000
This statement imports all the data in the text file to the table "Temp_Table" in the "LocalDb" database, on the server "MyServer" using a trusted connection (-T),

I found this a really easy way of doing an export/import. In my case the source table contained a large amount of binary data, so transferring the data to my local machine took just under an hour, at a rate of 0.14 rows per second, resulting in a 375MB table. However, the import was very fast, and even with all that data it only took 35 seconds, averaging around 14 rows per second.
Padding strings in SQL Server 2008 29 January 2013 at 13:12

There are often cases when you need to format a string or number into a string with a specified length. SQL Server 2008 doesn't have an inbuilt "PAD" function, but you can get around that with some String functions.

For example, you want to format an integer value into a 6-digit string, where the integer is of variable length, and any missing digits must be padded with a '0'. In SQL Management Studio, run the following script to generate some test data:

CREATE TABLE #temp (Id int null)

INSERT INTO #temp (id) VALUES (1)
INSERT INTO #temp (id) VALUES (2)
INSERT INTO #temp (id) VALUES (NULL)
INSERT INTO #temp (id) VALUES (5)
INSERT INTO #temp (id) VALUES (47)
INSERT INTO #temp (id) VALUES (123)
INSERT INTO #temp (id) VALUES (54789)
INSERT INTO #temp (id) VALUES (154789)

You can now use a combination of LEFT / RIGHT, LTRIM and REPLICATE to pad your string with leading or trailing characters:

SELECT 
	Id AS OriginalValue,
	(SELECT RIGHT(REPLICATE('0', 6) + LTRIM(Id), 6)) AS PaddedLeft,
	(SELECT LEFT(LTRIM(Id) + REPLICATE('0', 6), 6)) AS PaddedRight
	FROM #temp

Running this will result in the following output:
Results grid

Editing Video Tags with taglib-sharp 24 January 2013 at 20:12

I've been using XBMC at home, and I've been trying to find software to edit Genres on my movie files so I can sort them a little better. To my surprise, I couldn't find anything that seems to be able to edit tags on all the file types that I used. My next step was looking for libraries, and I was pleasantly surprised with taglib-sharp - available at https://github.com/mono/taglib-sharp.

I installed using the Nuget Package Manager in Visual Studio, and it literally took me 2 minutes to figure out a basic example and it worked perfectly on a number of files I tried!

using (TagLib.File tagFile = TagLib.File.Create("D:\\Temp\\101 Dalmatians.avi"))
{
    tagFile.Tag.Genres = new string[] { "Animated", "Family" };
    tagFile.Save();
}

I love software that just works.