Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit be38a85

Browse files
authored
Telemetry for detecting git (#3950)
* Add telemetry to store where and which version of git.exe was detected * Strip trailing newline from git version
1 parent 4543131 commit be38a85

File tree

2 files changed

+82
-18
lines changed

2 files changed

+82
-18
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Diagnostics.Tracing;
6+
using DevHome.Telemetry;
7+
using Microsoft.Diagnostics.Telemetry;
8+
using Microsoft.Diagnostics.Telemetry.Internal;
9+
10+
namespace DevHome.Common.TelemetryEvents.GitExtension;
11+
12+
// How git.exe was located
13+
public enum GitDetectStatus
14+
{
15+
// git.exe was not found on the system
16+
NotFound,
17+
18+
// In the PATH environment variable
19+
PathEnvironmentVariable,
20+
21+
// Probed well-known registry keys to find a Git install location
22+
RegistryProbe,
23+
24+
// Probed well-known folders under Program Files [(x86)]
25+
ProgramFiles,
26+
}
27+
28+
[EventData]
29+
public class GitDetectEvent : EventBase
30+
{
31+
public override PartA_PrivTags PartA_PrivTags => PrivTags.ProductAndServicePerformance;
32+
33+
public string Status { get; }
34+
35+
public string Version { get; }
36+
37+
public GitDetectEvent(GitDetectStatus status, string version)
38+
{
39+
Status = status.ToString();
40+
Version = version;
41+
}
42+
43+
public override void ReplaceSensitiveStrings(Func<string, string> replaceSensitiveStrings)
44+
{
45+
// This event so far has no sensitive strings
46+
}
47+
}

extensions/GitExtension/FileExplorerGitIntegration/Models/GitDetect.cs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using DevHome.Common.TelemetryEvents.GitExtension;
5+
using DevHome.Common.TelemetryEvents.SourceControlIntegration;
6+
using DevHome.Telemetry;
47
using Microsoft.Win32;
58
using Microsoft.Windows.DevHome.SDK;
69
using Serilog;
@@ -13,26 +16,34 @@ public class GitDetect
1316

1417
private readonly ILogger _log = Log.ForContext<GitDetect>();
1518

19+
private struct DetectInfo
20+
{
21+
public bool Found;
22+
public string Version;
23+
}
24+
1625
public GitDetect()
1726
{
1827
GitConfiguration = new GitConfiguration(null);
1928
}
2029

2130
public bool DetectGit()
2231
{
23-
var gitExeFound = false;
32+
var detect = new DetectInfo { Found = false, Version = string.Empty };
33+
var status = GitDetectStatus.NotFound;
2434

25-
if (!gitExeFound)
35+
if (!detect.Found)
2636
{
2737
// Check if git.exe is present in PATH environment variable
28-
gitExeFound = ValidateGitConfigurationPath("git.exe");
29-
if (gitExeFound)
38+
detect = ValidateGitConfigurationPath("git.exe");
39+
if (detect.Found)
3040
{
41+
status = GitDetectStatus.PathEnvironmentVariable;
3142
GitConfiguration.StoreGitExeInstallPath("git.exe");
3243
}
3344
}
3445

35-
if (!gitExeFound)
46+
if (!detect.Found)
3647
{
3748
// Check execution of git.exe by finding install location in registry keys
3849
string[] registryPaths = { "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1", "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1" };
@@ -43,25 +54,31 @@ public bool DetectGit()
4354
if (!string.IsNullOrEmpty(gitPath))
4455
{
4556
var paths = FindSubdirectories(gitPath);
46-
gitExeFound = CheckForExeInPaths(paths);
47-
if (gitExeFound)
57+
detect = CheckForExeInPaths(paths);
58+
if (detect.Found)
4859
{
60+
status = GitDetectStatus.RegistryProbe;
4961
break;
5062
}
5163
}
5264
}
5365
}
5466

55-
if (!gitExeFound)
67+
if (!detect.Found)
5668
{
5769
// Search for git.exe in common file paths
5870
var programFiles = Environment.GetEnvironmentVariable("ProgramFiles");
5971
var programFilesX86 = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
6072
string[] possiblePaths = { $"{programFiles}\\Git\\bin", $"{programFilesX86}\\Git\\bin", $"{programFiles}\\Git\\cmd", $"{programFilesX86}\\Git\\cmd" };
61-
gitExeFound = CheckForExeInPaths(possiblePaths);
73+
detect = CheckForExeInPaths(possiblePaths);
74+
if (detect.Found)
75+
{
76+
status = GitDetectStatus.ProgramFiles;
77+
}
6278
}
6379

64-
return gitExeFound;
80+
TelemetryFactory.Get<ITelemetry>().Log("GitDetect_Event", LogLevel.Critical, new GitDetectEvent(status, detect.Version));
81+
return detect.Found;
6582
}
6683

6784
private string[] FindSubdirectories(string installLocation)
@@ -85,35 +102,35 @@ private string[] FindSubdirectories(string installLocation)
85102
}
86103
}
87104

88-
private bool CheckForExeInPaths(string[] possiblePaths)
105+
private DetectInfo CheckForExeInPaths(string[] possiblePaths)
89106
{
90107
// Iterate through the possible paths to find the git.exe file
91108
foreach (var path in possiblePaths.Where(x => !string.IsNullOrEmpty(x)))
92109
{
93110
var gitPath = Path.Combine(path, "git.exe");
94-
var isValid = ValidateGitConfigurationPath(gitPath);
111+
var detect = ValidateGitConfigurationPath(gitPath);
95112

96113
// If the git.exe file is found, store the install path and log the information
97-
if (isValid)
114+
if (detect.Found)
98115
{
99116
GitConfiguration.StoreGitExeInstallPath(gitPath);
100117
_log.Information("Git Exe Install Path found");
101-
return true;
118+
return detect;
102119
}
103120
}
104121

105122
_log.Debug("Git.exe not found in paths examined");
106-
return false;
123+
return new DetectInfo { Found = false, Version = string.Empty };
107124
}
108125

109-
public bool ValidateGitConfigurationPath(string path)
126+
private DetectInfo ValidateGitConfigurationPath(string path)
110127
{
111128
var result = GitExecute.ExecuteGitCommand(path, string.Empty, "--version");
112129
if (result.Status == ProviderOperationStatus.Success && result.Output != null && result.Output.Contains("git version"))
113130
{
114-
return true;
131+
return new DetectInfo { Found = true, Version = result.Output.Replace("git version", string.Empty).TrimEnd() };
115132
}
116133

117-
return false;
134+
return new DetectInfo { Found = false, Version = string.Empty };
118135
}
119136
}

0 commit comments

Comments
 (0)