summary refs log tree commit diff
path: root/GitRepoViewer/Pages/TestPage.razor
blob: 2d29609096c6a84ba128933ff980fd6fc0537ab1 (plain) (blame)
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
@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;
}