Friday, October 19, 2012

After recently importing a Subversion repository into Git, along with the associated .gitignore file, I noticed that git status was performing poorly. The solution was to split the monolithic .gitignore file in the root folder into multiple .gitignore files spread throughout the repository.

I wrote the following PowerShell script to do so:
$content = type .gitignore | % { $_.Trim() } | ? { $_.StartsWith('/') } | % { $_.SubString(1) }
$content | % {
    $folder = split-path -Parent $_    
    if($folder.length -gt 0) {
        if(-not (test-path $folder)) {
            md $folder | Out-Null
        }
        $pattern = split-path -Leaf $_
        $newPattern = '/' + $pattern
        $ignoreFile = "$folder\.gitignore"
        $exists = $false
        if((test-path $ignoreFile ) -and (type $ignore) -notcontains $newPattern) {
            $exists = $true
        }
        if(!$exists) {
            Add-Content -Force -Path $ignoreFile $newPattern
        }
    }
}

Friday, October 12, 2012

Adding credentials to the Windows credential store from the command line

Service users often do not have permission to start GUI applications, so the following tool can be used to store credentials that can be used to access a server, for example to copy a backup onto a remote file share.

cmdkey.exe

The syntax to add credentials to the store is:

cmdkey /add:targetname /user:username /pass

Monday, October 8, 2012

Git Cookbook

This page lists some useful commands that I use with Git:
  • Show all branches that contain a commit. Useful to see which branches a topic branch has been merged into.
    • git branch --all --contains commit
  • Show which branches have not been merged into the current or specified branch.
    • git branch --all --no-merge [branch]
  • Pretty-print history tree
    • git log --oneline --graph --decorate --all
  • List all changes that will be sent with the next push
    • git log --branches --not --remotes
  • Update from remote
    • git fetch
    • git rebase -p
  • Get name of remote tracking branch for the current branch
    • git branch -vv | ? { $_ -match '^\*' } | % { $_ -match '\[([^:]+)' | Out-Null; $Matches[1] }
  • Get the most recently modified topic branch
    •  git for-each-ref --sort=-committerdate --format='%(refname)' refs/remotes/origin/topic | select -first 1

Wednesday, June 20, 2012

Get status of all Git repositories using PowerShell

The following PowerShell one-liner will get the status of all Git repositories:
dir -r -i .git -fo | % { pushd $_.fullname; cd ..; write-host -fore yellow (get-location).Path; git status; popd }
Note that this will not work for bare repositories.

Wednesday, June 6, 2012

Installing (and booting) Windows 8 on a VHD

Windows 8 (and Server 2012) greatly simplifies installing Windows 7 / Windows Server 2008 R2 / Windows 8 / Windows Server 2012 to a VHD or VHDX for booting without having to mess around with partitions. Convert-WindowsImage.ps1 is a fantastic tool that you can run from Windows 8 or Server 2012 to install a new operating system without rebooting your machine. To install Windows 8 Release Preview on a VHDX, I did the following:

  1. Download and unblock the PowerShell script Convert-WindowsImage.ps1. I couldn't find the unblock checkbox on the file properties dialog, so I used the Sysinternals streams.exe utility to remove the block:
    streams -d Convert-WindowsImage.ps1
  2. I then mounted the Windows 8 ISO image by double clicking on it in Explorer, taking advantage of the belated ISO mounting feature. In my case, it mounted to E:.
  3. Next, I ran Convert-WindowsImage.ps1 with the following command line:
    .\Convert-WindowsImage.ps1 -SourcePath E:\sources\install.wim -VHDFormat VHD -SizeBytes 50GB -VHDPath Windows8RP.vhd -VHDType Fixed -Edition Professional
  4. Mount the VHDX using diskmgmt.msc. I tried using Mount-DiskImage, but kept running into the following error: "Mount-DiskImage : One or more parameter values passed to the method were invalid." My drive was mounted as F:
  5. Run the following command to add your new VHD to the boot menu:
    bcdboot F:\Windows
  6. Reboot and select the new O/S in the boot menu.

Saturday, April 14, 2012

Redirecting all output from a PowerShell script using the new PowerShell 3.0 asterisk / star / * redirection:


powershell -Command { .\scriptfile.ps1 *> out.log }

Other types of redirection:
  • Pipeline: 1
  • Error: 2
  • Warning: 3
  • Verbose: 4
  • Debug: 5
  • All streams: *

Thursday, April 5, 2012

NuGet and Semantic Versioning

NuGet version 1.6 introduced support for Semantic Versioning, however I have since discovered that NuGet only supports v1.0.0 of the SemVer specification. Unfortunately, this is much more limiting than the latest version, which supports more and better scenarios such as continuous integration builds.

Semantic versions in NuGet must match the following regular expression:


(?\d+(\.\d+){2})(?-[a-z][0-9a-z-]*)?

This means a version number with three components (e.g. 1.0.0) optionally followed by a pre-release version number. The pre-release version number must start with a dash (-) and a letter between a-z, and can then be any number of digits, letters and dashes (-). The regular expression is case insensitive.

For example,

  • 0.1.0
  • 0.1.0-alpha
  • 0.1.0-Alpha
  • 0.1.0-a-2