Diverse cleanups
- print commands with \n - extractDepotPathsAndChangeFromGitLog -> extractSettings, returning dict. - store keepRepoPath in [git-p4: ] line - create a main() function, so git-p4 can be pychecked - use --destination for clone destination. This simplifies logic for --keep-path Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
Han-Wen Nienhuys committed
May 23, 2007 at 18:49 UTC
bb6e09b27afeaae780dabfda7a07d59fc7efc4cf
1 file changed
+134
-90
contrib/fast-import/git-p4
+134
-90
@@ -56,7 +56,7 @@ def read_pipe_lines(c):
56
57
def system(cmd):
58
if verbose:
59
- sys.stderr.write("executing %s" % cmd)
59
+ sys.stderr.write("executing %s\n" % cmd)
60
if os.system(cmd) != 0:
61
die("command failed: %s" % cmd)
62
@@ -112,7 +112,8 @@ def currentGitBranch():
112
return read_pipe("git name-rev HEAD").split(" ")[1].strip()
113
114
def isValidGitDir(path):
115
- if os.path.exists(path + "/HEAD") and os.path.exists(path + "/refs") and os.path.exists(path + "/objects"):
115
+ if (os.path.exists(path + "/HEAD")
116
+ and os.path.exists(path + "/refs") and os.path.exists(path + "/objects")):
117
return True;
118
return False
119
@@ -133,7 +134,7 @@ def extractLogMessageFromGitCommit(commit):
134
logMessage += log
135
return logMessage
136
136
-def extractDepotPathsAndChangeFromGitLog(log):
137
+def extractSettingsGitLog(log):
138
values = {}
139
for line in log.split("\n"):
140
line = line.strip()
@@ -151,11 +152,12 @@ def extractDepotPathsAndChangeFromGitLog(log):
152
153
values[key] = val
154
154
- paths = values.get("depot-path").split(',')
155
- return paths, values.get("change")
155
+ values['depot-paths'] = values.get("depot-paths").split(',')
156
+ return values
157
158
def gitBranchExists(branch):
158
- proc = subprocess.Popen(["git", "rev-parse", branch], stderr=subprocess.PIPE, stdout=subprocess.PIPE);
159
+ proc = subprocess.Popen(["git", "rev-parse", branch],
160
+ stderr=subprocess.PIPE, stdout=subprocess.PIPE);
161
return proc.wait() == 0;
162
163
def gitConfig(key):
@@ -211,7 +213,11 @@ class P4RollBack(Command):
213
line = line.strip()
214
ref = refPrefix + line
215
log = extractLogMessageFromGitCommit(ref)
214
- depotPaths, change = extractDepotPathsAndChangeFromGitLog(log)
216
+ settings = extractSettingsGitLog(log)
217
+
218
+ depotPaths = settings['depot-paths']
219
+ change = settings['change']
220
+
221
changed = False
222
223
if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange)
@@ -220,13 +226,17 @@ class P4RollBack(Command):
226
system("git update-ref -d %s `git rev-parse %s`" % (ref, ref))
227
continue
228
223
- while len(change) > 0 and int(change) > maxChange:
229
+ while change and int(change) > maxChange:
230
changed = True
231
if self.verbose:
232
print "%s is at %s ; rewinding towards %s" % (ref, change, maxChange)
233
system("git update-ref %s \"%s^\"" % (ref, ref))
234
log = extractLogMessageFromGitCommit(ref)
229
- depotPaths, change = extractDepotPathsAndChangeFromGitLog(log)
235
+ settings = extractSettingsGitLog(log)
236
+
237
+
238
+ depotPaths = settings['depot-paths']
239
+ change = settings['change']
240
241
if changed:
242
print "%s rewound to %s" % (ref, change)
@@ -474,10 +484,12 @@ class P4Submit(Command):
484
return False
485
486
depotPath = ""
487
+ settings = None
488
if gitBranchExists("p4"):
478
- [depotPaths, dummy] = extractDepotPathsAndChangeFromGitLog(extractLogMessageFromGitCommit("p4"))
489
+ settings = extractSettingsGitLog(extractLogMessageFromGitCommit("p4"))
490
if len(depotPath) == 0 and gitBranchExists("origin"):
480
- [depotPaths, dummy] = extractDepotPathsAndChangeFromGitLog(extractLogMessageFromGitCommit("origin"))
491
+ settings = extractSettingsGitLog(extractLogMessageFromGitCommit("origin"))
492
+ depotPaths = settings['depot-paths']
493
494
if len(depotPath) == 0:
495
print "Internal error: cannot locate perforce depot path from existing branches"
@@ -686,8 +698,11 @@ class P4Sync(Command):
698
699
self.gitStream.write("data <<EOT\n")
700
self.gitStream.write(details["desc"])
689
- self.gitStream.write("\n[git-p4: depot-path = \"%s\": change = %s]\n"
690
- % (','.join (branchPrefixes), details["change"]))
701
+ self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s: "
702
+ "options = %s]\n"
703
+ % (','.join (branchPrefixes), details["change"],
704
+ details['options']
705
+ ))
706
self.gitStream.write("EOT\n\n")
707
708
if len(parent) > 0:
@@ -883,12 +898,13 @@ class P4Sync(Command):
898
if (not line.startswith("origin/")) or line.endswith("HEAD\n"):
899
continue
900
886
- headName = line[len("origin/")]
901
+ headName = line[len("origin/"):]
902
remoteHead = self.refPrefix + headName
903
originHead = "origin/" + headName
904
890
- [originPreviousDepotPaths, originP4Change] = extractDepotPathsAndChangeFromGitLog(extractLogMessageFromGitCommit(originHead))
891
- if len(originPreviousDepotPaths) == 0 or len(originP4Change) == 0:
905
+ original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))
906
+ if (not original.has_key('depot-paths')
907
+ or not original.has_key('change')):
908
continue
909
910
update = False
@@ -897,20 +913,36 @@ class P4Sync(Command):
913
print "creating %s" % remoteHead
914
update = True
915
else:
900
- [p4PreviousDepotPaths, p4Change] = extractDepotPathsAndChangeFromGitLog(extractLogMessageFromGitCommit(remoteHead))
901
- if len(p4Change) > 0:
902
- if originPreviousDepotPaths == p4PreviousDepotPaths:
903
- originP4Change = int(originP4Change)
904
- p4Change = int(p4Change)
916
+ settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))
917
+ if settings.has_key('change') > 0:
918
+ if settings['depot-paths'] == original['depot-paths']:
919
+ originP4Change = int(original['change'])
920
+ p4Change = int(settings['change'])
921
if originP4Change > p4Change:
906
- print "%s (%s) is newer than %s (%s). Updating p4 branch from origin." % (originHead, originP4Change, remoteHead, p4Change)
922
+ print ("%s (%s) is newer than %s (%s). "
923
+ "Updating p4 branch from origin."
924
+ % (originHead, originP4Change,
925
+ remoteHead, p4Change))
926
update = True
927
else:
909
- print "Ignoring: %s was imported from %s while %s was imported from %s" % (originHead, originPreviousDepotPaths, remoteHead, p4PreviousDepotPaths)
928
+ print ("Ignoring: %s was imported from %s while "
929
+ "%s was imported from %s"
930
+ % (originHead, ','.join(original['depot-paths']),
931
+ remoteHead, ','.join(settings['depot-paths'])))
932
933
if update:
934
system("git update-ref %s %s" % (remoteHead, originHead))
935
936
+ def updateOptionDict(self, d):
937
+ option_keys = {}
938
+ if self.keepRepoPath:
939
+ option_keys['keepRepoPath'] = 1
940
+
941
+ d["options"] = ' '.join(sorted(option_keys.keys()))
942
+
943
+ def readOptions(self, d):
944
+ self.keepRepoPath = (d.has_key('options')
945
+ and ('keepRepoPath' in d['options']))
946
947
def run(self, args):
948
self.depotPaths = []
@@ -942,7 +974,8 @@ class P4Sync(Command):
974
if not gitBranchExists(self.refPrefix + "HEAD") and self.importIntoRemotes:
975
system("git symbolic-ref %sHEAD %s" % (self.refPrefix, self.branch))
976
945
- if args == []:
977
+ ### FIXME
978
+ if 1:
979
if self.hasOrigin:
980
self.createOrUpdateBranchesFromOrigin()
981
self.listExistingP4GitBranches()
@@ -958,19 +991,22 @@ class P4Sync(Command):
991
p4Change = 0
992
for branch in self.p4BranchesInGit:
993
logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch)
961
- (depotPaths, change) = extractDepotPathsAndChangeFromGitLog(logMsg)
994
+
995
+ settings = extractSettingsGitLog(logMsg)
996
997
if self.verbose:
998
print "path %s change %s" % (','.join(depotPaths), change)
999
966
- if len(depotPaths) > 0 and len(change) > 0:
967
- change = int(change) + 1
1000
+ self.readOptions(settings)
1001
+ if (settings.has_key('depot-paths')
1002
+ and settings.has_key ('change')):
1003
+ change = int(settings['change']) + 1
1004
p4Change = max(p4Change, change)
1005
970
- if len(self.previousDepotPaths) == 0:
1006
+ depotPaths = sorted(settings['depot-paths'])
1007
+ if self.previousDepotPaths == []:
1008
self.previousDepotPaths = depotPaths
1009
else:
973
- ## FIXME
1010
paths = []
1011
for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
1012
for i in range(0, max(len(cur), len(prev))):
@@ -982,7 +1018,7 @@ class P4Sync(Command):
1018
self.previousDepotPaths = paths
1019
1020
if p4Change > 0:
985
- self.depotPaths = self.previousDepotPaths
1021
+ self.depotPaths = sorted(self.previousDepotPaths)
1022
self.changeRange = "@%s,#head" % p4Change
1023
self.initialParent = parseRevision(self.branch)
1024
if not self.silent and not self.detectBranches:
@@ -1001,7 +1037,7 @@ class P4Sync(Command):
1037
' '.join (args)))
1038
sys.exit(1)
1039
1004
- self.depotPaths = args
1040
+ self.depotPaths = sorted(args)
1041
1042
self.revision = ""
1043
self.users = {}
@@ -1088,7 +1124,7 @@ class P4Sync(Command):
1124
fileCnt = fileCnt + 1
1125
1126
details["change"] = newestRevision
1091
-
1127
+ self.updateOptionDict(details)
1128
try:
1129
self.commit(details, self.extractFilesFromCommit(details), self.branch, self.depotPaths)
1130
except IOError:
@@ -1135,6 +1171,7 @@ class P4Sync(Command):
1171
cnt = 1
1172
for change in changes:
1173
description = p4Cmd("describe %s" % change)
1174
+ self.updateOptionDict(description)
1175
1176
if not self.silent:
1177
sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
@@ -1237,7 +1274,12 @@ class P4Clone(P4Sync):
1274
def __init__(self):
1275
P4Sync.__init__(self)
1276
self.description = "Creates a new git repository and imports from Perforce into it"
1240
- self.usage = "usage: %prog [options] //depot/path[@revRange] [directory]"
1277
+ self.usage = "usage: %prog [options] //depot/path[@revRange]"
1278
+ self.options.append(
1279
+ optparse.make_option("--destination", dest="cloneDestination",
1280
+ action='store', default=None,
1281
+ help="where to leave result of the clone"))
1282
+ self.cloneDestination = None
1283
self.needsGit = False
1284
1285
def run(self, args):
@@ -1245,32 +1287,28 @@ class P4Clone(P4Sync):
1287
1288
if len(args) < 1:
1289
return False
1248
- destination = ""
1249
- if self.keepRepoPath:
1250
- destination = args[-1]
1251
- args = args[:-1]
1252
- elif len(args) == 2:
1253
- destination = args[1]
1254
- elif len(args) > 2:
1255
- return False
1290
+
1291
+ if self.keepRepoPath and not self.cloneDestination:
1292
+ sys.stderr.write("Must specify destination for --keep-path\n")
1293
+ sys.exit(1)
1294
1295
depotPaths = args
1296
for p in depotPaths:
1297
if not p.startswith("//"):
1298
return False
1299
1262
- if not destination:
1300
+ if not self.cloneDestination:
1301
depotPath = args[0]
1302
depotDir = re.sub("(@[^@]*)$", "", depotPath)
1303
depotDir = re.sub("(#[^#]*)$", "", depotDir)
1304
depotDir = re.sub(r"\.\.\.$,", "", depotDir)
1305
depotDir = re.sub(r"/$", "", depotDir)
1306
1269
- destination = os.path.split(depotDir)[1]
1307
+ self.cloneDestination = os.path.split(depotDir)[1]
1308
1271
- print "Importing from %s into %s" % (`depotPaths`, destination)
1272
- os.makedirs(destination)
1273
- os.chdir(destination)
1309
+ print "Importing from %s into %s" % (`depotPaths`, self.cloneDestination)
1310
+ os.makedirs(self.cloneDestination)
1311
+ os.chdir(self.cloneDestination)
1312
system("git init")
1313
gitdir = os.getcwd() + "/.git"
1314
if not P4Sync.run(self, depotPaths):
@@ -1310,54 +1348,60 @@ commands = {
1348
"rollback" : P4RollBack()
1349
}
1350
1313
-if len(sys.argv[1:]) == 0:
1314
- printUsage(commands.keys())
1315
- sys.exit(2)
1316
-
1317
-cmd = ""
1318
-cmdName = sys.argv[1]
1319
-try:
1320
- cmd = commands[cmdName]
1321
-except KeyError:
1322
- print "unknown command %s" % cmdName
1323
- print ""
1324
- printUsage(commands.keys())
1325
- sys.exit(2)
1351
1327
-options = cmd.options
1328
-cmd.gitdir = gitdir
1352
+def main():
1353
+ if len(sys.argv[1:]) == 0:
1354
+ printUsage(commands.keys())
1355
+ sys.exit(2)
1356
1330
-args = sys.argv[2:]
1331
-
1332
-if len(options) > 0:
1333
- options.append(optparse.make_option("--git-dir", dest="gitdir"))
1357
+ cmd = ""
1358
+ cmdName = sys.argv[1]
1359
+ try:
1360
+ cmd = commands[cmdName]
1361
+ except KeyError:
1362
+ print "unknown command %s" % cmdName
1363
+ print ""
1364
+ printUsage(commands.keys())
1365
+ sys.exit(2)
1366
+
1367
+ options = cmd.options
1368
+ cmd.gitdir = gitdir
1369
+
1370
+ args = sys.argv[2:]
1371
+
1372
+ if len(options) > 0:
1373
+ options.append(optparse.make_option("--git-dir", dest="gitdir"))
1374
+
1375
+ parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
1376
+ options,
1377
+ description = cmd.description,
1378
+ formatter = HelpFormatter())
1379
+
1380
+ (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
1381
+ global verbose
1382
+ verbose = cmd.verbose
1383
+ if cmd.needsGit:
1384
+ gitdir = cmd.gitdir
1385
+ if len(gitdir) == 0:
1386
+ gitdir = ".git"
1387
+ if not isValidGitDir(gitdir):
1388
+ gitdir = read_pipe("git rev-parse --git-dir").strip()
1389
+ if os.path.exists(gitdir):
1390
+ cdup = read_pipe("git rev-parse --show-cdup").strip()
1391
+ if len(cdup) > 0:
1392
+ os.chdir(cdup);
1393
1335
- parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
1336
- options,
1337
- description = cmd.description,
1338
- formatter = HelpFormatter())
1394
+ if not isValidGitDir(gitdir):
1395
+ if isValidGitDir(gitdir + "/.git"):
1396
+ gitdir += "/.git"
1397
+ else:
1398
+ die("fatal: cannot locate git repository at %s" % gitdir)
1399
1340
- (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
1400
+ os.environ["GIT_DIR"] = gitdir
1401
1342
-verbose = cmd.verbose
1343
-if cmd.needsGit:
1344
- gitdir = cmd.gitdir
1345
- if len(gitdir) == 0:
1346
- gitdir = ".git"
1347
- if not isValidGitDir(gitdir):
1348
- gitdir = read_pipe("git rev-parse --git-dir").strip()
1349
- if os.path.exists(gitdir):
1350
- cdup = read_pipe("git rev-parse --show-cdup").strip()
1351
- if len(cdup) > 0:
1352
- os.chdir(cdup);
1353
-
1354
- if not isValidGitDir(gitdir):
1355
- if isValidGitDir(gitdir + "/.git"):
1356
- gitdir += "/.git"
1357
- else:
1358
- die("fatal: cannot locate git repository at %s" % gitdir)
1402
+ if not cmd.run(args):
1403
+ parser.print_help()
1404
1360
- os.environ["GIT_DIR"] = gitdir
1405
1362
-if not cmd.run(args):
1363
- parser.print_help()
1406
+if __name__ == '__main__':
1407
+ main()