{"thread":{"id":"9634","subject":"[PATCH] git-p4: Make 'git-p4 branches' work after an initial clone with git clone from an origin-updated repository.","startedAt":"2007-08-24T15:44:16Z","lastAt":"2007-08-24T15:44:16Z","messageCount":1,"participants":["Simon Hausmann"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"51448","messageId":"200708241744.19063.simon@lst.de","threadId":"9634","inReplyTo":null,"subject":"[PATCH] git-p4: Make 'git-p4 branches' work after an initial clone with git clone from an origin-updated repository.","fromName":"Simon Hausmann","fromEmail":"simon@lst.de","sentAt":"2007-08-24T15:44:16Z","receivedAt":"2007-08-24T15:44:16Z","isPatch":true,"sender":{"key":"hausmann@kde.org","avatar":"https://gravatar.com/avatar/bc9aad4fb31dce17eb66e690e7b51fe980c62da3c225c785da35dd806b8da778?d=mp&s=160"},"body":"After a clone with \"git clone\" of a repository the p4 branches are only in remotes/origin/p4/* and not in remotes/p4/*.\nSeparate the code for detection and creation out of the P4Sync command class into standalone methods and use them\nfrom the P4Branches command.\n\nSigned-off-by: Simon Hausmann <simon@lst.de>\n---\n contrib/fast-import/git-p4 |  104 +++++++++++++++++++++++---------------------\n 1 files changed, 55 insertions(+), 49 deletions(-)\n\ndiff --git a/contrib/fast-import/git-p4 b/contrib/fast-import/git-p4\nindex 6d01062..b571e30 100755\n--- a/contrib/fast-import/git-p4\n+++ b/contrib/fast-import/git-p4\n@@ -231,6 +231,56 @@ def findUpstreamBranchPoint(head = \"HEAD\"):\n \n     return [\"\", settings]\n \n+def createOrUpdateBranchesFromOrigin(localRefPrefix = \"refs/remotes/p4/\", silent=True):\n+    if not silent:\n+        print (\"Creating/updating branch(es) in %s based on origin branch(es)\"\n+               % localRefPrefix)\n+\n+    originPrefix = \"origin/p4/\"\n+\n+    for line in read_pipe_lines(\"git rev-parse --symbolic --remotes\"):\n+        line = line.strip()\n+        if (not line.startswith(originPrefix)) or line.endswith(\"HEAD\"):\n+            continue\n+\n+        headName = line[len(originPrefix):]\n+        remoteHead = localRefPrefix + headName\n+        originHead = line\n+\n+        original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))\n+        if (not original.has_key('depot-paths')\n+            or not original.has_key('change')):\n+            continue\n+\n+        update = False\n+        if not gitBranchExists(remoteHead):\n+            if verbose:\n+                print \"creating %s\" % remoteHead\n+            update = True\n+        else:\n+            settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))\n+            if settings.has_key('change') > 0:\n+                if settings['depot-paths'] == original['depot-paths']:\n+                    originP4Change = int(original['change'])\n+                    p4Change = int(settings['change'])\n+                    if originP4Change > p4Change:\n+                        print (\"%s (%s) is newer than %s (%s). \"\n+                               \"Updating p4 branch from origin.\"\n+                               % (originHead, originP4Change,\n+                                  remoteHead, p4Change))\n+                        update = True\n+                else:\n+                    print (\"Ignoring: %s was imported from %s while \"\n+                           \"%s was imported from %s\"\n+                           % (originHead, ','.join(original['depot-paths']),\n+                              remoteHead, ','.join(settings['depot-paths'])))\n+\n+        if update:\n+            system(\"git update-ref %s %s\" % (remoteHead, originHead))\n+\n+def originP4BranchesExist():\n+        return gitBranchExists(\"origin\") or gitBranchExists(\"origin/p4\") or gitBranchExists(\"origin/p4/master\")\n+\n class Command:\n     def __init__(self):\n         self.usage = \"usage: %prog [options]\"\n@@ -1041,53 +1091,6 @@ class P4Sync(Command):\n         for branch in branches.keys():\n             self.initialParents[self.refPrefix + branch] = branches[branch]\n \n-    def createOrUpdateBranchesFromOrigin(self):\n-        if not self.silent:\n-            print (\"Creating/updating branch(es) in %s based on origin branch(es)\"\n-                   % self.refPrefix)\n-\n-        originPrefix = \"origin/p4/\"\n-\n-        for line in read_pipe_lines(\"git rev-parse --symbolic --remotes\"):\n-            line = line.strip()\n-            if (not line.startswith(originPrefix)) or line.endswith(\"HEAD\"):\n-                continue\n-\n-            headName = line[len(originPrefix):]\n-            remoteHead = self.refPrefix + headName\n-            originHead = line\n-\n-            original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))\n-            if (not original.has_key('depot-paths')\n-                or not original.has_key('change')):\n-                continue\n-\n-            update = False\n-            if not gitBranchExists(remoteHead):\n-                if self.verbose:\n-                    print \"creating %s\" % remoteHead\n-                update = True\n-            else:\n-                settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))\n-                if settings.has_key('change') > 0:\n-                    if settings['depot-paths'] == original['depot-paths']:\n-                        originP4Change = int(original['change'])\n-                        p4Change = int(settings['change'])\n-                        if originP4Change > p4Change:\n-                            print (\"%s (%s) is newer than %s (%s). \"\n-                                   \"Updating p4 branch from origin.\"\n-                                   % (originHead, originP4Change,\n-                                      remoteHead, p4Change))\n-                            update = True\n-                    else:\n-                        print (\"Ignoring: %s was imported from %s while \"\n-                               \"%s was imported from %s\"\n-                               % (originHead, ','.join(original['depot-paths']),\n-                                  remoteHead, ','.join(settings['depot-paths'])))\n-\n-            if update:\n-                system(\"git update-ref %s %s\" % (remoteHead, originHead))\n-\n     def updateOptionDict(self, d):\n         option_keys = {}\n         if self.keepRepoPath:\n@@ -1108,7 +1111,7 @@ class P4Sync(Command):\n         # map from branch depot path to parent branch\n         self.knownBranches = {}\n         self.initialParents = {}\n-        self.hasOrigin = gitBranchExists(\"origin\") or gitBranchExists(\"origin/p4\") or gitBranchExists(\"origin/p4/master\")\n+        self.hasOrigin = originP4BranchesExist()\n         if not self.syncWithOrigin:\n             self.hasOrigin = False\n \n@@ -1135,7 +1138,7 @@ class P4Sync(Command):\n         # merge with previous imports, if possible.\n         if args == []:\n             if self.hasOrigin:\n-                self.createOrUpdateBranchesFromOrigin()\n+                createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)\n             self.listExistingP4GitBranches()\n \n             if len(self.p4BranchesInGit) > 1:\n@@ -1518,6 +1521,9 @@ class P4Branches(Command):\n         self.verbose = False\n \n     def run(self, args):\n+        if originP4BranchesExist():\n+            createOrUpdateBranchesFromOrigin()\n+\n         cmdline = \"git rev-parse --symbolic \"\n         cmdline += \" --remotes\"\n \n-- \n1.5.3.rc6.1.ge31f\n"}]}