Code

Added support for git-p4 sync/rebase --with-origin. See git-p4.txt for details :)
authorSimon Hausmann <simon@lst.de>
Thu, 17 May 2007 20:17:49 +0000 (22:17 +0200)
committerSimon Hausmann <simon@lst.de>
Thu, 17 May 2007 20:17:49 +0000 (22:17 +0200)
Signed-off-by: Simon Hausmann <simon@lst.de>
contrib/fast-import/git-p4
contrib/fast-import/git-p4.txt

index cb9961a571eb8dacb43f085a803d942c83f1e245..e653e8cd37aa0336fb97ce4f11265f2e23b8f914 100755 (executable)
@@ -419,7 +419,8 @@ class P4Sync(Command):
                 optparse.make_option("--known-branches", dest="knownBranches"),
                 optparse.make_option("--data-cache", dest="dataCache", action="store_true"),
                 optparse.make_option("--command-cache", dest="commandCache", action="store_true"),
-                optparse.make_option("--detect-labels", dest="detectLabels", action="store_true")
+                optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
+                optparse.make_option("--with-origin", dest="syncWithOrigin", action="store_true")
         ]
         self.description = """Imports from Perforce into a git repository.\n
     example:
@@ -441,6 +442,7 @@ class P4Sync(Command):
         self.detectBranches = False
         self.detectLabels = False
         self.changesFile = ""
+        self.syncWithOrigin = False
 
     def p4File(self, depotPath):
         return os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
@@ -831,6 +833,21 @@ class P4Sync(Command):
         # importing into default remotes/p4/* layout?
         defaultImport = False
 
+        if self.syncWithOrigin and gitBranchExists("origin") and gitBranchExists("refs/remotes/p4/master"):
+            print "Syncing with origin first as requested by calling git fetch origin"
+            system("git fetch origin")
+            [originPreviousDepotPath, originP4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("origin"))
+            [p4PreviousDepotPath, p4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("p4"))
+            if len(originPreviousDepotPath) > 0 and len(originP4Change) > 0 and len(p4Change) > 0:
+                if originPreviousDepotPath == p4PreviousDepotPath:
+                    originP4Change = int(originP4Change)
+                    p4Change = int(p4Change)
+                    if originP4Change > p4Change:
+                        print "origin (%s) is newer than p4 (%s). Updating p4 branch from origin." % (originP4Change, p4Change)
+                        system("git update-ref refs/remotes/p4/master origin");
+                else:
+                    print "Cannot sync with origin. It was imported from %s while remotes/p4 was imported from %s" % (originPreviousDepotPath, p4PreviousDepotPath)
+
         if len(self.branch) == 0:
             self.branch = "refs/remotes/p4/master"
             if gitBranchExists("refs/heads/p4"):
@@ -1037,11 +1054,13 @@ class P4Sync(Command):
 class P4Rebase(Command):
     def __init__(self):
         Command.__init__(self)
-        self.options = [ ]
+        self.options = [ optparse.make_option("--with-origin", dest="syncWithOrigin", action="store_true") ]
         self.description = "Fetches the latest revision from perforce and rebases the current work (branch) against it"
+        self.syncWithOrigin = False
 
     def run(self, args):
         sync = P4Sync()
+        sync.syncWithOrigin = self.syncWithOrigin
         sync.run([])
         print "Rebasing the current branch"
         oldHead = mypopen("git rev-parse HEAD").read()[:-1]
index 32aeb0ac0ba7be3fd88b3c23649fea40e687ab98..ac8e6cff0bff9b34231037ea54c9e8fe696f0bd6 100644 (file)
@@ -63,6 +63,26 @@ It is recommended to run 'git repack -a -d -f' from time to time when using
 incremental imports to optimally combine the individual git packs that each
 incremental import creates through the use of git-fast-import.
 
+
+A useful setup may be that you have a periodically updated git repository
+somewhere that contains a complete import of a Perforce project. That git
+repository can be used to clone the working repository from and one would
+import from Perforce directly after cloning using git-p4. If the connection to
+the Perforce server is slow and the working repository hasn't been synced for a
+while it may be desirable to fetch changes from the origin git repository using
+the efficient git protocol. git-p4 supports this through
+
+  git-p4 sync --with-origin
+
+or
+
+  git-p4 rebase --with-origin
+
+In that case "git fetch origin" is called and if it turns out that the origin
+branch is newer than the git "p4" import branch then the latter is updated from
+the former and the direct import from Perforce is resumed, which will result in
+fewer changes to be imported using the slower perforce connection.
+
 Updating
 ========