Migrate to GitInfo for embedding git information (#911)

* Migrate to GitInfo for embedding git information

* Adjust gitignore to reflect new git system

* Fix changes from broken merge

* Remove vestigial code in VersionHelper

---------

Co-authored-by: sudokoko <koko@drones.gay>
This commit is contained in:
Josh 2023-10-29 18:50:16 -05:00 committed by GitHub
parent aa3caea363
commit b8324d0c10
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 30 additions and 232 deletions

11
.gitignore vendored
View file

@ -27,17 +27,10 @@ png/
/ProjectLighthouse/r/*
/ProjectLighthouse/logs/*
lighthouse.config.json
lighthouse.yml
lighthouse.yml.configme
censor.yml
gitBranch.txt
gitVersion.txt
gitRemotes.txt
gitUnpushed.txt
logs/*
**.yml*
**/logs
npTicket*
r.tar.gz
lighthouse.yml.configme
# MSBuild stuff
bin/

View file

@ -6,7 +6,6 @@
<option name="PASS_PARENT_ENVS" value="1" />
<envs>
<env name="ASPNETCORE_ENVIRONMENT" value="Development" />
<env name="ASPNETCORE_HOSTINGSTARTUPASSEMBLIES" value="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" />
</envs>
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />

View file

@ -17,30 +17,5 @@
<ItemGroup>
<ProjectReference Include="..\ProjectLighthouse\ProjectLighthouse.csproj" />
</ItemGroup>
<ItemGroup>
<None Remove="gitVersion.txt" />
<EmbeddedResource Include="gitVersion.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="gitBranch.txt" />
<EmbeddedResource Include="gitBranch.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="gitRemotes.txt" />
<EmbeddedResource Include="gitRemotes.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="gitUnpushed.txt" />
<EmbeddedResource Include="gitUnpushed.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="git describe --long --always --dirty --exclude=\* --abbrev=8 &gt; &quot;$(ProjectDir)/gitVersion.txt&quot;" />
<Exec Command="git branch --show-current &gt; &quot;$(ProjectDir)/gitBranch.txt&quot;" />
<Exec Command="git remote -v &gt; &quot;$(ProjectDir)/gitRemotes.txt&quot;" />
<Exec Command="git log --branches --not --remotes --oneline &gt; &quot;$(ProjectDir)/gitUnpushed.txt&quot;" />
</Target>
</Project>

View file

@ -12,30 +12,5 @@
<ItemGroup>
<ProjectReference Include="..\ProjectLighthouse\ProjectLighthouse.csproj" />
</ItemGroup>
<ItemGroup>
<None Remove="gitVersion.txt" />
<EmbeddedResource Include="gitVersion.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="gitBranch.txt" />
<EmbeddedResource Include="gitBranch.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="gitRemotes.txt" />
<EmbeddedResource Include="gitRemotes.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="gitUnpushed.txt" />
<EmbeddedResource Include="gitUnpushed.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="git describe --long --always --dirty --exclude=\* --abbrev=8 &gt; &quot;$(ProjectDir)/gitVersion.txt&quot;" />
<Exec Command="git branch --show-current &gt; &quot;$(ProjectDir)/gitBranch.txt&quot;" />
<Exec Command="git remote -v &gt; &quot;$(ProjectDir)/gitRemotes.txt&quot;" />
<Exec Command="git log --branches --not --remotes --oneline &gt; &quot;$(ProjectDir)/gitUnpushed.txt&quot;" />
</Target>
</Project>

View file

@ -15,11 +15,4 @@
<p><b>Build</b>: @VersionHelper.Build</p>
<p><b>CommitHash</b>: @VersionHelper.CommitHash</p>
<p><b>IsDirty</b>: @VersionHelper.IsDirty</p>
<p><b>CanCheckForUpdates</b>: @VersionHelper.CanCheckForUpdates</p>
<p><b>CommitsOutOfDate</b>: @VersionHelper.CommitsOutOfDate</p>
<h2>Remotes</h2>
@foreach (string remote in VersionHelper.Remotes)
{
<p>@remote</p>
}
<p><b>RepositoryUrl</b>: @VersionHelper.RepositoryUrl</p>

View file

@ -200,7 +200,7 @@
<div class="ui black attached inverted segment">
<div class="ui container">
@{
string? remoteUrl = VersionHelper.DetermineRemoteUrl();
string? remoteUrl = VersionHelper.RepositoryUrl;
}
<span>
@Model.Translate(BaseLayoutStrings.GeneratedBy, VersionHelper.FullVersion)

View file

@ -13,34 +13,4 @@
<ProjectReference Include="..\ProjectLighthouse\ProjectLighthouse.csproj" />
<PackageReference Include="QRCoder" Version="1.4.3" />
</ItemGroup>
<ItemGroup>
<None Remove="gitVersion.txt" />
<EmbeddedResource Include="gitVersion.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="gitBranch.txt" />
<EmbeddedResource Include="gitBranch.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="gitRemotes.txt" />
<EmbeddedResource Include="gitRemotes.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="gitUnpushed.txt" />
<EmbeddedResource Include="gitUnpushed.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.13" />
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="git describe --long --always --dirty --exclude=\* --abbrev=8 &gt; &quot;$(ProjectDir)/gitVersion.txt&quot;" />
<Exec Command="git branch --show-current &gt; &quot;$(ProjectDir)/gitBranch.txt&quot;" />
<Exec Command="git remote -v &gt; &quot;$(ProjectDir)/gitRemotes.txt&quot;" />
<Exec Command="git log --branches --not --remotes --oneline &gt; &quot;$(ProjectDir)/gitUnpushed.txt&quot;" />
</Target>
</Project>

View file

@ -37,18 +37,7 @@ public class WebsiteStartup
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
#if DEBUG
services.AddRazorPages().WithRazorPagesAtContentRoot().AddRazorRuntimeCompilation((options) =>
{
// jank but works
string projectDir = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", ".."));
options.FileProviders.Clear();
options.FileProviders.Add(new PhysicalFileProvider(projectDir));
});
#else
services.AddRazorPages().WithRazorPagesAtContentRoot();
#endif
services.AddDbContext<DatabaseContext>(builder =>
{

View file

@ -1,15 +0,0 @@
using System;
using System.IO;
namespace LBPUnion.ProjectLighthouse.Helpers;
public static class ResourceHelper
{
public static string ReadManifestFile(string fileName)
{
using Stream stream = typeof(StartupTasks).Assembly.GetManifestResourceStream($"{typeof(StartupTasks).Namespace}.{fileName}");
using StreamReader reader = new(stream ?? throw new Exception("The assembly or manifest resource is null."));
return reader.ReadToEnd().Trim();
}
}

View file

@ -1,79 +1,17 @@
using System.Linq;
using System.Text.RegularExpressions;
using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types.Logging;
namespace LBPUnion.ProjectLighthouse.Helpers;
public static partial class VersionHelper
public static class VersionHelper
{
static VersionHelper()
{
try
{
CommitHash = ResourceHelper.ReadManifestFile("gitVersion.txt");
Branch = ResourceHelper.ReadManifestFile("gitBranch.txt");
string commitNumber = $"{CommitHash}_{Build}";
FullRevision = Branch == "main" ? $"r{commitNumber}" : $"{Branch}_r{commitNumber}";
public static string CommitHash => ThisAssembly.Git.Commit;
public static string Branch => ThisAssembly.Git.Branch;
string remotesFile = ResourceHelper.ReadManifestFile("gitRemotes.txt");
string[] lines = remotesFile.Split('\n');
// line[0] line[1] line[2]
// origin git@github.com:LBPUnion/project-lighthouse.git (fetch)
// linq is a serious and painful catastrophe but its useful so i'm gonna keep using it
Remotes = lines.Select(line => line.Split("\t")[1]).ToArray();
CommitsOutOfDate = ResourceHelper.ReadManifestFile("gitUnpushed.txt").Split('\n').Length;
CanCheckForUpdates = true;
}
catch
{
Logger.Error("Project Lighthouse was built incorrectly. Please make sure git is available when building.",
LogArea.Startup);
CommitHash = "invalid";
Branch = "invalid";
CanCheckForUpdates = false;
}
if (!IsDirty) return;
Logger.Warn("This is a modified version of Project Lighthouse. " +
"Please make sure you are properly disclosing the source code to any users who may be using this instance.",
LogArea.Startup);
CanCheckForUpdates = false;
}
[GeneratedRegex(@"((git|ssh|http(s)?)|(git@[\w\.-]+))(:(\/\/)?)([\w\.@\:\/\-~]+)((\.git)(\/))?( .+)?")]
private static partial Regex GitRemoteRegex();
#nullable enable
/// <summary>
/// Determines the URL of the git remote.
/// </summary>
public static string? DetermineRemoteUrl()
{
string? remote = Remotes?.FirstOrDefault();
if (remote == null) return null;
Match match = GitRemoteRegex().Match(remote);
if (!match.Success || match.Groups.Count != 12) return null;
return match.Groups[7].Value;
}
#nullable disable
public static string CommitHash { get; set; }
public static string Branch { get; set; }
/// <summary>
/// The full revision string. States current revision hash and, if not main, the branch.
/// </summary>
private static string FullRevision { get; set; }
private static string FullRevision => (Branch == "main" ? "" : $"{Branch}_") + $"r{CommitHash}_{Build}";
/// <summary>
/// The server's branding (environment version) to show to LBP clients. Shows the environment name next to the
/// revision.
@ -81,20 +19,15 @@ public static partial class VersionHelper
public static string EnvVer => $"{ServerConfiguration.Instance.Customization.EnvironmentName} {FullRevision}";
public static string FullVersion =>
$"Project Lighthouse {ServerConfiguration.Instance.Customization.EnvironmentName} {Branch}@{CommitHash} {Build}";
public static bool IsDirty => CommitHash.EndsWith("-dirty") ||
CommitsOutOfDate != 1 ||
CommitHash == "invalid" ||
Branch == "invalid";
public static int CommitsOutOfDate { get; set; }
public static bool CanCheckForUpdates { get; set; }
public static string[] Remotes { get; set; }
public static bool IsDirty => ThisAssembly.Git.IsDirty;
public static string RepositoryUrl => ThisAssembly.Git.RepositoryUrl;
public const string Build =
#if DEBUG
#if DEBUG
"Debug";
#elif RELEASE
#elif RELEASE
"Release";
#else
"Unknown";
#endif
#else
"Unknown";
#endif
}

View file

@ -10,6 +10,10 @@
<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="GitInfo" Version="3.3.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Pfim" Version="0.11.2" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.2" />
<PackageReference Include="Discord.Net.Webhook" Version="3.12.0" />
@ -29,33 +33,8 @@
<PackageReference Include="DistributedLock.MySql" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
<None Remove="gitVersion.txt" />
<EmbeddedResource Include="gitVersion.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="gitBranch.txt" />
<EmbeddedResource Include="gitBranch.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="gitRemotes.txt" />
<EmbeddedResource Include="gitRemotes.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<None Remove="gitUnpushed.txt" />
<EmbeddedResource Include="gitUnpushed.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ProjectLighthouse.Localization\ProjectLighthouse.Localization.csproj" />
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="git describe --long --always --dirty --exclude=\* --abbrev=8 &gt; &quot;$(ProjectDir)/gitVersion.txt&quot;" />
<Exec Command="git branch --show-current &gt; &quot;$(ProjectDir)/gitBranch.txt&quot;" />
<Exec Command="git remote -v &gt; &quot;$(ProjectDir)/gitRemotes.txt&quot;" />
<Exec Command="git log --branches --not --remotes --oneline &gt; &quot;$(ProjectDir)/gitUnpushed.txt&quot;" />
</Target>
</Project>

View file

@ -53,6 +53,13 @@ public static class StartupTasks
// Version info depends on ServerConfig
Logger.Info($"You are running version {VersionHelper.FullVersion}", LogArea.Startup);
if (VersionHelper.IsDirty)
{
Logger.Warn("This is a modified version of Project Lighthouse. " +
"Please make sure you are properly disclosing the source code to any users who may be using this instance.",
LogArea.Startup);
}
Logger.Info("Connecting to the database...", LogArea.Startup);
await using DatabaseContext database = DatabaseContext.CreateNewInstance();