Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions capella_diff_tools/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import datetime
import logging
import os
import pathlib
import sys
import typing as t
Expand Down Expand Up @@ -65,14 +64,15 @@ def main(
"""
logging.basicConfig(level="DEBUG")
model.pop("revision", None)
model["path"] = _ensure_git(model["path"])
_ensure_git(model)
old_model = capellambse.MelodyModel(**model, revision=old_version)
new_model = capellambse.MelodyModel(**model, revision=new_version)

metadata: types.Metadata = {
"model": model,
"old_revision": _get_revision_info(old_model, old_version),
"new_revision": _get_revision_info(new_model, new_version),
"commit_log": _get_commit_log(new_model, old_version, new_version),
}
objects = compare.compare_all_objects(old_model, new_model)
diagrams = compare.compare_all_diagrams(old_model, new_model)
Expand All @@ -92,18 +92,19 @@ def main(
report_file.write(report.generate_html(result))


def _ensure_git(path: str | os.PathLike[str]) -> str:
proto, path = fh.split_protocol(path)
def _ensure_git(model: dict[str, t.Any]) -> None:
proto, path = fh.split_protocol(model["path"])
if proto == "file":
assert isinstance(path, pathlib.Path)
path = "git+" + path.resolve().as_uri()
path = path.resolve()
if "entrypoint" not in model and path.is_file():
model["entrypoint"] = path.name
path = path.parent
model["path"] = "git+" + path.as_uri()
elif proto != "git":
raise click.UsageError("The 'model' must point to a git repository")

proto, _ = fh.split_protocol(path)
if proto != "git":
raise click.Abort("The 'model' must point to a git repository")

assert isinstance(path, str)
return path
assert isinstance(model["path"], str)


def _get_revision_info(
Expand Down Expand Up @@ -132,6 +133,36 @@ def _get_revision_info(
}


def _get_commit_log(
model: capellambse.MelodyModel,
old_version: str,
new_version: str,
) -> list[types.RevisionInfo]:
res = model._loader.resources["\x00"]
assert isinstance(res, fh.git.GitFileHandler)
commits: list[types.RevisionInfo] = []
rawlog = res._git(
"log",
"-z",
"--reverse",
"--format=format:%H%x00%aN%x00%aI%x00%B",
f"{old_version}..{new_version}",
encoding="utf-8",
).split("\x00")
log = capellambse.helpers.ntuples(4, rawlog)
for hash, author, date_str, description in log:
commits.append(
{
"hash": hash,
"revision": hash,
"author": author,
"date": datetime.datetime.fromisoformat(date_str),
"description": description.rstrip(),
}
)
return commits


class CustomYAMLDumper(yaml.SafeDumper):
"""A custom YAML dumper that can serialize markupsafe.Markup."""

Expand Down
50 changes: 46 additions & 4 deletions capella_diff_tools/report.html.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<b>Repository: </b>{{ data["metadata"]["model"]["path"] | e }} <br/>
<b>Entry point: </b>{{ data["metadata"]["model"]["entrypoint"] | e }}
</p>
<p>The review of changes covers the following commits:</p>
<p>The review of changes covers the following range of commits:</p>
<table>
<thead>
<tr>
Expand All @@ -84,9 +84,9 @@
<td>{{ data["metadata"]["new_revision"]["date"] | e }}</td>
</tr>
<tr>
<th>Commit message</th>
<td>{{ data["metadata"]["old_revision"]["description"] | e }}</td>
<td>{{ data["metadata"]["new_revision"]["description"] | e }}</td>
<th>Summary</th>
<td>{{ data["metadata"]["old_revision"]["description"].split("\n") | first | e }}</td>
<td>{{ data["metadata"]["new_revision"]["description"].split("\n") | first | e }}</td>
</tr>
<tr>
<th>Commit ID (hash)</th>
Expand All @@ -96,6 +96,48 @@
</tbody>
</table>

<p>Detailed information about the contained commits:</p>

<table>
<tbody>
{% for commit in data.metadata.commit_log -%}
<tr>
<td>{{ commit.hash | e }}</td>
<td>{{ commit.date | e }}</td>
<td>{{ commit.author | e }}</td>
<td style="white-space: preserve">{{ commit.description | e }}</td>
</tr>
{% else -%}
<tr>
<td>{{ data.metadata.old_revision.hash }}</td>
<td>{{ data.metadata.old_revision.date }}</td>
<td>{{ data.metadata.old_revision.author }}</td>
<td style="white-space: preserve">{{ data.metadata.old_revision.description }}</td>
</tr>
<tr>
<td colspan="4" style="font-style: italic; color: red">
Error: The supplied report does not contain the commit log.
Detailed information is only available for the first and last commit.
</td>
</tr>
<tr>
<td>{{ data.metadata.new_revision.hash }}</td>
<td>{{ data.metadata.new_revision.date }}</td>
<td>{{ data.metadata.new_revision.author }}</td>
<td style="white-space: preserve">{{ data.metadata.new_revision.description }}</td>
</tr>
{% endfor -%}
</tbody>
<thead>
<tr>
<th>Commit ID (hash)</th>
<th>Date &amp; time</th>
<th>Author</th>
<th>Commit message</th>
</tr>
</thead>
</table>

{% macro pretty_stats(stats) %}
<span style="font-weight: normal;">(
{% if stats.created %}<span style="color:green">+{{stats["created"]}}</span> / {% endif %}
Expand Down
1 change: 1 addition & 0 deletions capella_diff_tools/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Metadata(te.TypedDict):
"""The 'modelinfo' used to load the models, sans the revision key."""
new_revision: RevisionInfo
old_revision: RevisionInfo
commit_log: list[RevisionInfo]


class RevisionInfo(te.TypedDict, total=False):
Expand Down
24 changes: 24 additions & 0 deletions ci-templates/gitlab/compare-to-tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0
# yaml-language-server: $schema=https://gitlab.com/gitlab-org/gitlab/-/raw/master/app/assets/javascripts/editor/schema/ci.json

compare-to-tag:
image: python:3.12
script:
- oldversion="$(git describe --tags --abbrev=0 || true)"
- if ! [[ $oldversion ]]; then echo >&2 No recent tag found to compare against; exit 0; fi
- |-
# Install capella-diff-tools from PyPI or Github
if [[ ${CAPELLA_DIFF_TOOLS_VERSION:-vX.Y.Z} = v*.*.* ]]; then
pip install "capella-diff-tools${CAPELLA_DIFF_TOOLS_VERSION:+==$CAPELLA_DIFF_TOOLS_VERSION}"
else
pip install "git+https://github.com/DSD-DBS/capella-diff-tools.git@$CAPELLA_DIFF_TOOLS_VERSION"
fi
- capella-diff-tool ${ENTRYPOINT:-.} "$oldversion" HEAD -o model-diff.yml -r model-diff.html

artifacts:
paths: [model-diff.html, model-diff.yml]

variables:
CAPELLA_DIFF_TOOLS_VERSION:
description: Version of capella-diff-tools to install. Should be the same as the version of this template.