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