Code

Fix branch setup after initial clone.
[git.git] / contrib / fast-import / git-p4
index cb9961a571eb8dacb43f085a803d942c83f1e245..2f3615bd7274f22c1132ec96670ba23f519f2d27 100755 (executable)
@@ -7,8 +7,6 @@
 #            2007 Trolltech ASA
 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
 #
-# TODO: Add an option to sync/rebase to fetch and rebase from origin first.
-#
 
 import optparse, sys, os, marshal, popen2, subprocess, shelve
 import tempfile, getopt, sha, os.path, time, platform
@@ -419,7 +417,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 +440,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()
@@ -828,17 +828,29 @@ class P4Sync(Command):
         self.changeRange = ""
         self.initialParent = ""
         self.previousDepotPath = ""
-        # 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"):
                 system("git update-ref %s refs/heads/p4" % self.branch)
-                system("git symbolic-ref refs/remotes/p4/HEAD refs/remotes/p4/master")
                 system("git branch -D p4");
-            else:
-                defaultImport = True
+            if not gitBranchExists("refs/remotes/p4/HEAD"):
+                system("git symbolic-ref refs/remotes/p4/HEAD %s" % self.branch)
 
         if len(args) == 0:
             if not gitBranchExists(self.branch) and gitBranchExists("origin"):
@@ -848,8 +860,6 @@ class P4Sync(Command):
                 if not branch.startswith("refs"):
                     branch = "refs/heads/" + branch
                 system("git update-ref %s origin" % branch)
-                if defaultImport:
-                    system("git symbolic-ref refs/remotes/p4/HEAD %s" % branch)
 
             [self.previousDepotPath, p4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit(self.branch))
             if len(self.previousDepotPath) > 0 and len(p4Change) > 0:
@@ -1037,11 +1047,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]