Skip to content

Instantly share code, notes, and snippets.

@danielcrenna
Last active October 16, 2018 17:56
Show Gist options
  • Save danielcrenna/6c1a057264248a80d515f9d6b642f709 to your computer and use it in GitHub Desktop.
Save danielcrenna/6c1a057264248a80d515f9d6b642f709 to your computer and use it in GitHub Desktop.
parse git hash out of an embedded resource or existing git DB
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
namespace DevOps
{
public class GitTools
{
public static string GetGitHash(string @namespace)
{
string result = "<not found>";
try
{
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(@namespace + ".version.txt"))
using (StreamReader reader = new StreamReader(stream))
{
result = reader.ReadToEnd().Trim('\n');
}
return result;
}
catch (Exception e)
{
Debug.WriteLine("Exception while looking up pre-built git hash");
Debug.WriteLine(e.ToString());
Debug.WriteLine("");
}
try
{
string location = Assembly.GetExecutingAssembly().Location;
string directory = Path.GetDirectoryName(location);
while(directory != null)
{
string gitDir = Path.Combine(directory, ".git");
if(Directory.Exists(gitDir))
{
// Found the ".git" directory
string head = File.ReadAllText(Path.Combine(gitDir, "HEAD")).TrimEnd(); // <- remove trailing newline
const string refText = "ref: ";
if(head.StartsWith(refText)) // Referencing the leaf of a branch
{
string refPath = head.Substring(refText.Length);
string refContent = File.ReadAllText(Path.Combine(gitDir, refPath)).TrimEnd(); // <- remove trailing newline
if(refContent.Length == 40) // <- probably a valid hash
{
return refContent + " (" + result + ")";
}
}
else // Detached Head
{
if(head.Length == 40) // <- probably a valid hash
{
return head + " (" + result + ")";
}
}
break;
}
directory = Path.GetDirectoryName(directory);
}
}
catch(Exception e)
{
Debug.WriteLine("Exception while determining git hash");
Debug.WriteLine(e.ToString());
Debug.WriteLine("");
}
return result;
}
}
}
<EmbeddedResource Include="version.txt" />
Copyright 2018 Conatus Creative Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"C:\Program Files\Git\bin\git.exe" rev-parse --short HEAD > "$(ProjectDir)\version.txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment