[Treepkg-commits] r520 - trunk/treepkg

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Sep 2 10:45:28 CEST 2011


Author: bricks
Date: 2011-09-02 10:45:28 +0200 (Fri, 02 Sep 2011)
New Revision: 520

Modified:
   trunk/treepkg/git.py
Log:
Refactor git branching

If a checkout is already available and the branch is changed in
the config git command would always fail because it doesn't know
the branch to track. Therefore always check if the branch is 
locally available and if not checkout the remote branch


Modified: trunk/treepkg/git.py
===================================================================
--- trunk/treepkg/git.py	2011-09-02 08:43:23 UTC (rev 519)
+++ trunk/treepkg/git.py	2011-09-02 08:45:28 UTC (rev 520)
@@ -34,15 +34,27 @@
                     defaults to master
         """
         self.url = url
+        self.local_branch = "local"
         self.branch = branch
+        if ":" in branch:
+            branches = branch.split(":")
+            self.local_branch = branches[0]
+            self.branch = branches[1]
+        # use master as default
+        if not branch:
+            self.branch = "master"
 
     def checkout(self, localdir):
         """Clones the repository at url into the localdir"""
         run.call(cmdexpand("git clone -q $url $localdir", **locals()))
         if branch:
-            run.call(cmdexpand("git checkout -q --track -b local $branch",
-                            branch=self.branch), cwd=localdir)
+            self.checkout_branch(localdir)
 
+    def checkout_branch(self, localdir):
+        run.call(cmdexpand("git checkout -q --track -b $local $branch",
+            branch=self.branch, local=self.local_branch), cwd=localdir)
+
+
     def export(self, localdir, destdir):
         """Exports the working copy in localdir to destdir"""
         dest = destdir + os.sep
@@ -53,11 +65,25 @@
         """Copies the working copy to destdir (including .git dir)"""
         shutils.copytree(localdir, destdir)
 
-    def update(self, localdir, branch=None):
+    def update(self, localdir):
         """Runs git pull on the localdir."""
         run.call(cmdexpand("git pull -q"), cwd=localdir)
-        if branch:
-            run.call(cmdexpand("git checkout -q $branch", branch=branch),
+        output = capture_output(cmdexpand("git branch"), cwd=localdir)
+        branches = output.splitlines()
+        cur_branch = None
+        all_branches = []
+        for tbranch in branches:
+            tbranch = tbranch.strip()
+            if tbranch.startswith("*"):
+                cur_branch = tbranch[2:]
+                tbranch = curbranch
+            all_branches.append(tbranch)
+        if not self.local_branch in all_branches:
+            self.checkout_branch(localdir)
+        # TODO: check if self.local_branch is curbranch
+        # doesn't hurt if a checkout is done on the current branch
+        if self.branch:
+            run.call(cmdexpand("git checkout -q $branch", branch=self.local_branch),
                     cwd=localdir)
 
 
@@ -101,14 +127,12 @@
         """
         gitdir = os.path.join(self.localdir, ".git")
         branch = self.repository.branch
-        if not branch:
-            branch = "master"
         if os.path.exists(gitdir):
             self.log_info("Updating the working copy in %r for repo " \
                     "%s and branch %s", self.localdir,
                     self.repository.url,
                     branch)
-            self.repository.update(self.localdir, self.repository.branch)
+            self.repository.update(self.localdir)
         else:
             # TODO: better check if localdir contains files
             if os.path.exists(self.localdir):



More information about the Treepkg-commits mailing list