Refactor merge

This commit is contained in:
Benjamin Bohard 2020-09-10 15:26:49 +02:00
parent 619b74c815
commit 7e0d5c15e5
1 changed files with 64 additions and 38 deletions

View File

@ -57,6 +57,53 @@ def normalize_branch(args):
def git_integration(func):
def commit_into_master(paths, repo, comment):
author = repo.default_signature
committer = author
repo.checkout('refs/heads/master')
index = repo.index
for fp in paths:
index.add(fp)
index.write()
tree = index.write_tree()
master_ref = repo.references['refs/heads/master']
parents = [master_ref.peel().hex]
master_head = repo.create_commit('refs/heads/master',
author,
committer,
comment,
tree,
parents)
return master_head
def commit_into_branch(paths, branch, repo, comment):
author = repo.default_signature
committer = author
repo.checkout(f'refs/heads/{branch}')
index = repo.index
for fp in paths:
index.add(fp)
index.write()
tree = index.write_tree()
branch_ref = repo.references[f'refs/heads/{branch}']
parents = [branch_ref.peel().hex]
branch_commit = repo.create_commit(f'refs/heads/{branch}',
author,
committer,
comment,
tree,
parents)
return branch_commit
def merge_master_into_branch(master_commit, branch, repo):
author = repo.default_signature
committer = author
repo.checkout(f'refs/heads/{branch}')
repo.merge(master_commit)
tree = repo.index.write_tree()
merge_commit = repo.create_commit('HEAD', author, committer, 'Merge master into xelatex/*', tree, [repo.head.target, master_commit])
def inner(args):
try:
repo = pygit2.Repository('./')
@ -78,8 +125,6 @@ def git_integration(func):
master_add_paths = [fp for fp in repo_status
if (fp.startswith('content/') or fp.startswith('slides/')) and repo_status[fp] in to_add_status]
author = repo.default_signature
committer = author
if func.__name__ == 'init':
comment = 'Initialisation de la formation'
@ -90,43 +135,24 @@ def git_integration(func):
else:
comment = 'Travail sur la formation'
if repo.branches['master'].is_checked_out():
if master_add_paths:
repo.checkout('refs/heads/master')
index = repo.index
for fp in master_add_paths:
index.add(fp)
index.write()
tree = index.write_tree()
master_ref = repo.references['refs/heads/master']
parents = [master_ref.peel().hex]
master_head = repo.create_commit('refs/heads/master',
author,
committer,
comment,
tree,
parents)
master_head = commit_into_master(master_add_paths, repo, comment)
else:
master_head = repo.revparse_single('refs/heads/master')
if branch_add_paths:
repo.checkout(f'refs/heads/{branch_name}')
index = repo.index
for fp in branch_add_paths:
index.add(fp)
index.write()
tree = index.write_tree()
branch_ref = repo.references[f'refs/heads/{branch_name}']
parents = [branch_ref.peel().hex]
repo.create_commit(f'refs/heads/{branch_name}',
author,
committer,
comment,
tree,
parents)
commit_into_branch(branch_add_paths, branch_name, repo, comment)
elif repo.branches[branch_name].is_checked_out():
if branch_add_paths:
commit_into_branch(branch_add_paths, branch_name, repo, comment)
if master_add_paths:
master_head = commit_into_master(master_add_paths, repo, comment)
else:
master_head = repo.revparse_single('refs/heads/master')
if master_head not in repo.walk(branch_ref.target):
repo.merge(master_head)
tree = repo.index.write_tree()
merge_commit = repo.create_commit('HEAD', author, committer, 'Merge master into xelatex/*', tree, [repo.head.target, master_head])
merge_master_into_branch(master_head, branch_name, repo)
return inner