summary refs log tree commit diff
path: root/GitRepoViewer/Pages/GitLog.razor
blob: e518bb6ab8950cec4a4795f05c429af17ad0c109 (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
@page "/GitLog"
@using System.Web
@using LibGit
@using LibGit.Extensions
<h3>GitLog</h3>
<p>Repo: @Repo</p>

@if (heads.Count > 0)
{
    <p>
        Revision:
        <details>
            <summary>@Rev</summary>
        </details>
    </p>
}

@if (commits != null)
{
    <table class="table">
        <thead>
        <tr>
            <th>Heads</th>
            <th>Commit</th>
            <th>Author</th>
            <th>Message</th>
        </tr>
        </thead>
        <tbody>
            @foreach (var commit in commits)
            {
                <tr>
                    <td>
                        @foreach (var _ref in heads.Where(x=>x.CommitId == commit.CommitId))
                        {
                            <p>@_ref.Name</p>
                        }
                    </td>
                    <td>@commit.CommitId[..7]</td>
                    <td>@commit.AuthorName</td>
                    <td>@commit.Message</td>
                </tr>
            }
        </tbody>
    </table>
}

@code {

    string? Repo { get; set; }
    string? Rev { get; set; }
    
    List<CommitObject> commits = new();
    List<GitRef> heads = new();

    protected override async Task OnInitializedAsync()
    {
        var query = new Uri(NavigationManager.Uri).Query;
        var queryDictionary = HttpUtility.ParseQueryString(query);
        if((Repo = queryDictionary["repo"]) == null)
        {
            return;
        }
        
        var repo = new GitRepo(new WebRepoSource(queryDictionary["repo"])
        {
            SessionStorage = sessionStorage
        });
        
        if((Rev = queryDictionary["rev"]) == null)
        {
            Rev = "HEAD";
        }

        var ss = new SemaphoreSlim(2,2);
        
        var _heads = repo.GetRefs().GetAsyncEnumerator();
        while (await _heads.MoveNextAsync())
        {
            heads.Add(_heads.Current);
            var isCached = await ((WebRepoSource)repo.RepoSource).HasObjectCached(_heads.Current.CommitId);
            Console.WriteLine(_heads.Current.Name+ " - cache miss: " + !isCached);
            if (!isCached)
            {

                var _c = _heads.Current.CommitId;
#pragma warning disable CS4014
                Task.Run(async () =>
#pragma warning restore CS4014
                {
                    await ss.WaitAsync();
                    Console.WriteLine("hi from task");
                    var a = new GitRepo(new WebRepoSource(queryDictionary["repo"])
                    {
                        SessionStorage = sessionStorage
                    }).GetCommits(_c).GetAsyncEnumerator();
                    while (
                        await a.MoveNextAsync() 
                        && !await ((WebRepoSource)repo.RepoSource)
                            .HasObjectCached(a.Current.CommitId)
                    ) Console.WriteLine($"Prefetched commit {a.Current.CommitId} with {a.Current.ParentIds.Count()} parents");
                    Console.WriteLine($"Reached already-cached log: {a.Current.CommitId}");
                    ss.Release();
                });
            }
        }
        

        var log = repo.GetCommits(heads.First(x=>x.Name == Rev).CommitId).GetAsyncEnumerator();
        while (await log.MoveNextAsync())
        {
            commits.Add(log.Current);
            if (commits.Count % 50 == 0)
            {
                StateHasChanged();
                await Task.Delay(1);
            }
        }
        
    }
}