#!/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, '')}," + 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, "") 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")