Updated (sync.py)
This commit is contained in:
parent
97542fc5d8
commit
54eae258a2
29
sync.py
29
sync.py
|
@ -4,19 +4,11 @@ def run_command(command):
|
|||
result = subprocess.run(command, shell=True, text=True, capture_output=True)
|
||||
return result.stdout.strip()
|
||||
|
||||
def get_largest_diff_file():
|
||||
diff_stats = run_command("git diff --numstat").splitlines()
|
||||
largest_diff = 0
|
||||
largest_file = ""
|
||||
for line in diff_stats:
|
||||
parts = line.split()
|
||||
if len(parts) >= 3:
|
||||
added, deleted, filename = parts[0], parts[1], parts[2]
|
||||
total_changes = int(added) + int(deleted)
|
||||
if total_changes > largest_diff:
|
||||
largest_diff = total_changes
|
||||
largest_file = filename
|
||||
return largest_file
|
||||
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
|
||||
|
@ -24,18 +16,19 @@ def has_remote_changes():
|
|||
return "Your branch is behind" in status
|
||||
|
||||
def has_local_changes():
|
||||
status = run_command("git status")
|
||||
return "nothing to commit" not in status
|
||||
# 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 has_local_changes():
|
||||
elif first_diff:
|
||||
print("Adding and committing local changes...")
|
||||
run_command("git add .")
|
||||
largest_diff_file = get_largest_diff_file()
|
||||
commit_message = f'Updated {largest_diff_file}' if largest_diff_file else "commit"
|
||||
# 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:
|
||||
|
|
Loading…
Reference in New Issue