Skip to content

Commit

Permalink
git commit date
Browse files Browse the repository at this point in the history
for #401
  • Loading branch information
mcmonkey4eva committed Jun 15, 2024
1 parent 52a7a1d commit 45da8dc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
33 changes: 30 additions & 3 deletions src/Core/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public class Program
/// <summary>If a version update is available, this is the message.</summary>
public static string VersionUpdateMessage = null;

/// <summary>Date of the current git commit, if known.</summary>
public static string CurrentGitDate = null;

/// <summary>Primary execution entry point.</summary>
public static void Main(string[] args)
{
Expand All @@ -92,6 +95,7 @@ public static void Main(string[] args)
{
Logs.Debug($"Unhandled exception: {e.ExceptionObject}");
};
List<Task> waitFor = [];
//Utilities.CheckDotNet("8");
PrepExtensions();
try
Expand Down Expand Up @@ -124,7 +128,7 @@ public static void Main(string[] args)
timer.Check("Initial settings load");
if (ServerSettings.CheckForUpdates)
{
Utilities.RunCheckedTask(async () =>
waitFor.Add(Utilities.RunCheckedTask(async () =>
{
JObject vers = (await Utilities.UtilWebClient.GetStringAsync("https://mcmonkeyprojects.github.io/swarm/update.json", GlobalProgramCancel)).ParseToJson();
string versId = $"{vers["version"]}";
Expand All @@ -139,10 +143,25 @@ public static void Main(string[] args)
}
else
{
Logs.Info($"Swarm is up to date! Version {Utilities.Version} is the latest.");
Logs.Init($"Swarm is up to date! Version {Utilities.Version} is the latest.");
}
});
}));
}
waitFor.Add(Utilities.RunCheckedTask(async () =>
{
try
{
string commitDate = await Utilities.RunGitProcess("show --no-patch --format=%ci HEAD");
DateTimeOffset date = DateTimeOffset.Parse(commitDate.Trim()).ToUniversalTime();
CurrentGitDate = $"{date:yyyy-MM-dd HH:mm:ss}";
Logs.Init($"Current git commit marked as date {CurrentGitDate}");
}
catch (Exception ex)
{
Logs.Error($"Failed to get git commit date: {ex}");
CurrentGitDate = "Git failed to load";
}
}));
RunOnAllExtensions(e => e.OnPreInit());
timer.Check("Extension PreInit");
Logs.Init("Prepping options...");
Expand Down Expand Up @@ -185,6 +204,14 @@ public static void Main(string[] args)
Logs.Init("Launching server...");
Web.Launch();
timer.Check("Web launch");
try
{
Task.WaitAll([.. waitFor], Utilities.TimedCancel(TimeSpan.FromSeconds(5)));
}
catch (Exception ex)
{
Logs.Debug($"Startup tasks took too long: {ex}");
}
Task.Run(() =>
{
Thread.Sleep(500);
Expand Down
2 changes: 1 addition & 1 deletion src/Pages/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
@RenderBody()
<script src="/js/[email protected]"></script>
@RenderSection("Scripts", required: false)
<div id="version_display" class="version-display">StableSwarmUI v<span>@Utilities.Version</span></div>
<div id="version_display" class="version-display">StableSwarmUI v<span>@Utilities.Version</span><span> (@Program.CurrentGitDate)</span></div>
</body>
</html>
13 changes: 13 additions & 0 deletions src/Utils/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -747,4 +747,17 @@ void Warn()
}
});
}

/// <summary>Launch, run, and return the text output of, a 'git' command input.</summary>
public static async Task<string> RunGitProcess(string args)
{
ProcessStartInfo start = new("git", args)
{
RedirectStandardOutput = true,
UseShellExecute = false
};
Process p = Process.Start(start);
await p.WaitForExitAsync(Program.GlobalProgramCancel);
return await p.StandardOutput.ReadToEndAsync();
}
}
17 changes: 3 additions & 14 deletions src/WebAPI/AdminAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,20 +353,9 @@ static JArray sessWrangle(IEnumerable<string> addresses)
public static async Task<JObject> UpdateAndRestart(Session session)
{
Logs.Warning($"User {session.User.UserID} requested update-and-restart.");
static async Task<string> launchGit(string args)
{
ProcessStartInfo start = new("git", args)
{
RedirectStandardOutput = true,
UseShellExecute = false
};
Process p = Process.Start(start);
await p.WaitForExitAsync(Program.GlobalProgramCancel);
return await p.StandardOutput.ReadToEndAsync();
}
string priorHash = (await launchGit("rev-parse HEAD")).Trim();
await launchGit("pull");
string localHash = (await launchGit("rev-parse HEAD")).Trim();
string priorHash = (await Utilities.RunGitProcess("rev-parse HEAD")).Trim();
await Utilities.RunGitProcess("pull");
string localHash = (await Utilities.RunGitProcess("rev-parse HEAD")).Trim();
Logs.Debug($"Update checker: prior hash was {priorHash}, new hash is {localHash}");
if (priorHash == localHash)
{
Expand Down

0 comments on commit 45da8dc

Please sign in to comment.