Added support for git-p4 sync/rebase --with-origin. See git-p4.txt for details :)
Signed-off-by: Simon Hausmann <simon@lst.de>
Simon Hausmann committed
May 17, 2007 at 22:17 UTC
ef48f9093ced75ca20295f42b993bd390873573d
2 files changed
+41
-2
contrib/fast-import/git-p4
+21
-2
@@ -419,7 +419,8 @@ class P4Sync(Command):
419
optparse.make_option("--known-branches", dest="knownBranches"),
420
optparse.make_option("--data-cache", dest="dataCache", action="store_true"),
421
optparse.make_option("--command-cache", dest="commandCache", action="store_true"),
422
- optparse.make_option("--detect-labels", dest="detectLabels", action="store_true")
422
+ optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
423
+ optparse.make_option("--with-origin", dest="syncWithOrigin", action="store_true")
424
]
425
self.description = """Imports from Perforce into a git repository.\n
426
example:
@@ -441,6 +442,7 @@ class P4Sync(Command):
442
self.detectBranches = False
443
self.detectLabels = False
444
self.changesFile = ""
445
+ self.syncWithOrigin = False
446
447
def p4File(self, depotPath):
448
return os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
@@ -831,6 +833,21 @@ class P4Sync(Command):
833
# importing into default remotes/p4/* layout?
834
defaultImport = False
835
836
+ if self.syncWithOrigin and gitBranchExists("origin") and gitBranchExists("refs/remotes/p4/master"):
837
+ print "Syncing with origin first as requested by calling git fetch origin"
838
+ system("git fetch origin")
839
+ [originPreviousDepotPath, originP4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("origin"))
840
+ [p4PreviousDepotPath, p4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("p4"))
841
+ if len(originPreviousDepotPath) > 0 and len(originP4Change) > 0 and len(p4Change) > 0:
842
+ if originPreviousDepotPath == p4PreviousDepotPath:
843
+ originP4Change = int(originP4Change)
844
+ p4Change = int(p4Change)
845
+ if originP4Change > p4Change:
846
+ print "origin (%s) is newer than p4 (%s). Updating p4 branch from origin." % (originP4Change, p4Change)
847
+ system("git update-ref refs/remotes/p4/master origin");
848
+ else:
849
+ print "Cannot sync with origin. It was imported from %s while remotes/p4 was imported from %s" % (originPreviousDepotPath, p4PreviousDepotPath)
850
+
851
if len(self.branch) == 0:
852
self.branch = "refs/remotes/p4/master"
853
if gitBranchExists("refs/heads/p4"):
@@ -1037,11 +1054,13 @@ class P4Sync(Command):
1054
class P4Rebase(Command):
1055
def __init__(self):
1056
Command.__init__(self)
1040
- self.options = [ ]
1057
+ self.options = [ optparse.make_option("--with-origin", dest="syncWithOrigin", action="store_true") ]
1058
self.description = "Fetches the latest revision from perforce and rebases the current work (branch) against it"
1059
+ self.syncWithOrigin = False
1060
1061
def run(self, args):
1062
sync = P4Sync()
1063
+ sync.syncWithOrigin = self.syncWithOrigin
1064
sync.run([])
1065
print "Rebasing the current branch"
1066
oldHead = mypopen("git rev-parse HEAD").read()[:-1]
contrib/fast-import/git-p4.txt
+20
@@ -63,6 +63,26 @@ It is recommended to run 'git repack -a -d -f' from time to time when using
63
incremental imports to optimally combine the individual git packs that each
64
incremental import creates through the use of git-fast-import.
65
66
+
67
+A useful setup may be that you have a periodically updated git repository
68
+somewhere that contains a complete import of a Perforce project. That git
69
+repository can be used to clone the working repository from and one would
70
+import from Perforce directly after cloning using git-p4. If the connection to
71
+the Perforce server is slow and the working repository hasn't been synced for a
72
+while it may be desirable to fetch changes from the origin git repository using
73
+the efficient git protocol. git-p4 supports this through
74
+
75
+ git-p4 sync --with-origin
76
+
77
+or
78
+
79
+ git-p4 rebase --with-origin
80
+
81
+In that case "git fetch origin" is called and if it turns out that the origin
82
+branch is newer than the git "p4" import branch then the latter is updated from
83
+the former and the direct import from Perforce is resumed, which will result in
84
+fewer changes to be imported using the slower perforce connection.
85
+
86
Updating
87
========
88