Skip to content

Commit f1120f6

Browse files
committed
feat: merge command & tests added & readme file updated
1 parent 6620804 commit f1120f6

File tree

6 files changed

+2672
-0
lines changed

6 files changed

+2672
-0
lines changed

clony/cli.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from clony.internals.checkout import restore_files, switch_branch_or_commit
3131
from clony.internals.commit import make_commit
3232
from clony.internals.log import display_commit_logs, parse_commit_object
33+
from clony.internals.merge import perform_merge
3334
from clony.internals.reset import reset_head, validate_commit_reference
3435
from clony.internals.staging import stage_file
3536
from clony.internals.status import get_status
@@ -643,3 +644,53 @@ def checkout(target: str, paths: tuple, force: bool):
643644
if not switch_branch_or_commit(target, force):
644645
logger.error("Checkout failed.")
645646
sys.exit(1)
647+
648+
649+
# Merge command to perform a three-way merge with the current branch
650+
@cli.command()
651+
@click.argument("base", required=True)
652+
@click.argument("other", required=True)
653+
def merge(base: str, other: str):
654+
"""Perform a three-way merge with the current branch.
655+
656+
Merge changes from BRANCH or COMMIT into the current branch, with BASE
657+
as the common ancestor. Conflicts will be displayed in a tabular format
658+
for manual resolution.
659+
"""
660+
661+
try:
662+
# Validate the commits
663+
if not validate_commit_reference(base):
664+
# Log the error and exit
665+
logger.error(f"Invalid base commit: {base}")
666+
sys.exit(1)
667+
668+
if not validate_commit_reference(other):
669+
# Log the error and exit
670+
logger.error(f"Invalid other commit: {other}")
671+
sys.exit(1)
672+
673+
# Perform the merge
674+
conflicts = perform_merge(base, other)
675+
676+
# Exit with a status code indicating if there were conflicts
677+
if conflicts > 0:
678+
# Log the warning
679+
logger.warning(
680+
f"Merge completed with {conflicts} conflict(s). "
681+
f"Manual resolution required."
682+
)
683+
684+
# Exit with a status code of 1
685+
sys.exit(1)
686+
else:
687+
# Log the info
688+
logger.info("Merge completed successfully with no conflicts.")
689+
690+
# Exit with a status code of 0
691+
sys.exit(0)
692+
693+
except Exception as e:
694+
# Log the error and exit
695+
logger.error(f"Error performing merge: {str(e)}")
696+
sys.exit(1)

clony/internals/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from clony.internals.checkout import restore_files, switch_branch_or_commit
99
from clony.internals.commit import make_commit
1010
from clony.internals.log import display_commit_logs, get_commit_logs
11+
from clony.internals.merge import perform_merge
1112
from clony.internals.reset import reset_head
1213
from clony.internals.staging import stage_file
1314
from clony.internals.status import FileStatus, get_status
@@ -23,4 +24,5 @@
2324
"display_commit_logs",
2425
"switch_branch_or_commit",
2526
"restore_files",
27+
"perform_merge",
2628
]

0 commit comments

Comments
 (0)