Question

I am inquiring to examine the possibilities of using values I receive while issuing git bash terminal commands within my web application to create a modified semantic versioning scheme utilizing AngularJS.

I retrieve my count by issuing

git rev-list HEAD --count

and abbreviated hash

git rev-parse --short HEAD

These commands return values of interest to me, and now my question is, is it possible to somehow harvest these values in a usable way to reference within my page, and if so, is there a decent method to do this using Angular?

Was it helpful?

Solution 2

Edit

While continuing to research the topic, I have encountered some gotchas in my first answer. For this project, executing pre-build commands in Visual Studio is conflicting with other team members due to the hard coded path for the git bash terminal varying per workstation. To remedy this, I have introduced the idea of utilizing git hooks, running a Shell Script upon a post-commit action in git. This method will ensure the task at hand will be accomplished universally, point to the local git bash on each workstation, alleviating the potential to break the build in case the git bash path varies (which is a likely case).

Here is my new solution

navigate in your local repo .git > hooks > post-commit.sample and replace with the following

revision=$(git rev-parse --short HEAD);

build=$(git rev-list HEAD --count); 

version=$"{"$'"build":''"'$build$'"'$","$'"revision":'$'"'$revision$'"'$"}"; 

echo $version > "YourWebProject\version.txt";

Ensure to rename pre-commit.sample to pre-commit which will make the script now executable upon the action. Once executed, we will have a generated text file structured like the following JSON object

{
    "build":"# representing your git commit count",
    "revision":"abbreviated hash for last commit"
}

Create your Angular controller

function VersioningCtrl($scope, $http) {
    $http.get('version.txt')
    .success(function (data) {
        if (data && status === 200) {
            $scope.version = {
                build: data.build,
                revision: data.revision
            }
        }
    });
}

Markup

<div data-ng-controller="VersioningCtrl">
    Version 0.0.{{ version.build }}.{{ version.revision }}
</div>

Another gotcha

Due to the nature of this file being generated on the fly, I found it beneficial to include it within the git ignore file which was causing previous issues with git prompting me to always commit and track the new file.

I find this solution to be a bit more universal and efficient

OTHER TIPS

First Solution (see second answer)

1. Your Solution > Right Click > Properties > Build Events > Edit Pre-Build:

"C:\Program Files (x86)\Git\bin\git.exe" rev-list HEAD --count > "$(ProjectDir)build.txt"
"C:\Program Files (x86)\Git\bin\git.exe" rev-parse --short HEAD > "$(ProjectDir)revision.txt"

This will create two text files upon your next build, in this case, build.txt and revision.txt which will be located inside your project folder. To then reference these files in your application

2. Your Solution > Right Click > Add > Existing Item > Navigate to your text files

3. both text files > Right Click > Properties:

Build Action: Embedded Resource

Copy to Output Directory: Copy Always

4. Angular Controller

function VersioningCtrl($scope, $http) {
     $http.get('revision.txt')
          .success(function (data) {
          if (data && status === 200) {
              $scope.revision = data;
          }
      });

     $http.get('build.txt')
          .success(function (data) {
          if (data && status === 200) {
              $scope.build = data;
          }
     });
}

5. Markup

<div data-ng-controller="VersioningCtrl">
    <p>Version 0.0.{{ build }}.{{ revision }}</p>
</div>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top