From 274d5961052d8f38bffb1041f4f467b1be2317a4 Mon Sep 17 00:00:00 2001 From: Ian Homer Date: Sat, 23 Sep 2023 19:34:09 +0100 Subject: [PATCH] Git synk iteration --- bin/git-synk | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/bin/git-synk b/bin/git-synk index 9d7d6142..030bfaa0 100755 --- a/bin/git-synk +++ b/bin/git-synk @@ -4,10 +4,13 @@ # Synchronise a local git repository with remote # +import datetime import os import re from subprocess import call, getoutput, STDOUT +fetch_window = 600 + def is_git_repo(path): return os.path.isdir(path) and ( @@ -34,10 +37,32 @@ def synk(path): f"Directory {path} is not a git repository and can not be synked" ) os.chdir(path) - print(path) - print("synching") + repository_name = os.path.basename(path) branch = get_branch_name() - print(f"Branch {branch}") + changes = int(getoutput("git status --porcelain | wc -l")) + auto_commit = getoutput("git config core.autocommit") == "true" + original_url = getoutput("git config --get remote.origin.url") + + if changes > 0: + if auto_commit: + os.system("git add -A ; git commit --quiet -m sync") + else: + print(f"∷ ◉ {repository_name} - set autocommit with git synk -a to commit local") + return + print("▻ ", end="") + + dated_file = f"{path}/.git/FETCH_HEAD" + if not os.path.exists(dated_file): + dated_file = f"{path}/.git/HEAD" + last_modified = datetime.datetime.fromtimestamp(os.path.getmtime(dated_file)) + now = datetime.datetime.now() + if last_modified + datetime.timedelta(seconds=fetch_window) < now: + print(".", end="") + print(".", end="") + else: + print("∷ ", end="") + + print("") def run():