22 May 2012

Setting Up A GitHub Specific PowerShell Profile

1 Comment Uncategorized

I was very excited to see that after several months of work Phil and Paul finally released their much talked about GitHub client for Windows. It has a great Metro style, and is very fluid and functional.  Here is an example of what my client looks like

As you can see it is very clean, and there are no typical windows borders or title bar which is common for Metro styled apps. As I was exploring the app, I stumbled on the fact that if you are using PowerShell as your default shell.

You can actually customize the PowerShell profile used by the app to be different and separate from your default Windows PowerShell profile. This can be useful if you want to install a separate set of modules or configure it in a different way than your default Windows PowerShell profile. To do this you create the PowerShell profile in the following path.

%USERPROFILE%\Documents\WindowsPowerShell\GitHub.PowerShell_profile.ps1

Here is how I configured my GitHub specific PowerShell profile.

###############################################################################
# global variables
###############################################################################
$profileDir = [System.IO.Path]::GetDirectoryName($profile)

###############################################################################
# Set up a simple prompt, adding the git prompt parts inside git repos
###############################################################################
function prompt {
    $realLASTEXITCODE = $LASTEXITCODE
    
    Write-Host

    # Reset color, which can be messed up by Enable-GitColors
    $Host.UI.RawUI.ForegroundColor = $GitPromptSettings.DefaultForegroundColor

    Write-VcsStatus

    Write-Host($pwd) -ForegroundColor Green

    $global:LASTEXITCODE = $realLASTEXITCODE
    
    return "$ "
}

###############################################################################
# Exposes the environment vars in a batch and sets them in this PS session
###############################################################################
function Get-Batchfile($file) 
{
    $theCmd = "`"$file`" & set" 
    cmd /c $theCmd | Foreach-Object {
        $thePath, $theValue = $_.split('=')
        Set-Item -path env:$thePath -value $theValue
    }
}


###############################################################################
# Sets the VS variables for this PS session to use
###############################################################################
function VsVars32($version = "10.0")
{
    $theKey = "HKLM:SOFTWARE\Microsoft\VisualStudio\" + $version
    $theVsKey = get-ItemProperty $theKey
    $theVsInstallPath = [System.IO.Path]::GetDirectoryName($theVsKey.InstallDir)
    $theVsToolsDir = [System.IO.Path]::GetDirectoryName($theVsInstallPath)
    $theVsToolsDir = [System.IO.Path]::Combine($theVsToolsDir, "Tools")
    $theBatchFile = [System.IO.Path]::Combine($theVsToolsDir, "vsvars32.bat")
    Get-Batchfile $theBatchFile
    [System.Console]::Title = "Visual Studio " + $version + " Windows Powershell"
}

###############################################################################
# aliases
###############################################################################

set-alias wide   format-wide

###############################################################################
# functions
###############################################################################

set-content function:\mklink "cmd /c mklink `$args"

###############################################################################
# setup posh-git
###############################################################################

Push-Location (Split-Path -Path $MyInvocation.MyCommand.Definition -Parent)

# add Git to PATH
$env:path += ";" + (Get-Item "Env:ProgramFiles(x86)").Value + "\Git\bin"

# import posh-git
Import-Module posh-git

# customize git prompt display settings
$global:GitPromptSettings.BeforeText = '['
$global:GitPromptSettings.AfterText = '] '
$global:GitPromptSettings.BranchAheadForegroundColor = [ConsoleColor]::DarkGreen
$global:GitPromptSettings.WorkingForegroundColor = [ConsoleColor]::Magenta
$global:GitPromptSettings.UntrackedForegroundColor = [ConsoleColor]::Magenta

# set TERM to cygwin
Enable-GitColors

Pop-Location

Start-SshAgent -Quiet

Let me know your configuration and any tips you have about the new GitHub app. A big thanks should be given to Phil and Paul for making our lives better.

21 May 2012

Using Git Bash in Console2

1 Comment Uncategorized

After reading Scott Hansleman’s post about Console2 I decided to give it a try full time. Overall I have to say I am very impressed and have no intention on switching back. It really is an impressive application, so if you are interested in trying something different, go to Scott’s post and follow his directions on setting it up.

As a long time mysysgit user for all my Git source control needs, I decided to find out if I could marry the two together to create a nice intersection between Console2 and msysgit’s Bash client.  And after a bit of searching and stumbling on my own post about setting up the bash client in Visual Studio, I answered my own question on how to set it up for Console2.  Here is what I cam up with:

  • Title: Git Bash
  • Icon: C:\Program Files (x86)\Git\etc\git.ico
  • Shell: C:\Windows\SysWOW64\cmd.exe /c “”C:\Program Files (x86)\Git\bin\sh.exe” –login -i”

 

13 Dec 2011

Cleaning Up Your Git Repository For NuGet 1.6

10 Comments Uncategorized

NuGet 1.6 was released today. And with it came some great new features, one that I am particularly excited about is.

Using NuGet Without Checking In Packages (Package Restore)

NuGet 1.6 now has first class support for the workflow in which NuGet packages are not added to source control, but instead are restored at build time if missing. For more details, read the Using NuGet without committing packages to source control topic.

The reason for this is that it finally allows us to remove the packages directory and all the packages from our repository.  Which I always thought created a mess of source control systems, and required unnecessary check-ins. It also created a huge amount of bloat especially since some projects (cough… NUnit) included so much extra crap that nobody needed and required all that extra storage space in the repository and commit history.

Setup

Let’s assume that you have a solution that is either already using NuGet, or planning to use it, and that you want to set up the no-commit workflow.

Right click on the Solution node in Solution Explorer and select Enable NuGet Package Restore.

Enable NuGet Package Restore Context Menu item

That’s it! You’re all set.

(taken from http://docs.nuget.org/docs/Workflows/Using-NuGet-without-committing-packages)

.gitignore

Next we will want to add the following to your .gitignore file. (create one if you don’t already have one) And add the following to the top of it.

# Don't include NuGet Packages
packages/

This will make sure any changes in the packages directory will be ignored when adding to the repository.

Make sure to commit these changes before the next step.

$ git add .
$ git commit -am "updating project for NuGet 1.6"
$ git push origin master

Cleaning up your repository

This next step is optional, but the purpose of it is to reduce the size of your repository. And for the more OCD among us, remove all reference to the packages directory so it is like it never existed.

$ git filter-branch --index-filter 'git rm --cached --ignore-unmatch -r ./packages/*'
Rewrite 48dc599c80e20527ed902928085e7861e6b3cbe6 (266/266)
Ref 'refs/heads/master' was rewritten

$ git push origin master --force
Counting objects: 1074, done.
Delta compression using 2 threads.
Compressing objects: 100% (677/677), done.
Writing objects: 100% (1058/1058), 148.85 KiB, done.
Total 1058 (delta 590), reused 602 (delta 378)
To git @ github.com:nberardi/some-project.git
 + 48dc599...051452f master -> master (forced update)

Cleanup and reclaiming space

While git filter-branch rewrites the history for you, the objects will remain in your local repo until they’ve been dereferenced and garbage collected. If you are working in your main repo you might want to force these objects to be purged.

$ rm -rf .git/refs/original/

$ git reflog expire --expire=now --all

$ git gc --prune=now
Counting objects: 2437, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (1378/1378), done.
Writing objects: 100% (2437/2437), done.
Total 2437 (delta 1461), reused 1802 (delta 1048)

$ git gc --aggressive --prune=now
Counting objects: 2437, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2426/2426), done.
Writing objects: 100% (2437/2437), done.
Total 2437 (delta 1483), reused 0 (delta 0)

Note that pushing the branch to a new or empty GitHub repo and then making a fresh clone from GitHub will have the same effect.

(taken from http://help.github.com/remove-sensitive-data/)

Happy Coding