From 51d820e22a4517dbb06d38a4f07f7c48522ef811 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Mon, 5 Jun 2023 03:25:53 +0200 Subject: Initial commit --- GitRepoViewer/Pages/TestPage.razor | 225 +++++++++++++++++++++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 GitRepoViewer/Pages/TestPage.razor (limited to 'GitRepoViewer/Pages/TestPage.razor') diff --git a/GitRepoViewer/Pages/TestPage.razor b/GitRepoViewer/Pages/TestPage.razor new file mode 100644 index 0000000..2d29609 --- /dev/null +++ b/GitRepoViewer/Pages/TestPage.razor @@ -0,0 +1,225 @@ +@page "/TestPage" +@using System.Collections.Concurrent +@using Blazored.SessionStorage +@using LibGit +@using LibGit.Extensions + +

TestPage

+ +

+ Repo URL: + +

+ + +@if (!IsValidRepo) +{ +

Invalid Repo

+

This can happen if the repo isn't reachable, doesn't support dumb-http transport or only provides packs.

+

In the latter case, it might be worth running this script in that directory on the server, if you have permission to:

+
mv objects/pack/ objects/pack-tmp; for pack in objects/pack-tmp/*.pack; do cat $pack | git unpack-objects; done; mv objects/pack-tmp/ objects/pack
+} +else +{ +

+ + @code { + + Dictionary knownRefPrefixes = new Dictionary() + { + { "refs/heads/", "Head" }, + { "refs/pull/", "Pull" }, + { "refs/tags/", "Tag" }, + { "refs/remotes/", "Remote" }, + { "refs/notes/", "Note" }, + { "refs/stash", "Stash" }, + { "refs/replace/", "Replace" }, + { "refs/wip/", "WIP" }, + { "refs/keep-around/", "Keep Around" }, + { "refs/merges/", "Merge" }, + }; + + } + + + @foreach (var (prefix, friendlyName) in knownRefPrefixes.Where(x => Refs.Any(y => y.Key.StartsWith(x.Key)))) + { + @friendlyName: + + } + + + @* Other: *@ + @* *@ +

+ +

@_commits.Count commits

+ + + --files-- + + + + + +
+ + @foreach (var commit in _commits.Values.OrderByDescending(x => x.CommitDate)) + { + + @* *@ + + + + + } +
@commit.Length@commit.Message@commit.CommitterName@commit.CommitDate
+ + @if (data != null) + { +
+            @if (data is string)
+            {
+                @data
+            }
+            else
+            {
+                @ObjectExtensions.ToJson(data, unsafeContent: true)
+            }
+        
+ } +} + +@code { + + GitRepo repo { get; set; } + readonly ConcurrentDictionary _commits = new(); + bool IsValidRepo { get; set; } = true; + Dictionary Refs = new(); + + string CurrentRef + { + get => _currentRef; + set + { + _currentRef = value; + _commits.Clear(); + RefChanged(); + } + } + + string RepoUrl + { + get => _repoUrl; + set + { + _repoUrl = value; + if (!value.StartsWith("http")) + { + _repoUrl = NavigationManager.BaseUri + value; + } + _commits.Clear(); + Refs.Clear(); + RepoChanged(); + } + } + + dynamic data { get; set; } + + protected override async Task OnInitializedAsync() + { + RepoUrl = "fosscord-server.git"; + } + + protected async Task RepoChanged() + { + Console.WriteLine("Repo changed!"); + repo = new GitRepo(new WebRepoSource(RepoUrl)); + ((WebRepoSource)repo.RepoSource).SessionStorage = sessionStorage; + var client = new HttpClient(); + var response = await client.GetAsync(RepoUrl + "/info/refs"); + if (!response.IsSuccessStatusCode) + { + IsValidRepo = false; + StateHasChanged(); + return; + } + IsValidRepo = true; + var content = await response.Content.ReadAsStringAsync(); + data = content; + StateHasChanged(); + var lines = content.Split("\n").ToList(); + var delay = Math.Max(lines.Count / 100, 50); + var index = 0; + + while (lines.Count > 0) + { + var line = lines[0]; + lines.Remove(line); + if (line == "") + { + break; + } + var parts = line.Split("\t"); + Refs.Add(parts[1], parts[0]); + data = new + { + index, + delay, + untilNext = delay - index % delay, + line, + lines, + refs = Refs + }; + if (++index % delay == 0) + { + StateHasChanged(); + await Task.Delay(1); + } + } + response = await client.GetAsync(RepoUrl + "/HEAD"); + content = await response.Content.ReadAsStringAsync(); + if (content.StartsWith("ref: ")) + CurrentRef = content.Substring(5).Trim(); + StateHasChanged(); + } + + protected async Task RefChanged(string refName = null) + { + string currentRef; + data = currentRef = Refs[CurrentRef]; + Console.WriteLine($"Ref changed: {currentRef}"); + StateHasChanged(); + //await LoadObject(currentRef); + await LoadGitLogSince(currentRef); + } + + protected async Task LoadGitLogSince(string refName) + { + _commits.Clear(); + var commit = await repo.GetCommit(refName); + _commits.TryAdd(commit.CommitId, commit); + while (commit.ParentIds.Count > 0) + { + Console.WriteLine($"{commit.CommitId[..7]} | {commit.AuthorName.PadRight(16)} | {commit.Message.PadRight(32)[..32]} | {commit.TreeId}"); + // var tree = await commit.GetTreeAsync(); + // await PrintTreeRecursive(tree); + + commit = await repo.GetCommit(commit.ParentIds.First()); + await Task.Delay(1); + } + } + + private string _repoUrl = "http://localhost:5194/fosscord-server.git"; + private string _currentRef; +} \ No newline at end of file -- cgit 1.5.1