commit:     31ae597d8dbe927729df0af181a1312fbbe84130
Author:     Jauhien Piatlicki <jauhien <AT> gentoo <DOT> org>
AuthorDate: Sun Aug 16 22:03:21 2015 +0000
Commit:     Jauhien Piatlicki <jauhien <AT> gentoo <DOT> org>
CommitDate: Sun Aug 16 22:29:47 2015 +0000
URL:        https://gitweb.gentoo.org/proj/g-sorcery.git/commit/?id=31ae597d

[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

Reply via email to