| 1 |
from __future__ import annotations |
| 2 |
from typing import Set, Tuple, TypedDict |
| 3 |
|
| 4 |
import time |
| 5 |
|
| 6 |
from .blame import BlameResult |
| 7 |
|
| 8 |
Changes = Set[Tuple[str, str]] |
| 9 |
|
| 10 |
|
| 11 |
class Changeset(TypedDict): |
| 12 |
author: str |
| 13 |
first_commit: float |
| 14 |
changes: Changes |
| 15 |
|
| 16 |
|
| 17 |
def by_first_commit(item: Changeset): |
| 18 |
"""Order two changesets by their first commit date.""" |
| 19 |
return item["first_commit"] |
| 20 |
|
| 21 |
|
| 22 |
def convert_blame_to_changesets(blame_json: BlameResult) -> list[Changeset]: |
| 23 |
"""Convert a blame dict into a list of changesets. |
| 24 |
|
| 25 |
The blame information in `blame_json` should be a dict of the following |
| 26 |
structure: |
| 27 |
|
| 28 |
{ |
| 29 |
'authors': [ |
| 30 |
'A.N. Author <[email protected]>', |
| 31 |
], |
| 32 |
'blame': { |
| 33 |
'path/one': { |
| 34 |
'key1': [0, 1346095921.0], |
| 35 |
}, |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
It will be transformed into a list of changesets which can be fed into |
| 40 |
`InternalContext.serialize_changeset`: |
| 41 |
|
| 42 |
[ |
| 43 |
{ |
| 44 |
'author': 'A.N. Author <[email protected]>', |
| 45 |
'first_commit': 1346095921.0, |
| 46 |
'changes': { |
| 47 |
('path/one', 'key1'), |
| 48 |
} |
| 49 |
}, |
| 50 |
] |
| 51 |
|
| 52 |
""" |
| 53 |
now = time.time() |
| 54 |
changesets: list[Changeset] = [ |
| 55 |
{"author": author, "first_commit": now, "changes": set()} |
| 56 |
for author in blame_json["authors"] |
| 57 |
] |
| 58 |
|
| 59 |
for path, keys_info in blame_json["blame"].items(): |
| 60 |
for key, (author_index, timestamp) in keys_info.items(): |
| 61 |
changeset = changesets[author_index] |
| 62 |
changeset["changes"].add((path, key)) |
| 63 |
if timestamp < changeset["first_commit"]: |
| 64 |
changeset["first_commit"] = timestamp |
| 65 |
|
| 66 |
return sorted(changesets, key=by_first_commit) |