|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
3 | 6 | using System.Linq; |
4 | 7 | using System.Text; |
5 | | -using Microsoft.Build.Utilities; |
6 | 8 | using Microsoft.Build.Framework; |
7 | | -using System.IO; |
| 9 | +using Microsoft.Build.Utilities; |
8 | 10 |
|
9 | 11 | namespace DotNetOpenAuth.BuildTasks { |
10 | 12 | public class GetBuildVersion : Task { |
@@ -52,18 +54,51 @@ private string GetGitHeadCommitId() { |
52 | 54 | return string.Empty; |
53 | 55 | } |
54 | 56 |
|
55 | | - string headContent = string.Empty; |
| 57 | + string commitId = string.Empty; |
| 58 | + |
| 59 | + // First try asking Git for the HEAD commit id |
| 60 | + try { |
| 61 | + string cmdPath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.System), "cmd.exe"); |
| 62 | + ProcessStartInfo psi = new ProcessStartInfo(cmdPath, "/c git rev-parse HEAD"); |
| 63 | + psi.WindowStyle = ProcessWindowStyle.Hidden; |
| 64 | + psi.RedirectStandardOutput = true; |
| 65 | + psi.UseShellExecute = false; |
| 66 | + Process git = Process.Start(psi); |
| 67 | + commitId = git.StandardOutput.ReadLine().Trim(); |
| 68 | + git.WaitForExit(); |
| 69 | + if (git.ExitCode != 0) { |
| 70 | + commitId = null; |
| 71 | + } |
| 72 | + if (commitId != null && commitId.Length == 40) { |
| 73 | + return commitId; |
| 74 | + } |
| 75 | + } catch (InvalidOperationException) { |
| 76 | + } catch (Win32Exception) { |
| 77 | + } |
| 78 | + |
| 79 | + // Failing being able to use the git command to figure out the HEAD commit ID, try the filesystem directly. |
56 | 80 | try { |
57 | | - headContent = File.ReadAllText(Path.Combine(this.GitRepoRoot, @".git/HEAD")).Trim(); |
| 81 | + string headContent = File.ReadAllText(Path.Combine(this.GitRepoRoot, @".git/HEAD")).Trim(); |
58 | 82 | if (headContent.StartsWith("ref:", StringComparison.Ordinal)) { |
59 | 83 | string refName = headContent.Substring(5).Trim(); |
60 | | - headContent = File.ReadAllText(Path.Combine(this.GitRepoRoot, @".git/" + refName)).Trim(); |
| 84 | + string refPath = Path.Combine(this.GitRepoRoot, ".git/" + refName); |
| 85 | + if (File.Exists(refPath)) { |
| 86 | + commitId = File.ReadAllText(refPath).Trim(); |
| 87 | + } else { |
| 88 | + string packedRefPath = Path.Combine(this.GitRepoRoot, ".git/packed-refs"); |
| 89 | + string matchingLine = File.ReadAllLines(packedRefPath).FirstOrDefault(line => line.EndsWith(refName)); |
| 90 | + if (matchingLine != null) { |
| 91 | + commitId = matchingLine.Substring(0, matchingLine.IndexOf(' ')); |
| 92 | + } |
| 93 | + } |
| 94 | + } else { |
| 95 | + commitId = headContent; |
61 | 96 | } |
62 | 97 | } catch (FileNotFoundException) { |
63 | 98 | } catch (DirectoryNotFoundException) { |
64 | 99 | } |
65 | 100 |
|
66 | | - return headContent.Trim(); |
| 101 | + return commitId.Trim(); |
67 | 102 | } |
68 | 103 |
|
69 | 104 | private Version ReadVersionFromFile() { |
|
0 commit comments