summary refs log tree commit diff
path: root/GitRepoViewer/Pages/TestPage.razor
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-05 03:25:53 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-05 03:25:53 +0200
commit51d820e22a4517dbb06d38a4f07f7c48522ef811 (patch)
tree4a7749cf77223dff2414fd4b73cb17df43d7449e /GitRepoViewer/Pages/TestPage.razor
downloadGitTools-51d820e22a4517dbb06d38a4f07f7c48522ef811.tar.xz
Initial commit HEAD master
Diffstat (limited to 'GitRepoViewer/Pages/TestPage.razor')
-rw-r--r--GitRepoViewer/Pages/TestPage.razor225
1 files changed, 225 insertions, 0 deletions
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
+
+<h3>TestPage</h3>
+
+<p>
+    Repo URL:
+    <InputText @bind-Value="RepoUrl"></InputText>
+</p>
+
+
+@if (!IsValidRepo)
+{
+    <p>Invalid Repo</p>
+    <p>This can happen if the repo isn't reachable, doesn't support dumb-http transport or only provides packs.</p>
+    <p>In the latter case, it might be worth running this script in that directory on the server, if you have permission to:</p>
+    <pre>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</pre>
+}
+else
+{
+    <p>
+
+        @code {
+
+            Dictionary<string, string> knownRefPrefixes = new Dictionary<string, string>()
+            {
+                { "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))))
+        {
+            <span>@friendlyName:</span>
+            <select @bind="CurrentRef">
+                @foreach (var refName in Refs.Where(x => x.Key.StartsWith(prefix)).Select(x => x.Key.Replace(prefix, "")).OrderBy(x => x))
+                {
+                    <option value="@(prefix + refName)">@refName</option>
+                }
+            </select>
+        }
+
+
+        @* Other: *@
+        @* <select @bind="CurrentRef"> *@
+        @* @foreach (var refName in Refs.Where(x => x.Key.StartsWith("refs/pull")).Select(x => x.Key.Replace("refs/pull/", "")).OrderBy(x => x)) *@
+        @* { *@
+        @* <option value="refs/pull/@refName">@refName</option> *@
+        @* } *@
+        @* </select> *@
+    </p>
+
+    <p>@_commits.Count commits</p>
+
+    <table>
+        <thead>--files--</thead>
+        <tbody>
+        <tr>
+    
+        </tr>
+        </tbody>
+    </table>
+    <table>
+        @foreach (var commit in _commits.Values.OrderByDescending(x => x.CommitDate))
+        {
+            <tr>
+                @* <td>@commit.Length</td> *@
+                <td style="width: 50%;">@commit.Message</td>
+                <td style="width: 25%;">@commit.CommitterName</td>
+                <td style="width: 25%;">@commit.CommitDate</td>
+            </tr>
+        }
+    </table>
+
+    @if (data != null)
+    {
+        <pre style="max-width: 100%; white-space: pre-wrap;">
+            @if (data is string)
+            {
+                @data
+            }
+            else
+            {
+                @ObjectExtensions.ToJson(data, unsafeContent: true)
+            }
+        </pre>
+    }
+}
+
+@code {
+
+    GitRepo repo { get; set; }
+    readonly ConcurrentDictionary<string, CommitObject> _commits = new();
+    bool IsValidRepo { get; set; } = true;
+    Dictionary<string, string> 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