# Creating a simple remote git repository

This is for hosting git on a Windows filesystem. Suppose you have a mapped network drive ‘M:’, then

cd /d M:\git
mkdir myrepo.git
cd myrepo.git
git init --bare


Then get a local working copy, e.g.

cd /d C:\temp\git
git clone M:\git\myrepo.git


However you need to actually put something into the repo, including the master branch (implicit in this case):

cd myrepo
git commit -m'Initial commit'
git push origin master


And that’s all you need to do. You now have a git repo on your network drive.

Git supports the file:// protocol so you can repeat the cloning part as follows:

cd /d C:\temp\git
git clone file:////yourserver/dvcs/git/myrepo.git
cd myrepo
git commit -m'Initial commit'
git push origin master


# Colourful console text

A test using a github gist. You might have to refresh the page to see it:

The code just outputs some red text in a console. And that’s it. The code, hosted directly on this side is here:

#include <iostream>
#include <string>
#include <windows.h>

int main(int argc, char const *argv[])
{
HANDLE hConsole = ::GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleScreenBufferInfo;
::GetConsoleScreenBufferInfo(hConsole, &consoleScreenBufferInfo);
::SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
DWORD bytesWritten = 0;
std::string msg("Hello there!\n");
::WriteConsole(hConsole, msg.data(), static_cast<DWORD>(msg.size()), &bytesWritten, 0);
::SetConsoleTextAttribute(hConsole, consoleScreenBufferInfo.wAttributes );

return 0;
}


# Git On Linux

Add git_completion.bash to your .bashrc file.

PS1='$\033[32m$\u@\h$\033[00m$:$\033[34m$\w$\033[31m$$(__git_ps1)$\033[00m$\$ '

to your .bashrc to get a bash prompt with colour.

Some useful (and fun) tutorial Git videos:

And some regular web pages:

And then a touch of reality:

# MVVMLight Using Two Views

In the previous article, I quickly showed how to create a single-view, single-window WPF application using MVVM Light. The trend in WPF applications is to have a single window holding multiple views so that there are less pop-up dialogs or child windows. This article shows how to construct a simple two view application using MVVM and WPF.

## Getting Started

• Requires VS2010
• Ensure that you have Nuget installed.
• Manage Nuget Package References and add MVVM Light

Note that the XAML, in particular, is elided for brevity and you should go to the git repository for the original code.

## Hosting Multiple Views

The application structure is similar to the previous article: we have a MainWindow, a ViewModelLocator, and a MainViewModel.

A picture is worth a thousand words, so without further ado, here is what the project structure looks like in VS2010:

The project is laid out in typical MVVM style: 3 folders for Models, ViewModels, and Views. In this case we do not have any Models so they can be ignored.

Starting with the Views: we simply have two UserControl XAML files, that have contain the views that we want to render. The first view is the one from the previous article. The second is just a text label.

All the work involved in rendering two different views for two different view-models happens in MainViewModel.cs, MainWindow.xaml, and App.xaml.

Looking at the MainWindow XAML, we see the following;

<Window x:Class="TwoViews.MainWindow"
DataContext="{Binding Main,
Source={StaticResource Locator}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<ContentControl Content="{Binding CurrentViewModel}" />

<DockPanel Grid.Row="1" >
<Button Command="{Binding SecondViewCommand}"
Content="Second View"
DockPanel.Dock="Right" />
<Button Command="{Binding FirstViewCommand}"
Content="First View"
DockPanel.Dock="Left" />
</DockPanel>
</Grid>
</Window>


As before, we use the ViewModelLocator to bind our Main view model to the MainWindow. This time, however, we have a ContentControl that binds to a new property called CurrentViewModel, and two buttons that bind to commands that switch the view models. Even though the buttons are labelled as switching the views, it is actually the view-models that are updated.

The next step in getting this to work, is implementing a DataTemplate, per view model that renders a View associated with a ViewModel. We do this in the App.xaml (though we could do it any Resource section we choose):

<Application x:Class="TwoViews.App"
xmlns:views="clr-namespace:TwoViews.Views"
xmlns:vm="clr-namespace:TwoViews.ViewModels"
StartupUri="MainWindow.xaml"
>
<Application.Resources>
<vm:ViewModelLocator x:Key="Locator"  />
<DataTemplate DataType="{x:Type vm:SecondViewModel}">
<views:SecondView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:FirstViewModel}">
<views:FirstView />
</DataTemplate>
</Application.Resources>
</Application>


For example, this quite literally says ‘if my data type is FirstViewModel, then the WPF framework should render the FirstView UserControl.

So when the Content attribute of our the ContentControl is set to an object of type FirstViewModel the framework renders the correct View for us.

It should also be noted that because the Content attribute has been set to a particular ViewModel, for example FirstViewModel, it is also set as the DataContext for the view that is rendered, i.e. FirstView, and the data-binding between FirstView and FirstViewModel therefore work.

The last part of the application that wires all of this together is the MainViewModel class. Clearly we only want a single instance of each view model, so we just declare static instances of each one:

public class MainViewModel : ViewModelBase
{
private ViewModelBase _currentViewModel;

readonly static FirstViewModel _firstViewModel = new FirstViewModel();
readonly static SecondViewModel _secondViewModel = new SecondViewModel();

public ViewModelBase CurrentViewModel
{
get
{
return _currentViewModel;
}
set
{
if (_currentViewModel == value)
return;
_currentViewModel = value;
RaisePropertyChanged("CurrentViewModel");
}
}

public ICommand FirstViewCommand { get; private set; }
public ICommand SecondViewCommand { get; private set; }

public MainViewModel()
{
CurrentViewModel = MainViewModel._firstViewModel;
FirstViewCommand = new RelayCommand(() => ExecuteFirstViewCommand());
SecondViewCommand = new RelayCommand(() => ExecuteSecondViewCommand());
}

private void ExecuteFirstViewCommand()
{
CurrentViewModel = MainViewModel._firstViewModel;
}

private void ExecuteSecondViewCommand()
{
CurrentViewModel = MainViewModel._secondViewModel;
}
}


Note that in the CurrentViewModel property we also have to RaisePropertyChanged via the INPC interface that ViewModelBase defines. This is so that the data-binding works in WPF, i.e. when we click on the buttons the view changes: if this line is omitted, you cannot change the views by clicking on the buttons.

If we run the code we can now see that we can switch between the two views and both views maintain their state (as we are using a static instance of each):

## Finishing Off

You might feel that the code above looks repetitive: it is. Many, if not all, MVVM frameworks provided ‘runtime assistance’ in automating this kind of thing. By that I mean that by naming your classes according to convention, e.g. by always using the ‘View’ and ‘ViewModel’ suffixes, MVVM frameworks heavily use reflection and can associate your views and view-models at run time. Indeed, even the MainViewModel above is often generalised into something that is provided by the MVVM framework.

## Footnotes

The example code is on github.

# MVVMLight Hello World in 10 Minutes.

This article shows a trivial ‘Hello World’ MVVM WPF application using the MVVM Light library.

## Getting Started

• Firstly, start VS2010, and create a new WPF project.
• Ensure that you have Nuget installed.
• Manage Nuget Package References and add ‘MVVM Light’

MVVM Light has now added a ViewModel folder containing the MainViewModel and the ViewModelLocator.

## Edit the Main Window

Simply add a button, and defer the DataContext binding to the ViewModelLocator (elided):

<Window x:Class="MvvmLightTest.MainWindow"
DataContext="{Binding Main,
Source={StaticResource Locator}}">
<Grid>
<Button Command="{Binding ShowPopUp}" Content="Show Pop Up" />
</Grid>
</Window>


Then in the MainViewModel we define the ShowPopUp command:

public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
ShowPopUp = new RelayCommand(() => ShowPopUpExecute(), () => true);
}

public ICommand ShowPopUp { get; private set; }

private void ShowPopUpExecute()
{
MessageBox.Show("Hello!");
}
}


Compile the code, and we get:

The MainViewModel inherits from the ViewModelBase class which also gives us the RaisePropertyChanged method, which we would call if we change the value of a property that we bind to (see this CodeProject article for more information). An example of property data-binding is also included in the example code.

## How does all this work?

Firstly, our App.xaml contains an application wide instance of the ViewModelLocator:

<Application x:Class="MvvmLightTest.App"
xmlns:vm="clr-namespace:MvvmLightTest.ViewModel"
StartupUri="MainWindow.xaml">
<Application.Resources>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
</Application.Resources>
</Application>


We then defer the DataContext binding to the ViewModelLocator (elided) in the MainWindow:

<Window x:Class="MvvmLightTest.MainWindow"
DataContext="{Binding Main,
Source={StaticResource Locator}}">
<Grid>
<Button Command="{Binding ShowPopUp}" Content="Show Pop Up" />
</Grid>
</Window>


This simply returns the static instance of the MainViewModel held in the ViewModelLocator instance.

The example code is on github.

# More Git

Since I’m using git a little bit more often now, this is just a little note to remind me how to do things in git that I would usually do in svn or hg. Yet another git cheatsheet!

## Reverting

This is quite straightforward for my purposes:

git reset --hard HEAD


Running just git reset -h gives:

usage: git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]
or: git reset [-q] <commit> [--] <paths>...
or: git reset --patch [<commit>] [--] [<paths>...]

--hard                reset HEAD, index and working tree
--merge               reset HEAD, index and working tree
--keep                reset HEAD but keep local changes
-p, --patch           select hunks interactively


## Cleaning up

To revert files use:

git clean


with -h:

usage: git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...

-q, --quiet           do not print names of files removed
-n, --dry-run         dry run
-f, --force           force
-d                    remove whole directories
-e, --exclude <pattern>
-x                    remove ignored files, too
-X                    remove only ignored files


If you just want to remove ignored files, run git clean -f -X.
One nice feature is that you can get back to a clean working copy by doing:

git clean -f -x


## Moving and Committing.

One very nice feature of git is that you can move files around using the operating system commands, and git tracks the moves automatically.

Untracked files still need to be added via git add, and then git commit --all is the closest analogy to svn commit.

Although a little confusing at first, most git commands do nothing unless you specify a particular argument. Compare this to Subversion: I can’t recall the number of times that people have made accidental commits because Subversion just blindly commits everything.

# A WPF/MVVM Countdown Timer on GitHub

I have started working on a simple countdown timer that is written in C#, XAML, and uses the ‘MVVM’ pattern. The v0.1 code is checked into GitHub, where you can download it, fork it, and play around (yes, I’m using semantic versioning). The purpose of this is to refresh my memory about XAML, and C#, and also to play around with CodeRush and Refactor! that make writing C# much easier.

The version 0.1 features are:

• Countdown fixed and starts at 25 minutes.
• Plays a start/stop sound using the SystemSounds.
• Settings not implemented.
• The window stays top-most.
• The Windows 7 taskbar icon shows the countdown progress.

This will go up onto CodeProject, I expect, once I’ve finished it off. The icon, if you are interested, was created using Icon Workshop which, at the time of writing, has money off.

# Setting up a global gitignore on Windows

I’m taking this as a reference for files that should be ignored by git:

Then copying them into .gitignore_global in %USERPROFILE% and then following these instructions http://help.github.com/ignore-files/ and running:

 git config --global core.excludesfile ~/.gitignore_global 

this now means that any git add only adds the files we care about such as source code and makefiles etc.

.