aboutsummaryrefslogtreecommitdiffstats
path: root/osc_fast_export.py
blob: 3b7c727a0fd2941dd68ac71255e48e5abe8195e8 (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
#!/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 subprocess

import xml.etree.ElementTree as ET

authorsfile = "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()
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():
    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):
    return f"data {len(dt)}\n{dt}"


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

    print("commit refs/heads/master")
    print(f"mark :{mark}")
    print(f"committer {author} {date} +0000")
    print(export_data(entry.msg))
    if last_mark:
        print(f"from :{last_mark}")
    print("deleteall")
    print("M 644 inline tralala")
    print(export_data("tralalala"))

    return mark


if __name__ == "__main__":
    last_mark = None

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