Skip to content

Commit

Permalink
Inject current branch slug into the documentation link
Browse files Browse the repository at this point in the history
Resolves #8995
  • Loading branch information
RussKie committed Mar 14, 2021
1 parent afcd295 commit 335c2c7
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions GitCommands/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

[assembly: AssemblyDescription("GitExtensions commands")]

[assembly: InternalsVisibleTo("GitExtensions")]
[assembly: InternalsVisibleTo("GitCommands.Tests")]
[assembly: InternalsVisibleTo("GitUI.Tests")]
37 changes: 37 additions & 0 deletions GitCommands/Settings/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using GitCommands.Settings;
using GitCommands.Utils;
Expand Down Expand Up @@ -62,6 +63,7 @@ public static class AppSettings
public static readonly string UserPluginsDirectoryName = "UserPlugins";
private static string _applicationExecutablePath = Application.ExecutablePath;
private static readonly ISshPathLocator SshPathLocatorInstance = new SshPathLocator();
private static string? _documentationBaseUrl;

public static Lazy<string?> ApplicationDataPath { get; private set; }
public static readonly Lazy<string?> LocalApplicationDataPath;
Expand Down Expand Up @@ -117,6 +119,39 @@ static AppSettings()
MigrateAvatarSettings();
}

/// <summary>
/// Gets the base part of the documentation link for the current application version,
/// which looks something like "https://git-extensions-documentation.readthedocs.org/"
/// for the master branch, and "https://git-extensions-documentation.readthedocs.org/release-X.Y"
/// for a release/X.Y branch.
/// </summary>
public static string DocumentationBaseUrl
{
get => _documentationBaseUrl ?? throw new InvalidOperationException($"Call {nameof(SetDocumentationBaseUrl)} first to set the documentation base URL.");
}

internal static void SetDocumentationBaseUrl(string currentGitBranch)
{
if (_documentationBaseUrl is not null)
{
throw new InvalidOperationException("Documentation base URL can only be set once");
}

string? docVersion = null;

if (!string.IsNullOrWhiteSpace(currentGitBranch))
{
// We expect current branch to be something line "release/X.Y"
Match match = Regex.Match(currentGitBranch, "release/\\d*\\.\\d*");
if (match.Success)
{
docVersion = $"en/{currentGitBranch.Replace("/", "-")}/";
}
}

_documentationBaseUrl = $"https://git-extensions-documentation.readthedocs.org/{docVersion}";
}

public static bool? TelemetryEnabled
{
get => GetBool("TelemetryEnabled");
Expand Down Expand Up @@ -2058,5 +2093,7 @@ public override void SetValue<T>(string name, T value, Func<T, string?> encode)
{
AppSettings.SettingsContainer.SetValue(PathFor(name), value, encode);
}

public void ResetDocumentationBaseUrl() => AppSettings._documentationBaseUrl = null;
}
}
2 changes: 2 additions & 0 deletions GitExtensions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ private static void Main()
// There's no perf hit calling Initialise() multiple times.
UserEnvironmentInformation.Initialise(ThisAssembly.Git.Sha, ThisAssembly.Git.IsDirty);

AppSettings.SetDocumentationBaseUrl(ThisAssembly.Git.Branch);

ThemeModule.Load();
Application.ApplicationExit += (s, e) => ThemeModule.Unload();

Expand Down
2 changes: 1 addition & 1 deletion GitUI/CommandsDialogs/FormBrowse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1881,7 +1881,7 @@ private void CloseToolStripMenuItemClick(object sender, EventArgs e)
private void UserManualToolStripMenuItemClick(object sender, EventArgs e)
{
// Point to the default documentation, will work also if the old doc version is removed
OsShellUtil.OpenUrlInDefaultBrowser(@"https://git-extensions-documentation.readthedocs.org");
OsShellUtil.OpenUrlInDefaultBrowser(AppSettings.DocumentationBaseUrl);
}

private void CleanupToolStripMenuItemClick(object sender, EventArgs e)
Expand Down
5 changes: 2 additions & 3 deletions GitUI/UserManual/StandardHtmlUserManual.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using GitCommands;

namespace GitUI.UserManual
{
public class StandardHtmlUserManual : IProvideUserManual
{
private const string _location = @"https://git-extensions-documentation.readthedocs.org/";

private readonly string _subFolder;
private readonly string _anchorName;

Expand All @@ -19,7 +18,7 @@ public string GetUrl()
{
var subFolder = string.IsNullOrEmpty(_subFolder) ? string.Empty : _subFolder + ".html";

return (_location + subFolder).Combine("#", _anchorName);
return (AppSettings.DocumentationBaseUrl + subFolder).Combine("#", _anchorName);
}
}
}
22 changes: 22 additions & 0 deletions UnitTests/GitCommands.Tests/Settings/AppSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using FluentAssertions;
using GitCommands;
using GitCommands.Settings;
using GitCommands.Utils;
Expand All @@ -16,6 +17,27 @@ internal sealed class AppSettingsTests
{
private const string SettingsFileContent = @"<?xml version=""1.0"" encoding=""utf-8""?><dictionary />";

[TestCase(null, "https://git-extensions-documentation.readthedocs.org/")]
[TestCase("", "https://git-extensions-documentation.readthedocs.org/")]
[TestCase("\t", "https://git-extensions-documentation.readthedocs.org/")]
[TestCase("master", "https://git-extensions-documentation.readthedocs.org/")]
[TestCase("feature/test/mystuff", "https://git-extensions-documentation.readthedocs.org/")]
[TestCase("releases", "https://git-extensions-documentation.readthedocs.org/")]
[TestCase("releases/4.5", "https://git-extensions-documentation.readthedocs.org/")]
[TestCase("release", "https://git-extensions-documentation.readthedocs.org/")]
[TestCase("release/a", "https://git-extensions-documentation.readthedocs.org/")]
[TestCase("release/5", "https://git-extensions-documentation.readthedocs.org/")]
[TestCase("release/a4.5", "https://git-extensions-documentation.readthedocs.org/")]
[TestCase("release/4.5", "https://git-extensions-documentation.readthedocs.org/en/release-4.5/")]
[TestCase("release/40.501", "https://git-extensions-documentation.readthedocs.org/en/release-40.501/")]
public void SetDocumentationBaseUrl_should_currectly_append_verison(string currentGitBranch, string expected)
{
AppSettings.GetTestAccessor().ResetDocumentationBaseUrl();

AppSettings.SetDocumentationBaseUrl(currentGitBranch);
AppSettings.DocumentationBaseUrl.Should().Be(expected);
}

[Test]
[TestCaseSource(nameof(TestCases))]
public void Should_return_default_value(PropertyInfo property, object value, object defaultValue, bool isSetting)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using GitCommands;
using GitUI.UserManual;
using NUnit.Framework;

Expand All @@ -12,6 +13,9 @@ public class StandardHtmlUserManualFixture
[TestCase("merge_conflicts", "merge-conflicts", "https://git-extensions-documentation.readthedocs.org/merge_conflicts.html#merge-conflicts")]
public void GetUrl(string subFolder, string anchor, string expected)
{
AppSettings.GetTestAccessor().ResetDocumentationBaseUrl();
AppSettings.SetDocumentationBaseUrl("master");

var sut = new StandardHtmlUserManual(subFolder, anchor);

sut.GetUrl().Should().Be(expected);
Expand Down

0 comments on commit 335c2c7

Please sign in to comment.