# commit-converter.py # # This script will convert a git repository's history into a CSV file # suitable for importing into the `action` table of a Gitea database, # because Gitea does not show user activity for commits -- only for # pushes, comments, and that sort of thing. # # Usage: # git log --date=format:"%Y-%m-%dT%H:%M:%S%z" --pretty="%H|%s|%ae|%an|%ce|%cn|%ct|%cd" | sed 's/00$/:00/g' > /tmp/raw-commits.txt import json import csv with open('/tmp/raw-commits.txt', 'r') as file: outfile = open('/tmp/output.csv', 'w') writer = csv.writer(outfile, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL) commits = [] data = [] i = 5 for line in file.readlines(): line = line.strip() pline = line.split('|') commit = { 'Sha1': pline[0], 'Message': pline[1], 'AuthorEmail': pline[2], 'AuthorName': pline[3], 'CommitterEmail': pline[4], 'CommitterName': pline[5], 'Timestamp': pline[7] } if 0 == len(commits): commits.append(commit) continue prior_commit = commits[-1] commits.append(commit) struct = {'Commits': [commit], 'HeadCommit': commit, 'CompareURL': 'davisr/rcu2/compare/{}...{}'.format( prior_commit['Sha1'], commit['Sha1']), 'Len': 1} je = json.JSONEncoder().encode(struct) final = [str(i+1), '1', '5', '1', '3', '0', '0', 'refs/heads/develop', '0', je, pline[6]] data.append(final) i+=1 writer.writerows(data) outfile.close()