Skip to content

Instantly share code, notes, and snippets.

@tfwio
Last active December 28, 2015 00:19
Show Gist options
  • Save tfwio/7412860 to your computer and use it in GitHub Desktop.
Save tfwio/7412860 to your computer and use it in GitHub Desktop.
Searches "%programfiles%/git/bin/" and "%programfiles (x86)%/git/bin/" for existing 'git.exe' (so you don't need git.exe in your environment path). Original source: http://versioning.codeplex.com/SourceControl/latest#MSBuildVersioning/GitInfoProvider.cs
using System;
namespace MSBuildVersioning
{
/// <summary>
/// Provides Mercurial information for a particular file path, by executing and scraping
/// information from the hg.exe command-line program.
/// </summary>
public class GitInfoProvider : SourceControlInfoProvider
{
private int? revisionNumber;
private string revisionId;
private bool? isWorkingCopyDirty;
private string branch;
private string tags;
public string GetTimeStamp(){
return DateTime.Now.ToString("yyyyMMddHHmm");
}
public override string SourceControlName
{
get { return "Git"; }
}
public virtual int GetRevisionNumber()
{
if (revisionNumber == null)
{
InitRevision();
}
return (int)revisionNumber;
}
public virtual string GetRevisionId()
{
if (revisionId == null)
{
InitRevision();
}
return revisionId;
}
public object GetLongRevisionId()
{
throw new NotImplementedException();
}
public string GitCommand { get;set; }
void UpdateCommand()
{
string GitCmd = "git.exe";
string ProgramsX64 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles);
string ProgramsX86 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86);
string git86 = System.IO.Path.Combine(ProgramsX86,"git/bin/git.exe");
string git64 = System.IO.Path.Combine(ProgramsX64,"git/bin/git.exe");
if (System.IO.File.Exists(git86)) GitCmd = git86;
if (System.IO.File.Exists(git64)) GitCmd = git64;
GitCommand = GitCmd;
}
private void InitRevision()
{
UpdateCommand();
ExecuteCommand(GitCommand, "rev-list HEAD", output =>
{
if (revisionId == null)
{
revisionId = output;
revisionNumber = 1;
}
else
{
revisionNumber += 1;
}
},
null);
}
public virtual bool IsWorkingCopyDirty()
{
if (isWorkingCopyDirty == null)
{
UpdateCommand();
ExecuteCommand(GitCommand, "diff-index --quiet HEAD", (exitCode, error) =>
{
if (exitCode == 0)
{
isWorkingCopyDirty = false;
return false;
}
else if (exitCode == 1)
{
isWorkingCopyDirty = true;
return false;
}
else
{
return true;
}
});
}
return (bool)isWorkingCopyDirty;
}
public virtual string GetBranch()
{
if (branch == null)
{
UpdateCommand();
branch = ExecuteCommand(GitCommand, "describe --all")[0];
}
return branch;
}
public virtual string GetTags()
{
UpdateCommand();
if (tags == null)
{
tags = ExecuteCommand(GitCommand, "describe")[0];
}
return tags;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace MSBuildVersioning
{
/// <summary>
/// Replaces tokens in a string with information from a <c>GitInfoProvider</c>.
/// </summary>
public class GitVersionTokenReplacer : VersionTokenReplacer
{
public GitVersionTokenReplacer(GitInfoProvider infoProvider)
{
AddToken("REVNUM", () => infoProvider.GetRevisionNumber().ToString());
AddToken("REVNUM_MOD", x => (infoProvider.GetRevisionNumber() % x).ToString());
AddToken("REVNUM_DIV", x => (infoProvider.GetRevisionNumber() / x).ToString());
AddToken("REVID", () => infoProvider.GetRevisionId());
AddToken("DIRTY", () => infoProvider.IsWorkingCopyDirty() ? "1" : "0");
AddToken("BRANCH", () => infoProvider.GetBranch());
AddToken("TAGS", () => infoProvider.GetTags());
AddToken("TIMESTAMP", () => infoProvider.GetTimeStamp());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment