Skip to content

Instantly share code, notes, and snippets.

@achimnol
Created August 23, 2012 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save achimnol/3438309 to your computer and use it in GitHub Desktop.
Save achimnol/3438309 to your computer and use it in GitHub Desktop.
A commit ID mapping creator for a hg repository and a (converted) git repository based on commit timestamps
#! /usr/bin/env python3
import os, sys
import subprocess
from datetime import datetime
def execute(cmd):
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
stdout, stderr = proc.communicate()
return stdout.decode('utf-8')
def parse_logitem(line):
pieces = line.split(' ')
timestamp = datetime.strptime(' '.join(pieces[:3]), '%Y-%m-%d %H:%M:%S %z')
tt = datetime.utctimetuple(timestamp) # normalize the datetime object to naive UTC one.
timestamp_utc = datetime(tt[0], tt[1], tt[2], tt[3], tt[4], tt[5], tzinfo=None)
commit_id = pieces[3]
short_desc = ' '.join(pieces[4:])
return timestamp_utc, commit_id, short_desc
if __name__ == '__main__':
hg_path = sys.argv[1]
git_path = sys.argv[2]
basedir = os.path.abspath(os.path.dirname(__file__))
os.chdir(os.path.join(basedir, hg_path))
print('reading hg log...', file=sys.stderr)
hg_log_output = execute("hg log --template '{date|isodatesec} {node} {desc|firstline}\n' -r :")
hg_log = {}
for line in hg_log_output.splitlines():
timestamp, commit_id, short_desc = parse_logitem(line)
hg_log[timestamp] = (commit_id, short_desc)
os.chdir(os.path.join(basedir, git_path))
print('reading git log...', file=sys.stderr)
git_log_output = execute("git --no-pager log --format='%ai %H %s' --reverse --all")
git_log = {}
for line in git_log_output.splitlines():
timestamp, commit_id, short_desc = parse_logitem(line)
git_log[timestamp] = (commit_id, short_desc)
count_match, count_mismatch = 0, 0
for hg_time, hg_info in hg_log.items():
if hg_time in git_log:
print('{0} => {1}'.format(hg_info[0], git_log[hg_time][0]))
count_match += 1
else:
print('Mismatch: {0} at {2:%Y-%m-%d %H:%M:%S %z} - {1}'.format(hg_info[0], hg_info[1], hg_time))
count_mismatch += 1
print('Matched {0} commits, mismatched {1} commits.'.format(count_match, count_mismatch), file=sys.stderr)
# Just to make it sure...
count_match, count_mismatch = 0, 0
for git_time, git_info in git_log.items():
if git_time in hg_log:
count_match += 1
else:
count_mismatch += 1
print('In the reverse: Matched {0} commits, mismatched {1} commits.'.format(count_match, count_mismatch), file=sys.stderr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment