Skip to content

Commit 9814380

Browse files
committed
Optimize retrieval and propagation of X-Sha
Simplify retrieval by leveraging JQ support in GH CLI, and always ensure we propagate the latest value for the X-Sha header.
1 parent e2531c3 commit 9814380

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/File/GitHub.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public static class GitHub
3333
public static bool TryIsInstalled(out string output)
3434
=> Process.TryExecute("gh", "--version", out output) && output.StartsWith("gh version");
3535

36+
public static bool TryApi(string endpoint, string jq, out string? output)
37+
=> Process.TryExecute("gh", $"api {endpoint} --jq \"{jq}\"", out output);
38+
3639
public static bool TryApi(string endpoint, out JToken? json)
3740
{
3841
json = null;

src/File/Http/GitHubRawHandler.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,10 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
5959
(originalEtag != newEtag || originalSha == null) &&
6060
parts.Length > 2 &&
6161
GitHub.IsInstalled &&
62-
GitHub.TryApi($"repos/{parts[0]}/{parts[1]}/commits?per_page=1&path={string.Join('/', parts.Skip(4))}", out var json) &&
63-
json is JArray commits &&
64-
commits[0] is JObject obj &&
65-
obj.Property("sha") is JProperty prop &&
66-
prop != null &&
67-
prop.Value.Type == JTokenType.String)
62+
GitHub.TryApi($"repos/{parts[0]}/{parts[1]}/commits?per_page=1&path={string.Join('/', parts.Skip(4))}", ".[0]?.sha", out var json) &&
63+
json is { Length: > 0 })
6864
{
69-
newSha = prop.Value.ToObject<string>();
65+
newSha = json;
7066
}
7167

7268
// Just propagate back what we had initially, as an optimization for HEAD and cases
@@ -75,7 +71,13 @@ commits[0] is JObject obj &&
7571
newSha = originalSha;
7672

7773
if (newSha != null)
74+
{
75+
// Make sure the X-Sha matches what we have now.
76+
if (response.Headers.TryGetValues("X-Sha", out _))
77+
response.Headers.Remove("X-Sha");
78+
7879
response.Headers.TryAddWithoutValidation("X-Sha", newSha);
80+
}
7981

8082
return response;
8183
}

0 commit comments

Comments
 (0)