-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateService.cs
More file actions
422 lines (353 loc) · 14.6 KB
/
Copy pathUpdateService.cs
File metadata and controls
422 lines (353 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
using System.Net.Http.Json;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text.Json;
using cp.Models;
namespace cp;
public static class UpdateService
{
private const string Repo = "lepepe/git-cp";
private const string LatestReleaseApiUrl = $"https://api.github.com/repos/{Repo}/releases/latest";
private static readonly TimeSpan CheckInterval = TimeSpan.FromHours(24);
private static readonly HttpClient Http = CreateHttpClient();
private static HttpClient CreateHttpClient()
{
var client = new HttpClient();
client.DefaultRequestHeaders.UserAgent.ParseAdd("git-cp-update-check");
client.DefaultRequestHeaders.Accept.ParseAdd("application/vnd.github+json");
return client;
}
// ── Version ──────────────────────────────────────────────────────────────
public static string GetCurrentVersion()
{
var info = Assembly
.GetExecutingAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
?.InformationalVersion;
if (string.IsNullOrWhiteSpace(info))
return "0.0.0";
var plusIndex = info.IndexOf('+');
return plusIndex >= 0 ? info[..plusIndex] : info;
}
public static bool IsDevBuild(string version) =>
version == "0.0.0" || !Version.TryParse(version, out _);
// ── Platform ─────────────────────────────────────────────────────────────
public static string? GetAssetNameForCurrentPlatform()
{
if (OperatingSystem.IsWindows())
return "git-cp-win-x64.exe";
if (OperatingSystem.IsMacOS())
return RuntimeInformation.OSArchitecture == Architecture.Arm64
? "git-cp-osx-arm64"
: "git-cp-osx-x64";
if (OperatingSystem.IsLinux() && RuntimeInformation.OSArchitecture == Architecture.X64)
return "git-cp-linux-x64";
return null;
}
// ── Passive check (background, cached) ────────────────────────────────────
public static async Task<UpdateCheckResult?> CheckForUpdateInBackgroundAsync(
string currentVersion,
CancellationToken ct = default
)
{
if (IsDevBuild(currentVersion))
return null;
try
{
var cache = await ReadCacheAsync(ct);
if (cache is not null && DateTimeOffset.UtcNow - cache.LastCheckedUtc < CheckInterval)
{
return IsNewer(cache.LatestKnownVersion, currentVersion)
? new UpdateCheckResult(NormalizeVersion(cache.LatestKnownVersion!))
: null;
}
var release = await FetchLatestReleaseAsync(ct);
if (release is null)
return null;
await WriteCacheAsync(new UpdateCache(DateTimeOffset.UtcNow, release.TagName), ct);
return IsNewer(release.TagName, currentVersion)
? new UpdateCheckResult(NormalizeVersion(release.TagName))
: null;
}
catch
{
return null;
}
}
// ── Explicit check (--update, bypasses cache interval) ────────────────────
public static async Task<UpdateCheckOutcome> CheckForUpdateNowAsync(
string currentVersion,
CancellationToken ct = default
)
{
var release = await FetchLatestReleaseAsync(ct);
if (release is null)
return new UpdateCheckOutcome(UpdateStatus.CheckFailed);
try
{
await WriteCacheAsync(new UpdateCache(DateTimeOffset.UtcNow, release.TagName), ct);
}
catch
{
// Best-effort — a cache write failure shouldn't block the update flow.
}
var latestVersion = NormalizeVersion(release.TagName);
if (!IsNewer(release.TagName, currentVersion))
return new UpdateCheckOutcome(UpdateStatus.UpToDate, LatestVersion: latestVersion);
var assetName = GetAssetNameForCurrentPlatform();
var asset = assetName is null ? null : release.Assets.FirstOrDefault(a => a.Name == assetName);
if (assetName is null || asset is null)
return new UpdateCheckOutcome(UpdateStatus.CheckFailed, LatestVersion: latestVersion);
var checksumAsset = release.Assets.FirstOrDefault(a => a.Name == "checksums.txt");
return new UpdateCheckOutcome(
UpdateStatus.UpdateAvailable,
LatestVersion: latestVersion,
DownloadUrl: asset.BrowserDownloadUrl,
AssetName: assetName,
ChecksumUrl: checksumAsset?.BrowserDownloadUrl
);
}
// ── Apply (download → verify → swap) ──────────────────────────────────────
public static async Task<UpdateApplyResult> ApplyUpdateAsync(
string downloadUrl,
string assetName,
string? checksumUrl,
CancellationToken ct = default
)
{
var currentExePath = Environment.ProcessPath;
if (string.IsNullOrWhiteSpace(currentExePath))
return new UpdateApplyResult(false, "Could not determine the current executable's path.");
var dir = Path.GetDirectoryName(currentExePath)!;
var tempPath = Path.Combine(dir, $"git-cp.download.{Environment.ProcessId}.tmp");
try
{
await DownloadToTempFileAsync(downloadUrl, tempPath, ct);
if (checksumUrl is not null)
{
var verified = await VerifyChecksumAsync(tempPath, assetName, checksumUrl, ct);
if (!verified)
{
TryDelete(tempPath);
return new UpdateApplyResult(
false,
"Checksum verification failed — update aborted for safety."
);
}
}
return OperatingSystem.IsWindows()
? ReplaceWindows(tempPath, currentExePath)
: ReplaceUnix(tempPath, currentExePath);
}
catch (Exception ex)
{
TryDelete(tempPath);
return new UpdateApplyResult(false, $"Update failed: {ex.Message}");
}
}
private static async Task DownloadToTempFileAsync(string url, string destinationPath, CancellationToken ct)
{
using var response = await Http.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, ct);
response.EnsureSuccessStatusCode();
var expectedLength = response.Content.Headers.ContentLength;
await using (var httpStream = await response.Content.ReadAsStreamAsync(ct))
await using (var fileStream = File.Create(destinationPath))
{
await httpStream.CopyToAsync(fileStream, ct);
if (expectedLength is not null && fileStream.Length != expectedLength)
throw new IOException(
$"Downloaded file size ({fileStream.Length}) does not match expected size ({expectedLength})."
);
}
}
private static async Task<bool> VerifyChecksumAsync(
string filePath,
string assetName,
string checksumUrl,
CancellationToken ct
)
{
string checksumsText;
try
{
using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct);
cts.CancelAfter(TimeSpan.FromSeconds(10));
checksumsText = await Http.GetStringAsync(checksumUrl, cts.Token);
}
catch
{
// No checksums reachable for this release — degrade to the size check already done.
return true;
}
var expectedHash = checksumsText
.Split('\n', StringSplitOptions.RemoveEmptyEntries)
.Select(line => line.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries))
.Where(parts => parts.Length == 2 && parts[1].TrimStart('*') == assetName)
.Select(parts => parts[0])
.FirstOrDefault();
if (expectedHash is null)
return true; // Asset not listed in checksums.txt — degrade gracefully.
await using var stream = File.OpenRead(filePath);
var actualHash = Convert.ToHexString(await SHA256.HashDataAsync(stream, ct));
return string.Equals(actualHash, expectedHash, StringComparison.OrdinalIgnoreCase);
}
// ── Platform swap ──────────────────────────────────────────────────────────
[System.Runtime.Versioning.UnsupportedOSPlatform("windows")]
private static UpdateApplyResult ReplaceUnix(string tempPath, string currentExePath)
{
try
{
MakeExecutable(tempPath);
File.Move(tempPath, currentExePath, overwrite: true);
return new UpdateApplyResult(
true,
"Updated successfully. The new version will be used the next time you run git cp."
);
}
catch (Exception ex)
{
var newPath = currentExePath + ".new";
try
{
if (File.Exists(newPath))
File.Delete(newPath);
File.Move(tempPath, newPath);
}
catch
{
TryDelete(tempPath);
}
return new UpdateApplyResult(
false,
$"Couldn't replace the running binary automatically ({ex.Message}). "
+ $"The downloaded update is at '{newPath}' — finish manually with: mv \"{newPath}\" \"{currentExePath}\""
);
}
}
[System.Runtime.Versioning.UnsupportedOSPlatform("windows")]
private static void MakeExecutable(string path)
{
var mode = File.GetUnixFileMode(path);
File.SetUnixFileMode(
path,
mode | UnixFileMode.UserExecute | UnixFileMode.GroupExecute | UnixFileMode.OtherExecute
);
}
private static UpdateApplyResult ReplaceWindows(string tempPath, string currentExePath)
{
var oldPath = currentExePath + ".old";
try
{
File.Move(currentExePath, oldPath, overwrite: true);
File.Move(tempPath, currentExePath);
return new UpdateApplyResult(true, "Updated successfully. Re-run git cp to use the new version.");
}
catch (Exception ex)
{
return new UpdateApplyResult(false, $"Update failed while swapping the executable: {ex.Message}");
}
}
/// <summary>Deletes a stale git-cp.exe.old left behind by a previous Windows update, if it's no longer locked.</summary>
public static void CleanupStaleWindowsOldFile()
{
if (!OperatingSystem.IsWindows())
return;
var currentExePath = Environment.ProcessPath;
if (string.IsNullOrWhiteSpace(currentExePath))
return;
var oldPath = currentExePath + ".old";
try
{
if (File.Exists(oldPath))
File.Delete(oldPath);
}
catch
{
// Still locked by a previous instance — retry on a future launch.
}
}
// ── GitHub API + cache ───────────────────────────────────────────────────
private static async Task<GitHubRelease?> FetchLatestReleaseAsync(CancellationToken ct)
{
try
{
using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct);
cts.CancelAfter(TimeSpan.FromSeconds(5));
return await Http.GetFromJsonAsync<GitHubRelease>(LatestReleaseApiUrl, cts.Token);
}
catch
{
return null;
}
}
private static string NormalizeVersion(string tagName) => tagName.StartsWith('v') ? tagName[1..] : tagName;
private static bool IsNewer(string? latestTagOrVersion, string currentVersion)
{
if (string.IsNullOrWhiteSpace(latestTagOrVersion))
return false;
if (!Version.TryParse(NormalizeVersion(latestTagOrVersion), out var latest))
return false;
if (!Version.TryParse(currentVersion, out var current))
return false;
return latest > current;
}
private static string GetCacheFilePath()
{
if (OperatingSystem.IsWindows())
{
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
return Path.Combine(localAppData, "git-cp", "update-cache.json");
}
var xdgConfig = Environment.GetEnvironmentVariable("XDG_CONFIG_HOME");
var configDir = !string.IsNullOrWhiteSpace(xdgConfig)
? xdgConfig
: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config");
return Path.Combine(configDir, "git-cp", "update-cache.json");
}
private static async Task<UpdateCache?> ReadCacheAsync(CancellationToken ct)
{
var path = GetCacheFilePath();
if (!File.Exists(path))
return null;
await using var stream = File.OpenRead(path);
return await JsonSerializer.DeserializeAsync<UpdateCache>(stream, cancellationToken: ct);
}
private static async Task WriteCacheAsync(UpdateCache cache, CancellationToken ct)
{
var path = GetCacheFilePath();
var dir = Path.GetDirectoryName(path)!;
Directory.CreateDirectory(dir);
var tempPath = path + ".tmp";
await using (var stream = File.Create(tempPath))
await JsonSerializer.SerializeAsync(stream, cache, cancellationToken: ct);
File.Move(tempPath, path, overwrite: true);
}
private static void TryDelete(string path)
{
try
{
if (File.Exists(path))
File.Delete(path);
}
catch
{
// Best-effort cleanup.
}
}
}
public enum UpdateStatus
{
UpToDate,
UpdateAvailable,
CheckFailed,
}
public record UpdateCheckResult(string LatestVersion);
public record UpdateCheckOutcome(
UpdateStatus Status,
string? LatestVersion = null,
string? DownloadUrl = null,
string? AssetName = null,
string? ChecksumUrl = null
);
public record UpdateApplyResult(bool Success, string Message);