aboutsummaryrefslogtreecommitdiffstats
path: root/osc_fast_export.py
blob: 2bfa4d6a5b619d10b099ac4e8d2b5daf17fbde29 (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
#!/usr/bin/python
# https://git-scm.com/book/en/v2/Git-and-Other-Systems-Migrating-to-Git#_custom_importer

import collections
import configparser
from datetime import datetime
import pathlib
import subprocess
from typing import List

import xml.etree.ElementTree as ET

authorsfile = pathlib.Path(".git", "authorsfile.txt")

# For reading section-less config files
# https://stackoverflow.com/a/2819788/164233
def FakeSecHead(fp):
    yield "[asection]\n"
    yield from fp


config = configparser.ConfigParser()
authors = {}
if authorsfile.exists():
    config.read_file(FakeSecHead(open(authorsfile)))
    authors = dict(config.items("asection"))


class LogEntry(
    collections.namedtuple("LogEntry", ["rev", "md5", "author", "date", "msg"])
):
    def __str__(self):
        return (
            f"{self.rev}, {self.md5[:12]}, {authors.get(self.author, '<none>')},"
            + f" {datetime.isoformat(self.date)}:\n{self.msg}"
        )


def osc_log() -> List[LogEntry]:
    try:
        log_str = subprocess.run(
            ["osc", "log", "--xml"], check=True, text=True, stdout=subprocess.PIPE
        ).stdout
    except subprocess.CalledProcessError:
        raise
    tree = ET.fromstring(log_str)
    log_list = [
        LogEntry(
            int(entry.attrib["revision"]),
            entry.attrib["srcmd5"],
            entry.findtext("author"),
            datetime.strptime(entry.findtext("date"), "%Y-%m-%d %H:%M:%S"),
            entry.findtext("msg"),
        )
        for entry in tree.iter("logentry")
    ]
    log_list.reverse()
    return log_list


def export_data(dt: str) -> str:
    return f"data {len(dt)}\n{dt}"


def print_export(entry: LogEntry) -> int:
    mark = entry.rev
    author = authors.get(entry.author, "<none>")
    date = int(datetime.timestamp(entry.date))

    print("commit refs/heads/master")
    print(f"mark :{mark}")
    if entry.md5:
        print(f"original-oid {entry.md5}")
    print(f"committer {author} {date} +0000")
    print(export_data(entry.msg))
    if last_mark:
        print(f"from :{last_mark}")
    # The following is dummy, needs to be replaced by actual content of each commit.
    print("deleteall")
    print("M 644 inline tralala")
    print(export_data("tralalala"))
    print("")

    return mark


if __name__ == "__main__":
    last_mark = None

    for logentry in osc_log():
        last_mark = print_export(logentry)
    print("done")