Refactor merge
This commit is contained in:
parent
619b74c815
commit
7e0d5c15e5
|
@ -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 master_add_paths:
|
if repo.branches['master'].is_checked_out():
|
||||||
repo.checkout('refs/heads/master')
|
if master_add_paths:
|
||||||
index = repo.index
|
master_head = commit_into_master(master_add_paths, repo, comment)
|
||||||
for fp in master_add_paths:
|
else:
|
||||||
index.add(fp)
|
master_head = repo.revparse_single('refs/heads/master')
|
||||||
index.write()
|
if branch_add_paths:
|
||||||
tree = index.write_tree()
|
commit_into_branch(branch_add_paths, branch_name, repo, comment)
|
||||||
master_ref = repo.references['refs/heads/master']
|
elif repo.branches[branch_name].is_checked_out():
|
||||||
parents = [master_ref.peel().hex]
|
if branch_add_paths:
|
||||||
master_head = repo.create_commit('refs/heads/master',
|
commit_into_branch(branch_add_paths, branch_name, repo, comment)
|
||||||
author,
|
if master_add_paths:
|
||||||
committer,
|
master_head = commit_into_master(master_add_paths, repo, comment)
|
||||||
comment,
|
else:
|
||||||
tree,
|
master_head = repo.revparse_single('refs/heads/master')
|
||||||
parents)
|
if master_head not in repo.walk(branch_ref.target):
|
||||||
else:
|
merge_master_into_branch(master_head, branch_name, repo)
|
||||||
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)
|
|
||||||
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])
|
|
||||||
|
|
||||||
|
|
||||||
return inner
|
return inner
|
||||||
|
|
Loading…
Reference in New Issue