Skip to content

Commit

Permalink
New scripts/sync.py and merge.py for repetitive git command with dev …
Browse files Browse the repository at this point in the history
…branch
  • Loading branch information
mario4tier committed Nov 17, 2024
1 parent 02650dd commit 7b446b7
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 10 deletions.
Binary file modified dist/ta-lib-0.6.0-src.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion scripts/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def verify_src_package(root_dir: str) -> bool:
# - Verify that src/tools/gen_code/gen_code exists (do not run it).
#
# If the host is not a Github Action also do:
# - Run 'sudo make install' (verify returnign zero)
# - Run 'sudo make install' (verify returning zero)

original_dir = os.getcwd()
os.chdir(root_dir)
Expand Down
81 changes: 81 additions & 0 deletions scripts/merge.py
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()
20 changes: 11 additions & 9 deletions scripts/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
import os
import subprocess
import sys
import glob

from common import verify_git_repo, get_version_string, verify_src_package

def package_linux(root_dir: str, version: str):
os.chdir(root_dir)

# Clean-up any previous packaging
for file in glob.glob(f'dist/ta-lib-*-src.tar.gz'):
os.remove(file)

os.system('rm -f ta-lib-git.tar.gz')
os.system(f'rm -f dist/ta-lib-{version}-src.tar.gz')

try:
subprocess.run(['rm', '-rf', 'ta-lib-git'], check=True, stderr=subprocess.DEVNULL)
except subprocess.CalledProcessError as e:
Expand All @@ -26,14 +30,12 @@ def package_linux(root_dir: str, version: str):
print(f"Error running 'sudo rm -rf ta-lib-git': {e}")
return

# Check if ./configure exists, if not create it.
if not os.path.isfile('./configure'):
print("'./configure' not found. Running 'autoreconf -fi'...")
try:
subprocess.run(['autoreconf', '-fi'], check=True)
except subprocess.CalledProcessError as e:
print(f"Error running 'autoreconf -fi': {e}")
return
# Always autoreconf before re-packaging
try:
subprocess.run(['autoreconf', '-fi'], check=True)
except subprocess.CalledProcessError as e:
print(f"Error running 'autoreconf -fi': {e}")
return

# Run ./configure
try:
Expand Down
58 changes: 58 additions & 0 deletions scripts/sync.py
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()

0 comments on commit 7b446b7

Please sign in to comment.