1
1
// Copyright (c) Microsoft Corporation.
2
2
// Licensed under the MIT License.
3
3
4
+ using DevHome . Common . TelemetryEvents . GitExtension ;
5
+ using DevHome . Common . TelemetryEvents . SourceControlIntegration ;
6
+ using DevHome . Telemetry ;
4
7
using Microsoft . Win32 ;
5
8
using Microsoft . Windows . DevHome . SDK ;
6
9
using Serilog ;
@@ -13,26 +16,34 @@ public class GitDetect
13
16
14
17
private readonly ILogger _log = Log . ForContext < GitDetect > ( ) ;
15
18
19
+ private struct DetectInfo
20
+ {
21
+ public bool Found ;
22
+ public string Version ;
23
+ }
24
+
16
25
public GitDetect ( )
17
26
{
18
27
GitConfiguration = new GitConfiguration ( null ) ;
19
28
}
20
29
21
30
public bool DetectGit ( )
22
31
{
23
- var gitExeFound = false ;
32
+ var detect = new DetectInfo { Found = false , Version = string . Empty } ;
33
+ var status = GitDetectStatus . NotFound ;
24
34
25
- if ( ! gitExeFound )
35
+ if ( ! detect . Found )
26
36
{
27
37
// 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 )
30
40
{
41
+ status = GitDetectStatus . PathEnvironmentVariable ;
31
42
GitConfiguration . StoreGitExeInstallPath ( "git.exe" ) ;
32
43
}
33
44
}
34
45
35
- if ( ! gitExeFound )
46
+ if ( ! detect . Found )
36
47
{
37
48
// Check execution of git.exe by finding install location in registry keys
38
49
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()
43
54
if ( ! string . IsNullOrEmpty ( gitPath ) )
44
55
{
45
56
var paths = FindSubdirectories ( gitPath ) ;
46
- gitExeFound = CheckForExeInPaths ( paths ) ;
47
- if ( gitExeFound )
57
+ detect = CheckForExeInPaths ( paths ) ;
58
+ if ( detect . Found )
48
59
{
60
+ status = GitDetectStatus . RegistryProbe ;
49
61
break ;
50
62
}
51
63
}
52
64
}
53
65
}
54
66
55
- if ( ! gitExeFound )
67
+ if ( ! detect . Found )
56
68
{
57
69
// Search for git.exe in common file paths
58
70
var programFiles = Environment . GetEnvironmentVariable ( "ProgramFiles" ) ;
59
71
var programFilesX86 = Environment . GetEnvironmentVariable ( "ProgramFiles(x86)" ) ;
60
72
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
+ }
62
78
}
63
79
64
- return gitExeFound ;
80
+ TelemetryFactory . Get < ITelemetry > ( ) . Log ( "GitDetect_Event" , LogLevel . Critical , new GitDetectEvent ( status , detect . Version ) ) ;
81
+ return detect . Found ;
65
82
}
66
83
67
84
private string [ ] FindSubdirectories ( string installLocation )
@@ -85,35 +102,35 @@ private string[] FindSubdirectories(string installLocation)
85
102
}
86
103
}
87
104
88
- private bool CheckForExeInPaths ( string [ ] possiblePaths )
105
+ private DetectInfo CheckForExeInPaths ( string [ ] possiblePaths )
89
106
{
90
107
// Iterate through the possible paths to find the git.exe file
91
108
foreach ( var path in possiblePaths . Where ( x => ! string . IsNullOrEmpty ( x ) ) )
92
109
{
93
110
var gitPath = Path . Combine ( path , "git.exe" ) ;
94
- var isValid = ValidateGitConfigurationPath ( gitPath ) ;
111
+ var detect = ValidateGitConfigurationPath ( gitPath ) ;
95
112
96
113
// If the git.exe file is found, store the install path and log the information
97
- if ( isValid )
114
+ if ( detect . Found )
98
115
{
99
116
GitConfiguration . StoreGitExeInstallPath ( gitPath ) ;
100
117
_log . Information ( "Git Exe Install Path found" ) ;
101
- return true ;
118
+ return detect ;
102
119
}
103
120
}
104
121
105
122
_log . Debug ( "Git.exe not found in paths examined" ) ;
106
- return false ;
123
+ return new DetectInfo { Found = false , Version = string . Empty } ;
107
124
}
108
125
109
- public bool ValidateGitConfigurationPath ( string path )
126
+ private DetectInfo ValidateGitConfigurationPath ( string path )
110
127
{
111
128
var result = GitExecute . ExecuteGitCommand ( path , string . Empty , "--version" ) ;
112
129
if ( result . Status == ProviderOperationStatus . Success && result . Output != null && result . Output . Contains ( "git version" ) )
113
130
{
114
- return true ;
131
+ return new DetectInfo { Found = true , Version = result . Output . Replace ( "git version" , string . Empty ) . TrimEnd ( ) } ;
115
132
}
116
133
117
- return false ;
134
+ return new DetectInfo { Found = false , Version = string . Empty } ;
118
135
}
119
136
}
0 commit comments