Automatically Versioning Your Code Based on Git Commit History

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi this is Tim from Tiger astronomy and a happy 2019 to you and in this video I want to show you how I've been versioning my code if you when you get just get started coding you probably don't worry about versioning too much and just use the default behavior that Visual Studio has but when you start getting into things like NuGet packages versioning becomes extremely critical and the technique i've developed to deal with that means that i never have to set manually a code version i basically link it to my git commit history and the version falls out of that naturally and very very easily so that's what this video is about and without further ado I'll show you how I retrofit an old style project to use this new technique okay so in this project this project is called horizon data interchange and it's a little command-line utility I wrote some time back with some contributions from it from Dave Weymouth and it not that it's important for this but the the program allows you to import and export horizon definition data from various astronomy programs and and allows you to easily share that data across all your planetarium software now as you can see from the code this is the global assembly info file that I use in the project and the version is set here in the file using assembly attributes and this is pretty much typical for what you'd see when you do file new project in Visual Studio so however it turns out that most people don't do versioning this way at least not anymore so I mentioned NuGet packages and NuGet uses something called semantic versioning or semver and that uses a is actually a semantic version 2.0 point zero and that's major version minor version and patch version and in the black bar at the top you can see examples of semantic versions and so this is highest version or most recent version to the left and oldest or oldest or lowest version to the right and this is showing actually the correct sort order for semantic versions so if there's a like 2.00 is fairly obvious 2.0 - 0 - RC dot - the - RC means this is a pre-release version - anything means a pre-release version and those come earlier in the sort order then if that is just missing so that's kind of counterintuitive but that's how assembler works so since NuGet uses some or something very very close to it and I've decided to standardize all my versions for everything around semantic versioning and I've mentioned that I use git as my version control system and I use a system called git flow which is just a branching strategy there's no magic there but git flow defines what kind of changes you do or what how you name your branches and when you need to change your version numbers so a kind of combine all these techniques and I use a utility called git version which basically infers the code version being built from the git commit history it's all very very handy okay so let's do this the first thing I want to do is give a quick introduction to get version so if we pop into the power show window over here and you can see I'm on my develop branch you should always make changes in develop or a branch off develop in git flow so git version has consists of a command line utility and also an MS build tasks for integrating into your build so the first thing I can do this is just the root the solution level of my top level of my solution directory you can just type in git version and see what get version would make of this project now because I've released version 1.0.0 already and now I've made changes on develop get version knows that I've created a new version here so it's it's used major version one minor version one and patch version zero so because I'm on the develop branch it also has inferred that this is an alpha release you could also consider it an integration test or an integration build so it just uses alpha for that and you can also see that this stuff here is very useful because it relates the it basically extracts the get commit ID this big long hash at the end so you can build that into your assembly informational version and it identifies for any given piece of executable code exactly which commit in get it came from so anyway so that's what that's what the command-line utility does now if we have a look at the code again and where is the code window we need to look at the global assembly info file now currently you can see in here that I'm using just a default versioning method that Visual Studio gave me when I did file a new project so the first thing is I'm gonna take that away that's gone now I never set my code version manually anymore it will be generated dynamically a build time and so the first thing I will do is build that code and we can see here the build running the little build panel you can see bottom right hand corner there is is reach sharper a reach sharper build utility which basically wraps the built in stuff in Visual Studio just gives you a nice little visual indication of what's going on so it does take a while to build because I use code contracts in this project and it's doing a static analysis of the files and that just does take a minute to do so we may pause the video at this point to come back when it's done okay here we go so the project has built I have a warning here may fix that later but for now if I go back to PowerShell and run this code now we can do that by drilling down into the know is the bin debug directory and then the executable now if we execute this code with no parameters it gives us a help screen and one of the things it prints out here is the version number so as you can see that's because I took the version attributes out of the global assembly info that's all reverted to zero zero zero zero so now we need to put back some other versioning strategy which sets the version of this code and that's what we'll do next so okay we now need to add the git version task NuGet package to our code so I'm going to do that in here manage and you get packages let me show both windows so you can see what's going on at least so over in the NuGet package window I'm going to just put in git version task in the search box and there it is we just go ahead and install that my one is doing that now we have to wait looks like okay so we're good that's installed okay so if I rebuild that now you okay so that's built and if we now run that again in PowerShell and we can see that the there we go okay so now we have a rather interesting version number so you have version 1.1 point 0 - alpha dot on + branch don't develop and then the sha-1 hash of the git commit so that is a more useful version number I would argue and it's not set anywhere in the code is it is now completely a consequence of the build process so it's impossible to forget to set the version number which is great ok so let's have a look at the file that we've built if we open up Windows Explorer and look at the file we've generated so it's in ta horizon being debug and here we have the executable and if we get the file properties up for that and go under the details tab you can see hopefully you can see that we've got version 1.1 point zero point zero and the product version here has that big long useful version string in it so and then that's the assembly information or version also has the same string in it so so there we are that's get version and the documentation website is very very comprehensive if there's all kinds of options and you know there's a way to do pretty much anything you want to do with it but it works kind of sensibly by default so this is what I'm doing with all my projects now there's a technique where you can change the name of your output file so that it contains the version that's being built which is it can be useful under certain circumstances so that's it from me for this video I've been Tim from Tigerette astronomy thank you for watching and goodbye
Info
Channel: Tim Long
Views: 2,436
Rating: 4.6799998 out of 5
Keywords: code, software, developer, software engineering, version, versioning, git, GitVersion, GitFlow, C#, .NET, practices
Id: 8WKDk8yPMUA
Channel Id: undefined
Length: 12min 9sec (729 seconds)
Published: Mon Dec 31 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.