@matheusbsbs / matheus-bagliano / commits / 86dff6b676
Fork 0

Cleanups & import into p4/master for local import

- import into master/local if --import-local is set - use Die() for exiting - if --verbose is set, raise Exception() - use joined strings iso. `list` for progress printing Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>

Han-Wen Nienhuys committed May 23, 2007 at 18:49 UTC 86dff6b6762149d5f3d4b44bb57f58a8399a33ee
1 file changed +32 -22
contrib/fast-import/git-p4
+32 -22
@@ -16,40 +16,44 @@ from sets import Set;
16
17 verbose = False
18
19 +def die(msg):
20 + if verbose:
21 + raise Exception(msg)
22 + else:
23 + sys.stderr.write(msg + "\n")
24 + sys.exit(1)
25 +
26 def write_pipe(c, str):
27 if verbose:
21 - sys.stderr.write('writing pipe: %s\n' % c)
28 + sys.stderr.write('Writing pipe: %s\n' % c)
29
30 pipe = os.popen(c, 'w')
31 val = pipe.write(str)
32 if pipe.close():
26 - sys.stderr.write('Command failed: %s' % c)
27 - sys.exit(1)
33 + die('Command failed: %s' % c)
34
35 return val
36
37 def read_pipe(c, ignore_error=False):
38 if verbose:
33 - sys.stderr.write('reading pipe: %s\n' % c)
39 + sys.stderr.write('Reading pipe: %s\n' % c)
40
41 pipe = os.popen(c, 'rb')
42 val = pipe.read()
43 if pipe.close() and not ignore_error:
38 - sys.stderr.write('Command failed: %s\n' % c)
39 - sys.exit(1)
44 + die('Command failed: %s' % c)
45
46 return val
47
48
49 def read_pipe_lines(c):
50 if verbose:
46 - sys.stderr.write('reading pipe: %s\n' % c)
51 + sys.stderr.write('Reading pipe: %s\n' % c)
52 ## todo: check return status
53 pipe = os.popen(c, 'rb')
54 val = pipe.readlines()
55 if pipe.close():
51 - sys.stderr.write('Command failed: %s\n' % c)
52 - sys.exit(1)
56 + die('Command failed: %s' % c)
57
58 return val
59
@@ -105,10 +109,6 @@ def p4Where(depotPath):
109 clientPath = clientPath[:-3]
110 return clientPath
111
108 -def die(msg):
109 - sys.stderr.write(msg + "\n")
110 - sys.exit(1)
111 -
112 def currentGitBranch():
113 return read_pipe("git name-rev HEAD").split(" ")[1].strip()
114
@@ -583,7 +583,8 @@ class P4Sync(Command):
583 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",
584 help="Import into refs/heads/ , not refs/remotes"),
585 optparse.make_option("--max-changes", dest="maxChanges"),
586 - optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true')
586 + optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',
587 + help="Keep entire BRANCH/DIR/SUBDIR prefix during import")
588 ]
589 self.description = """Imports from Perforce into a git repository.\n
590 example:
@@ -876,10 +877,14 @@ class P4Sync(Command):
877 if self.verbose:
878 print "Label changes: %s" % self.labels.keys()
879
880 + def guessProjectName(self):
881 + for p in self.depotPaths:
882 + return p [p.strip().rfind("/") + 1:]
883 +
884 def getBranchMapping(self):
885
886 ## FIXME - what's a P4 projectName ?
882 - self.projectName = self.depotPath[self.depotPath.strip().rfind("/") + 1:]
887 + self.projectName = self.guessProjectName()
888
889 for info in p4CmdList("branches"):
890 details = p4Cmd("branch -o %s" % info["branch"])
@@ -911,9 +916,11 @@ class P4Sync(Command):
916
917 for line in read_pipe_lines(cmdline):
918 line = line.strip()
914 - if self.importIntoRemotes and ((not line.startswith("p4/")) or line == "p4/HEAD"):
915 - continue
919
920 + ## only import to p4/
921 + if not line.startswith('p4/'):
922 + continue
923 + branch = line
924 if self.importIntoRemotes:
925 # strip off p4
926 branch = re.sub ("^p4/", "", line)
@@ -923,7 +930,8 @@ class P4Sync(Command):
930
931 def createOrUpdateBranchesFromOrigin(self):
932 if not self.silent:
926 - print "Creating/updating branch(es) in %s based on origin branch(es)" % self.refPrefix
933 + print ("Creating/updating branch(es) in %s based on origin branch(es)"
934 + % self.refPrefix)
935
936 for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
937 line = line.strip()
@@ -998,7 +1006,7 @@ class P4Sync(Command):
1006 system("git fetch origin")
1007
1008 if len(self.branch) == 0:
1001 - self.branch = self.refPrefix + "master"
1009 + self.branch = self.refPrefix + "p4/master"
1010 if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
1011 system("git update-ref %s refs/heads/p4" % self.branch)
1012 system("git branch -D p4");
@@ -1023,6 +1031,7 @@ class P4Sync(Command):
1031
1032 p4Change = 0
1033 for branch in self.p4BranchesInGit:
1034 + print self.p4BranchesInGit
1035 logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch)
1036
1037 settings = extractSettingsGitLog(logMsg)
@@ -1125,7 +1134,7 @@ class P4Sync(Command):
1134 self.gitStream = importProcess.stdin
1135 self.gitError = importProcess.stderr
1136
1128 - if len(self.revision) > 0:
1137 + if self.revision:
1138 print "Doing initial import of %s from revision %s" % (' '.join(self.depotPaths), self.revision)
1139
1140 details = { "user" : "git perforce import user", "time" : int(time.time()) }
@@ -1183,7 +1192,7 @@ class P4Sync(Command):
1192 changes.sort()
1193 else:
1194 if self.verbose:
1186 - print "Getting p4 changes for %s...%s" % (`self.depotPaths`,
1195 + print "Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
1196 self.changeRange)
1197 assert self.depotPaths
1198 output = read_pipe_lines("p4 changes " + ' '.join (["%s...%s" % (p, self.changeRange)
@@ -1344,7 +1353,7 @@ class P4Clone(P4Sync):
1353 if not self.cloneDestination:
1354 self.cloneDestination = self.defaultDestination()
1355
1347 - print "Importing from %s into %s" % (`depotPaths`, self.cloneDestination)
1356 + print "Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination)
1357 os.makedirs(self.cloneDestination)
1358 os.chdir(self.cloneDestination)
1359 system("git init")
@@ -1357,6 +1366,7 @@ class P4Clone(P4Sync):
1366 system("git checkout -f")
1367 else:
1368 print "Could not detect main branch. No checkout/master branch created."
1369 +
1370 return True
1371
1372 class HelpFormatter(optparse.IndentedHelpFormatter):