-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New scripts/sync.py and merge.py for repetitive git command with dev …
…branch
- Loading branch information
1 parent
02650dd
commit 7b446b7
Showing
5 changed files
with
151 additions
and
10 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Merge dev into main branch | ||
|
||
import subprocess | ||
import sys | ||
import os | ||
|
||
import sync | ||
|
||
def run_command(command): | ||
"""Run a shell command and return the output.""" | ||
result = subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) | ||
return result.stdout.strip() | ||
|
||
def main(): | ||
try: | ||
# Switch to dev branch if not already on it | ||
current_branch = run_command(['git', 'rev-parse', '--abbrev-ref', 'HEAD']) | ||
if current_branch != "dev": | ||
print("Switching to dev branch") | ||
run_command(['git', 'checkout', 'dev']) | ||
|
||
# Make sure the dev branch does not have uncommitted changes. | ||
try: | ||
run_command(['git', 'diff-index', '--quiet', 'HEAD', '--']) | ||
except subprocess.CalledProcessError: | ||
print("Uncommitted changes in the dev branch. Please commit or stash them before merging.") | ||
sys.exit(1) | ||
|
||
# Make sure the dev branch is up-to-date with the remote. | ||
run_command(['git', 'fetch', 'origin', 'dev']) | ||
if run_command(['git', 'rev-parse', 'dev']) != run_command(['git', 'rev-parse', 'origin/dev']): | ||
print("dev branch not up-to-date with remote. Do 'git push'.") | ||
sys.exit(1) | ||
|
||
# Call sync to verify that dev is up-to-date with main. | ||
# This is to avoid conflicts when merging dev into main. | ||
sync.main() | ||
|
||
# Switch to main branch | ||
print("Switching to main branch") | ||
run_command(['git', 'checkout', 'main']) | ||
run_command(['git', 'fetch', 'origin', 'main']) | ||
|
||
# Proceed to merge dev into main. Detect if there are conflicts, if yes | ||
# give instruction to resolve them. | ||
|
||
# Find the common ancestor of dev and main | ||
merge_base = run_command(['git', 'merge-base', 'dev', 'main']) | ||
|
||
# Check if there are any changes from dev that are not in main | ||
try: | ||
run_command(['git', 'diff', '--quiet', merge_base, 'dev']) | ||
print("No changes to merge from dev to main.") | ||
except subprocess.CalledProcessError: | ||
# Perform the actual merge | ||
try: | ||
run_command(['git', 'merge', '--ff-only', 'dev']) | ||
print("Merged dev into main.") | ||
|
||
# Rebase dev to keep on same last commit (that merge that was just done). | ||
run_command(['git', 'checkout', 'dev']) | ||
run_command(['git', 'rebase', 'main']) | ||
run_command(['git', 'push', 'origin', 'dev']) | ||
run_command(['git', 'push', 'origin', 'main']) | ||
except subprocess.CalledProcessError: | ||
print("Merge failed due to conflicts.") | ||
print("To resolve the conflicts, follow these steps:") | ||
print("1. Identify conflicted files using 'git status'.") | ||
print("2. Resolve manually by editing conflicted files.") | ||
print("3. Mark conflicts as resolved using 'git add <file>'.") | ||
print("4. Complete merge with 'git commit' and 'push'.") | ||
sys.exit(1) | ||
|
||
except subprocess.CalledProcessError as e: | ||
print(f"An error occurred: {e}") | ||
sys.exit(1) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Make local dev branch "catch up" with the remote main branch. | ||
# | ||
# Often needed to be done prior to push "dev". | ||
# | ||
# NOOP if nothing to merge | ||
|
||
import subprocess | ||
import sys | ||
|
||
def run_command(command): | ||
"""Run a shell command and return the output.""" | ||
result = subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) | ||
return result.stdout.strip() | ||
|
||
def main(): | ||
try: | ||
# Fetch the latest changes from the origin | ||
run_command(['git', 'fetch', 'origin']) | ||
run_command(['git', 'pull', 'origin', 'main']) | ||
|
||
# Checkout the dev branch | ||
run_command(['git', 'checkout', 'dev']) | ||
|
||
# Find the common ancestor of dev and main | ||
merge_base = run_command(['git', 'merge-base', 'dev', 'main']) | ||
|
||
# Check if there are any changes from main that are not in dev | ||
diff_output = subprocess.run(['git', 'diff', '--quiet', merge_base, 'main'], stderr=subprocess.DEVNULL) | ||
if diff_output.returncode == 0: | ||
print("No changes to merge from main to dev.") | ||
else: | ||
# Perform the actual merge | ||
merge_output = subprocess.run(['git', 'merge', '--no-commit', '--no-ff', 'main'], stderr=subprocess.DEVNULL) | ||
if merge_output.returncode == 0: | ||
# Check if there are any changes to commit | ||
diff_index_output = subprocess.run(['git', 'diff-index', '--quiet', 'HEAD', '--'], stderr=subprocess.DEVNULL) | ||
if diff_index_output.returncode == 0: | ||
print("No changes to merge from main to dev.") | ||
run_command(['git', 'merge', '--abort']) | ||
else: | ||
# Commit the merge if there are changes | ||
run_command(['git', 'commit', '-m', 'Merged main into dev']) | ||
print("Merged main into dev.") | ||
else: | ||
print("Merge failed due to conflicts. Next steps:") | ||
print("1. Identify conflicts with 'git status'.") | ||
print("2. Resolve manually by editing the conflicted files.") | ||
print("3. Mark as resolved using 'git add <file>'.") | ||
print("4. Complete merge with 'git commit'.") | ||
sys.exit(1) | ||
except subprocess.CalledProcessError as e: | ||
print(f"An error occurred: {e}") | ||
sys.exit(1) | ||
|
||
if __name__ == "__main__": | ||
main() |