frej / fast-export

A mercurial to git converter using git-fast-import
http://repo.or.cz/w/fast-export.git
808 stars 255 forks source link

added time and timezone into commit_data for commit_message_filter plugin #332

Closed qMBQx8GH closed 3 months ago

qMBQx8GH commented 3 months ago

Hi!

This is needed to create a plugin which uniqualizes git commit hashes when there are grafts in numerous mercurial branches exist.

Here is a demo which makes two git commits out of three in mecrial:

#!/bin/bash
# mercurial actions
rm -rf test.hg
mkdir test.hg
cd test.hg
hg init
echo line1 > file1
hg add
hg com -m 'initial commit'
hg branch branch1
echo line2 >> file1
hg com -m 'line2 added'
hg up default
hg branch branch2
hg graft -r 1
hg up default
hg log

cd ..
# git init
rm -rf test.git
mkdir test.git
cd test.git
git init

# migration
../fast-export/hg-fast-export.sh -r ../test.hg # --plugin unique_hash=desc

# check results
git log --all
cat .git/hg2git-marks
while read line; do echo && git log `echo $line|cut -d ' ' -f 2`; done < .git/hg2git-marks

The plugin code:

import hashlib

def build_filter(args):
    return UniqueFilter(args)

class UniqueFilter:
    def __init__(self, args):
        self.commits = {}
        self.modify = args

    def commit_message_filter(self, commit_data):
        key = self.make_key(commit_data)
        while key in self.commits:
            self.make_unique(commit_data)
            key = self.make_key(commit_data)
        self.commits[key] = True

    def make_key(self, commit_data):
        return hashlib.sha1(b'%s|%s|%d|%s' % (commit_data['author'], commit_data['desc'], commit_data['time'], commit_data['timezone'])).hexdigest()

    def make_unique(self, commit_data):
        if self.modify == 'desc':
            commit_data['desc'] += b' '
        else:
            commit_data['time'] += 1

Regards, Maxim

frej commented 3 months ago

As long as Russia is occupying parts of Ukraine, I will not consider contributions from Russian companies.