Skip to content

Sourcery refactored master branch #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions contrib/fast-import/import-zips.py
Original file line number Diff line number Diff line change
@@ -35,44 +35,59 @@ def printlines(list):
commit_time = 0
next_mark = 1
common_prefix = None
mark = dict()
mark = {}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 38-75 refactored with the following changes:


zip = ZipFile(zipfile, 'r')
for name in zip.namelist():
if name.endswith('/'):
continue
info = zip.getinfo(name)

if commit_time < info.date_time:
commit_time = info.date_time
if common_prefix == None:
commit_time = max(commit_time, info.date_time)
if common_prefix is None:
common_prefix = name[:name.rfind('/') + 1]
else:
while not name.startswith(common_prefix):
last_slash = common_prefix[:-1].rfind('/') + 1
common_prefix = common_prefix[:last_slash]

mark[name] = ':' + str(next_mark)
mark[name] = f':{str(next_mark)}'
next_mark += 1

printlines(('blob', 'mark ' + mark[name], \
'data ' + str(info.file_size)))
printlines(('blob', f'mark {mark[name]}', f'data {str(info.file_size)}'))
fast_import.write(zip.read(name) + "\n")

committer = committer_name + ' <' + committer_email + '> %d +0000' % \
mktime(commit_time + (0, 0, 0))

printlines(('commit ' + branch_ref, 'committer ' + committer, \
'data <<EOM', 'Imported from ' + zipfile + '.', 'EOM', \
'', 'deleteall'))

for name in mark.keys():
fast_import.write('M 100644 ' + mark[name] + ' ' +
name[len(common_prefix):] + "\n")

printlines(('', 'tag ' + path.basename(zipfile), \
'from ' + branch_ref, 'tagger ' + committer, \
'data <<EOM', 'Package ' + zipfile, 'EOM', ''))
committer = f'{committer_name} <{committer_email}' + '> %d +0000' % mktime(
commit_time + (0, 0, 0)
)

printlines(
(
f'commit {branch_ref}',
f'committer {committer}',
'data <<EOM',
f'Imported from {zipfile}.',
'EOM',
'',
'deleteall',
)
)

for name, value in mark.items():
fast_import.write((f'M 100644 {value} {name[len(common_prefix):]}' + "\n"))

printlines(
(
'',
f'tag {path.basename(zipfile)}',
f'from {branch_ref}',
f'tagger {committer}',
'data <<EOM',
f'Package {zipfile}',
'EOM',
'',
)
)

if fast_import.close():
exit(1)
221 changes: 108 additions & 113 deletions contrib/hg-to-git/hg-to-git.py
Original file line number Diff line number Diff line change
@@ -59,22 +59,21 @@ def usage():
#------------------------------------------------------------------------------

def getgitenv(user, date):
env = ''
elems = re.compile('(.*?)\s+<(.*)>').match(user)
if elems:
env += 'export GIT_AUTHOR_NAME="%s" ;' % elems.group(1)
env += 'export GIT_COMMITTER_NAME="%s" ;' % elems.group(1)
env += 'export GIT_AUTHOR_EMAIL="%s" ;' % elems.group(2)
env += 'export GIT_COMMITTER_EMAIL="%s" ;' % elems.group(2)
else:
env += 'export GIT_AUTHOR_NAME="%s" ;' % user
env += 'export GIT_COMMITTER_NAME="%s" ;' % user
env += 'export GIT_AUTHOR_EMAIL= ;'
env += 'export GIT_COMMITTER_EMAIL= ;'

env += 'export GIT_AUTHOR_DATE="%s" ;' % date
env += 'export GIT_COMMITTER_DATE="%s" ;' % date
return env
env = ''
if elems := re.compile('(.*?)\s+<(.*)>').match(user):
env += f'export GIT_AUTHOR_NAME="{elems[1]}" ;'
env += f'export GIT_COMMITTER_NAME="{elems[1]}" ;'
env += f'export GIT_AUTHOR_EMAIL="{elems[2]}" ;'
env += f'export GIT_COMMITTER_EMAIL="{elems[2]}" ;'
else:
env += f'export GIT_AUTHOR_NAME="{user}" ;'
env += f'export GIT_COMMITTER_NAME="{user}" ;'
env += 'export GIT_AUTHOR_EMAIL= ;'
env += 'export GIT_COMMITTER_EMAIL= ;'

env += f'export GIT_AUTHOR_DATE="{date}" ;'
env += f'export GIT_COMMITTER_DATE="{date}" ;'
return env
Comment on lines -62 to +76
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function getgitenv refactored with the following changes:


#------------------------------------------------------------------------------

@@ -124,35 +123,27 @@ def getgitenv(user, date):
hgparents["0"] = (None, None)
hgbranch["0"] = "master"
for cset in range(1, int(tip) + 1):
hgchildren[str(cset)] = ()
prnts = os.popen('hg log -r %d --template "{parents}"' % cset).read().strip().split(' ')
prnts = map(lambda x: x[:x.find(':')], prnts)
if prnts[0] != '':
parent = prnts[0].strip()
else:
parent = str(cset - 1)
hgchildren[parent] += ( str(cset), )
if len(prnts) > 1:
mparent = prnts[1].strip()
hgchildren[mparent] += ( str(cset), )
else:
mparent = None

hgparents[str(cset)] = (parent, mparent)

if mparent:
hgchildren[str(cset)] = ()
prnts = os.popen('hg log -r %d --template "{parents}"' % cset).read().strip().split(' ')
prnts = map(lambda x: x[:x.find(':')], prnts)
parent = prnts[0].strip() if prnts[0] != '' else str(cset - 1)
hgchildren[parent] += ( str(cset), )
if len(prnts) > 1:
mparent = prnts[1].strip()
hgchildren[mparent] += ( str(cset), )
else:
mparent = None

hgparents[str(cset)] = (parent, mparent)

if mparent:
# For merge changesets, take either one, preferably the 'master' branch
if hgbranch[mparent] == 'master':
hgbranch[str(cset)] = 'master'
else:
hgbranch[str(cset)] = hgbranch[parent]
else:
# Normal changesets
# For first children, take the parent branch, for the others create a new branch
if hgchildren[parent][0] == str(cset):
hgbranch[str(cset)] = hgbranch[parent]
else:
hgbranch[str(cset)] = "branch-" + str(cset)
hgbranch[str(cset)] = ('master' if hgbranch[mparent] == 'master' else
hgbranch[parent])
elif hgchildren[parent][0] == str(cset):
hgbranch[str(cset)] = hgbranch[parent]
else:
hgbranch[str(cset)] = f"branch-{str(cset)}"
Comment on lines -127 to +146
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 127-242 refactored with the following changes:

This removes the following comments ( why? ):

# For first children, take the parent branch, for the others create a new branch
# Normal changesets


if "0" not in hgvers:
print('creating repository')
@@ -161,85 +152,89 @@ def getgitenv(user, date):
# loop through every hg changeset
for cset in range(int(tip) + 1):

# incremental, already seen
if str(cset) in hgvers:
continue
hgnewcsets += 1

# get info
log_data = os.popen('hg log -r %d --template "{tags}\n{date|date}\n{author}\n"' % cset).readlines()
tag = log_data[0].strip()
date = log_data[1].strip()
user = log_data[2].strip()
parent = hgparents[str(cset)][0]
mparent = hgparents[str(cset)][1]

#get comment
(fdcomment, filecomment) = tempfile.mkstemp()
csetcomment = os.popen('hg log -r %d --template "{desc}"' % cset).read().strip()
os.write(fdcomment, csetcomment)
os.close(fdcomment)

print('-----------------------------------------')
print('cset:', cset)
print('branch:', hgbranch[str(cset)])
print('user:', user)
print('date:', date)
print('comment:', csetcomment)
if parent:
print('parent:', parent)
if mparent:
print('mparent:', mparent)
if tag:
print('tag:', tag)
print('-----------------------------------------')
# incremental, already seen
if str(cset) in hgvers:
continue
hgnewcsets += 1

# get info
log_data = os.popen('hg log -r %d --template "{tags}\n{date|date}\n{author}\n"' % cset).readlines()
tag = log_data[0].strip()
date = log_data[1].strip()
user = log_data[2].strip()
parent = hgparents[str(cset)][0]
mparent = hgparents[str(cset)][1]

#get comment
(fdcomment, filecomment) = tempfile.mkstemp()
csetcomment = os.popen('hg log -r %d --template "{desc}"' % cset).read().strip()
os.write(fdcomment, csetcomment)
os.close(fdcomment)

print('-----------------------------------------')
print('cset:', cset)
print('branch:', hgbranch[str(cset)])
print('user:', user)
print('date:', date)
print('comment:', csetcomment)
if parent:
print('parent:', parent)
if mparent:
print('mparent:', mparent)
if tag:
print('tag:', tag)
print('-----------------------------------------')

# checkout the parent if necessary
if cset != 0:
if hgbranch[str(cset)] == "branch-" + str(cset):
print('creating new branch', hgbranch[str(cset)])
os.system('git checkout -b %s %s' % (hgbranch[str(cset)], hgvers[parent]))
else:
print('checking out branch', hgbranch[str(cset)])
os.system('git checkout %s' % hgbranch[str(cset)])
if cset != 0:
if hgbranch[str(cset)] == f"branch-{str(cset)}":
print('creating new branch', hgbranch[str(cset)])
os.system(f'git checkout -b {hgbranch[str(cset)]} {hgvers[parent]}')
else:
print('checking out branch', hgbranch[str(cset)])
os.system(f'git checkout {hgbranch[str(cset)]}')

# merge
if mparent:
if hgbranch[parent] == hgbranch[str(cset)]:
otherbranch = hgbranch[mparent]
else:
otherbranch = hgbranch[parent]
print('merging', otherbranch, 'into', hgbranch[str(cset)])
os.system(getgitenv(user, date) + 'git merge --no-commit -s ours "" %s %s' % (hgbranch[str(cset)], otherbranch))

# remove everything except .git and .hg directories
os.system('find . \( -path "./.hg" -o -path "./.git" \) -prune -o ! -name "." -print | xargs rm -rf')

# repopulate with checkouted files
os.system('hg update -C %d' % cset)

# add new files
os.system('git ls-files -x .hg --others | git update-index --add --stdin')
# delete removed files
os.system('git ls-files -x .hg --deleted | git update-index --remove --stdin')
if mparent:
if hgbranch[parent] == hgbranch[str(cset)]:
otherbranch = hgbranch[mparent]
else:
otherbranch = hgbranch[parent]
print('merging', otherbranch, 'into', hgbranch[str(cset)])
os.system(
f'{getgitenv(user, date)}git merge --no-commit -s ours "" {hgbranch[str(cset)]} {otherbranch}'
)

# remove everything except .git and .hg directories
os.system('find . \( -path "./.hg" -o -path "./.git" \) -prune -o ! -name "." -print | xargs rm -rf')

# repopulate with checkouted files
os.system('hg update -C %d' % cset)

# add new files
os.system('git ls-files -x .hg --others | git update-index --add --stdin')
# delete removed files
os.system('git ls-files -x .hg --deleted | git update-index --remove --stdin')

# commit
os.system(getgitenv(user, date) + 'git commit --allow-empty --allow-empty-message -a -F %s' % filecomment)
os.unlink(filecomment)
os.system(
f'{getgitenv(user, date)}git commit --allow-empty --allow-empty-message -a -F {filecomment}'
)
os.unlink(filecomment)

# tag
if tag and tag != 'tip':
os.system(getgitenv(user, date) + 'git tag %s' % tag)
if tag and tag != 'tip':
os.system(f'{getgitenv(user, date)}git tag {tag}')

# delete branch if not used anymore...
if mparent and len(hgchildren[str(cset)]):
print("Deleting unused branch:", otherbranch)
os.system('git branch -d %s' % otherbranch)

# retrieve and record the version
vvv = os.popen('git show --quiet --pretty=format:%H').read()
print('record', cset, '->', vvv)
hgvers[str(cset)] = vvv
if mparent and len(hgchildren[str(cset)]):
print("Deleting unused branch:", otherbranch)
os.system(f'git branch -d {otherbranch}')

# retrieve and record the version
vvv = os.popen('git show --quiet --pretty=format:%H').read()
print('record', cset, '->', vvv)
hgvers[str(cset)] = vvv

if hgnewcsets >= opt_nrepack and opt_nrepack != -1:
os.system('git repack -a -d')
Loading