Make --with-origin the default for syncing.
[gitweb.git] / contrib / fast-import / git-p4
index 0597daa8491f3c4604448fded7f75bf7db95d203..ed5a5c593ccd3f85ae3698f5891160765f1f1b4d 100755 (executable)
@@ -118,6 +118,9 @@ def gitBranchExists(branch):
     proc = subprocess.Popen(["git", "rev-parse", branch], stderr=subprocess.PIPE, stdout=subprocess.PIPE);
     return proc.wait() == 0;
 
+def gitConfig(key):
+    return mypopen("git config %s" % key).read()[:-1]
+
 class Command:
     def __init__(self):
         self.usage = "usage: %prog [options]"
@@ -412,7 +415,7 @@ class P4Submit(Command):
 
         if len(args) == 0:
             self.master = currentGitBranch()
-            if len(self.master) == 0 or not os.path.exists("%s/refs/heads/%s" % (gitdir, self.master)):
+            if len(self.master) == 0 or not gitBranchExists("refs/heads/%s" % self.master):
                 die("Detecting current git branch failed!")
         elif len(args) == 1:
             self.master = args[0]
@@ -514,7 +517,6 @@ class P4Sync(Command):
                 optparse.make_option("--changesfile", dest="changesFile"),
                 optparse.make_option("--silent", dest="silent", action="store_true"),
                 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
-                optparse.make_option("--with-origin", dest="syncWithOrigin", action="store_true"),
                 optparse.make_option("--verbose", dest="verbose", action="store_true"),
                 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false"),
                 optparse.make_option("--max-changes", dest="maxChanges")
@@ -536,12 +538,15 @@ class P4Sync(Command):
         self.detectBranches = False
         self.detectLabels = False
         self.changesFile = ""
-        self.syncWithOrigin = False
+        self.syncWithOrigin = True
         self.verbose = False
         self.importIntoRemotes = True
         self.maxChanges = ""
         self.isWindows = (platform.system() == "Windows")
 
+        if gitConfig("git-p4.syncFromOrigin") == "false":
+            self.syncWithOrigin = False
+
     def p4File(self, depotPath):
         return os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
 
@@ -802,19 +807,23 @@ class P4Sync(Command):
         for line in mypopen("git rev-parse --symbolic --remotes"):
             if (not line.startswith("origin/")) or line.endswith("HEAD\n"):
                 continue
+
             headName = line[len("origin/"):-1]
             remoteHead = self.refPrefix + headName
             originHead = "origin/" + headName
 
+            [originPreviousDepotPath, originP4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit(originHead))
+            if len(originPreviousDepotPath) == 0 or len(originP4Change) == 0:
+                continue
+
             update = False
-            if not os.path.exists(gitdir + "/" + remoteHead):
+            if not gitBranchExists(remoteHead):
                 if self.verbose:
                     print "creating %s" % remoteHead
                 update = True
             else:
-                [originPreviousDepotPath, originP4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit(originHead))
                 [p4PreviousDepotPath, p4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit(remoteHead))
-                if len(originPreviousDepotPath) > 0 and len(originP4Change) > 0 and len(p4Change) > 0:
+                if len(p4Change) > 0:
                     if originPreviousDepotPath == p4PreviousDepotPath:
                         originP4Change = int(originP4Change)
                         p4Change = int(p4Change)
@@ -841,6 +850,12 @@ class P4Sync(Command):
         else:
             self.refPrefix = "refs/heads/"
 
+        if self.syncWithOrigin:
+            if gitBranchExists("origin"):
+                if not self.silent:
+                    print "Syncing with origin first by calling git fetch origin"
+                system("git fetch origin")
+
         createP4HeadRef = False;
 
         if len(self.branch) == 0:
@@ -1107,13 +1122,11 @@ class P4Sync(Command):
 class P4Rebase(Command):
     def __init__(self):
         Command.__init__(self)
-        self.options = [ optparse.make_option("--with-origin", dest="syncWithOrigin", action="store_true") ]
+        self.options = [ ]
         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]