Skip to content

Commit 8dca18b

Browse files
author
Lessley Dennington
committed
trace2: detect event/normal formats
Add the infrastructure to detect whether a user has enabled the TRACE2 event format or normal format targets. This implementation has been designed to be extended to include the perf format target in a future series. Additionally it involved some refactoring/cleanup to set the Application Path and InstallationDirectory in the CommandContext, rather than in GCM/UI helper Program.cs files.
1 parent 1790779 commit 8dca18b

File tree

20 files changed

+279
-75
lines changed

20 files changed

+279
-75
lines changed

src/shared/Atlassian.Bitbucket.UI.Avalonia/Program.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using System.CommandLine;
2+
using System.Diagnostics;
33
using System.Threading;
44
using Atlassian.Bitbucket.UI.Commands;
55
using Atlassian.Bitbucket.UI.Controls;
@@ -45,9 +45,7 @@ private static void AppMain(object o)
4545
{
4646
string[] args = (string[]) o;
4747

48-
string appPath = ApplicationBase.GetEntryApplicationPath();
49-
string installDir = ApplicationBase.GetInstallationDirectory();
50-
using (var context = new CommandContext(appPath, installDir))
48+
using (var context = new CommandContext(args))
5149
using (var app = new HelperApplication(context))
5250
{
5351
app.RegisterCommand(new CredentialsCommandImpl(context));

src/shared/Core.Tests/StringExtensionsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ public void StringExtensions_TrimUntilLastIndexOf_Character_Null_ThrowsArgumentN
245245
[InlineData("foo://", "://", "")]
246246
[InlineData("foo://bar", "://", "bar")]
247247
[InlineData("foo://bar/", "://", "bar/")]
248+
[InlineData("foo:/bar/baz", ":", "/bar/baz")]
248249
public void StringExtensions_TrimUntilLastIndexOf_String(string input, string trim, string expected)
249250
{
250251
string actual = StringExtensions.TrimUntilLastIndexOf(input, trim);

src/shared/Core.Tests/Trace2Tests.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Text.RegularExpressions;
23
using GitCredentialManager.Tests.Objects;
34
using Xunit;
@@ -6,6 +7,38 @@ namespace GitCredentialManager.Tests;
67

78
public class Trace2Tests
89
{
10+
[PlatformTheory(Platforms.Posix)]
11+
[InlineData("af_unix:foo", "foo")]
12+
[InlineData("af_unix:stream:foo-bar", "foo-bar")]
13+
[InlineData("af_unix:dgram:foo-bar-baz", "foo-bar-baz")]
14+
public void TryParseEventTarget_Posix_Returns_Expected_Value(string input, string expected)
15+
{
16+
var environment = new TestEnvironment();
17+
var settings = new TestSettings();
18+
19+
var trace2 = new Trace2(environment, settings.GetTrace2Settings(), new []{""}, DateTimeOffset.UtcNow);
20+
var isSuccessful = trace2.TryGetPipeName(input, out var actual);
21+
22+
Assert.True(isSuccessful);
23+
Assert.Matches(actual, expected);
24+
}
25+
26+
[PlatformTheory(Platforms.Windows)]
27+
[InlineData("\\\\.\\pipe\\git-foo", "git-foo")]
28+
[InlineData("\\\\.\\pipe\\git-foo-bar", "git-foo-bar")]
29+
[InlineData("\\\\.\\pipe\\foo\\git-bar", "git-bar")]
30+
public void TryParseEventTarget_Windows_Returns_Expected_Value(string input, string expected)
31+
{
32+
var environment = new TestEnvironment();
33+
var settings = new TestSettings();
34+
35+
var trace2 = new Trace2(environment, settings.GetTrace2Settings(), new []{""}, DateTimeOffset.UtcNow);
36+
var isSuccessful = trace2.TryGetPipeName(input, out var actual);
37+
38+
Assert.True(isSuccessful);
39+
Assert.Matches(actual, expected);
40+
}
41+
942
[Theory]
1043
[InlineData("20190408T191610.507018Z-H9b68c35f-P000059a8")]
1144
[InlineData("")]
@@ -16,7 +49,8 @@ public void SetSid_Envar_Returns_Expected_Value(string parentSid)
1649
var environment = new TestEnvironment();
1750
environment.Variables.Add("GIT_TRACE2_PARENT_SID", parentSid);
1851

19-
var trace2 = new Trace2(environment);
52+
var settings = new TestSettings();
53+
var trace2 = new Trace2(environment, settings.GetTrace2Settings(), new []{""}, DateTimeOffset.UtcNow);
2054
var sid = trace2.SetSid();
2155

2256
Assert.Matches(rx, sid);

src/shared/Core/ApplicationBase.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.IO;
45
using System.Reflection;
6+
using System.IO.Pipes;
57
using System.Text;
68
using System.Threading;
79
using System.Threading.Tasks;
@@ -75,25 +77,16 @@ public Task<int> RunAsync(string[] args)
7577
Context.Trace.WriteLine("Tracing of secrets is enabled. Trace output may contain sensitive information.");
7678
}
7779

80+
// Enable TRACE2 tracing
81+
Context.Trace2.Start(Context.Streams.Error, Context.FileSystem, Context.ApplicationPath);
82+
7883
return RunInternalAsync(args);
7984
}
8085

8186
protected abstract Task<int> RunInternalAsync(string[] args);
8287

8388
#region Helpers
8489

85-
public static string GetEntryApplicationPath()
86-
{
87-
return PlatformUtils.GetNativeEntryPath() ??
88-
Process.GetCurrentProcess().MainModule?.FileName ??
89-
Environment.GetCommandLineArgs()[0];
90-
}
91-
92-
public static string GetInstallationDirectory()
93-
{
94-
return AppContext.BaseDirectory;
95-
}
96-
9790
/// <summary>
9891
/// Wait until a debugger has attached to the currently executing process.
9992
/// </summary>

src/shared/Core/CommandContext.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
24
using System.IO;
35
using GitCredentialManager.Interop.Linux;
46
using GitCredentialManager.Interop.MacOS;
@@ -15,7 +17,7 @@ public interface ICommandContext : IDisposable
1517
/// <summary>
1618
/// Absolute path the application entry executable.
1719
/// </summary>
18-
string ApplicationPath { get; }
20+
string ApplicationPath { get; set; }
1921

2022
/// <summary>
2123
/// Absolute path to the Git Credential Manager installation directory.
@@ -47,6 +49,11 @@ public interface ICommandContext : IDisposable
4749
/// </summary>
4850
ITrace Trace { get; }
4951

52+
/// <summary>
53+
/// Application TRACE2 tracing system.
54+
/// </summary>
55+
ITrace2 Trace2 { get; }
56+
5057
/// <summary>
5158
/// File system abstraction (exists mainly for testing).
5259
/// </summary>
@@ -78,12 +85,11 @@ public interface ICommandContext : IDisposable
7885
/// </summary>
7986
public class CommandContext : DisposableObject, ICommandContext
8087
{
81-
public CommandContext(string appPath, string installDir)
88+
public CommandContext(string[] argv)
8289
{
83-
EnsureArgument.NotNullOrWhiteSpace(appPath, nameof (appPath));
84-
85-
ApplicationPath = appPath;
86-
InstallationDirectory = installDir;
90+
var applicationStartTime = DateTimeOffset.UtcNow;
91+
ApplicationPath = GetEntryApplicationPath();
92+
InstallationDirectory = GetInstallationDirectory();
8793

8894
Streams = new StandardStreams();
8995
Trace = new Trace();
@@ -139,6 +145,7 @@ public CommandContext(string appPath, string installDir)
139145
throw new PlatformNotSupportedException();
140146
}
141147

148+
Trace2 = new Trace2(Environment, Settings.GetTrace2Settings(), argv, applicationStartTime);
142149
HttpClientFactory = new HttpClientFactory(FileSystem, Trace, Settings, Streams);
143150
CredentialStore = new CredentialStore(this);
144151
}
@@ -177,7 +184,7 @@ private static string GetGitPath(IEnvironment environment, IFileSystem fileSyste
177184

178185
#region ICommandContext
179186

180-
public string ApplicationPath { get; }
187+
public string ApplicationPath { get; set; }
181188

182189
public string InstallationDirectory { get; }
183190

@@ -191,6 +198,8 @@ private static string GetGitPath(IEnvironment environment, IFileSystem fileSyste
191198

192199
public ITrace Trace { get; }
193200

201+
public ITrace2 Trace2 { get; }
202+
194203
public IFileSystem FileSystem { get; }
195204

196205
public ICredentialStore CredentialStore { get; }
@@ -214,5 +223,17 @@ protected override void ReleaseManagedResources()
214223
}
215224

216225
#endregion
226+
227+
public static string GetEntryApplicationPath()
228+
{
229+
return PlatformUtils.GetNativeEntryPath() ??
230+
Process.GetCurrentProcess().MainModule?.FileName ??
231+
System.Environment.GetCommandLineArgs()[0];
232+
}
233+
234+
public static string GetInstallationDirectory()
235+
{
236+
return AppContext.BaseDirectory;
237+
}
217238
}
218239
}

src/shared/Core/Constants.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public static class EnvironmentVariables
5454
public const string GcmAuthority = "GCM_AUTHORITY";
5555
public const string GitTerminalPrompts = "GIT_TERMINAL_PROMPT";
5656
public const string GcmAllowWia = "GCM_ALLOW_WINDOWSAUTH";
57+
public const string GitTrace2Event = "GIT_TRACE2_EVENT";
58+
public const string GitTrace2Normal = "GIT_TRACE2";
5759

5860
/*
5961
* Unlike other environment variables, these proxy variables are normally lowercase only.
@@ -164,6 +166,13 @@ public static class Remote
164166
public const string FetchUrl = "url";
165167
public const string PushUrl = "pushUrl";
166168
}
169+
170+
public static class Trace2
171+
{
172+
public const string SectionName = "trace2";
173+
public const string EventTarget = "eventtarget";
174+
public const string NormalTarget = "normaltarget";
175+
}
167176
}
168177

169178
public static class WindowsRegistry

src/shared/Core/Settings.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ public interface ISettings : IDisposable
166166
/// of host provider auto-detection. Use a zero or negative value to disable probing.
167167
/// </summary>
168168
int AutoDetectProviderTimeout { get; }
169+
170+
/// <summary>
171+
/// Get TRACE2 settings.
172+
/// </summary>
173+
/// <returns>TRACE2 settings object.</returns>
174+
Trace2Settings GetTrace2Settings();
169175
}
170176

171177
public class ProxyConfiguration
@@ -504,6 +510,25 @@ public bool IsInteractionAllowed
504510

505511
public bool GetTracingEnabled(out string value) => _environment.Variables.TryGetValue(KnownEnvars.GcmTrace, out value) && !value.IsFalsey();
506512

513+
public Trace2Settings GetTrace2Settings()
514+
{
515+
var settings = new Trace2Settings();
516+
517+
if (TryGetSetting(Constants.EnvironmentVariables.GitTrace2Event, KnownGitCfg.Trace2.SectionName,
518+
Constants.GitConfiguration.Trace2.EventTarget, out string value))
519+
{
520+
settings.FormatTargetsAndValues.Add(Trace2FormatTarget.Event, value);
521+
}
522+
523+
if (TryGetSetting(Constants.EnvironmentVariables.GitTrace2Normal, KnownGitCfg.Trace2.SectionName,
524+
Constants.GitConfiguration.Trace2.NormalTarget, out value))
525+
{
526+
settings.FormatTargetsAndValues.Add(Trace2FormatTarget.Normal, value);
527+
}
528+
529+
return settings;
530+
}
531+
507532
public bool IsSecretTracingEnabled => _environment.Variables.GetBooleanyOrDefault(KnownEnvars.GcmTraceSecrets, false);
508533

509534
public bool IsMsalTracingEnabled => _environment.Variables.GetBooleanyOrDefault(Constants.EnvironmentVariables.GcmTraceMsAuth, false);

src/shared/Core/StringExtensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,17 @@ public static string TrimMiddle(this string str, string value, StringComparison
228228

229229
return str;
230230
}
231+
232+
/// <summary>
233+
/// Check whether string contains a specified substring.
234+
/// </summary>
235+
/// <param name="str">String to check.</param>
236+
/// <param name="value">String to locate.</param>
237+
/// <param name="comparisonType">Comparison rule for comparing the strings.</param>
238+
/// <returns>True if the string contains the substring, false if not.</returns>
239+
public static bool Contains(this string str, string value, StringComparison comparisonType)
240+
{
241+
return str?.IndexOf(value, comparisonType) >= 0;
242+
}
231243
}
232244
}

0 commit comments

Comments
 (0)