BD-js/sync.py

39 lines
1.3 KiB
Python

import subprocess
def run_command(command):
result = subprocess.run(command, shell=True, text=True, capture_output=True)
return result.stdout.strip()
def get_first_alphabetical_diff():
# Get the list of modified files
modified_files = run_command("git diff --name-only").splitlines()
# Return the first file in alphabetical order if there are any modified files, otherwise return None
return sorted(modified_files)[0] if modified_files else None
def has_remote_changes():
subprocess.run("git fetch", shell=True) # Ensure we have the latest info
status = run_command("git status")
return "Your branch is behind" in status
def has_local_changes():
# Instead of checking for the presence of changes, return the first file that has been changed
return get_first_alphabetical_diff()
def main():
first_diff = has_local_changes()
if has_remote_changes():
print("Pulling changes...")
run_command("git pull")
elif first_diff:
print("Adding and committing local changes...")
run_command("git add .")
# Use the first differing file in the commit message
commit_message = f'Updated ({first_diff})'
run_command(f'git commit -S -m "{commit_message}"')
run_command("git push")
else:
print("No changes to update.")
if __name__ == "__main__":
main()