Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # git-p4.py -- A tool for bidirectional operation between a Perforce depot and git. |
| 4 | # |
| 5 | # Author: Simon Hausmann <hausmann@kde.org> |
Simon Hausmann | 83dce55 | 2007-03-19 22:26:36 +0100 | [diff] [blame] | 6 | # Copyright: 2007 Simon Hausmann <hausmann@kde.org> |
| 7 | # 2007 Trolltech ASA |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 8 | # License: MIT <http://www.opensource.org/licenses/mit-license.php> |
| 9 | # |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 10 | # TODO: * Consider making --with-origin the default, assuming that the git |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 11 | # protocol is always more efficient. (needs manual testing first :) |
Simon Hausmann | 24f7b53 | 2007-05-20 23:42:22 +0200 | [diff] [blame] | 12 | # |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 13 | |
Simon Hausmann | 0848358 | 2007-05-15 14:31:06 +0200 | [diff] [blame] | 14 | import optparse, sys, os, marshal, popen2, subprocess, shelve |
Simon Hausmann | 25df95c | 2007-05-15 15:15:39 +0200 | [diff] [blame] | 15 | import tempfile, getopt, sha, os.path, time, platform |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 16 | from sets import Set; |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 17 | |
| 18 | gitdir = os.environ.get("GIT_DIR", "") |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 19 | |
Simon Hausmann | caace11 | 2007-05-15 14:57:57 +0200 | [diff] [blame] | 20 | def mypopen(command): |
| 21 | return os.popen(command, "rb"); |
| 22 | |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 23 | def p4CmdList(cmd): |
| 24 | cmd = "p4 -G %s" % cmd |
| 25 | pipe = os.popen(cmd, "rb") |
| 26 | |
| 27 | result = [] |
| 28 | try: |
| 29 | while True: |
| 30 | entry = marshal.load(pipe) |
| 31 | result.append(entry) |
| 32 | except EOFError: |
| 33 | pass |
| 34 | pipe.close() |
| 35 | |
| 36 | return result |
| 37 | |
| 38 | def p4Cmd(cmd): |
| 39 | list = p4CmdList(cmd) |
| 40 | result = {} |
| 41 | for entry in list: |
| 42 | result.update(entry) |
| 43 | return result; |
| 44 | |
Simon Hausmann | cb2c9db | 2007-03-24 09:15:11 +0100 | [diff] [blame] | 45 | def p4Where(depotPath): |
| 46 | if not depotPath.endswith("/"): |
| 47 | depotPath += "/" |
| 48 | output = p4Cmd("where %s..." % depotPath) |
Simon Hausmann | dc52403 | 2007-05-21 09:34:56 +0200 | [diff] [blame] | 49 | if output["code"] == "error": |
| 50 | return "" |
Simon Hausmann | cb2c9db | 2007-03-24 09:15:11 +0100 | [diff] [blame] | 51 | clientPath = "" |
| 52 | if "path" in output: |
| 53 | clientPath = output.get("path") |
| 54 | elif "data" in output: |
| 55 | data = output.get("data") |
| 56 | lastSpace = data.rfind(" ") |
| 57 | clientPath = data[lastSpace + 1:] |
| 58 | |
| 59 | if clientPath.endswith("..."): |
| 60 | clientPath = clientPath[:-3] |
| 61 | return clientPath |
| 62 | |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 63 | def die(msg): |
| 64 | sys.stderr.write(msg + "\n") |
| 65 | sys.exit(1) |
| 66 | |
| 67 | def currentGitBranch(): |
Simon Hausmann | caace11 | 2007-05-15 14:57:57 +0200 | [diff] [blame] | 68 | return mypopen("git name-rev HEAD").read().split(" ")[1][:-1] |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 69 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 70 | def isValidGitDir(path): |
| 71 | if os.path.exists(path + "/HEAD") and os.path.exists(path + "/refs") and os.path.exists(path + "/objects"): |
| 72 | return True; |
| 73 | return False |
| 74 | |
Simon Hausmann | 463e8af | 2007-05-17 09:13:54 +0200 | [diff] [blame] | 75 | def parseRevision(ref): |
| 76 | return mypopen("git rev-parse %s" % ref).read()[:-1] |
| 77 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 78 | def system(cmd): |
| 79 | if os.system(cmd) != 0: |
| 80 | die("command failed: %s" % cmd) |
| 81 | |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 82 | def extractLogMessageFromGitCommit(commit): |
| 83 | logMessage = "" |
| 84 | foundTitle = False |
Simon Hausmann | caace11 | 2007-05-15 14:57:57 +0200 | [diff] [blame] | 85 | for log in mypopen("git cat-file commit %s" % commit).readlines(): |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 86 | if not foundTitle: |
| 87 | if len(log) == 1: |
Simon Hausmann | 1c09418 | 2007-05-01 23:15:48 +0200 | [diff] [blame] | 88 | foundTitle = True |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 89 | continue |
| 90 | |
| 91 | logMessage += log |
| 92 | return logMessage |
| 93 | |
| 94 | def extractDepotPathAndChangeFromGitLog(log): |
| 95 | values = {} |
| 96 | for line in log.split("\n"): |
| 97 | line = line.strip() |
| 98 | if line.startswith("[git-p4:") and line.endswith("]"): |
| 99 | line = line[8:-1].strip() |
| 100 | for assignment in line.split(":"): |
| 101 | variable = assignment.strip() |
| 102 | value = "" |
| 103 | equalPos = assignment.find("=") |
| 104 | if equalPos != -1: |
| 105 | variable = assignment[:equalPos].strip() |
| 106 | value = assignment[equalPos + 1:].strip() |
| 107 | if value.startswith("\"") and value.endswith("\""): |
| 108 | value = value[1:-1] |
| 109 | values[variable] = value |
| 110 | |
| 111 | return values.get("depot-path"), values.get("change") |
| 112 | |
Simon Hausmann | 8136a63 | 2007-03-22 21:27:14 +0100 | [diff] [blame] | 113 | def gitBranchExists(branch): |
Simon Hausmann | caace11 | 2007-05-15 14:57:57 +0200 | [diff] [blame] | 114 | proc = subprocess.Popen(["git", "rev-parse", branch], stderr=subprocess.PIPE, stdout=subprocess.PIPE); |
| 115 | return proc.wait() == 0; |
Simon Hausmann | 8136a63 | 2007-03-22 21:27:14 +0100 | [diff] [blame] | 116 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 117 | class Command: |
| 118 | def __init__(self): |
| 119 | self.usage = "usage: %prog [options]" |
Simon Hausmann | 8910ac0 | 2007-03-26 08:18:55 +0200 | [diff] [blame] | 120 | self.needsGit = True |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 121 | |
| 122 | class P4Debug(Command): |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 123 | def __init__(self): |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 124 | Command.__init__(self) |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 125 | self.options = [ |
| 126 | ] |
Simon Hausmann | c8c3911 | 2007-03-19 21:02:30 +0100 | [diff] [blame] | 127 | self.description = "A tool to debug the output of p4 -G." |
Simon Hausmann | 8910ac0 | 2007-03-26 08:18:55 +0200 | [diff] [blame] | 128 | self.needsGit = False |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 129 | |
| 130 | def run(self, args): |
| 131 | for output in p4CmdList(" ".join(args)): |
| 132 | print output |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 133 | return True |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 134 | |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 135 | class P4RollBack(Command): |
| 136 | def __init__(self): |
| 137 | Command.__init__(self) |
| 138 | self.options = [ |
Simon Hausmann | 0c66a78 | 2007-05-23 20:07:57 +0200 | [diff] [blame^] | 139 | optparse.make_option("--verbose", dest="verbose", action="store_true"), |
| 140 | optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true") |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 141 | ] |
| 142 | self.description = "A tool to debug the multi-branch import. Don't use :)" |
Simon Hausmann | 52102d4 | 2007-05-21 23:44:24 +0200 | [diff] [blame] | 143 | self.verbose = False |
Simon Hausmann | 0c66a78 | 2007-05-23 20:07:57 +0200 | [diff] [blame^] | 144 | self.rollbackLocalBranches = False |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 145 | |
| 146 | def run(self, args): |
| 147 | if len(args) != 1: |
| 148 | return False |
| 149 | maxChange = int(args[0]) |
Simon Hausmann | 0c66a78 | 2007-05-23 20:07:57 +0200 | [diff] [blame^] | 150 | |
| 151 | if self.rollbackLocalBranches: |
| 152 | refPrefix = "refs/heads/" |
| 153 | lines = mypopen("git rev-parse --symbolic --branches").readlines() |
| 154 | else: |
| 155 | refPrefix = "refs/remotes/" |
| 156 | lines = mypopen("git rev-parse --symbolic --remotes").readlines() |
| 157 | |
| 158 | for line in lines: |
| 159 | if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"): |
| 160 | ref = refPrefix + line[:-1] |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 161 | log = extractLogMessageFromGitCommit(ref) |
| 162 | depotPath, change = extractDepotPathAndChangeFromGitLog(log) |
| 163 | changed = False |
Simon Hausmann | 52102d4 | 2007-05-21 23:44:24 +0200 | [diff] [blame] | 164 | |
| 165 | if len(p4Cmd("changes -m 1 %s...@%s" % (depotPath, maxChange))) == 0: |
| 166 | print "Branch %s did not exist at change %s, deleting." % (ref, maxChange) |
| 167 | system("git update-ref -d %s `git rev-parse %s`" % (ref, ref)) |
| 168 | continue |
| 169 | |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 170 | while len(change) > 0 and int(change) > maxChange: |
| 171 | changed = True |
Simon Hausmann | 52102d4 | 2007-05-21 23:44:24 +0200 | [diff] [blame] | 172 | if self.verbose: |
| 173 | print "%s is at %s ; rewinding towards %s" % (ref, change, maxChange) |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 174 | system("git update-ref %s \"%s^\"" % (ref, ref)) |
| 175 | log = extractLogMessageFromGitCommit(ref) |
| 176 | depotPath, change = extractDepotPathAndChangeFromGitLog(log) |
| 177 | |
| 178 | if changed: |
Simon Hausmann | 52102d4 | 2007-05-21 23:44:24 +0200 | [diff] [blame] | 179 | print "%s rewound to %s" % (ref, change) |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 180 | |
| 181 | return True |
| 182 | |
Simon Hausmann | 711544b | 2007-04-01 15:40:46 +0200 | [diff] [blame] | 183 | class P4Submit(Command): |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 184 | def __init__(self): |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 185 | Command.__init__(self) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 186 | self.options = [ |
| 187 | optparse.make_option("--continue", action="store_false", dest="firstTime"), |
| 188 | optparse.make_option("--origin", dest="origin"), |
| 189 | optparse.make_option("--reset", action="store_true", dest="reset"), |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 190 | optparse.make_option("--log-substitutions", dest="substFile"), |
| 191 | optparse.make_option("--noninteractive", action="store_false"), |
Simon Hausmann | 04219c0 | 2007-03-21 10:11:20 +0100 | [diff] [blame] | 192 | optparse.make_option("--dry-run", action="store_true"), |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 193 | optparse.make_option("--direct", dest="directSubmit", action="store_true"), |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 194 | ] |
| 195 | self.description = "Submit changes from git to the perforce depot." |
Simon Hausmann | c9b50e6 | 2007-03-29 19:15:24 +0200 | [diff] [blame] | 196 | self.usage += " [name of git branch to submit into perforce depot]" |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 197 | self.firstTime = True |
| 198 | self.reset = False |
| 199 | self.interactive = True |
| 200 | self.dryRun = False |
| 201 | self.substFile = "" |
| 202 | self.firstTime = True |
Simon Hausmann | 9512497 | 2007-03-23 09:16:07 +0100 | [diff] [blame] | 203 | self.origin = "" |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 204 | self.directSubmit = False |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 205 | |
| 206 | self.logSubstitutions = {} |
| 207 | self.logSubstitutions["<enter description here>"] = "%log%" |
| 208 | self.logSubstitutions["\tDetails:"] = "\tDetails: %log%" |
| 209 | |
| 210 | def check(self): |
| 211 | if len(p4CmdList("opened ...")) > 0: |
| 212 | die("You have files opened with perforce! Close them before starting the sync.") |
| 213 | |
| 214 | def start(self): |
| 215 | if len(self.config) > 0 and not self.reset: |
Simon Hausmann | c3c4624 | 2007-05-16 09:43:13 +0200 | [diff] [blame] | 216 | die("Cannot start sync. Previous sync config found at %s\nIf you want to start submitting again from scratch maybe you want to call git-p4 submit --reset" % self.configFile) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 217 | |
| 218 | commits = [] |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 219 | if self.directSubmit: |
| 220 | commits.append("0") |
| 221 | else: |
| 222 | for line in mypopen("git rev-list --no-merges %s..%s" % (self.origin, self.master)).readlines(): |
| 223 | commits.append(line[:-1]) |
| 224 | commits.reverse() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 225 | |
| 226 | self.config["commits"] = commits |
| 227 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 228 | def prepareLogMessage(self, template, message): |
| 229 | result = "" |
| 230 | |
| 231 | for line in template.split("\n"): |
| 232 | if line.startswith("#"): |
| 233 | result += line + "\n" |
| 234 | continue |
| 235 | |
| 236 | substituted = False |
| 237 | for key in self.logSubstitutions.keys(): |
| 238 | if line.find(key) != -1: |
| 239 | value = self.logSubstitutions[key] |
| 240 | value = value.replace("%log%", message) |
| 241 | if value != "@remove@": |
| 242 | result += line.replace(key, value) + "\n" |
| 243 | substituted = True |
| 244 | break |
| 245 | |
| 246 | if not substituted: |
| 247 | result += line + "\n" |
| 248 | |
| 249 | return result |
| 250 | |
| 251 | def apply(self, id): |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 252 | if self.directSubmit: |
| 253 | print "Applying local change in working directory/index" |
| 254 | diff = self.diffStatus |
| 255 | else: |
| 256 | print "Applying %s" % (mypopen("git log --max-count=1 --pretty=oneline %s" % id).read()) |
| 257 | diff = mypopen("git diff-tree -r --name-status \"%s^\" \"%s\"" % (id, id)).readlines() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 258 | filesToAdd = set() |
| 259 | filesToDelete = set() |
Simon Hausmann | d336c15 | 2007-05-16 09:41:26 +0200 | [diff] [blame] | 260 | editedFiles = set() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 261 | for line in diff: |
| 262 | modifier = line[0] |
| 263 | path = line[1:].strip() |
| 264 | if modifier == "M": |
Simon Hausmann | d336c15 | 2007-05-16 09:41:26 +0200 | [diff] [blame] | 265 | system("p4 edit \"%s\"" % path) |
| 266 | editedFiles.add(path) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 267 | elif modifier == "A": |
| 268 | filesToAdd.add(path) |
| 269 | if path in filesToDelete: |
| 270 | filesToDelete.remove(path) |
| 271 | elif modifier == "D": |
| 272 | filesToDelete.add(path) |
| 273 | if path in filesToAdd: |
| 274 | filesToAdd.remove(path) |
| 275 | else: |
| 276 | die("unknown modifier %s for %s" % (modifier, path)) |
| 277 | |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 278 | if self.directSubmit: |
| 279 | diffcmd = "cat \"%s\"" % self.diffFile |
| 280 | else: |
| 281 | diffcmd = "git format-patch -k --stdout \"%s^\"..\"%s\"" % (id, id) |
Simon Hausmann | 47a130b | 2007-05-20 16:33:21 +0200 | [diff] [blame] | 282 | patchcmd = diffcmd + " | git apply " |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 283 | tryPatchCmd = patchcmd + "--check -" |
| 284 | applyPatchCmd = patchcmd + "--check --apply -" |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 285 | |
Simon Hausmann | 47a130b | 2007-05-20 16:33:21 +0200 | [diff] [blame] | 286 | if os.system(tryPatchCmd) != 0: |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 287 | print "Unfortunately applying the change failed!" |
| 288 | print "What do you want to do?" |
| 289 | response = "x" |
| 290 | while response != "s" and response != "a" and response != "w": |
| 291 | response = raw_input("[s]kip this patch / [a]pply the patch forcibly and with .rej files / [w]rite the patch to a file (patch.txt) ") |
| 292 | if response == "s": |
| 293 | print "Skipping! Good luck with the next patches..." |
| 294 | return |
| 295 | elif response == "a": |
Simon Hausmann | 47a130b | 2007-05-20 16:33:21 +0200 | [diff] [blame] | 296 | os.system(applyPatchCmd) |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 297 | if len(filesToAdd) > 0: |
| 298 | print "You may also want to call p4 add on the following files:" |
| 299 | print " ".join(filesToAdd) |
| 300 | if len(filesToDelete): |
| 301 | print "The following files should be scheduled for deletion with p4 delete:" |
| 302 | print " ".join(filesToDelete) |
| 303 | die("Please resolve and submit the conflict manually and continue afterwards with git-p4 submit --continue") |
| 304 | elif response == "w": |
| 305 | system(diffcmd + " > patch.txt") |
| 306 | print "Patch saved to patch.txt in %s !" % self.clientPath |
| 307 | die("Please resolve and submit the conflict manually and continue afterwards with git-p4 submit --continue") |
| 308 | |
Simon Hausmann | 47a130b | 2007-05-20 16:33:21 +0200 | [diff] [blame] | 309 | system(applyPatchCmd) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 310 | |
| 311 | for f in filesToAdd: |
| 312 | system("p4 add %s" % f) |
| 313 | for f in filesToDelete: |
| 314 | system("p4 revert %s" % f) |
| 315 | system("p4 delete %s" % f) |
| 316 | |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 317 | logMessage = "" |
| 318 | if not self.directSubmit: |
| 319 | logMessage = extractLogMessageFromGitCommit(id) |
| 320 | logMessage = logMessage.replace("\n", "\n\t") |
| 321 | logMessage = logMessage[:-1] |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 322 | |
Simon Hausmann | caace11 | 2007-05-15 14:57:57 +0200 | [diff] [blame] | 323 | template = mypopen("p4 change -o").read() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 324 | |
| 325 | if self.interactive: |
| 326 | submitTemplate = self.prepareLogMessage(template, logMessage) |
Simon Hausmann | caace11 | 2007-05-15 14:57:57 +0200 | [diff] [blame] | 327 | diff = mypopen("p4 diff -du ...").read() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 328 | |
| 329 | for newFile in filesToAdd: |
| 330 | diff += "==== new file ====\n" |
| 331 | diff += "--- /dev/null\n" |
| 332 | diff += "+++ %s\n" % newFile |
| 333 | f = open(newFile, "r") |
| 334 | for line in f.readlines(): |
| 335 | diff += "+" + line |
| 336 | f.close() |
| 337 | |
Simon Hausmann | 25df95c | 2007-05-15 15:15:39 +0200 | [diff] [blame] | 338 | separatorLine = "######## everything below this line is just the diff #######" |
| 339 | if platform.system() == "Windows": |
| 340 | separatorLine += "\r" |
| 341 | separatorLine += "\n" |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 342 | |
| 343 | response = "e" |
Simon Hausmann | 5315025 | 2007-03-21 21:04:12 +0100 | [diff] [blame] | 344 | firstIteration = True |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 345 | while response == "e": |
Simon Hausmann | 5315025 | 2007-03-21 21:04:12 +0100 | [diff] [blame] | 346 | if not firstIteration: |
Simon Hausmann | d336c15 | 2007-05-16 09:41:26 +0200 | [diff] [blame] | 347 | response = raw_input("Do you want to submit this change? [y]es/[e]dit/[n]o/[s]kip ") |
Simon Hausmann | 5315025 | 2007-03-21 21:04:12 +0100 | [diff] [blame] | 348 | firstIteration = False |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 349 | if response == "e": |
| 350 | [handle, fileName] = tempfile.mkstemp() |
| 351 | tmpFile = os.fdopen(handle, "w+") |
Simon Hausmann | 5315025 | 2007-03-21 21:04:12 +0100 | [diff] [blame] | 352 | tmpFile.write(submitTemplate + separatorLine + diff) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 353 | tmpFile.close() |
Simon Hausmann | 25df95c | 2007-05-15 15:15:39 +0200 | [diff] [blame] | 354 | defaultEditor = "vi" |
| 355 | if platform.system() == "Windows": |
| 356 | defaultEditor = "notepad" |
| 357 | editor = os.environ.get("EDITOR", defaultEditor); |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 358 | system(editor + " " + fileName) |
Simon Hausmann | 25df95c | 2007-05-15 15:15:39 +0200 | [diff] [blame] | 359 | tmpFile = open(fileName, "rb") |
Simon Hausmann | 5315025 | 2007-03-21 21:04:12 +0100 | [diff] [blame] | 360 | message = tmpFile.read() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 361 | tmpFile.close() |
| 362 | os.remove(fileName) |
Simon Hausmann | 5315025 | 2007-03-21 21:04:12 +0100 | [diff] [blame] | 363 | submitTemplate = message[:message.index(separatorLine)] |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 364 | |
| 365 | if response == "y" or response == "yes": |
| 366 | if self.dryRun: |
| 367 | print submitTemplate |
| 368 | raw_input("Press return to continue...") |
| 369 | else: |
Simon Hausmann | 7944f14 | 2007-05-21 11:04:26 +0200 | [diff] [blame] | 370 | if self.directSubmit: |
| 371 | print "Submitting to git first" |
| 372 | os.chdir(self.oldWorkingDirectory) |
| 373 | pipe = os.popen("git commit -a -F -", "wb") |
| 374 | pipe.write(submitTemplate) |
| 375 | pipe.close() |
| 376 | os.chdir(self.clientPath) |
| 377 | |
| 378 | pipe = os.popen("p4 submit -i", "wb") |
| 379 | pipe.write(submitTemplate) |
| 380 | pipe.close() |
Simon Hausmann | d336c15 | 2007-05-16 09:41:26 +0200 | [diff] [blame] | 381 | elif response == "s": |
| 382 | for f in editedFiles: |
| 383 | system("p4 revert \"%s\"" % f); |
| 384 | for f in filesToAdd: |
| 385 | system("p4 revert \"%s\"" % f); |
| 386 | system("rm %s" %f) |
| 387 | for f in filesToDelete: |
| 388 | system("p4 delete \"%s\"" % f); |
| 389 | return |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 390 | else: |
| 391 | print "Not submitting!" |
| 392 | self.interactive = False |
| 393 | else: |
| 394 | fileName = "submit.txt" |
| 395 | file = open(fileName, "w+") |
| 396 | file.write(self.prepareLogMessage(template, logMessage)) |
| 397 | file.close() |
| 398 | print "Perforce submit template written as %s. Please review/edit and then use p4 submit -i < %s to submit directly!" % (fileName, fileName) |
| 399 | |
| 400 | def run(self, args): |
Simon Hausmann | 9512497 | 2007-03-23 09:16:07 +0100 | [diff] [blame] | 401 | global gitdir |
| 402 | # make gitdir absolute so we can cd out into the perforce checkout |
| 403 | gitdir = os.path.abspath(gitdir) |
| 404 | os.environ["GIT_DIR"] = gitdir |
Simon Hausmann | c9b50e6 | 2007-03-29 19:15:24 +0200 | [diff] [blame] | 405 | |
| 406 | if len(args) == 0: |
| 407 | self.master = currentGitBranch() |
| 408 | if len(self.master) == 0 or not os.path.exists("%s/refs/heads/%s" % (gitdir, self.master)): |
| 409 | die("Detecting current git branch failed!") |
| 410 | elif len(args) == 1: |
| 411 | self.master = args[0] |
| 412 | else: |
| 413 | return False |
| 414 | |
Simon Hausmann | 9512497 | 2007-03-23 09:16:07 +0100 | [diff] [blame] | 415 | depotPath = "" |
| 416 | if gitBranchExists("p4"): |
| 417 | [depotPath, dummy] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("p4")) |
| 418 | if len(depotPath) == 0 and gitBranchExists("origin"): |
| 419 | [depotPath, dummy] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("origin")) |
| 420 | |
| 421 | if len(depotPath) == 0: |
| 422 | print "Internal error: cannot locate perforce depot path from existing branches" |
| 423 | sys.exit(128) |
| 424 | |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 425 | self.clientPath = p4Where(depotPath) |
Simon Hausmann | 9512497 | 2007-03-23 09:16:07 +0100 | [diff] [blame] | 426 | |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 427 | if len(self.clientPath) == 0: |
Simon Hausmann | 9512497 | 2007-03-23 09:16:07 +0100 | [diff] [blame] | 428 | print "Error: Cannot locate perforce checkout of %s in client view" % depotPath |
| 429 | sys.exit(128) |
| 430 | |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 431 | print "Perforce checkout for depot path %s located at %s" % (depotPath, self.clientPath) |
Simon Hausmann | 7944f14 | 2007-05-21 11:04:26 +0200 | [diff] [blame] | 432 | self.oldWorkingDirectory = os.getcwd() |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 433 | |
| 434 | if self.directSubmit: |
| 435 | self.diffStatus = mypopen("git diff -r --name-status HEAD").readlines() |
Simon Hausmann | cbf5efa | 2007-05-21 10:08:11 +0200 | [diff] [blame] | 436 | if len(self.diffStatus) == 0: |
| 437 | print "No changes in working directory to submit." |
| 438 | return True |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 439 | patch = mypopen("git diff -p --binary --diff-filter=ACMRTUXB HEAD").read() |
| 440 | self.diffFile = gitdir + "/p4-git-diff" |
| 441 | f = open(self.diffFile, "wb") |
| 442 | f.write(patch) |
| 443 | f.close(); |
| 444 | |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 445 | os.chdir(self.clientPath) |
| 446 | response = raw_input("Do you want to sync %s with p4 sync? [y]es/[n]o " % self.clientPath) |
Simon Hausmann | 9512497 | 2007-03-23 09:16:07 +0100 | [diff] [blame] | 447 | if response == "y" or response == "yes": |
| 448 | system("p4 sync ...") |
| 449 | |
| 450 | if len(self.origin) == 0: |
| 451 | if gitBranchExists("p4"): |
| 452 | self.origin = "p4" |
| 453 | else: |
| 454 | self.origin = "origin" |
| 455 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 456 | if self.reset: |
| 457 | self.firstTime = True |
| 458 | |
| 459 | if len(self.substFile) > 0: |
| 460 | for line in open(self.substFile, "r").readlines(): |
| 461 | tokens = line[:-1].split("=") |
| 462 | self.logSubstitutions[tokens[0]] = tokens[1] |
| 463 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 464 | self.check() |
| 465 | self.configFile = gitdir + "/p4-git-sync.cfg" |
| 466 | self.config = shelve.open(self.configFile, writeback=True) |
| 467 | |
| 468 | if self.firstTime: |
| 469 | self.start() |
| 470 | |
| 471 | commits = self.config.get("commits", []) |
| 472 | |
| 473 | while len(commits) > 0: |
| 474 | self.firstTime = False |
| 475 | commit = commits[0] |
| 476 | commits = commits[1:] |
| 477 | self.config["commits"] = commits |
| 478 | self.apply(commit) |
| 479 | if not self.interactive: |
| 480 | break |
| 481 | |
| 482 | self.config.close() |
| 483 | |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 484 | if self.directSubmit: |
| 485 | os.remove(self.diffFile) |
| 486 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 487 | if len(commits) == 0: |
| 488 | if self.firstTime: |
| 489 | print "No changes found to apply between %s and current HEAD" % self.origin |
| 490 | else: |
| 491 | print "All changes applied!" |
Simon Hausmann | 7944f14 | 2007-05-21 11:04:26 +0200 | [diff] [blame] | 492 | os.chdir(self.oldWorkingDirectory) |
| 493 | response = raw_input("Do you want to sync from Perforce now using git-p4 rebase? [y]es/[n]o ") |
Simon Hausmann | 80b5910 | 2007-04-09 12:43:40 +0200 | [diff] [blame] | 494 | if response == "y" or response == "yes": |
Simon Hausmann | 80b5910 | 2007-04-09 12:43:40 +0200 | [diff] [blame] | 495 | rebase = P4Rebase() |
| 496 | rebase.run([]) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 497 | os.remove(self.configFile) |
| 498 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 499 | return True |
| 500 | |
Simon Hausmann | 711544b | 2007-04-01 15:40:46 +0200 | [diff] [blame] | 501 | class P4Sync(Command): |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 502 | def __init__(self): |
| 503 | Command.__init__(self) |
| 504 | self.options = [ |
| 505 | optparse.make_option("--branch", dest="branch"), |
| 506 | optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"), |
| 507 | optparse.make_option("--changesfile", dest="changesFile"), |
| 508 | optparse.make_option("--silent", dest="silent", action="store_true"), |
Simon Hausmann | ef48f90 | 2007-05-17 22:17:49 +0200 | [diff] [blame] | 509 | optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"), |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 510 | optparse.make_option("--with-origin", dest="syncWithOrigin", action="store_true"), |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 511 | optparse.make_option("--verbose", dest="verbose", action="store_true"), |
Simon Hausmann | 01a9c9c | 2007-05-23 00:07:35 +0200 | [diff] [blame] | 512 | optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false"), |
| 513 | optparse.make_option("--max-changes", dest="maxChanges") |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 514 | ] |
| 515 | self.description = """Imports from Perforce into a git repository.\n |
| 516 | example: |
| 517 | //depot/my/project/ -- to import the current head |
| 518 | //depot/my/project/@all -- to import everything |
| 519 | //depot/my/project/@1,6 -- to import only from revision 1 to 6 |
| 520 | |
| 521 | (a ... is not needed in the path p4 specification, it's added implicitly)""" |
| 522 | |
| 523 | self.usage += " //depot/path[@revRange]" |
| 524 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 525 | self.silent = False |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 526 | self.createdBranches = Set() |
| 527 | self.committedChanges = Set() |
Simon Hausmann | 569d1bd | 2007-03-22 21:34:16 +0100 | [diff] [blame] | 528 | self.branch = "" |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 529 | self.detectBranches = False |
Simon Hausmann | cb53e1f | 2007-04-08 00:12:02 +0200 | [diff] [blame] | 530 | self.detectLabels = False |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 531 | self.changesFile = "" |
Simon Hausmann | ef48f90 | 2007-05-17 22:17:49 +0200 | [diff] [blame] | 532 | self.syncWithOrigin = False |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 533 | self.verbose = False |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 534 | self.importIntoRemotes = True |
Simon Hausmann | 01a9c9c | 2007-05-23 00:07:35 +0200 | [diff] [blame] | 535 | self.maxChanges = "" |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 536 | |
| 537 | def p4File(self, depotPath): |
| 538 | return os.popen("p4 print -q \"%s\"" % depotPath, "rb").read() |
| 539 | |
| 540 | def extractFilesFromCommit(self, commit): |
| 541 | files = [] |
| 542 | fnum = 0 |
| 543 | while commit.has_key("depotFile%s" % fnum): |
| 544 | path = commit["depotFile%s" % fnum] |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 545 | if not path.startswith(self.depotPath): |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 546 | # if not self.silent: |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 547 | # print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, self.depotPath, change) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 548 | fnum = fnum + 1 |
| 549 | continue |
| 550 | |
| 551 | file = {} |
| 552 | file["path"] = path |
| 553 | file["rev"] = commit["rev%s" % fnum] |
| 554 | file["action"] = commit["action%s" % fnum] |
| 555 | file["type"] = commit["type%s" % fnum] |
| 556 | files.append(file) |
| 557 | fnum = fnum + 1 |
| 558 | return files |
| 559 | |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 560 | def splitFilesIntoBranches(self, commit): |
Simon Hausmann | d590467 | 2007-05-19 11:07:32 +0200 | [diff] [blame] | 561 | branches = {} |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 562 | |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 563 | fnum = 0 |
| 564 | while commit.has_key("depotFile%s" % fnum): |
| 565 | path = commit["depotFile%s" % fnum] |
| 566 | if not path.startswith(self.depotPath): |
| 567 | # if not self.silent: |
| 568 | # print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, self.depotPath, change) |
| 569 | fnum = fnum + 1 |
| 570 | continue |
| 571 | |
| 572 | file = {} |
| 573 | file["path"] = path |
| 574 | file["rev"] = commit["rev%s" % fnum] |
| 575 | file["action"] = commit["action%s" % fnum] |
| 576 | file["type"] = commit["type%s" % fnum] |
| 577 | fnum = fnum + 1 |
| 578 | |
| 579 | relPath = path[len(self.depotPath):] |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 580 | |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 581 | for branch in self.knownBranches.keys(): |
Simon Hausmann | af8da89 | 2007-05-21 23:25:51 +0200 | [diff] [blame] | 582 | if relPath.startswith(branch + "/"): # add a trailing slash so that a commit into qt/4.2foo doesn't end up in qt/4.2 |
Simon Hausmann | d590467 | 2007-05-19 11:07:32 +0200 | [diff] [blame] | 583 | if branch not in branches: |
| 584 | branches[branch] = [] |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 585 | branches[branch].append(file) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 586 | |
| 587 | return branches |
| 588 | |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 589 | def commit(self, details, files, branch, branchPrefix, parent = ""): |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 590 | epoch = details["time"] |
| 591 | author = details["user"] |
| 592 | |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 593 | if self.verbose: |
| 594 | print "commit into %s" % branch |
| 595 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 596 | self.gitStream.write("commit %s\n" % branch) |
| 597 | # gitStream.write("mark :%s\n" % details["change"]) |
| 598 | self.committedChanges.add(int(details["change"])) |
| 599 | committer = "" |
Simon Hausmann | b607e71 | 2007-05-20 10:55:54 +0200 | [diff] [blame] | 600 | if author not in self.users: |
| 601 | self.getUserMapFromPerforceServer() |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 602 | if author in self.users: |
Simon Hausmann | 0828ab1 | 2007-03-20 20:59:30 +0100 | [diff] [blame] | 603 | committer = "%s %s %s" % (self.users[author], epoch, self.tz) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 604 | else: |
Simon Hausmann | 0828ab1 | 2007-03-20 20:59:30 +0100 | [diff] [blame] | 605 | committer = "%s <a@b> %s %s" % (author, epoch, self.tz) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 606 | |
| 607 | self.gitStream.write("committer %s\n" % committer) |
| 608 | |
| 609 | self.gitStream.write("data <<EOT\n") |
| 610 | self.gitStream.write(details["desc"]) |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 611 | self.gitStream.write("\n[git-p4: depot-path = \"%s\": change = %s]\n" % (branchPrefix, details["change"])) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 612 | self.gitStream.write("EOT\n\n") |
| 613 | |
| 614 | if len(parent) > 0: |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 615 | if self.verbose: |
| 616 | print "parent %s" % parent |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 617 | self.gitStream.write("from %s\n" % parent) |
| 618 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 619 | for file in files: |
| 620 | path = file["path"] |
| 621 | if not path.startswith(branchPrefix): |
| 622 | # if not silent: |
| 623 | # print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path, branchPrefix, details["change"]) |
| 624 | continue |
| 625 | rev = file["rev"] |
| 626 | depotPath = path + "#" + rev |
| 627 | relPath = path[len(branchPrefix):] |
| 628 | action = file["action"] |
| 629 | |
| 630 | if file["type"] == "apple": |
| 631 | print "\nfile %s is a strange apple file that forks. Ignoring!" % path |
| 632 | continue |
| 633 | |
| 634 | if action == "delete": |
| 635 | self.gitStream.write("D %s\n" % relPath) |
| 636 | else: |
| 637 | mode = 644 |
| 638 | if file["type"].startswith("x"): |
| 639 | mode = 755 |
| 640 | |
| 641 | data = self.p4File(depotPath) |
| 642 | |
| 643 | self.gitStream.write("M %s inline %s\n" % (mode, relPath)) |
| 644 | self.gitStream.write("data %s\n" % len(data)) |
| 645 | self.gitStream.write(data) |
| 646 | self.gitStream.write("\n") |
| 647 | |
| 648 | self.gitStream.write("\n") |
| 649 | |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 650 | change = int(details["change"]) |
| 651 | |
Simon Hausmann | 9bda3a8 | 2007-05-19 12:05:40 +0200 | [diff] [blame] | 652 | if self.labels.has_key(change): |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 653 | label = self.labels[change] |
| 654 | labelDetails = label[0] |
| 655 | labelRevisions = label[1] |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 656 | if self.verbose: |
| 657 | print "Change %s is labelled %s" % (change, labelDetails) |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 658 | |
| 659 | files = p4CmdList("files %s...@%s" % (branchPrefix, change)) |
| 660 | |
| 661 | if len(files) == len(labelRevisions): |
| 662 | |
| 663 | cleanedFiles = {} |
| 664 | for info in files: |
| 665 | if info["action"] == "delete": |
| 666 | continue |
| 667 | cleanedFiles[info["depotFile"]] = info["rev"] |
| 668 | |
| 669 | if cleanedFiles == labelRevisions: |
| 670 | self.gitStream.write("tag tag_%s\n" % labelDetails["label"]) |
| 671 | self.gitStream.write("from %s\n" % branch) |
| 672 | |
| 673 | owner = labelDetails["Owner"] |
| 674 | tagger = "" |
| 675 | if author in self.users: |
| 676 | tagger = "%s %s %s" % (self.users[owner], epoch, self.tz) |
| 677 | else: |
| 678 | tagger = "%s <a@b> %s %s" % (owner, epoch, self.tz) |
| 679 | self.gitStream.write("tagger %s\n" % tagger) |
| 680 | self.gitStream.write("data <<EOT\n") |
| 681 | self.gitStream.write(labelDetails["Description"]) |
| 682 | self.gitStream.write("EOT\n\n") |
| 683 | |
| 684 | else: |
Simon Hausmann | a46668f | 2007-03-28 17:05:38 +0200 | [diff] [blame] | 685 | if not self.silent: |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 686 | print "Tag %s does not match with change %s: files do not match." % (labelDetails["label"], change) |
| 687 | |
| 688 | else: |
Simon Hausmann | a46668f | 2007-03-28 17:05:38 +0200 | [diff] [blame] | 689 | if not self.silent: |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 690 | print "Tag %s does not match with change %s: file count is different." % (labelDetails["label"], change) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 691 | |
Simon Hausmann | b607e71 | 2007-05-20 10:55:54 +0200 | [diff] [blame] | 692 | def getUserMapFromPerforceServer(self): |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 693 | self.users = {} |
| 694 | |
| 695 | for output in p4CmdList("users"): |
| 696 | if not output.has_key("User"): |
| 697 | continue |
| 698 | self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">" |
| 699 | |
Simon Hausmann | b607e71 | 2007-05-20 10:55:54 +0200 | [diff] [blame] | 700 | cache = open(gitdir + "/p4-usercache.txt", "wb") |
| 701 | for user in self.users.keys(): |
| 702 | cache.write("%s\t%s\n" % (user, self.users[user])) |
| 703 | cache.close(); |
| 704 | |
| 705 | def loadUserMapFromCache(self): |
| 706 | self.users = {} |
| 707 | try: |
| 708 | cache = open(gitdir + "/p4-usercache.txt", "rb") |
| 709 | lines = cache.readlines() |
| 710 | cache.close() |
| 711 | for line in lines: |
| 712 | entry = line[:-1].split("\t") |
| 713 | self.users[entry[0]] = entry[1] |
| 714 | except IOError: |
| 715 | self.getUserMapFromPerforceServer() |
| 716 | |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 717 | def getLabels(self): |
| 718 | self.labels = {} |
| 719 | |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 720 | l = p4CmdList("labels %s..." % self.depotPath) |
Simon Hausmann | 10c3211 | 2007-04-08 10:15:47 +0200 | [diff] [blame] | 721 | if len(l) > 0 and not self.silent: |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 722 | print "Finding files belonging to labels in %s" % self.depotPath |
Simon Hausmann | 01ce1fe | 2007-04-07 23:46:50 +0200 | [diff] [blame] | 723 | |
| 724 | for output in l: |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 725 | label = output["label"] |
| 726 | revisions = {} |
| 727 | newestChange = 0 |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 728 | if self.verbose: |
| 729 | print "Querying files for label %s" % label |
| 730 | for file in p4CmdList("files %s...@%s" % (self.depotPath, label)): |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 731 | revisions[file["depotFile"]] = file["rev"] |
| 732 | change = int(file["change"]) |
| 733 | if change > newestChange: |
| 734 | newestChange = change |
| 735 | |
Simon Hausmann | 9bda3a8 | 2007-05-19 12:05:40 +0200 | [diff] [blame] | 736 | self.labels[newestChange] = [output, revisions] |
| 737 | |
| 738 | if self.verbose: |
| 739 | print "Label changes: %s" % self.labels.keys() |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 740 | |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 741 | def getBranchMapping(self): |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 742 | self.projectName = self.depotPath[self.depotPath[:-1].rfind("/") + 1:] |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 743 | |
| 744 | for info in p4CmdList("branches"): |
| 745 | details = p4Cmd("branch -o %s" % info["branch"]) |
| 746 | viewIdx = 0 |
| 747 | while details.has_key("View%s" % viewIdx): |
| 748 | paths = details["View%s" % viewIdx].split(" ") |
| 749 | viewIdx = viewIdx + 1 |
| 750 | # require standard //depot/foo/... //depot/bar/... mapping |
| 751 | if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."): |
| 752 | continue |
| 753 | source = paths[0] |
| 754 | destination = paths[1] |
| 755 | if source.startswith(self.depotPath) and destination.startswith(self.depotPath): |
| 756 | source = source[len(self.depotPath):-4] |
| 757 | destination = destination[len(self.depotPath):-4] |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 758 | if destination not in self.knownBranches: |
| 759 | self.knownBranches[destination] = source |
| 760 | if source not in self.knownBranches: |
| 761 | self.knownBranches[source] = source |
| 762 | |
| 763 | def listExistingP4GitBranches(self): |
| 764 | self.p4BranchesInGit = [] |
| 765 | |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 766 | cmdline = "git rev-parse --symbolic " |
| 767 | if self.importIntoRemotes: |
| 768 | cmdline += " --remotes" |
| 769 | else: |
| 770 | cmdline += " --branches" |
| 771 | |
| 772 | for line in mypopen(cmdline).readlines(): |
Simon Hausmann | 5728405 | 2007-05-23 00:15:50 +0200 | [diff] [blame] | 773 | if self.importIntoRemotes and ((not line.startswith("p4/")) or line == "p4/HEAD\n"): |
| 774 | continue |
| 775 | if self.importIntoRemotes: |
| 776 | # strip off p4 |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 777 | branch = line[3:-1] |
Simon Hausmann | 5728405 | 2007-05-23 00:15:50 +0200 | [diff] [blame] | 778 | else: |
| 779 | branch = line[:-1] |
| 780 | self.p4BranchesInGit.append(branch) |
| 781 | self.initialParents[self.refPrefix + branch] = parseRevision(line[:-1]) |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 782 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 783 | def run(self, args): |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 784 | self.depotPath = "" |
Simon Hausmann | 179caeb | 2007-03-22 22:17:42 +0100 | [diff] [blame] | 785 | self.changeRange = "" |
| 786 | self.initialParent = "" |
Simon Hausmann | cd6cc0d | 2007-05-15 16:15:26 +0200 | [diff] [blame] | 787 | self.previousDepotPath = "" |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 788 | # map from branch depot path to parent branch |
| 789 | self.knownBranches = {} |
| 790 | self.initialParents = {} |
Simon Hausmann | 179caeb | 2007-03-22 22:17:42 +0100 | [diff] [blame] | 791 | |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 792 | if self.importIntoRemotes: |
| 793 | self.refPrefix = "refs/remotes/p4/" |
| 794 | else: |
Simon Hausmann | 5728405 | 2007-05-23 00:15:50 +0200 | [diff] [blame] | 795 | self.refPrefix = "refs/heads/" |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 796 | |
Simon Hausmann | faf1bd2 | 2007-05-21 10:05:30 +0200 | [diff] [blame] | 797 | createP4HeadRef = False; |
| 798 | |
Simon Hausmann | 5728405 | 2007-05-23 00:15:50 +0200 | [diff] [blame] | 799 | if self.syncWithOrigin and gitBranchExists("origin") and gitBranchExists(self.refPrefix + "master") and not self.detectBranches and self.importIntoRemotes: |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 800 | ### needs to be ported to multi branch import |
| 801 | |
Simon Hausmann | ef48f90 | 2007-05-17 22:17:49 +0200 | [diff] [blame] | 802 | print "Syncing with origin first as requested by calling git fetch origin" |
| 803 | system("git fetch origin") |
| 804 | [originPreviousDepotPath, originP4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("origin")) |
| 805 | [p4PreviousDepotPath, p4Change] = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit("p4")) |
| 806 | if len(originPreviousDepotPath) > 0 and len(originP4Change) > 0 and len(p4Change) > 0: |
| 807 | if originPreviousDepotPath == p4PreviousDepotPath: |
| 808 | originP4Change = int(originP4Change) |
| 809 | p4Change = int(p4Change) |
| 810 | if originP4Change > p4Change: |
| 811 | print "origin (%s) is newer than p4 (%s). Updating p4 branch from origin." % (originP4Change, p4Change) |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 812 | system("git update-ref " + self.refPrefix + "master origin"); |
Simon Hausmann | ef48f90 | 2007-05-17 22:17:49 +0200 | [diff] [blame] | 813 | else: |
| 814 | print "Cannot sync with origin. It was imported from %s while remotes/p4 was imported from %s" % (originPreviousDepotPath, p4PreviousDepotPath) |
| 815 | |
Simon Hausmann | 569d1bd | 2007-03-22 21:34:16 +0100 | [diff] [blame] | 816 | if len(self.branch) == 0: |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 817 | self.branch = self.refPrefix + "master" |
| 818 | if gitBranchExists("refs/heads/p4") and self.importIntoRemotes: |
Simon Hausmann | 48df6fd | 2007-05-17 21:18:53 +0200 | [diff] [blame] | 819 | system("git update-ref %s refs/heads/p4" % self.branch) |
Simon Hausmann | 48df6fd | 2007-05-17 21:18:53 +0200 | [diff] [blame] | 820 | system("git branch -D p4"); |
Simon Hausmann | faf1bd2 | 2007-05-21 10:05:30 +0200 | [diff] [blame] | 821 | # create it /after/ importing, when master exists |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 822 | if not gitBranchExists(self.refPrefix + "HEAD") and self.importIntoRemotes: |
Simon Hausmann | faf1bd2 | 2007-05-21 10:05:30 +0200 | [diff] [blame] | 823 | createP4HeadRef = True |
Simon Hausmann | 179caeb | 2007-03-22 22:17:42 +0100 | [diff] [blame] | 824 | |
Simon Hausmann | 33be3e6 | 2007-05-21 08:44:16 +0200 | [diff] [blame] | 825 | # this needs to be called after the conversion from heads/p4 to remotes/p4/master |
| 826 | self.listExistingP4GitBranches() |
| 827 | if len(self.p4BranchesInGit) > 1 and not self.silent: |
| 828 | print "Importing from/into multiple branches" |
| 829 | self.detectBranches = True |
| 830 | |
Simon Hausmann | 967f72e | 2007-03-23 09:30:41 +0100 | [diff] [blame] | 831 | if len(args) == 0: |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 832 | if not gitBranchExists(self.branch) and gitBranchExists("origin") and not self.detectBranches: |
| 833 | ### needs to be ported to multi branch import |
Simon Hausmann | 967f72e | 2007-03-23 09:30:41 +0100 | [diff] [blame] | 834 | if not self.silent: |
| 835 | print "Creating %s branch in git repository based on origin" % self.branch |
Simon Hausmann | 8ead4fd | 2007-05-17 20:26:58 +0200 | [diff] [blame] | 836 | branch = self.branch |
| 837 | if not branch.startswith("refs"): |
| 838 | branch = "refs/heads/" + branch |
| 839 | system("git update-ref %s origin" % branch) |
Simon Hausmann | 967f72e | 2007-03-23 09:30:41 +0100 | [diff] [blame] | 840 | |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 841 | if self.verbose: |
| 842 | print "branches: %s" % self.p4BranchesInGit |
| 843 | |
| 844 | p4Change = 0 |
| 845 | for branch in self.p4BranchesInGit: |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 846 | depotPath, change = extractDepotPathAndChangeFromGitLog(extractLogMessageFromGitCommit(self.refPrefix + branch)) |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 847 | |
| 848 | if self.verbose: |
| 849 | print "path %s change %s" % (depotPath, change) |
| 850 | |
| 851 | if len(depotPath) > 0 and len(change) > 0: |
| 852 | change = int(change) + 1 |
| 853 | p4Change = max(p4Change, change) |
| 854 | |
| 855 | if len(self.previousDepotPath) == 0: |
| 856 | self.previousDepotPath = depotPath |
| 857 | else: |
| 858 | i = 0 |
| 859 | l = min(len(self.previousDepotPath), len(depotPath)) |
| 860 | while i < l and self.previousDepotPath[i] == depotPath[i]: |
| 861 | i = i + 1 |
| 862 | self.previousDepotPath = self.previousDepotPath[:i] |
| 863 | |
| 864 | if p4Change > 0: |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 865 | self.depotPath = self.previousDepotPath |
Simon Hausmann | d590467 | 2007-05-19 11:07:32 +0200 | [diff] [blame] | 866 | self.changeRange = "@%s,#head" % p4Change |
Simon Hausmann | 463e8af | 2007-05-17 09:13:54 +0200 | [diff] [blame] | 867 | self.initialParent = parseRevision(self.branch) |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 868 | if not self.silent and not self.detectBranches: |
Simon Hausmann | 967f72e | 2007-03-23 09:30:41 +0100 | [diff] [blame] | 869 | print "Performing incremental import into %s git branch" % self.branch |
Simon Hausmann | 569d1bd | 2007-03-22 21:34:16 +0100 | [diff] [blame] | 870 | |
Simon Hausmann | f9162f6 | 2007-05-17 09:02:45 +0200 | [diff] [blame] | 871 | if not self.branch.startswith("refs/"): |
| 872 | self.branch = "refs/heads/" + self.branch |
Simon Hausmann | 179caeb | 2007-03-22 22:17:42 +0100 | [diff] [blame] | 873 | |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 874 | if len(self.depotPath) != 0: |
| 875 | self.depotPath = self.depotPath[:-1] |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 876 | |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 877 | if len(args) == 0 and len(self.depotPath) != 0: |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 878 | if not self.silent: |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 879 | print "Depot path: %s" % self.depotPath |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 880 | elif len(args) != 1: |
| 881 | return False |
| 882 | else: |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 883 | if len(self.depotPath) != 0 and self.depotPath != args[0]: |
| 884 | print "previous import used depot path %s and now %s was specified. this doesn't work!" % (self.depotPath, args[0]) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 885 | sys.exit(1) |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 886 | self.depotPath = args[0] |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 887 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 888 | self.revision = "" |
| 889 | self.users = {} |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 890 | |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 891 | if self.depotPath.find("@") != -1: |
| 892 | atIdx = self.depotPath.index("@") |
| 893 | self.changeRange = self.depotPath[atIdx:] |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 894 | if self.changeRange == "@all": |
| 895 | self.changeRange = "" |
| 896 | elif self.changeRange.find(",") == -1: |
| 897 | self.revision = self.changeRange |
| 898 | self.changeRange = "" |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 899 | self.depotPath = self.depotPath[0:atIdx] |
| 900 | elif self.depotPath.find("#") != -1: |
| 901 | hashIdx = self.depotPath.index("#") |
| 902 | self.revision = self.depotPath[hashIdx:] |
| 903 | self.depotPath = self.depotPath[0:hashIdx] |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 904 | elif len(self.previousDepotPath) == 0: |
| 905 | self.revision = "#head" |
| 906 | |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 907 | if self.depotPath.endswith("..."): |
| 908 | self.depotPath = self.depotPath[:-3] |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 909 | |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 910 | if not self.depotPath.endswith("/"): |
| 911 | self.depotPath += "/" |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 912 | |
Simon Hausmann | b607e71 | 2007-05-20 10:55:54 +0200 | [diff] [blame] | 913 | self.loadUserMapFromCache() |
Simon Hausmann | cb53e1f | 2007-04-08 00:12:02 +0200 | [diff] [blame] | 914 | self.labels = {} |
| 915 | if self.detectLabels: |
| 916 | self.getLabels(); |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 917 | |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 918 | if self.detectBranches: |
| 919 | self.getBranchMapping(); |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 920 | if self.verbose: |
| 921 | print "p4-git branches: %s" % self.p4BranchesInGit |
| 922 | print "initial parents: %s" % self.initialParents |
| 923 | for b in self.p4BranchesInGit: |
| 924 | if b != "master": |
| 925 | b = b[len(self.projectName):] |
| 926 | self.createdBranches.add(b) |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 927 | |
Simon Hausmann | f291b4e | 2007-04-14 11:21:50 +0200 | [diff] [blame] | 928 | self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60)) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 929 | |
Simon Hausmann | 0848358 | 2007-05-15 14:31:06 +0200 | [diff] [blame] | 930 | importProcess = subprocess.Popen(["git", "fast-import"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE); |
| 931 | self.gitOutput = importProcess.stdout |
| 932 | self.gitStream = importProcess.stdin |
| 933 | self.gitError = importProcess.stderr |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 934 | |
| 935 | if len(self.revision) > 0: |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 936 | print "Doing initial import of %s from revision %s" % (self.depotPath, self.revision) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 937 | |
| 938 | details = { "user" : "git perforce import user", "time" : int(time.time()) } |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 939 | details["desc"] = "Initial import of %s from the state at revision %s" % (self.depotPath, self.revision) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 940 | details["change"] = self.revision |
| 941 | newestRevision = 0 |
| 942 | |
| 943 | fileCnt = 0 |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 944 | for info in p4CmdList("files %s...%s" % (self.depotPath, self.revision)): |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 945 | change = int(info["change"]) |
| 946 | if change > newestRevision: |
| 947 | newestRevision = change |
| 948 | |
| 949 | if info["action"] == "delete": |
Simon Hausmann | c45b1cf | 2007-04-08 10:13:32 +0200 | [diff] [blame] | 950 | # don't increase the file cnt, otherwise details["depotFile123"] will have gaps! |
| 951 | #fileCnt = fileCnt + 1 |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 952 | continue |
| 953 | |
| 954 | for prop in [ "depotFile", "rev", "action", "type" ]: |
| 955 | details["%s%s" % (prop, fileCnt)] = info[prop] |
| 956 | |
| 957 | fileCnt = fileCnt + 1 |
| 958 | |
| 959 | details["change"] = newestRevision |
| 960 | |
| 961 | try: |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 962 | self.commit(details, self.extractFilesFromCommit(details), self.branch, self.depotPath) |
Simon Hausmann | c715706 | 2007-03-20 21:13:49 +0100 | [diff] [blame] | 963 | except IOError: |
Simon Hausmann | fd4ca86 | 2007-04-13 22:21:10 +0200 | [diff] [blame] | 964 | print "IO error with git fast-import. Is your git version recent enough?" |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 965 | print self.gitError.read() |
| 966 | |
| 967 | else: |
| 968 | changes = [] |
| 969 | |
Simon Hausmann | 0828ab1 | 2007-03-20 20:59:30 +0100 | [diff] [blame] | 970 | if len(self.changesFile) > 0: |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 971 | output = open(self.changesFile).readlines() |
| 972 | changeSet = Set() |
| 973 | for line in output: |
| 974 | changeSet.add(int(line)) |
| 975 | |
| 976 | for change in changeSet: |
| 977 | changes.append(change) |
| 978 | |
| 979 | changes.sort() |
| 980 | else: |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 981 | if self.verbose: |
| 982 | print "Getting p4 changes for %s...%s" % (self.depotPath, self.changeRange) |
Simon Hausmann | caace11 | 2007-05-15 14:57:57 +0200 | [diff] [blame] | 983 | output = mypopen("p4 changes %s...%s" % (self.depotPath, self.changeRange)).readlines() |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 984 | |
| 985 | for line in output: |
| 986 | changeNum = line.split(" ")[1] |
| 987 | changes.append(changeNum) |
| 988 | |
| 989 | changes.reverse() |
| 990 | |
Simon Hausmann | 01a9c9c | 2007-05-23 00:07:35 +0200 | [diff] [blame] | 991 | if len(self.maxChanges) > 0: |
| 992 | changes = changes[0:min(int(self.maxChanges), len(changes))] |
| 993 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 994 | if len(changes) == 0: |
Simon Hausmann | 0828ab1 | 2007-03-20 20:59:30 +0100 | [diff] [blame] | 995 | if not self.silent: |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 996 | print "No changes to import!" |
Simon Hausmann | 1f52af6 | 2007-04-08 00:07:02 +0200 | [diff] [blame] | 997 | return True |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 998 | |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 999 | self.updatedBranches = set() |
| 1000 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1001 | cnt = 1 |
| 1002 | for change in changes: |
| 1003 | description = p4Cmd("describe %s" % change) |
| 1004 | |
Simon Hausmann | 0828ab1 | 2007-03-20 20:59:30 +0100 | [diff] [blame] | 1005 | if not self.silent: |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 1006 | sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes))) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1007 | sys.stdout.flush() |
| 1008 | cnt = cnt + 1 |
| 1009 | |
| 1010 | try: |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1011 | if self.detectBranches: |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 1012 | branches = self.splitFilesIntoBranches(description) |
Simon Hausmann | d590467 | 2007-05-19 11:07:32 +0200 | [diff] [blame] | 1013 | for branch in branches.keys(): |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 1014 | branchPrefix = self.depotPath + branch + "/" |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1015 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1016 | parent = "" |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1017 | |
Simon Hausmann | d590467 | 2007-05-19 11:07:32 +0200 | [diff] [blame] | 1018 | filesForCommit = branches[branch] |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1019 | |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1020 | if self.verbose: |
| 1021 | print "branch is %s" % branch |
| 1022 | |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 1023 | self.updatedBranches.add(branch) |
| 1024 | |
Simon Hausmann | 8f9b2e0 | 2007-05-18 22:13:26 +0200 | [diff] [blame] | 1025 | if branch not in self.createdBranches: |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1026 | self.createdBranches.add(branch) |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1027 | parent = self.knownBranches[branch] |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1028 | if parent == branch: |
| 1029 | parent = "" |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1030 | elif self.verbose: |
| 1031 | print "parent determined through known branches: %s" % parent |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1032 | |
Simon Hausmann | 8f9b2e0 | 2007-05-18 22:13:26 +0200 | [diff] [blame] | 1033 | # main branch? use master |
| 1034 | if branch == "main": |
| 1035 | branch = "master" |
| 1036 | else: |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1037 | branch = self.projectName + branch |
Simon Hausmann | 8f9b2e0 | 2007-05-18 22:13:26 +0200 | [diff] [blame] | 1038 | |
| 1039 | if parent == "main": |
| 1040 | parent = "master" |
| 1041 | elif len(parent) > 0: |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1042 | parent = self.projectName + parent |
Simon Hausmann | 8f9b2e0 | 2007-05-18 22:13:26 +0200 | [diff] [blame] | 1043 | |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 1044 | branch = self.refPrefix + branch |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1045 | if len(parent) > 0: |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 1046 | parent = self.refPrefix + parent |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1047 | |
| 1048 | if self.verbose: |
| 1049 | print "looking for initial parent for %s; current parent is %s" % (branch, parent) |
| 1050 | |
| 1051 | if len(parent) == 0 and branch in self.initialParents: |
| 1052 | parent = self.initialParents[branch] |
| 1053 | del self.initialParents[branch] |
| 1054 | |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 1055 | self.commit(description, filesForCommit, branch, branchPrefix, parent) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1056 | else: |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 1057 | files = self.extractFilesFromCommit(description) |
Simon Hausmann | 8f87253 | 2007-05-01 23:23:00 +0200 | [diff] [blame] | 1058 | self.commit(description, files, self.branch, self.depotPath, self.initialParent) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1059 | self.initialParent = "" |
| 1060 | except IOError: |
| 1061 | print self.gitError.read() |
| 1062 | sys.exit(1) |
| 1063 | |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 1064 | if not self.silent: |
| 1065 | print "" |
| 1066 | if len(self.updatedBranches) > 0: |
| 1067 | sys.stdout.write("Updated branches: ") |
| 1068 | for b in self.updatedBranches: |
| 1069 | sys.stdout.write("%s " % b) |
| 1070 | sys.stdout.write("\n") |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1071 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1072 | |
| 1073 | self.gitStream.close() |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1074 | if importProcess.wait() != 0: |
| 1075 | die("fast-import failed: %s" % self.gitError.read()) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1076 | self.gitOutput.close() |
| 1077 | self.gitError.close() |
| 1078 | |
Simon Hausmann | faf1bd2 | 2007-05-21 10:05:30 +0200 | [diff] [blame] | 1079 | if createP4HeadRef: |
Simon Hausmann | 65d2ade | 2007-05-23 16:41:46 +0200 | [diff] [blame] | 1080 | system("git symbolic-ref %sHEAD %s" % (self.refPrefix, self.branch)) |
Simon Hausmann | faf1bd2 | 2007-05-21 10:05:30 +0200 | [diff] [blame] | 1081 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1082 | return True |
| 1083 | |
Simon Hausmann | 01ce1fe | 2007-04-07 23:46:50 +0200 | [diff] [blame] | 1084 | class P4Rebase(Command): |
| 1085 | def __init__(self): |
| 1086 | Command.__init__(self) |
Simon Hausmann | ef48f90 | 2007-05-17 22:17:49 +0200 | [diff] [blame] | 1087 | self.options = [ optparse.make_option("--with-origin", dest="syncWithOrigin", action="store_true") ] |
Simon Hausmann | 01ce1fe | 2007-04-07 23:46:50 +0200 | [diff] [blame] | 1088 | self.description = "Fetches the latest revision from perforce and rebases the current work (branch) against it" |
Simon Hausmann | ef48f90 | 2007-05-17 22:17:49 +0200 | [diff] [blame] | 1089 | self.syncWithOrigin = False |
Simon Hausmann | 01ce1fe | 2007-04-07 23:46:50 +0200 | [diff] [blame] | 1090 | |
| 1091 | def run(self, args): |
| 1092 | sync = P4Sync() |
Simon Hausmann | ef48f90 | 2007-05-17 22:17:49 +0200 | [diff] [blame] | 1093 | sync.syncWithOrigin = self.syncWithOrigin |
Simon Hausmann | 01ce1fe | 2007-04-07 23:46:50 +0200 | [diff] [blame] | 1094 | sync.run([]) |
| 1095 | print "Rebasing the current branch" |
Simon Hausmann | caace11 | 2007-05-15 14:57:57 +0200 | [diff] [blame] | 1096 | oldHead = mypopen("git rev-parse HEAD").read()[:-1] |
Simon Hausmann | 01ce1fe | 2007-04-07 23:46:50 +0200 | [diff] [blame] | 1097 | system("git rebase p4") |
Simon Hausmann | 1f52af6 | 2007-04-08 00:07:02 +0200 | [diff] [blame] | 1098 | system("git diff-tree --stat --summary -M %s HEAD" % oldHead) |
Simon Hausmann | 01ce1fe | 2007-04-07 23:46:50 +0200 | [diff] [blame] | 1099 | return True |
| 1100 | |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1101 | class P4Clone(P4Sync): |
| 1102 | def __init__(self): |
| 1103 | P4Sync.__init__(self) |
| 1104 | self.description = "Creates a new git repository and imports from Perforce into it" |
| 1105 | self.usage = "usage: %prog [options] //depot/path[@revRange] [directory]" |
| 1106 | self.needsGit = False |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1107 | |
| 1108 | def run(self, args): |
Simon Hausmann | 59fa417 | 2007-05-20 15:15:34 +0200 | [diff] [blame] | 1109 | global gitdir |
| 1110 | |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1111 | if len(args) < 1: |
| 1112 | return False |
| 1113 | depotPath = args[0] |
| 1114 | dir = "" |
| 1115 | if len(args) == 2: |
| 1116 | dir = args[1] |
| 1117 | elif len(args) > 2: |
| 1118 | return False |
| 1119 | |
| 1120 | if not depotPath.startswith("//"): |
| 1121 | return False |
| 1122 | |
| 1123 | if len(dir) == 0: |
| 1124 | dir = depotPath |
| 1125 | atPos = dir.rfind("@") |
| 1126 | if atPos != -1: |
| 1127 | dir = dir[0:atPos] |
| 1128 | hashPos = dir.rfind("#") |
| 1129 | if hashPos != -1: |
| 1130 | dir = dir[0:hashPos] |
| 1131 | |
| 1132 | if dir.endswith("..."): |
| 1133 | dir = dir[:-3] |
| 1134 | |
| 1135 | if dir.endswith("/"): |
| 1136 | dir = dir[:-1] |
| 1137 | |
| 1138 | slashPos = dir.rfind("/") |
| 1139 | if slashPos != -1: |
| 1140 | dir = dir[slashPos + 1:] |
| 1141 | |
| 1142 | print "Importing from %s into %s" % (depotPath, dir) |
| 1143 | os.makedirs(dir) |
| 1144 | os.chdir(dir) |
| 1145 | system("git init") |
Simon Hausmann | 64ffb06 | 2007-05-20 15:24:01 +0200 | [diff] [blame] | 1146 | gitdir = os.getcwd() + "/.git" |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1147 | if not P4Sync.run(self, [depotPath]): |
| 1148 | return False |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1149 | if self.branch != "master": |
Simon Hausmann | 8f9b2e0 | 2007-05-18 22:13:26 +0200 | [diff] [blame] | 1150 | if gitBranchExists("refs/remotes/p4/master"): |
| 1151 | system("git branch master refs/remotes/p4/master") |
| 1152 | system("git checkout -f") |
| 1153 | else: |
| 1154 | print "Could not detect main branch. No checkout/master branch created." |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1155 | return True |
| 1156 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1157 | class HelpFormatter(optparse.IndentedHelpFormatter): |
| 1158 | def __init__(self): |
| 1159 | optparse.IndentedHelpFormatter.__init__(self) |
| 1160 | |
| 1161 | def format_description(self, description): |
| 1162 | if description: |
| 1163 | return description + "\n" |
| 1164 | else: |
| 1165 | return "" |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1166 | |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 1167 | def printUsage(commands): |
| 1168 | print "usage: %s <command> [options]" % sys.argv[0] |
| 1169 | print "" |
| 1170 | print "valid commands: %s" % ", ".join(commands) |
| 1171 | print "" |
| 1172 | print "Try %s <command> --help for command specific help." % sys.argv[0] |
| 1173 | print "" |
| 1174 | |
| 1175 | commands = { |
| 1176 | "debug" : P4Debug(), |
Simon Hausmann | 711544b | 2007-04-01 15:40:46 +0200 | [diff] [blame] | 1177 | "submit" : P4Submit(), |
Simon Hausmann | 01ce1fe | 2007-04-07 23:46:50 +0200 | [diff] [blame] | 1178 | "sync" : P4Sync(), |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1179 | "rebase" : P4Rebase(), |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 1180 | "clone" : P4Clone(), |
| 1181 | "rollback" : P4RollBack() |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | if len(sys.argv[1:]) == 0: |
| 1185 | printUsage(commands.keys()) |
| 1186 | sys.exit(2) |
| 1187 | |
| 1188 | cmd = "" |
| 1189 | cmdName = sys.argv[1] |
| 1190 | try: |
| 1191 | cmd = commands[cmdName] |
| 1192 | except KeyError: |
| 1193 | print "unknown command %s" % cmdName |
| 1194 | print "" |
| 1195 | printUsage(commands.keys()) |
| 1196 | sys.exit(2) |
| 1197 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1198 | options = cmd.options |
| 1199 | cmd.gitdir = gitdir |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1200 | |
Simon Hausmann | e20a9e5 | 2007-03-26 00:13:51 +0200 | [diff] [blame] | 1201 | args = sys.argv[2:] |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 1202 | |
Simon Hausmann | e20a9e5 | 2007-03-26 00:13:51 +0200 | [diff] [blame] | 1203 | if len(options) > 0: |
| 1204 | options.append(optparse.make_option("--git-dir", dest="gitdir")) |
| 1205 | |
| 1206 | parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName), |
| 1207 | options, |
| 1208 | description = cmd.description, |
| 1209 | formatter = HelpFormatter()) |
| 1210 | |
| 1211 | (cmd, args) = parser.parse_args(sys.argv[2:], cmd); |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 1212 | |
Simon Hausmann | 8910ac0 | 2007-03-26 08:18:55 +0200 | [diff] [blame] | 1213 | if cmd.needsGit: |
| 1214 | gitdir = cmd.gitdir |
| 1215 | if len(gitdir) == 0: |
| 1216 | gitdir = ".git" |
| 1217 | if not isValidGitDir(gitdir): |
Simon Hausmann | 81f2373 | 2007-05-15 23:06:43 +0200 | [diff] [blame] | 1218 | gitdir = mypopen("git rev-parse --git-dir").read()[:-1] |
Simon Hausmann | dc1a93b | 2007-05-16 12:12:39 +0200 | [diff] [blame] | 1219 | if os.path.exists(gitdir): |
Simon Hausmann | 5c4153e | 2007-05-17 07:42:38 +0200 | [diff] [blame] | 1220 | cdup = mypopen("git rev-parse --show-cdup").read()[:-1]; |
| 1221 | if len(cdup) > 0: |
| 1222 | os.chdir(cdup); |
Simon Hausmann | 8910ac0 | 2007-03-26 08:18:55 +0200 | [diff] [blame] | 1223 | |
Simon Hausmann | 2061865 | 2007-03-21 13:05:30 +0100 | [diff] [blame] | 1224 | if not isValidGitDir(gitdir): |
Simon Hausmann | 8910ac0 | 2007-03-26 08:18:55 +0200 | [diff] [blame] | 1225 | if isValidGitDir(gitdir + "/.git"): |
| 1226 | gitdir += "/.git" |
| 1227 | else: |
| 1228 | die("fatal: cannot locate git repository at %s" % gitdir) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1229 | |
Simon Hausmann | 8910ac0 | 2007-03-26 08:18:55 +0200 | [diff] [blame] | 1230 | os.environ["GIT_DIR"] = gitdir |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1231 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1232 | if not cmd.run(args): |
| 1233 | parser.print_help() |
| 1234 | |