Forge

Jessie / forge public

Self-hosted git - made by qwen 3.8 27b

commit: 0c9688e9

Branches

main
Commit.razor 6.1 kB · 171 lines RawHistory
@page "/{Owner}/{Repo}/commit/{Sha}"
@inherits RepoPageBase
@inject RefService Refs
@inject ForgeDb Db

@if (Repository is not null && _commit is not null && _diff is not null)
{
    <PageTitle>@_commit.Subject · @_commit.ShortSha</PageTitle>
    <HighlightAssets/>

    <RepoHeader Repository="Repository" Active="commits" IsOwner="IsOwner"/>

    @if (_error is not null)
    {
        <div class="alert error">
            <p>@_error</p>
            @if (_conflicts.Count > 0)
            {
                <ul>
                    @foreach (var file in _conflicts)
                    {
                        <li><code>@file</code></li>
                    }
                </ul>
            }
        </div>
    }

    <section class="card commit-detail">
        <h2 class="commit-title">@(_commit.Subject.Length == 0 ? T["(no message)"] : _commit.Subject)</h2>
        @if (_commit.Body.Length > 0)
        {
            <pre class="commit-body">@_commit.Body</pre>
        }
        <dl class="meta">
            <dt>@T["Author"]</dt>
            <dd><Avatar Name="@_commit.AuthorName"/> @_commit.AuthorName &lt;@_commit.AuthorEmail&gt; · <TimeAgo Value="_commit.AuthorDate"/></dd>
            @if (_commit.CommitterName != _commit.AuthorName || _commit.CommitterEmail != _commit.AuthorEmail)
            {
                <dt>@T["Committer"]</dt>
                <dd>@_commit.CommitterName &lt;@_commit.CommitterEmail&gt; · <TimeAgo Value="_commit.CommitDate"/></dd>
            }
            <dt>@T["Commit"]</dt>
            <dd>
                <code>@_commit.Sha</code>
                <button type="button" class="icon-button inline js-only" data-copy="@_commit.Sha"
                        title="@T["Copy the full SHA"]" aria-label="@T["Copy the full SHA"]">
                    <Icon Name="copy" Size="14"/>
                </button>
            </dd>
            <dt>@(_commit.Parents.Count == 1 ? T["Parent"] : T["Parents"])</dt>
            <dd>
                @if (_commit.Parents.Count == 0)
                {
                    <span class="muted">@T["none (first commit)"]</span>
                }
                @foreach (var parent in _commit.Parents)
                {
                    <a class="sha" href="@Urls.Commit(Repository, parent)">@(parent[..8])</a>
                }
            </dd>
        </dl>
        <p><a class="button" href="@Urls.Tree(Repository, _commit.Sha)"><Icon Name="code"/> @T["Browse files at this commit"]</a></p>

        @* Reverting and cherry-picking need exactly one parent to know what "this commit's change" is. *@
        @if (IsOwner && _commit.Parents.Count == 1 && _branches.Count > 0)
        {
            <form method="post" @formname="apply-commit" @onsubmit="ApplyAsync" class="toolbar apply-form">
                <AntiforgeryToken/>
                <label>
                    <span class="muted">@T["Apply to branch"]</span>
                    <select name="Branch">
                        @foreach (var branch in _branches)
                        {
                            <option value="@branch" selected="@(branch == Repository.DefaultBranch)">@branch</option>
                        }
                    </select>
                </label>
                <button type="submit" name="Action" value="revert" class="button">@T["Revert"]</button>
                <button type="submit" name="Action" value="pick" class="button">@T["Cherry-pick"]</button>
            </form>
        }
    </section>

    <h3>
        @T.Plural(_diff.Stats.Count, "{0} changed file", "{0} changed files"):
        <span class="added">+@_diff.Stats.Sum(s => s.Added ?? 0)</span>
        <span class="deleted">−@_diff.Stats.Sum(s => s.Deleted ?? 0)</span>
    </h3>
    @if (_commit.Parents.Count > 1)
    {
        <p class="muted">@T["Merge commit: changes are shown against the first parent."]</p>
    }

    <DiffOptions Path="@Urls.Commit(Repository, _commit.Sha)" Split="Split" IgnoreWhitespace="IgnoreWhitespace"/>
    <DiffView Diff="_diff.Patch" Split="Split" Repository="Repository" HeadSha="@_commit.Sha"/>
}

@code {
    [Parameter]
    public string Sha { get; set; } = "";

    [SupplyParameterFromQuery(Name = "view")]
    private string? View { get; set; }

    [SupplyParameterFromQuery(Name = "w")]
    private string? Whitespace { get; set; }

    [SupplyParameterFromForm(FormName = "apply-commit")]
    private string? Branch { get; set; }

    [SupplyParameterFromForm(FormName = "apply-commit")]
    private string? Action { get; set; }

    private CommitInfo? _commit;
    private CommitDiff? _diff;
    private List<string> _branches = [];
    private string? _error;
    private IReadOnlyList<string> _conflicts = [];

    private bool Split => View == "split";
    private bool IgnoreWhitespace => Whitespace == "1";

    protected override async Task OnInitializedAsync()
    {
        if (!await LoadRepoAsync())
        {
            return;
        }

        // ResolveCommitAsync accepts hex only, so nothing else from the URL reaches git.
        var sha = await Git.ResolveCommitAsync(RepoPath, Sha);
        _commit = sha is null ? null : await Git.GetCommitAsync(RepoPath, sha);
        if (_commit is null)
        {
            NotFound();
            return;
        }

        _diff = await Git.GetDiffAsync(RepoPath, _commit.Sha, IgnoreWhitespace);
        if (IsOwner)
        {
            _branches = (await Git.ListRefsAsync(RepoPath)).Where(r => r.Kind == RefKind.Branch).Select(r => r.Name).ToList();
        }
    }

    private async Task ApplyAsync()
    {
        if (Repository is null || _commit is null || !IsOwner || Action is not ("revert" or "pick"))
        {
            return;
        }

        var user = await Db.Users.FindAsync(Me.UserId);
        if (user is null)
        {
            return;
        }

        var revert = Action == "revert";
        var result = await Refs.ApplyCommitAsync(Repository, Refs.IdentityFor(user), Branch ?? "", _commit.Sha, revert);
        if (!result.Ok)
        {
            _error = T[result.Error!];
            _conflicts = result.Conflicts ?? [];
            return;
        }

        Redirect(Urls.Commit(Repository, result.Sha!));
    }
}

Keyboard shortcuts

/
Search repositories
s
Search file names and code
y
Open the permalink of the file being viewed
?
Show this help