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>...]

    -q, --quiet           be quiet, only report errors
    --mixed               reset HEAD and index
    --soft                reset only HEAD
    --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>
                          add <pattern> to ignore rules
    -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 TimeSpan Custom Control

I have added yet another CodeProject article entitled A WPF Short TimeSpan Custom Control that builds up on my previous article that creates a simple spinner control.

The introduction to the article is here, but go to the CodeProject web site for the full article:

Introduction

In my previous two articles, I first described a simple short TimeSpan UserControl that used sliders to pick the hours, minutes, and seconds of a TimeSpan. In my second article I described a custom spinner control that could be used to replace the sliders.

In this third article, I will update the TimeSpan control to use the SpinnerControl, and make the TimeSpan control a custom control, rather than a UserControl (in particular so that we can apply custom themes to it). I refer to this as a ShortTimeSpanControl as it only represents a positive TimeSpan from 00:00:00 to 23:59:59. A full TimeSpan is described here:

A TimeSpan value can be represented as [-]d.hh:mm:ss.ff, where the optional minus sign indicates a negative time interval, the d component is days, hh is hours as measured on a 24-hour clock, mm is minutes, ss is seconds, and ff is fractions of a second. That is, a time interval consists of a positive or negative number of days without a time of day, or a number of days with a time of day, or only a time of day.

With the generic theme supplied, this is a selection control, where the user can choose a TimeSpan. As it is a custom control, you can apply any custom theme you like, and, perhaps, just make it a read-only control (interactively, that is), that updates its Value via data-binding.

….read the full article at A WPF Short TimeSpan Custom Control

A WPF Spinner Custom Control

I have added another article to Code Project: A WPF Spinner Custom Control which describes the process of creating a simple spinner control in WPF using the framework.

This follows up from my previous post where I created a simple UserControl to select a TimeSpan. In the absence of a spinner control I used sliders instead. Writing a spinner control has been an educational experience: whilst I have previously written controls, writing a new one from scratch and adhering to the various framework guidelines and conventions (to a degree) meant that I had to read through much more of the MSDN documentation than I would previously have taken a look at.

What makes the article a little different to some of the others out there, is that I try and mention each part of a custom control: the code, the generic theme, and then applying a custom theme to it.

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:

git – .gitignore for Visual Studio Projects and Solutions – Stack Overflow.

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.

.

C/C++ – Python interop

Making Python work with C/C++ can be achieved in a couple of ways, either writing a DLL with a Python API, or using the ctypes library module.

When working with python, it is useful to have virtual environments so that we have a clean and isolated python environment. Although it is not necessary for this example, I’ll run through the steps necessary:

Then:

c:\> cd c:\python27\scripts
c:\python27\scripts> easy_install virtualenv
c:\python27\scripts> easy_install pip

and then:

C:\Python27\Scripts>virtualenv --distribute --prompt=(myenv) %USERPROFILE%\myenv

New python executable in C:\Users\user\myenv\Scripts\python.exe
Installing distribute...........................................................
................................................................................
....................................................done.
Installing pip.................done.

Then enter your virtualenv

c:\> C:\Users\user\myenv\Scripts\activate.bat
(myenv) c:\>

where you can now use pip install to download and install python modules into the virtualenv as necessary.

If you want to use the Python Tools for Visual Studio, ensure that you change %USERPROFILE% to $(UserProfile) in the (python) project settings so that you run python from the correct virutalenv.

ctypes example

The library documentation on ctypes can be found here.

If we start from scratch in Visual Studio, we can create a solution that contains two projects: a simple python program to demonstrate ctypes, and a simple dll from the VS wizards.

Quoting verbatim from the python docs: ctypes exports the cdll, and on Windows windll and oledll objects, for loading dynamic link libraries. So by using the LoadLibrary method on cdll we can load our dll into python, and then start accessing the methods.

The following python except demonstrates this in practice:

#dll_path = '....'
print "Loading dll {0}".format(dll_path)

#  load it
mydll = cdll.LoadLibrary(dll_path)

#  function has to be extern "C" and have an undecorated name:
result = mydll.fnSimpleDll()
print "Function returned:  {0}".format(result)

#  first way to call C++ decorated method - by ordinal
cppFunction = mydll[1]
cppresult = cppFunction()
print "C++ decorated function by ordinal returned:  {0}".format(cppresult)

#  second way - by decorated name
newCppFunction = getattr(mydll, "?cppFunction@@YAHXZ")
newCppResult = newCppFunction()
print "C++ decorated function by ordinal returned:  {0}".format(newCppResult)

print "Finished"

Using depends we can examine our dll and see the ordinals and decorated names (or use a .def file if we really want to). Prefix functions in C++ with extern "C" makes them directly available in python as if they were a standard python module as the function names are undecorated.

For a library or project where you use code generation, you can easily generate a python stub to your dlls or .so files that is available cross platform. Other obvious uses are to use python as a harness for your C/C++ dlls and run python based tests against them: this can be particularly useful when Python has some benefit or feature that C++ does not have built-in or available in your environment, that you might want to take advantage of during testing, such as regular expressions, REST calls, and so on.

Attached is a zip file containing the example shown here.

  • PythonCTypes.zip (requires VS2010, python 2.7, PTVS, and a virtualenv as described above).

Skinning applications

ritzenhoff

Now that Microsoft has rolled out the new XBox 360 dashboard, which uses the Metro, and Windows 8 follows next year, using the same UI, I wonder if there is going to be a new era of skinning applications: the Metro UI style promotes simplicity and layout over anything else. The result of this is that there is often a large amount of empty space, and high contrast controls, allowing for a background image to be subtly applied in the background. In fact you can already see something like this happening in the Zune application.

For example, the Ritzenhoff designs (see the image above), compliment the Metro UI as they tend to use bold primary colours, and could be applied to Metro-style applications without breaking Metro-UI guidelines. Hopefully under Windows 8 we will start to see some attractive looking applications on Windows, where UI design starts becoming more about form over function as we are currently seeing with HTML 5.