From 31ae597d8dbe927729df0af181a1312fbbe84130 Mon Sep 17 00:00:00 2001 From: Jauhien Piatlicki Date: Mon, 17 Aug 2015 00:03:21 +0200 Subject: [g_sorcery/git_syncer] detect changes of branch and remote URL --- g_sorcery/git_syncer/git_syncer.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/g_sorcery/git_syncer/git_syncer.py b/g_sorcery/git_syncer/git_syncer.py index 0f6c58e..abde034 100644 --- a/g_sorcery/git_syncer/git_syncer.py +++ b/g_sorcery/git_syncer/git_syncer.py @@ -12,6 +12,8 @@ """ import os +import shutil +import subprocess from g_sorcery.compatibility import TemporaryDirectory @@ -46,8 +48,11 @@ class GITSyncer(Syncer): branch = "master" if os.path.exists(path): - #TODO: allow changing of remotes/branches - self.pull(path) + if self.branch_not_changed(path, branch) and self.remote_url_not_changed(path, db_uri): + self.pull(path) + else: + shutil.rmtree(path) + self.clone(db_uri, branch, path) else: self.clone(db_uri, branch, path) @@ -65,3 +70,19 @@ class GITSyncer(Syncer): def pull(self, path): if os.system("cd " + path + " && git pull"): raise SyncError("sync failed (pulling): " + path) + + + def branch_not_changed(self, path, branch): + try: + result = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=path).rstrip().decode("utf-8") + except Exception: + return False + return result == branch + + + def remote_url_not_changed(self, path, url): + try: + result = subprocess.check_output(["git", "config", "--get", "remote.origin.url"], cwd=path).rstrip().decode("utf-8") + except Exception: + return False + return result == url -- cgit v1.2.3-65-gdbad