Pi66

git.pi66.xyz
A web interface for the pi66.xyz Git server
git clone https://git.pi66.xyz/git.pi66.xyz

src/plugins/log_builder.py

 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
import os
from models import Repo
from dataclasses import asdict
from git import compute_bars
from progress import progress, progress_done


def _total_removed_lines(files):
    return sum(f.removed_lines for f in files)


def _total_added_lines(files):
    return sum(f.added_lines for f in files)


def _create_commit_page(api, repo_name, repo_desc, commit):
    dest = os.path.join(
        api.config.tree.draft_dest if api.mode == "draft" else api.config.tree.release_dest,
        f"{repo_name.lower()}/commits/{commit.commit_hash}.html"
    )

    if os.path.exists(dest):
        return

    patch_html = api.md_to_html(
        config=api.config,
        md_content=f"```diff\n{commit.patch}```"
    )

    html = api.jinja_handler(api.config, '{% extends "Log-commit.html" %}', plugins={
        "repo_name": repo_name,
        "repo_desc": repo_desc,
        "page_title": f"Pi66 - {repo_name} - {commit.message}",
        "metadata_content": repo_desc or "A web interface for the pi66.xyz Git server",
        "commit_date": commit.date,
        "commit_hash": commit.commit_hash,
        "commit_message": commit.message,
        "commit_author": commit.author,
        "changed_files": [asdict(f) for f in commit.files],
        "diff_content": patch_html,
        "bars":compute_bars(commit.files),
    })

    api.save_html(html, dest)


def create_log_pages(api, repos: list[Repo]):
    total = len(repos)
    for i, repo in enumerate(repos, 1):
        progress("Logs", i, total)
        try:
            for commit in repo.commits:
                _create_commit_page(api, repo.name, repo.desc, commit)

            html = api.jinja_handler(api.config, '{% extends "Log.html" %}', plugins={
                "repo_name": repo.name,
                "repo_desc": repo.desc,
                "page_title": f"Pi66 - {repo.name} - Log",
                "metadata_content": repo.desc or "A web interface for the pi66.xyz Git server",
                "commits": repo.commits,
                "total_added": _total_added_lines,
                "total_removed": _total_removed_lines,
                "len": len,
            })

            dest = os.path.join(api.config.tree.draft_dest if api.mode == "draft" else api.config.tree.release_dest, f"{repo.name.lower()}/log.html")
            api.save_html(html, dest)
        except Exception as e:
            api.log.warn(f"Error building log for {repo.name}: {e}")
    progress_done("Logs", total)