Skip to content
This repository was archived by the owner on Mar 20, 2019. It is now read-only.

Commit a089cb8

Browse files
committed
Fix for git commit id check when refs are packed.
1 parent 82d7211 commit a089cb8

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

lib/DotNetOpenAuth.BuildTasks.dll

7 KB
Binary file not shown.

lib/DotNetOpenAuth.BuildTasks.pdb

16 KB
Binary file not shown.

src/DotNetOpenAuth.BuildTasks/GetBuildVersion.cs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Diagnostics;
5+
using System.IO;
36
using System.Linq;
47
using System.Text;
5-
using Microsoft.Build.Utilities;
68
using Microsoft.Build.Framework;
7-
using System.IO;
9+
using Microsoft.Build.Utilities;
810

911
namespace DotNetOpenAuth.BuildTasks {
1012
public class GetBuildVersion : Task {
@@ -52,18 +54,51 @@ private string GetGitHeadCommitId() {
5254
return string.Empty;
5355
}
5456

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.
5680
try {
57-
headContent = File.ReadAllText(Path.Combine(this.GitRepoRoot, @".git/HEAD")).Trim();
81+
string headContent = File.ReadAllText(Path.Combine(this.GitRepoRoot, @".git/HEAD")).Trim();
5882
if (headContent.StartsWith("ref:", StringComparison.Ordinal)) {
5983
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;
6196
}
6297
} catch (FileNotFoundException) {
6398
} catch (DirectoryNotFoundException) {
6499
}
65100

66-
return headContent.Trim();
101+
return commitId.Trim();
67102
}
68103

69104
private Version ReadVersionFromFile() {

0 commit comments

Comments
 (0)