| | | 1 | | using System.Diagnostics; |
| | | 2 | | |
| | | 3 | | namespace DirectSight.Reporting; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Helper class to retrieve information from Git. |
| | | 7 | | /// </summary> |
| | | 8 | | internal static class GitHelper |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Gets the Git information. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <returns>The Git information.</returns> |
| | | 14 | | public static GitInformation GetGitInformation() |
| | 0 | 15 | | { |
| | 0 | 16 | | GitInformation gitInformation = new GitInformation(); |
| | 0 | 17 | | gitInformation.Branch = ExecuteGitCommand("rev-parse --abbrev-ref HEAD"); |
| | 0 | 18 | | gitInformation.Sha = ExecuteGitCommand("rev-parse HEAD"); |
| | 0 | 19 | | gitInformation.TimeStamp = ExecuteGitCommand("show -s --format=%ct"); |
| | 0 | 20 | | return gitInformation; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the file hash. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="path">The path.</param> |
| | | 27 | | /// <returns>The Git information.</returns> |
| | | 28 | | public static string GetFileHash(string path) |
| | 0 | 29 | | { |
| | 0 | 30 | | return ExecuteGitCommand("hash-object " + path); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | private static string ExecuteGitCommand(string arguments) |
| | 0 | 34 | | { |
| | | 35 | | try |
| | 0 | 36 | | { |
| | 0 | 37 | | ProcessStartInfo processStartInfo = new ProcessStartInfo |
| | 0 | 38 | | { |
| | 0 | 39 | | FileName = "git", |
| | 0 | 40 | | Arguments = arguments, |
| | 0 | 41 | | RedirectStandardOutput = true, |
| | 0 | 42 | | UseShellExecute = false, |
| | 0 | 43 | | CreateNoWindow = true |
| | 0 | 44 | | }; |
| | | 45 | | |
| | 0 | 46 | | using (Process process = new Process { StartInfo = processStartInfo }) |
| | 0 | 47 | | { |
| | 0 | 48 | | process.Start(); |
| | 0 | 49 | | string output = process.StandardOutput.ReadToEnd(); |
| | 0 | 50 | | process.WaitForExit(); |
| | | 51 | | |
| | 0 | 52 | | return output.Trim(); |
| | | 53 | | } |
| | | 54 | | } |
| | 0 | 55 | | catch (System.Exception) |
| | 0 | 56 | | { |
| | 0 | 57 | | return string.Empty; |
| | | 58 | | } |
| | 0 | 59 | | } |
| | | 60 | | } |