|
30 | 30 | from clony.internals.checkout import restore_files, switch_branch_or_commit |
31 | 31 | from clony.internals.commit import make_commit |
32 | 32 | from clony.internals.log import display_commit_logs, parse_commit_object |
| 33 | +from clony.internals.merge import perform_merge |
33 | 34 | from clony.internals.reset import reset_head, validate_commit_reference |
34 | 35 | from clony.internals.staging import stage_file |
35 | 36 | from clony.internals.status import get_status |
@@ -643,3 +644,53 @@ def checkout(target: str, paths: tuple, force: bool): |
643 | 644 | if not switch_branch_or_commit(target, force): |
644 | 645 | logger.error("Checkout failed.") |
645 | 646 | 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) |
0 commit comments