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 | # |
Simon Hausmann | c8cbbee | 2007-05-28 14:43:25 +0200 | [diff] [blame] | 5 | # Author: Simon Hausmann <simon@lst.de> |
| 6 | # Copyright: 2007 Simon Hausmann <simon@lst.de> |
Simon Hausmann | 83dce55 | 2007-03-19 22:26:36 +0100 | [diff] [blame] | 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 | # |
| 10 | |
Simon Hausmann | 0848358 | 2007-05-15 14:31:06 +0200 | [diff] [blame] | 11 | import optparse, sys, os, marshal, popen2, subprocess, shelve |
Simon Hausmann | 25df95c | 2007-05-15 15:15:39 +0200 | [diff] [blame] | 12 | import tempfile, getopt, sha, os.path, time, platform |
Han-Wen Nienhuys | ce6f33c | 2007-05-23 16:46:29 -0300 | [diff] [blame] | 13 | import re |
Han-Wen Nienhuys | 8b41a97 | 2007-05-23 18:20:53 -0300 | [diff] [blame] | 14 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 15 | from sets import Set; |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 16 | |
Han-Wen Nienhuys | 4addad2 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 17 | verbose = False |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 18 | |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 19 | def die(msg): |
| 20 | if verbose: |
| 21 | raise Exception(msg) |
| 22 | else: |
| 23 | sys.stderr.write(msg + "\n") |
| 24 | sys.exit(1) |
| 25 | |
Han-Wen Nienhuys | bce4c5f | 2007-05-23 17:14:33 -0300 | [diff] [blame] | 26 | def write_pipe(c, str): |
Han-Wen Nienhuys | 4addad2 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 27 | if verbose: |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 28 | sys.stderr.write('Writing pipe: %s\n' % c) |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 29 | |
Han-Wen Nienhuys | bce4c5f | 2007-05-23 17:14:33 -0300 | [diff] [blame] | 30 | pipe = os.popen(c, 'w') |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 31 | val = pipe.write(str) |
Han-Wen Nienhuys | bce4c5f | 2007-05-23 17:14:33 -0300 | [diff] [blame] | 32 | if pipe.close(): |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 33 | die('Command failed: %s' % c) |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 34 | |
| 35 | return val |
| 36 | |
Han-Wen Nienhuys | 4addad2 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 37 | def read_pipe(c, ignore_error=False): |
| 38 | if verbose: |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 39 | sys.stderr.write('Reading pipe: %s\n' % c) |
Han-Wen Nienhuys | 8b41a97 | 2007-05-23 18:20:53 -0300 | [diff] [blame] | 40 | |
Han-Wen Nienhuys | bce4c5f | 2007-05-23 17:14:33 -0300 | [diff] [blame] | 41 | pipe = os.popen(c, 'rb') |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 42 | val = pipe.read() |
Han-Wen Nienhuys | 4addad2 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 43 | if pipe.close() and not ignore_error: |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 44 | die('Command failed: %s' % c) |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 45 | |
| 46 | return val |
| 47 | |
| 48 | |
Han-Wen Nienhuys | bce4c5f | 2007-05-23 17:14:33 -0300 | [diff] [blame] | 49 | def read_pipe_lines(c): |
Han-Wen Nienhuys | 4addad2 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 50 | if verbose: |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 51 | sys.stderr.write('Reading pipe: %s\n' % c) |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 52 | ## todo: check return status |
Han-Wen Nienhuys | bce4c5f | 2007-05-23 17:14:33 -0300 | [diff] [blame] | 53 | pipe = os.popen(c, 'rb') |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 54 | val = pipe.readlines() |
Han-Wen Nienhuys | bce4c5f | 2007-05-23 17:14:33 -0300 | [diff] [blame] | 55 | if pipe.close(): |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 56 | die('Command failed: %s' % c) |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 57 | |
| 58 | return val |
Simon Hausmann | caace11 | 2007-05-15 14:57:57 +0200 | [diff] [blame] | 59 | |
Han-Wen Nienhuys | 6754a29 | 2007-05-23 17:41:50 -0300 | [diff] [blame] | 60 | def system(cmd): |
Han-Wen Nienhuys | 4addad2 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 61 | if verbose: |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 62 | sys.stderr.write("executing %s\n" % cmd) |
Han-Wen Nienhuys | 6754a29 | 2007-05-23 17:41:50 -0300 | [diff] [blame] | 63 | if os.system(cmd) != 0: |
| 64 | die("command failed: %s" % cmd) |
| 65 | |
Scott Lamb | 9f90c73 | 2007-07-15 20:58:10 -0700 | [diff] [blame] | 66 | def p4CmdList(cmd, stdin=None, stdin_mode='w+b'): |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 67 | cmd = "p4 -G %s" % cmd |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 68 | if verbose: |
| 69 | sys.stderr.write("Opening pipe: %s\n" % cmd) |
Scott Lamb | 9f90c73 | 2007-07-15 20:58:10 -0700 | [diff] [blame] | 70 | |
| 71 | # Use a temporary file to avoid deadlocks without |
| 72 | # subprocess.communicate(), which would put another copy |
| 73 | # of stdout into memory. |
| 74 | stdin_file = None |
| 75 | if stdin is not None: |
| 76 | stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode) |
| 77 | stdin_file.write(stdin) |
| 78 | stdin_file.flush() |
| 79 | stdin_file.seek(0) |
| 80 | |
| 81 | p4 = subprocess.Popen(cmd, shell=True, |
| 82 | stdin=stdin_file, |
| 83 | stdout=subprocess.PIPE) |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 84 | |
| 85 | result = [] |
| 86 | try: |
| 87 | while True: |
Scott Lamb | 9f90c73 | 2007-07-15 20:58:10 -0700 | [diff] [blame] | 88 | entry = marshal.load(p4.stdout) |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 89 | result.append(entry) |
| 90 | except EOFError: |
| 91 | pass |
Scott Lamb | 9f90c73 | 2007-07-15 20:58:10 -0700 | [diff] [blame] | 92 | exitCode = p4.wait() |
| 93 | if exitCode != 0: |
Simon Hausmann | ac3e0d7 | 2007-05-23 23:32:32 +0200 | [diff] [blame] | 94 | entry = {} |
| 95 | entry["p4ExitCode"] = exitCode |
| 96 | result.append(entry) |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 97 | |
| 98 | return result |
| 99 | |
| 100 | def p4Cmd(cmd): |
| 101 | list = p4CmdList(cmd) |
| 102 | result = {} |
| 103 | for entry in list: |
| 104 | result.update(entry) |
| 105 | return result; |
| 106 | |
Simon Hausmann | cb2c9db | 2007-03-24 09:15:11 +0100 | [diff] [blame] | 107 | def p4Where(depotPath): |
| 108 | if not depotPath.endswith("/"): |
| 109 | depotPath += "/" |
| 110 | output = p4Cmd("where %s..." % depotPath) |
Simon Hausmann | dc52403 | 2007-05-21 09:34:56 +0200 | [diff] [blame] | 111 | if output["code"] == "error": |
| 112 | return "" |
Simon Hausmann | cb2c9db | 2007-03-24 09:15:11 +0100 | [diff] [blame] | 113 | clientPath = "" |
| 114 | if "path" in output: |
| 115 | clientPath = output.get("path") |
| 116 | elif "data" in output: |
| 117 | data = output.get("data") |
| 118 | lastSpace = data.rfind(" ") |
| 119 | clientPath = data[lastSpace + 1:] |
| 120 | |
| 121 | if clientPath.endswith("..."): |
| 122 | clientPath = clientPath[:-3] |
| 123 | return clientPath |
| 124 | |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 125 | def currentGitBranch(): |
Han-Wen Nienhuys | b25b206 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 126 | return read_pipe("git name-rev HEAD").split(" ")[1].strip() |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 127 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 128 | def isValidGitDir(path): |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 129 | if (os.path.exists(path + "/HEAD") |
| 130 | and os.path.exists(path + "/refs") and os.path.exists(path + "/objects")): |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 131 | return True; |
| 132 | return False |
| 133 | |
Simon Hausmann | 463e8af | 2007-05-17 09:13:54 +0200 | [diff] [blame] | 134 | def parseRevision(ref): |
Han-Wen Nienhuys | b25b206 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 135 | return read_pipe("git rev-parse %s" % ref).strip() |
Simon Hausmann | 463e8af | 2007-05-17 09:13:54 +0200 | [diff] [blame] | 136 | |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 137 | def extractLogMessageFromGitCommit(commit): |
| 138 | logMessage = "" |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 139 | |
| 140 | ## fixme: title is first line of commit, not 1st paragraph. |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 141 | foundTitle = False |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 142 | for log in read_pipe_lines("git cat-file commit %s" % commit): |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 143 | if not foundTitle: |
| 144 | if len(log) == 1: |
Simon Hausmann | 1c09418 | 2007-05-01 23:15:48 +0200 | [diff] [blame] | 145 | foundTitle = True |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 146 | continue |
| 147 | |
| 148 | logMessage += log |
| 149 | return logMessage |
| 150 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 151 | def extractSettingsGitLog(log): |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 152 | values = {} |
| 153 | for line in log.split("\n"): |
| 154 | line = line.strip() |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 155 | m = re.search (r"^ *\[git-p4: (.*)\]$", line) |
| 156 | if not m: |
| 157 | continue |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 158 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 159 | assignments = m.group(1).split (':') |
| 160 | for a in assignments: |
| 161 | vals = a.split ('=') |
| 162 | key = vals[0].strip() |
| 163 | val = ('='.join (vals[1:])).strip() |
| 164 | if val.endswith ('\"') and val.startswith('"'): |
| 165 | val = val[1:-1] |
| 166 | |
| 167 | values[key] = val |
| 168 | |
Simon Hausmann | 845b42c | 2007-06-07 09:19:34 +0200 | [diff] [blame] | 169 | paths = values.get("depot-paths") |
| 170 | if not paths: |
| 171 | paths = values.get("depot-path") |
Simon Hausmann | a3fdd57 | 2007-06-07 22:54:32 +0200 | [diff] [blame] | 172 | if paths: |
| 173 | values['depot-paths'] = paths.split(',') |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 174 | return values |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 175 | |
Simon Hausmann | 8136a63 | 2007-03-22 21:27:14 +0100 | [diff] [blame] | 176 | def gitBranchExists(branch): |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 177 | proc = subprocess.Popen(["git", "rev-parse", branch], |
| 178 | stderr=subprocess.PIPE, stdout=subprocess.PIPE); |
Simon Hausmann | caace11 | 2007-05-15 14:57:57 +0200 | [diff] [blame] | 179 | return proc.wait() == 0; |
Simon Hausmann | 8136a63 | 2007-03-22 21:27:14 +0100 | [diff] [blame] | 180 | |
Simon Hausmann | 0126510 | 2007-05-25 10:36:10 +0200 | [diff] [blame] | 181 | def gitConfig(key): |
Han-Wen Nienhuys | 4addad2 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 182 | return read_pipe("git config %s" % key, ignore_error=True).strip() |
Simon Hausmann | 0126510 | 2007-05-25 10:36:10 +0200 | [diff] [blame] | 183 | |
Simon Hausmann | 062410b | 2007-07-18 10:56:31 +0200 | [diff] [blame] | 184 | def p4BranchesInGit(branchesAreInRemotes = True): |
| 185 | branches = {} |
| 186 | |
| 187 | cmdline = "git rev-parse --symbolic " |
| 188 | if branchesAreInRemotes: |
| 189 | cmdline += " --remotes" |
| 190 | else: |
| 191 | cmdline += " --branches" |
| 192 | |
| 193 | for line in read_pipe_lines(cmdline): |
| 194 | line = line.strip() |
| 195 | |
| 196 | ## only import to p4/ |
| 197 | if not line.startswith('p4/') or line == "p4/HEAD": |
| 198 | continue |
| 199 | branch = line |
| 200 | |
| 201 | # strip off p4 |
| 202 | branch = re.sub ("^p4/", "", line) |
| 203 | |
| 204 | branches[branch] = parseRevision(line) |
| 205 | return branches |
| 206 | |
Simon Hausmann | 9ceab36 | 2007-06-22 00:01:57 +0200 | [diff] [blame] | 207 | def findUpstreamBranchPoint(head = "HEAD"): |
Simon Hausmann | 86506fe | 2007-07-18 12:40:12 +0200 | [diff] [blame] | 208 | branches = p4BranchesInGit() |
| 209 | # map from depot-path to branch name |
| 210 | branchByDepotPath = {} |
| 211 | for branch in branches.keys(): |
| 212 | tip = branches[branch] |
| 213 | log = extractLogMessageFromGitCommit(tip) |
| 214 | settings = extractSettingsGitLog(log) |
| 215 | if settings.has_key("depot-paths"): |
| 216 | paths = ",".join(settings["depot-paths"]) |
| 217 | branchByDepotPath[paths] = "remotes/p4/" + branch |
| 218 | |
Simon Hausmann | 27d2d81 | 2007-06-12 14:31:59 +0200 | [diff] [blame] | 219 | settings = None |
Simon Hausmann | 27d2d81 | 2007-06-12 14:31:59 +0200 | [diff] [blame] | 220 | parent = 0 |
| 221 | while parent < 65535: |
Simon Hausmann | 9ceab36 | 2007-06-22 00:01:57 +0200 | [diff] [blame] | 222 | commit = head + "~%s" % parent |
Simon Hausmann | 27d2d81 | 2007-06-12 14:31:59 +0200 | [diff] [blame] | 223 | log = extractLogMessageFromGitCommit(commit) |
| 224 | settings = extractSettingsGitLog(log) |
Simon Hausmann | 86506fe | 2007-07-18 12:40:12 +0200 | [diff] [blame] | 225 | if settings.has_key("depot-paths"): |
| 226 | paths = ",".join(settings["depot-paths"]) |
| 227 | if branchByDepotPath.has_key(paths): |
| 228 | return [branchByDepotPath[paths], settings] |
Simon Hausmann | 27d2d81 | 2007-06-12 14:31:59 +0200 | [diff] [blame] | 229 | |
Simon Hausmann | 86506fe | 2007-07-18 12:40:12 +0200 | [diff] [blame] | 230 | parent = parent + 1 |
Simon Hausmann | 27d2d81 | 2007-06-12 14:31:59 +0200 | [diff] [blame] | 231 | |
Simon Hausmann | 86506fe | 2007-07-18 12:40:12 +0200 | [diff] [blame] | 232 | return ["", settings] |
Simon Hausmann | 27d2d81 | 2007-06-12 14:31:59 +0200 | [diff] [blame] | 233 | |
Simon Hausmann | 5ca4461 | 2007-08-24 17:44:16 +0200 | [diff] [blame^] | 234 | def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True): |
| 235 | if not silent: |
| 236 | print ("Creating/updating branch(es) in %s based on origin branch(es)" |
| 237 | % localRefPrefix) |
| 238 | |
| 239 | originPrefix = "origin/p4/" |
| 240 | |
| 241 | for line in read_pipe_lines("git rev-parse --symbolic --remotes"): |
| 242 | line = line.strip() |
| 243 | if (not line.startswith(originPrefix)) or line.endswith("HEAD"): |
| 244 | continue |
| 245 | |
| 246 | headName = line[len(originPrefix):] |
| 247 | remoteHead = localRefPrefix + headName |
| 248 | originHead = line |
| 249 | |
| 250 | original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead)) |
| 251 | if (not original.has_key('depot-paths') |
| 252 | or not original.has_key('change')): |
| 253 | continue |
| 254 | |
| 255 | update = False |
| 256 | if not gitBranchExists(remoteHead): |
| 257 | if verbose: |
| 258 | print "creating %s" % remoteHead |
| 259 | update = True |
| 260 | else: |
| 261 | settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead)) |
| 262 | if settings.has_key('change') > 0: |
| 263 | if settings['depot-paths'] == original['depot-paths']: |
| 264 | originP4Change = int(original['change']) |
| 265 | p4Change = int(settings['change']) |
| 266 | if originP4Change > p4Change: |
| 267 | print ("%s (%s) is newer than %s (%s). " |
| 268 | "Updating p4 branch from origin." |
| 269 | % (originHead, originP4Change, |
| 270 | remoteHead, p4Change)) |
| 271 | update = True |
| 272 | else: |
| 273 | print ("Ignoring: %s was imported from %s while " |
| 274 | "%s was imported from %s" |
| 275 | % (originHead, ','.join(original['depot-paths']), |
| 276 | remoteHead, ','.join(settings['depot-paths']))) |
| 277 | |
| 278 | if update: |
| 279 | system("git update-ref %s %s" % (remoteHead, originHead)) |
| 280 | |
| 281 | def originP4BranchesExist(): |
| 282 | return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master") |
| 283 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 284 | class Command: |
| 285 | def __init__(self): |
| 286 | self.usage = "usage: %prog [options]" |
Simon Hausmann | 8910ac0 | 2007-03-26 08:18:55 +0200 | [diff] [blame] | 287 | self.needsGit = True |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 288 | |
| 289 | class P4Debug(Command): |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 290 | def __init__(self): |
Simon Hausmann | 6ae8de8 | 2007-03-22 21:10:25 +0100 | [diff] [blame] | 291 | Command.__init__(self) |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 292 | self.options = [ |
Han-Wen Nienhuys | b1ce944 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 293 | optparse.make_option("--verbose", dest="verbose", action="store_true", |
| 294 | default=False), |
Han-Wen Nienhuys | 4addad2 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 295 | ] |
Simon Hausmann | c8c3911 | 2007-03-19 21:02:30 +0100 | [diff] [blame] | 296 | self.description = "A tool to debug the output of p4 -G." |
Simon Hausmann | 8910ac0 | 2007-03-26 08:18:55 +0200 | [diff] [blame] | 297 | self.needsGit = False |
Han-Wen Nienhuys | b1ce944 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 298 | self.verbose = False |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 299 | |
| 300 | def run(self, args): |
Han-Wen Nienhuys | b1ce944 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 301 | j = 0 |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 302 | for output in p4CmdList(" ".join(args)): |
Han-Wen Nienhuys | b1ce944 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 303 | print 'Element: %d' % j |
| 304 | j += 1 |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 305 | print output |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 306 | return True |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 307 | |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 308 | class P4RollBack(Command): |
| 309 | def __init__(self): |
| 310 | Command.__init__(self) |
| 311 | self.options = [ |
Simon Hausmann | 0c66a78 | 2007-05-23 20:07:57 +0200 | [diff] [blame] | 312 | optparse.make_option("--verbose", dest="verbose", action="store_true"), |
| 313 | optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true") |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 314 | ] |
| 315 | 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] | 316 | self.verbose = False |
Simon Hausmann | 0c66a78 | 2007-05-23 20:07:57 +0200 | [diff] [blame] | 317 | self.rollbackLocalBranches = False |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 318 | |
| 319 | def run(self, args): |
| 320 | if len(args) != 1: |
| 321 | return False |
| 322 | maxChange = int(args[0]) |
Simon Hausmann | 0c66a78 | 2007-05-23 20:07:57 +0200 | [diff] [blame] | 323 | |
Simon Hausmann | ad192f2 | 2007-05-23 23:44:19 +0200 | [diff] [blame] | 324 | if "p4ExitCode" in p4Cmd("changes -m 1"): |
Simon Hausmann | 66a2f52 | 2007-05-23 23:40:48 +0200 | [diff] [blame] | 325 | die("Problems executing p4"); |
| 326 | |
Simon Hausmann | 0c66a78 | 2007-05-23 20:07:57 +0200 | [diff] [blame] | 327 | if self.rollbackLocalBranches: |
| 328 | refPrefix = "refs/heads/" |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 329 | lines = read_pipe_lines("git rev-parse --symbolic --branches") |
Simon Hausmann | 0c66a78 | 2007-05-23 20:07:57 +0200 | [diff] [blame] | 330 | else: |
| 331 | refPrefix = "refs/remotes/" |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 332 | lines = read_pipe_lines("git rev-parse --symbolic --remotes") |
Simon Hausmann | 0c66a78 | 2007-05-23 20:07:57 +0200 | [diff] [blame] | 333 | |
| 334 | for line in lines: |
| 335 | if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"): |
Han-Wen Nienhuys | b25b206 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 336 | line = line.strip() |
| 337 | ref = refPrefix + line |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 338 | log = extractLogMessageFromGitCommit(ref) |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 339 | settings = extractSettingsGitLog(log) |
| 340 | |
| 341 | depotPaths = settings['depot-paths'] |
| 342 | change = settings['change'] |
| 343 | |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 344 | changed = False |
Simon Hausmann | 52102d4 | 2007-05-21 23:44:24 +0200 | [diff] [blame] | 345 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 346 | if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange) |
| 347 | for p in depotPaths]))) == 0: |
Simon Hausmann | 52102d4 | 2007-05-21 23:44:24 +0200 | [diff] [blame] | 348 | print "Branch %s did not exist at change %s, deleting." % (ref, maxChange) |
| 349 | system("git update-ref -d %s `git rev-parse %s`" % (ref, ref)) |
| 350 | continue |
| 351 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 352 | while change and int(change) > maxChange: |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 353 | changed = True |
Simon Hausmann | 52102d4 | 2007-05-21 23:44:24 +0200 | [diff] [blame] | 354 | if self.verbose: |
| 355 | print "%s is at %s ; rewinding towards %s" % (ref, change, maxChange) |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 356 | system("git update-ref %s \"%s^\"" % (ref, ref)) |
| 357 | log = extractLogMessageFromGitCommit(ref) |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 358 | settings = extractSettingsGitLog(log) |
| 359 | |
| 360 | |
| 361 | depotPaths = settings['depot-paths'] |
| 362 | change = settings['change'] |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 363 | |
| 364 | if changed: |
Simon Hausmann | 52102d4 | 2007-05-21 23:44:24 +0200 | [diff] [blame] | 365 | print "%s rewound to %s" % (ref, change) |
Simon Hausmann | 5834684 | 2007-05-21 22:57:06 +0200 | [diff] [blame] | 366 | |
| 367 | return True |
| 368 | |
Simon Hausmann | 711544b | 2007-04-01 15:40:46 +0200 | [diff] [blame] | 369 | class P4Submit(Command): |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 370 | def __init__(self): |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 371 | Command.__init__(self) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 372 | self.options = [ |
| 373 | optparse.make_option("--continue", action="store_false", dest="firstTime"), |
Han-Wen Nienhuys | 4addad2 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 374 | optparse.make_option("--verbose", dest="verbose", action="store_true"), |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 375 | optparse.make_option("--origin", dest="origin"), |
| 376 | optparse.make_option("--reset", action="store_true", dest="reset"), |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 377 | optparse.make_option("--log-substitutions", dest="substFile"), |
Simon Hausmann | 04219c0 | 2007-03-21 10:11:20 +0100 | [diff] [blame] | 378 | optparse.make_option("--dry-run", action="store_true"), |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 379 | optparse.make_option("--direct", dest="directSubmit", action="store_true"), |
Simon Hausmann | cb4f128 | 2007-05-25 22:34:30 +0200 | [diff] [blame] | 380 | optparse.make_option("--trust-me-like-a-fool", dest="trustMeLikeAFool", action="store_true"), |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 381 | ] |
| 382 | self.description = "Submit changes from git to the perforce depot." |
Simon Hausmann | c9b50e6 | 2007-03-29 19:15:24 +0200 | [diff] [blame] | 383 | self.usage += " [name of git branch to submit into perforce depot]" |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 384 | self.firstTime = True |
| 385 | self.reset = False |
| 386 | self.interactive = True |
| 387 | self.dryRun = False |
| 388 | self.substFile = "" |
| 389 | self.firstTime = True |
Simon Hausmann | 9512497 | 2007-03-23 09:16:07 +0100 | [diff] [blame] | 390 | self.origin = "" |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 391 | self.directSubmit = False |
Simon Hausmann | cb4f128 | 2007-05-25 22:34:30 +0200 | [diff] [blame] | 392 | self.trustMeLikeAFool = False |
Simon Hausmann | b0d10df | 2007-06-07 13:09:14 +0200 | [diff] [blame] | 393 | self.verbose = False |
Marius Storm-Olsen | f7baba8 | 2007-06-07 14:07:01 +0200 | [diff] [blame] | 394 | self.isWindows = (platform.system() == "Windows") |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 395 | |
| 396 | self.logSubstitutions = {} |
| 397 | self.logSubstitutions["<enter description here>"] = "%log%" |
| 398 | self.logSubstitutions["\tDetails:"] = "\tDetails: %log%" |
| 399 | |
| 400 | def check(self): |
| 401 | if len(p4CmdList("opened ...")) > 0: |
| 402 | die("You have files opened with perforce! Close them before starting the sync.") |
| 403 | |
| 404 | def start(self): |
| 405 | if len(self.config) > 0 and not self.reset: |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 406 | die("Cannot start sync. Previous sync config found at %s\n" |
| 407 | "If you want to start submitting again from scratch " |
| 408 | "maybe you want to call git-p4 submit --reset" % self.configFile) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 409 | |
| 410 | commits = [] |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 411 | if self.directSubmit: |
| 412 | commits.append("0") |
| 413 | else: |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 414 | for line in read_pipe_lines("git rev-list --no-merges %s..%s" % (self.origin, self.master)): |
Han-Wen Nienhuys | b25b206 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 415 | commits.append(line.strip()) |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 416 | commits.reverse() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 417 | |
| 418 | self.config["commits"] = commits |
| 419 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 420 | def prepareLogMessage(self, template, message): |
| 421 | result = "" |
| 422 | |
| 423 | for line in template.split("\n"): |
| 424 | if line.startswith("#"): |
| 425 | result += line + "\n" |
| 426 | continue |
| 427 | |
| 428 | substituted = False |
| 429 | for key in self.logSubstitutions.keys(): |
| 430 | if line.find(key) != -1: |
| 431 | value = self.logSubstitutions[key] |
| 432 | value = value.replace("%log%", message) |
| 433 | if value != "@remove@": |
| 434 | result += line.replace(key, value) + "\n" |
| 435 | substituted = True |
| 436 | break |
| 437 | |
| 438 | if not substituted: |
| 439 | result += line + "\n" |
| 440 | |
| 441 | return result |
| 442 | |
Simon Hausmann | ea99c3a | 2007-08-08 17:06:55 +0200 | [diff] [blame] | 443 | def prepareSubmitTemplate(self): |
| 444 | # remove lines in the Files section that show changes to files outside the depot path we're committing into |
| 445 | template = "" |
| 446 | inFilesSection = False |
| 447 | for line in read_pipe_lines("p4 change -o"): |
| 448 | if inFilesSection: |
| 449 | if line.startswith("\t"): |
| 450 | # path starts and ends with a tab |
| 451 | path = line[1:] |
| 452 | lastTab = path.rfind("\t") |
| 453 | if lastTab != -1: |
| 454 | path = path[:lastTab] |
| 455 | if not path.startswith(self.depotPath): |
| 456 | continue |
| 457 | else: |
| 458 | inFilesSection = False |
| 459 | else: |
| 460 | if line.startswith("Files:"): |
| 461 | inFilesSection = True |
| 462 | |
| 463 | template += line |
| 464 | |
| 465 | return template |
| 466 | |
Han-Wen Nienhuys | 7cb5cbe | 2007-05-23 16:55:48 -0300 | [diff] [blame] | 467 | def applyCommit(self, id): |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 468 | if self.directSubmit: |
| 469 | print "Applying local change in working directory/index" |
| 470 | diff = self.diffStatus |
| 471 | else: |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 472 | print "Applying %s" % (read_pipe("git log --max-count=1 --pretty=oneline %s" % id)) |
| 473 | diff = read_pipe_lines("git diff-tree -r --name-status \"%s^\" \"%s\"" % (id, id)) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 474 | filesToAdd = set() |
| 475 | filesToDelete = set() |
Simon Hausmann | d336c15 | 2007-05-16 09:41:26 +0200 | [diff] [blame] | 476 | editedFiles = set() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 477 | for line in diff: |
| 478 | modifier = line[0] |
| 479 | path = line[1:].strip() |
| 480 | if modifier == "M": |
Simon Hausmann | d336c15 | 2007-05-16 09:41:26 +0200 | [diff] [blame] | 481 | system("p4 edit \"%s\"" % path) |
| 482 | editedFiles.add(path) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 483 | elif modifier == "A": |
| 484 | filesToAdd.add(path) |
| 485 | if path in filesToDelete: |
| 486 | filesToDelete.remove(path) |
| 487 | elif modifier == "D": |
| 488 | filesToDelete.add(path) |
| 489 | if path in filesToAdd: |
| 490 | filesToAdd.remove(path) |
| 491 | else: |
| 492 | die("unknown modifier %s for %s" % (modifier, path)) |
| 493 | |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 494 | if self.directSubmit: |
| 495 | diffcmd = "cat \"%s\"" % self.diffFile |
| 496 | else: |
| 497 | diffcmd = "git format-patch -k --stdout \"%s^\"..\"%s\"" % (id, id) |
Simon Hausmann | 47a130b | 2007-05-20 16:33:21 +0200 | [diff] [blame] | 498 | patchcmd = diffcmd + " | git apply " |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 499 | tryPatchCmd = patchcmd + "--check -" |
| 500 | applyPatchCmd = patchcmd + "--check --apply -" |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 501 | |
Simon Hausmann | 47a130b | 2007-05-20 16:33:21 +0200 | [diff] [blame] | 502 | if os.system(tryPatchCmd) != 0: |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 503 | print "Unfortunately applying the change failed!" |
| 504 | print "What do you want to do?" |
| 505 | response = "x" |
| 506 | while response != "s" and response != "a" and response != "w": |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 507 | response = raw_input("[s]kip this patch / [a]pply the patch forcibly " |
| 508 | "and with .rej files / [w]rite the patch to a file (patch.txt) ") |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 509 | if response == "s": |
| 510 | print "Skipping! Good luck with the next patches..." |
| 511 | return |
| 512 | elif response == "a": |
Simon Hausmann | 47a130b | 2007-05-20 16:33:21 +0200 | [diff] [blame] | 513 | os.system(applyPatchCmd) |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 514 | if len(filesToAdd) > 0: |
| 515 | print "You may also want to call p4 add on the following files:" |
| 516 | print " ".join(filesToAdd) |
| 517 | if len(filesToDelete): |
| 518 | print "The following files should be scheduled for deletion with p4 delete:" |
| 519 | print " ".join(filesToDelete) |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 520 | die("Please resolve and submit the conflict manually and " |
| 521 | + "continue afterwards with git-p4 submit --continue") |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 522 | elif response == "w": |
| 523 | system(diffcmd + " > patch.txt") |
| 524 | print "Patch saved to patch.txt in %s !" % self.clientPath |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 525 | die("Please resolve and submit the conflict manually and " |
| 526 | "continue afterwards with git-p4 submit --continue") |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 527 | |
Simon Hausmann | 47a130b | 2007-05-20 16:33:21 +0200 | [diff] [blame] | 528 | system(applyPatchCmd) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 529 | |
| 530 | for f in filesToAdd: |
Simon Hausmann | e6b711f | 2007-06-11 23:40:25 +0200 | [diff] [blame] | 531 | system("p4 add \"%s\"" % f) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 532 | for f in filesToDelete: |
Simon Hausmann | e6b711f | 2007-06-11 23:40:25 +0200 | [diff] [blame] | 533 | system("p4 revert \"%s\"" % f) |
| 534 | system("p4 delete \"%s\"" % f) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 535 | |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 536 | logMessage = "" |
| 537 | if not self.directSubmit: |
| 538 | logMessage = extractLogMessageFromGitCommit(id) |
| 539 | logMessage = logMessage.replace("\n", "\n\t") |
Marius Storm-Olsen | f7baba8 | 2007-06-07 14:07:01 +0200 | [diff] [blame] | 540 | if self.isWindows: |
| 541 | logMessage = logMessage.replace("\n", "\r\n") |
Han-Wen Nienhuys | b25b206 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 542 | logMessage = logMessage.strip() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 543 | |
Simon Hausmann | ea99c3a | 2007-08-08 17:06:55 +0200 | [diff] [blame] | 544 | template = self.prepareSubmitTemplate() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 545 | |
| 546 | if self.interactive: |
| 547 | submitTemplate = self.prepareLogMessage(template, logMessage) |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 548 | diff = read_pipe("p4 diff -du ...") |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 549 | |
| 550 | for newFile in filesToAdd: |
| 551 | diff += "==== new file ====\n" |
| 552 | diff += "--- /dev/null\n" |
| 553 | diff += "+++ %s\n" % newFile |
| 554 | f = open(newFile, "r") |
| 555 | for line in f.readlines(): |
| 556 | diff += "+" + line |
| 557 | f.close() |
| 558 | |
Simon Hausmann | 25df95c | 2007-05-15 15:15:39 +0200 | [diff] [blame] | 559 | separatorLine = "######## everything below this line is just the diff #######" |
| 560 | if platform.system() == "Windows": |
| 561 | separatorLine += "\r" |
| 562 | separatorLine += "\n" |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 563 | |
| 564 | response = "e" |
Simon Hausmann | cb4f128 | 2007-05-25 22:34:30 +0200 | [diff] [blame] | 565 | if self.trustMeLikeAFool: |
| 566 | response = "y" |
| 567 | |
Simon Hausmann | 5315025 | 2007-03-21 21:04:12 +0100 | [diff] [blame] | 568 | firstIteration = True |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 569 | while response == "e": |
Simon Hausmann | 5315025 | 2007-03-21 21:04:12 +0100 | [diff] [blame] | 570 | if not firstIteration: |
Simon Hausmann | d336c15 | 2007-05-16 09:41:26 +0200 | [diff] [blame] | 571 | 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] | 572 | firstIteration = False |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 573 | if response == "e": |
| 574 | [handle, fileName] = tempfile.mkstemp() |
| 575 | tmpFile = os.fdopen(handle, "w+") |
Simon Hausmann | 5315025 | 2007-03-21 21:04:12 +0100 | [diff] [blame] | 576 | tmpFile.write(submitTemplate + separatorLine + diff) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 577 | tmpFile.close() |
Simon Hausmann | 25df95c | 2007-05-15 15:15:39 +0200 | [diff] [blame] | 578 | defaultEditor = "vi" |
| 579 | if platform.system() == "Windows": |
| 580 | defaultEditor = "notepad" |
| 581 | editor = os.environ.get("EDITOR", defaultEditor); |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 582 | system(editor + " " + fileName) |
Simon Hausmann | 25df95c | 2007-05-15 15:15:39 +0200 | [diff] [blame] | 583 | tmpFile = open(fileName, "rb") |
Simon Hausmann | 5315025 | 2007-03-21 21:04:12 +0100 | [diff] [blame] | 584 | message = tmpFile.read() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 585 | tmpFile.close() |
| 586 | os.remove(fileName) |
Simon Hausmann | 5315025 | 2007-03-21 21:04:12 +0100 | [diff] [blame] | 587 | submitTemplate = message[:message.index(separatorLine)] |
Marius Storm-Olsen | f7baba8 | 2007-06-07 14:07:01 +0200 | [diff] [blame] | 588 | if self.isWindows: |
| 589 | submitTemplate = submitTemplate.replace("\r\n", "\n") |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 590 | |
| 591 | if response == "y" or response == "yes": |
| 592 | if self.dryRun: |
| 593 | print submitTemplate |
| 594 | raw_input("Press return to continue...") |
| 595 | else: |
Simon Hausmann | 7944f14 | 2007-05-21 11:04:26 +0200 | [diff] [blame] | 596 | if self.directSubmit: |
| 597 | print "Submitting to git first" |
| 598 | os.chdir(self.oldWorkingDirectory) |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 599 | write_pipe("git commit -a -F -", submitTemplate) |
Simon Hausmann | 7944f14 | 2007-05-21 11:04:26 +0200 | [diff] [blame] | 600 | os.chdir(self.clientPath) |
| 601 | |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 602 | write_pipe("p4 submit -i", submitTemplate) |
Simon Hausmann | d336c15 | 2007-05-16 09:41:26 +0200 | [diff] [blame] | 603 | elif response == "s": |
| 604 | for f in editedFiles: |
| 605 | system("p4 revert \"%s\"" % f); |
| 606 | for f in filesToAdd: |
| 607 | system("p4 revert \"%s\"" % f); |
| 608 | system("rm %s" %f) |
| 609 | for f in filesToDelete: |
| 610 | system("p4 delete \"%s\"" % f); |
| 611 | return |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 612 | else: |
| 613 | print "Not submitting!" |
| 614 | self.interactive = False |
| 615 | else: |
| 616 | fileName = "submit.txt" |
| 617 | file = open(fileName, "w+") |
| 618 | file.write(self.prepareLogMessage(template, logMessage)) |
| 619 | file.close() |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 620 | print ("Perforce submit template written as %s. " |
| 621 | + "Please review/edit and then use p4 submit -i < %s to submit directly!" |
| 622 | % (fileName, fileName)) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 623 | |
| 624 | def run(self, args): |
Simon Hausmann | c9b50e6 | 2007-03-29 19:15:24 +0200 | [diff] [blame] | 625 | if len(args) == 0: |
| 626 | self.master = currentGitBranch() |
Simon Hausmann | 4280e53 | 2007-05-25 08:49:18 +0200 | [diff] [blame] | 627 | if len(self.master) == 0 or not gitBranchExists("refs/heads/%s" % self.master): |
Simon Hausmann | c9b50e6 | 2007-03-29 19:15:24 +0200 | [diff] [blame] | 628 | die("Detecting current git branch failed!") |
| 629 | elif len(args) == 1: |
| 630 | self.master = args[0] |
| 631 | else: |
| 632 | return False |
| 633 | |
Simon Hausmann | 27d2d81 | 2007-06-12 14:31:59 +0200 | [diff] [blame] | 634 | [upstream, settings] = findUpstreamBranchPoint() |
Simon Hausmann | ea99c3a | 2007-08-08 17:06:55 +0200 | [diff] [blame] | 635 | self.depotPath = settings['depot-paths'][0] |
Simon Hausmann | 27d2d81 | 2007-06-12 14:31:59 +0200 | [diff] [blame] | 636 | if len(self.origin) == 0: |
| 637 | self.origin = upstream |
Simon Hausmann | a3fdd57 | 2007-06-07 22:54:32 +0200 | [diff] [blame] | 638 | |
| 639 | if self.verbose: |
| 640 | print "Origin branch is " + self.origin |
Simon Hausmann | 9512497 | 2007-03-23 09:16:07 +0100 | [diff] [blame] | 641 | |
Simon Hausmann | ea99c3a | 2007-08-08 17:06:55 +0200 | [diff] [blame] | 642 | if len(self.depotPath) == 0: |
Simon Hausmann | 9512497 | 2007-03-23 09:16:07 +0100 | [diff] [blame] | 643 | print "Internal error: cannot locate perforce depot path from existing branches" |
| 644 | sys.exit(128) |
| 645 | |
Simon Hausmann | ea99c3a | 2007-08-08 17:06:55 +0200 | [diff] [blame] | 646 | self.clientPath = p4Where(self.depotPath) |
Simon Hausmann | 9512497 | 2007-03-23 09:16:07 +0100 | [diff] [blame] | 647 | |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 648 | if len(self.clientPath) == 0: |
Simon Hausmann | ea99c3a | 2007-08-08 17:06:55 +0200 | [diff] [blame] | 649 | print "Error: Cannot locate perforce checkout of %s in client view" % self.depotPath |
Simon Hausmann | 9512497 | 2007-03-23 09:16:07 +0100 | [diff] [blame] | 650 | sys.exit(128) |
| 651 | |
Simon Hausmann | ea99c3a | 2007-08-08 17:06:55 +0200 | [diff] [blame] | 652 | print "Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath) |
Simon Hausmann | 7944f14 | 2007-05-21 11:04:26 +0200 | [diff] [blame] | 653 | self.oldWorkingDirectory = os.getcwd() |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 654 | |
| 655 | if self.directSubmit: |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 656 | self.diffStatus = read_pipe_lines("git diff -r --name-status HEAD") |
Simon Hausmann | cbf5efa | 2007-05-21 10:08:11 +0200 | [diff] [blame] | 657 | if len(self.diffStatus) == 0: |
| 658 | print "No changes in working directory to submit." |
| 659 | return True |
Han-Wen Nienhuys | b016d39 | 2007-05-23 17:10:46 -0300 | [diff] [blame] | 660 | patch = read_pipe("git diff -p --binary --diff-filter=ACMRTUXB HEAD") |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 661 | self.diffFile = self.gitdir + "/p4-git-diff" |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 662 | f = open(self.diffFile, "wb") |
| 663 | f.write(patch) |
| 664 | f.close(); |
| 665 | |
Simon Hausmann | 51a2640 | 2007-04-15 09:59:56 +0200 | [diff] [blame] | 666 | os.chdir(self.clientPath) |
| 667 | 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] | 668 | if response == "y" or response == "yes": |
| 669 | system("p4 sync ...") |
| 670 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 671 | if self.reset: |
| 672 | self.firstTime = True |
| 673 | |
| 674 | if len(self.substFile) > 0: |
| 675 | for line in open(self.substFile, "r").readlines(): |
Han-Wen Nienhuys | b25b206 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 676 | tokens = line.strip().split("=") |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 677 | self.logSubstitutions[tokens[0]] = tokens[1] |
| 678 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 679 | self.check() |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 680 | self.configFile = self.gitdir + "/p4-git-sync.cfg" |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 681 | self.config = shelve.open(self.configFile, writeback=True) |
| 682 | |
| 683 | if self.firstTime: |
| 684 | self.start() |
| 685 | |
| 686 | commits = self.config.get("commits", []) |
| 687 | |
| 688 | while len(commits) > 0: |
| 689 | self.firstTime = False |
| 690 | commit = commits[0] |
| 691 | commits = commits[1:] |
| 692 | self.config["commits"] = commits |
Han-Wen Nienhuys | 7cb5cbe | 2007-05-23 16:55:48 -0300 | [diff] [blame] | 693 | self.applyCommit(commit) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 694 | if not self.interactive: |
| 695 | break |
| 696 | |
| 697 | self.config.close() |
| 698 | |
Simon Hausmann | c1b296b | 2007-05-20 16:55:05 +0200 | [diff] [blame] | 699 | if self.directSubmit: |
| 700 | os.remove(self.diffFile) |
| 701 | |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 702 | if len(commits) == 0: |
| 703 | if self.firstTime: |
| 704 | print "No changes found to apply between %s and current HEAD" % self.origin |
| 705 | else: |
| 706 | print "All changes applied!" |
Simon Hausmann | 7944f14 | 2007-05-21 11:04:26 +0200 | [diff] [blame] | 707 | os.chdir(self.oldWorkingDirectory) |
| 708 | 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] | 709 | if response == "y" or response == "yes": |
Simon Hausmann | 80b5910 | 2007-04-09 12:43:40 +0200 | [diff] [blame] | 710 | rebase = P4Rebase() |
| 711 | rebase.run([]) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 712 | os.remove(self.configFile) |
| 713 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 714 | return True |
| 715 | |
Simon Hausmann | 711544b | 2007-04-01 15:40:46 +0200 | [diff] [blame] | 716 | class P4Sync(Command): |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 717 | def __init__(self): |
| 718 | Command.__init__(self) |
| 719 | self.options = [ |
| 720 | optparse.make_option("--branch", dest="branch"), |
| 721 | optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"), |
| 722 | optparse.make_option("--changesfile", dest="changesFile"), |
| 723 | optparse.make_option("--silent", dest="silent", action="store_true"), |
Simon Hausmann | ef48f90 | 2007-05-17 22:17:49 +0200 | [diff] [blame] | 724 | optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"), |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 725 | optparse.make_option("--verbose", dest="verbose", action="store_true"), |
Han-Wen Nienhuys | d2c6dd3 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 726 | optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false", |
| 727 | help="Import into refs/heads/ , not refs/remotes"), |
Han-Wen Nienhuys | 8b41a97 | 2007-05-23 18:20:53 -0300 | [diff] [blame] | 728 | optparse.make_option("--max-changes", dest="maxChanges"), |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 729 | optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true', |
| 730 | help="Keep entire BRANCH/DIR/SUBDIR prefix during import") |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 731 | ] |
| 732 | self.description = """Imports from Perforce into a git repository.\n |
| 733 | example: |
| 734 | //depot/my/project/ -- to import the current head |
| 735 | //depot/my/project/@all -- to import everything |
| 736 | //depot/my/project/@1,6 -- to import only from revision 1 to 6 |
| 737 | |
| 738 | (a ... is not needed in the path p4 specification, it's added implicitly)""" |
| 739 | |
| 740 | self.usage += " //depot/path[@revRange]" |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 741 | self.silent = False |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 742 | self.createdBranches = Set() |
| 743 | self.committedChanges = Set() |
Simon Hausmann | 569d1bd | 2007-03-22 21:34:16 +0100 | [diff] [blame] | 744 | self.branch = "" |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 745 | self.detectBranches = False |
Simon Hausmann | cb53e1f | 2007-04-08 00:12:02 +0200 | [diff] [blame] | 746 | self.detectLabels = False |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 747 | self.changesFile = "" |
Simon Hausmann | 0126510 | 2007-05-25 10:36:10 +0200 | [diff] [blame] | 748 | self.syncWithOrigin = True |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 749 | self.verbose = False |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 750 | self.importIntoRemotes = True |
Simon Hausmann | 01a9c9c | 2007-05-23 00:07:35 +0200 | [diff] [blame] | 751 | self.maxChanges = "" |
Marius Storm-Olsen | c1f9197 | 2007-05-24 14:07:55 +0200 | [diff] [blame] | 752 | self.isWindows = (platform.system() == "Windows") |
Han-Wen Nienhuys | 8b41a97 | 2007-05-23 18:20:53 -0300 | [diff] [blame] | 753 | self.keepRepoPath = False |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 754 | self.depotPaths = None |
Simon Hausmann | 3c69964 | 2007-06-16 13:09:21 +0200 | [diff] [blame] | 755 | self.p4BranchesInGit = [] |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 756 | |
Simon Hausmann | 0126510 | 2007-05-25 10:36:10 +0200 | [diff] [blame] | 757 | if gitConfig("git-p4.syncFromOrigin") == "false": |
| 758 | self.syncWithOrigin = False |
| 759 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 760 | def extractFilesFromCommit(self, commit): |
| 761 | files = [] |
| 762 | fnum = 0 |
| 763 | while commit.has_key("depotFile%s" % fnum): |
| 764 | path = commit["depotFile%s" % fnum] |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 765 | |
| 766 | found = [p for p in self.depotPaths |
| 767 | if path.startswith (p)] |
| 768 | if not found: |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 769 | fnum = fnum + 1 |
| 770 | continue |
| 771 | |
| 772 | file = {} |
| 773 | file["path"] = path |
| 774 | file["rev"] = commit["rev%s" % fnum] |
| 775 | file["action"] = commit["action%s" % fnum] |
| 776 | file["type"] = commit["type%s" % fnum] |
| 777 | files.append(file) |
| 778 | fnum = fnum + 1 |
| 779 | return files |
| 780 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 781 | def stripRepoPath(self, path, prefixes): |
Han-Wen Nienhuys | 8b41a97 | 2007-05-23 18:20:53 -0300 | [diff] [blame] | 782 | if self.keepRepoPath: |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 783 | prefixes = [re.sub("^(//[^/]+/).*", r'\1', prefixes[0])] |
Han-Wen Nienhuys | 8b41a97 | 2007-05-23 18:20:53 -0300 | [diff] [blame] | 784 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 785 | for p in prefixes: |
| 786 | if path.startswith(p): |
| 787 | path = path[len(p):] |
| 788 | |
| 789 | return path |
Han-Wen Nienhuys | 6754a29 | 2007-05-23 17:41:50 -0300 | [diff] [blame] | 790 | |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 791 | def splitFilesIntoBranches(self, commit): |
Simon Hausmann | d590467 | 2007-05-19 11:07:32 +0200 | [diff] [blame] | 792 | branches = {} |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 793 | fnum = 0 |
| 794 | while commit.has_key("depotFile%s" % fnum): |
| 795 | path = commit["depotFile%s" % fnum] |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 796 | found = [p for p in self.depotPaths |
| 797 | if path.startswith (p)] |
| 798 | if not found: |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 799 | fnum = fnum + 1 |
| 800 | continue |
| 801 | |
| 802 | file = {} |
| 803 | file["path"] = path |
| 804 | file["rev"] = commit["rev%s" % fnum] |
| 805 | file["action"] = commit["action%s" % fnum] |
| 806 | file["type"] = commit["type%s" % fnum] |
| 807 | fnum = fnum + 1 |
| 808 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 809 | relPath = self.stripRepoPath(path, self.depotPaths) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 810 | |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 811 | for branch in self.knownBranches.keys(): |
Han-Wen Nienhuys | 6754a29 | 2007-05-23 17:41:50 -0300 | [diff] [blame] | 812 | |
| 813 | # add a trailing slash so that a commit into qt/4.2foo doesn't end up in qt/4.2 |
| 814 | if relPath.startswith(branch + "/"): |
Simon Hausmann | d590467 | 2007-05-19 11:07:32 +0200 | [diff] [blame] | 815 | if branch not in branches: |
| 816 | branches[branch] = [] |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 817 | branches[branch].append(file) |
Simon Hausmann | 6555b2c | 2007-06-17 11:25:34 +0200 | [diff] [blame] | 818 | break |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 819 | |
| 820 | return branches |
| 821 | |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 822 | ## Should move this out, doesn't use SELF. |
| 823 | def readP4Files(self, files): |
Han-Wen Nienhuys | b1ce944 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 824 | files = [f for f in files |
Han-Wen Nienhuys | 982bb8a | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 825 | if f['action'] != 'delete'] |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 826 | |
Han-Wen Nienhuys | b1ce944 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 827 | if not files: |
Han-Wen Nienhuys | f2eda79 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 828 | return |
| 829 | |
Scott Lamb | 7880019 | 2007-07-15 20:58:11 -0700 | [diff] [blame] | 830 | filedata = p4CmdList('-x - print', |
| 831 | stdin='\n'.join(['%s#%s' % (f['path'], f['rev']) |
| 832 | for f in files]), |
| 833 | stdin_mode='w+') |
| 834 | if "p4ExitCode" in filedata[0]: |
| 835 | die("Problems executing p4. Error: [%d]." |
| 836 | % (filedata[0]['p4ExitCode'])); |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 837 | |
Han-Wen Nienhuys | d2c6dd3 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 838 | j = 0; |
| 839 | contents = {} |
Han-Wen Nienhuys | b1ce944 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 840 | while j < len(filedata): |
Han-Wen Nienhuys | d2c6dd3 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 841 | stat = filedata[j] |
Han-Wen Nienhuys | b1ce944 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 842 | j += 1 |
| 843 | text = '' |
Han-Wen Nienhuys | 7530a40 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 844 | while j < len(filedata) and filedata[j]['code'] in ('text', |
| 845 | 'binary'): |
Han-Wen Nienhuys | b1ce944 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 846 | text += filedata[j]['data'] |
| 847 | j += 1 |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 848 | |
Han-Wen Nienhuys | 1b9a468 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 849 | |
| 850 | if not stat.has_key('depotFile'): |
| 851 | sys.stderr.write("p4 print fails with: %s\n" % repr(stat)) |
| 852 | continue |
| 853 | |
Han-Wen Nienhuys | b1ce944 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 854 | contents[stat['depotFile']] = text |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 855 | |
Han-Wen Nienhuys | d2c6dd3 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 856 | for f in files: |
| 857 | assert not f.has_key('data') |
| 858 | f['data'] = contents[f['path']] |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 859 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 860 | def commit(self, details, files, branch, branchPrefixes, parent = ""): |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 861 | epoch = details["time"] |
| 862 | author = details["user"] |
| 863 | |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 864 | if self.verbose: |
| 865 | print "commit into %s" % branch |
| 866 | |
Han-Wen Nienhuys | 96e07dd | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 867 | # start with reading files; if that fails, we should not |
| 868 | # create a commit. |
| 869 | new_files = [] |
| 870 | for f in files: |
| 871 | if [p for p in branchPrefixes if f['path'].startswith(p)]: |
| 872 | new_files.append (f) |
| 873 | else: |
| 874 | sys.stderr.write("Ignoring file outside of prefix: %s\n" % path) |
| 875 | files = new_files |
| 876 | self.readP4Files(files) |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 881 | self.gitStream.write("commit %s\n" % branch) |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 882 | # gitStream.write("mark :%s\n" % details["change"]) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 883 | self.committedChanges.add(int(details["change"])) |
| 884 | committer = "" |
Simon Hausmann | b607e71 | 2007-05-20 10:55:54 +0200 | [diff] [blame] | 885 | if author not in self.users: |
| 886 | self.getUserMapFromPerforceServer() |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 887 | if author in self.users: |
Simon Hausmann | 0828ab1 | 2007-03-20 20:59:30 +0100 | [diff] [blame] | 888 | committer = "%s %s %s" % (self.users[author], epoch, self.tz) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 889 | else: |
Simon Hausmann | 0828ab1 | 2007-03-20 20:59:30 +0100 | [diff] [blame] | 890 | committer = "%s <a@b> %s %s" % (author, epoch, self.tz) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 891 | |
| 892 | self.gitStream.write("committer %s\n" % committer) |
| 893 | |
| 894 | self.gitStream.write("data <<EOT\n") |
| 895 | self.gitStream.write(details["desc"]) |
Simon Hausmann | 6581de0 | 2007-06-11 10:01:58 +0200 | [diff] [blame] | 896 | self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s" |
| 897 | % (','.join (branchPrefixes), details["change"])) |
| 898 | if len(details['options']) > 0: |
| 899 | self.gitStream.write(": options = %s" % details['options']) |
| 900 | self.gitStream.write("]\nEOT\n\n") |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 901 | |
| 902 | if len(parent) > 0: |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 903 | if self.verbose: |
| 904 | print "parent %s" % parent |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 905 | self.gitStream.write("from %s\n" % parent) |
| 906 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 907 | for file in files: |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 908 | if file["type"] == "apple": |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 909 | print "\nfile %s is a strange apple file that forks. Ignoring!" % file['path'] |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 910 | continue |
| 911 | |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 912 | relPath = self.stripRepoPath(file['path'], branchPrefixes) |
| 913 | if file["action"] == "delete": |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 914 | self.gitStream.write("D %s\n" % relPath) |
| 915 | else: |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 916 | data = file['data'] |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 917 | |
Simon Hausmann | 74276ec | 2007-08-07 12:28:00 +0200 | [diff] [blame] | 918 | mode = "644" |
| 919 | if file["type"].startswith("x"): |
| 920 | mode = "755" |
| 921 | elif file["type"] == "symlink": |
| 922 | mode = "120000" |
| 923 | # p4 print on a symlink contains "target\n", so strip it off |
| 924 | data = data[:-1] |
| 925 | |
Marius Storm-Olsen | c1f9197 | 2007-05-24 14:07:55 +0200 | [diff] [blame] | 926 | if self.isWindows and file["type"].endswith("text"): |
| 927 | data = data.replace("\r\n", "\n") |
| 928 | |
Simon Hausmann | 74276ec | 2007-08-07 12:28:00 +0200 | [diff] [blame] | 929 | self.gitStream.write("M %s inline %s\n" % (mode, relPath)) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 930 | self.gitStream.write("data %s\n" % len(data)) |
| 931 | self.gitStream.write(data) |
| 932 | self.gitStream.write("\n") |
| 933 | |
| 934 | self.gitStream.write("\n") |
| 935 | |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 936 | change = int(details["change"]) |
| 937 | |
Simon Hausmann | 9bda3a8 | 2007-05-19 12:05:40 +0200 | [diff] [blame] | 938 | if self.labels.has_key(change): |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 939 | label = self.labels[change] |
| 940 | labelDetails = label[0] |
| 941 | labelRevisions = label[1] |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 942 | if self.verbose: |
| 943 | print "Change %s is labelled %s" % (change, labelDetails) |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 944 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 945 | files = p4CmdList("files " + ' '.join (["%s...@%s" % (p, change) |
| 946 | for p in branchPrefixes])) |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 947 | |
| 948 | if len(files) == len(labelRevisions): |
| 949 | |
| 950 | cleanedFiles = {} |
| 951 | for info in files: |
| 952 | if info["action"] == "delete": |
| 953 | continue |
| 954 | cleanedFiles[info["depotFile"]] = info["rev"] |
| 955 | |
| 956 | if cleanedFiles == labelRevisions: |
| 957 | self.gitStream.write("tag tag_%s\n" % labelDetails["label"]) |
| 958 | self.gitStream.write("from %s\n" % branch) |
| 959 | |
| 960 | owner = labelDetails["Owner"] |
| 961 | tagger = "" |
| 962 | if author in self.users: |
| 963 | tagger = "%s %s %s" % (self.users[owner], epoch, self.tz) |
| 964 | else: |
| 965 | tagger = "%s <a@b> %s %s" % (owner, epoch, self.tz) |
| 966 | self.gitStream.write("tagger %s\n" % tagger) |
| 967 | self.gitStream.write("data <<EOT\n") |
| 968 | self.gitStream.write(labelDetails["Description"]) |
| 969 | self.gitStream.write("EOT\n\n") |
| 970 | |
| 971 | else: |
Simon Hausmann | a46668f | 2007-03-28 17:05:38 +0200 | [diff] [blame] | 972 | if not self.silent: |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 973 | print ("Tag %s does not match with change %s: files do not match." |
| 974 | % (labelDetails["label"], change)) |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 975 | |
| 976 | else: |
Simon Hausmann | a46668f | 2007-03-28 17:05:38 +0200 | [diff] [blame] | 977 | if not self.silent: |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 978 | print ("Tag %s does not match with change %s: file count is different." |
| 979 | % (labelDetails["label"], change)) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 980 | |
Han-Wen Nienhuys | 183b8ef | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 981 | def getUserCacheFilename(self): |
Simon Hausmann | b2d2d16 | 2007-07-25 09:31:38 +0200 | [diff] [blame] | 982 | home = os.environ.get("HOME", os.environ.get("USERPROFILE")) |
| 983 | return home + "/.gitp4-usercache.txt" |
Han-Wen Nienhuys | 183b8ef | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 984 | |
Simon Hausmann | b607e71 | 2007-05-20 10:55:54 +0200 | [diff] [blame] | 985 | def getUserMapFromPerforceServer(self): |
Simon Hausmann | ebd8116 | 2007-05-24 00:24:52 +0200 | [diff] [blame] | 986 | if self.userMapFromPerforceServer: |
| 987 | return |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 988 | self.users = {} |
| 989 | |
| 990 | for output in p4CmdList("users"): |
| 991 | if not output.has_key("User"): |
| 992 | continue |
| 993 | self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">" |
| 994 | |
Han-Wen Nienhuys | 183b8ef | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 995 | |
| 996 | s = '' |
| 997 | for (key, val) in self.users.items(): |
| 998 | s += "%s\t%s\n" % (key, val) |
| 999 | |
| 1000 | open(self.getUserCacheFilename(), "wb").write(s) |
Simon Hausmann | ebd8116 | 2007-05-24 00:24:52 +0200 | [diff] [blame] | 1001 | self.userMapFromPerforceServer = True |
Simon Hausmann | b607e71 | 2007-05-20 10:55:54 +0200 | [diff] [blame] | 1002 | |
| 1003 | def loadUserMapFromCache(self): |
| 1004 | self.users = {} |
Simon Hausmann | ebd8116 | 2007-05-24 00:24:52 +0200 | [diff] [blame] | 1005 | self.userMapFromPerforceServer = False |
Simon Hausmann | b607e71 | 2007-05-20 10:55:54 +0200 | [diff] [blame] | 1006 | try: |
Han-Wen Nienhuys | 183b8ef | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1007 | cache = open(self.getUserCacheFilename(), "rb") |
Simon Hausmann | b607e71 | 2007-05-20 10:55:54 +0200 | [diff] [blame] | 1008 | lines = cache.readlines() |
| 1009 | cache.close() |
| 1010 | for line in lines: |
Han-Wen Nienhuys | b25b206 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1011 | entry = line.strip().split("\t") |
Simon Hausmann | b607e71 | 2007-05-20 10:55:54 +0200 | [diff] [blame] | 1012 | self.users[entry[0]] = entry[1] |
| 1013 | except IOError: |
| 1014 | self.getUserMapFromPerforceServer() |
| 1015 | |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 1016 | def getLabels(self): |
| 1017 | self.labels = {} |
| 1018 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1019 | l = p4CmdList("labels %s..." % ' '.join (self.depotPaths)) |
Simon Hausmann | 10c3211 | 2007-04-08 10:15:47 +0200 | [diff] [blame] | 1020 | if len(l) > 0 and not self.silent: |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1021 | print "Finding files belonging to labels in %s" % `self.depotPath` |
Simon Hausmann | 01ce1fe | 2007-04-07 23:46:50 +0200 | [diff] [blame] | 1022 | |
| 1023 | for output in l: |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 1024 | label = output["label"] |
| 1025 | revisions = {} |
| 1026 | newestChange = 0 |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 1027 | if self.verbose: |
| 1028 | print "Querying files for label %s" % label |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1029 | for file in p4CmdList("files " |
| 1030 | + ' '.join (["%s...@%s" % (p, label) |
| 1031 | for p in self.depotPaths])): |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 1032 | revisions[file["depotFile"]] = file["rev"] |
| 1033 | change = int(file["change"]) |
| 1034 | if change > newestChange: |
| 1035 | newestChange = change |
| 1036 | |
Simon Hausmann | 9bda3a8 | 2007-05-19 12:05:40 +0200 | [diff] [blame] | 1037 | self.labels[newestChange] = [output, revisions] |
| 1038 | |
| 1039 | if self.verbose: |
| 1040 | print "Label changes: %s" % self.labels.keys() |
Simon Hausmann | 1f4ba1c | 2007-03-26 22:34:34 +0200 | [diff] [blame] | 1041 | |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1042 | def guessProjectName(self): |
| 1043 | for p in self.depotPaths: |
Simon Hausmann | 6e5295c | 2007-06-11 08:50:57 +0200 | [diff] [blame] | 1044 | if p.endswith("/"): |
| 1045 | p = p[:-1] |
| 1046 | p = p[p.strip().rfind("/") + 1:] |
| 1047 | if not p.endswith("/"): |
| 1048 | p += "/" |
| 1049 | return p |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1050 | |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1051 | def getBranchMapping(self): |
Simon Hausmann | 6555b2c | 2007-06-17 11:25:34 +0200 | [diff] [blame] | 1052 | lostAndFoundBranches = set() |
| 1053 | |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1054 | for info in p4CmdList("branches"): |
| 1055 | details = p4Cmd("branch -o %s" % info["branch"]) |
| 1056 | viewIdx = 0 |
| 1057 | while details.has_key("View%s" % viewIdx): |
| 1058 | paths = details["View%s" % viewIdx].split(" ") |
| 1059 | viewIdx = viewIdx + 1 |
| 1060 | # require standard //depot/foo/... //depot/bar/... mapping |
| 1061 | if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."): |
| 1062 | continue |
| 1063 | source = paths[0] |
| 1064 | destination = paths[1] |
Simon Hausmann | 6509e19 | 2007-06-07 09:41:53 +0200 | [diff] [blame] | 1065 | ## HACK |
| 1066 | if source.startswith(self.depotPaths[0]) and destination.startswith(self.depotPaths[0]): |
| 1067 | source = source[len(self.depotPaths[0]):-4] |
| 1068 | destination = destination[len(self.depotPaths[0]):-4] |
Simon Hausmann | 6555b2c | 2007-06-17 11:25:34 +0200 | [diff] [blame] | 1069 | |
Simon Hausmann | 1a2edf4 | 2007-06-17 15:10:24 +0200 | [diff] [blame] | 1070 | if destination in self.knownBranches: |
| 1071 | if not self.silent: |
| 1072 | print "p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination) |
| 1073 | print "but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination) |
| 1074 | continue |
| 1075 | |
Simon Hausmann | 6555b2c | 2007-06-17 11:25:34 +0200 | [diff] [blame] | 1076 | self.knownBranches[destination] = source |
| 1077 | |
| 1078 | lostAndFoundBranches.discard(destination) |
| 1079 | |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1080 | if source not in self.knownBranches: |
Simon Hausmann | 6555b2c | 2007-06-17 11:25:34 +0200 | [diff] [blame] | 1081 | lostAndFoundBranches.add(source) |
| 1082 | |
| 1083 | |
| 1084 | for branch in lostAndFoundBranches: |
| 1085 | self.knownBranches[branch] = branch |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1086 | |
| 1087 | def listExistingP4GitBranches(self): |
Simon Hausmann | 144ff46 | 2007-07-18 17:27:50 +0200 | [diff] [blame] | 1088 | # branches holds mapping from name to commit |
| 1089 | branches = p4BranchesInGit(self.importIntoRemotes) |
| 1090 | self.p4BranchesInGit = branches.keys() |
| 1091 | for branch in branches.keys(): |
| 1092 | self.initialParents[self.refPrefix + branch] = branches[branch] |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1093 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1094 | def updateOptionDict(self, d): |
| 1095 | option_keys = {} |
| 1096 | if self.keepRepoPath: |
| 1097 | option_keys['keepRepoPath'] = 1 |
| 1098 | |
| 1099 | d["options"] = ' '.join(sorted(option_keys.keys())) |
| 1100 | |
| 1101 | def readOptions(self, d): |
| 1102 | self.keepRepoPath = (d.has_key('options') |
| 1103 | and ('keepRepoPath' in d['options'])) |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1104 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1105 | def run(self, args): |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1106 | self.depotPaths = [] |
Simon Hausmann | 179caeb | 2007-03-22 22:17:42 +0100 | [diff] [blame] | 1107 | self.changeRange = "" |
| 1108 | self.initialParent = "" |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1109 | self.previousDepotPaths = [] |
Han-Wen Nienhuys | ce6f33c | 2007-05-23 16:46:29 -0300 | [diff] [blame] | 1110 | |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1111 | # map from branch depot path to parent branch |
| 1112 | self.knownBranches = {} |
| 1113 | self.initialParents = {} |
Simon Hausmann | 5ca4461 | 2007-08-24 17:44:16 +0200 | [diff] [blame^] | 1114 | self.hasOrigin = originP4BranchesExist() |
Simon Hausmann | a43ff00 | 2007-06-11 09:59:27 +0200 | [diff] [blame] | 1115 | if not self.syncWithOrigin: |
| 1116 | self.hasOrigin = False |
Simon Hausmann | 179caeb | 2007-03-22 22:17:42 +0100 | [diff] [blame] | 1117 | |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 1118 | if self.importIntoRemotes: |
| 1119 | self.refPrefix = "refs/remotes/p4/" |
| 1120 | else: |
Marius Storm-Olsen | db77555 | 2007-06-07 15:13:59 +0200 | [diff] [blame] | 1121 | self.refPrefix = "refs/heads/p4/" |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 1122 | |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 1123 | if self.syncWithOrigin and self.hasOrigin: |
| 1124 | if not self.silent: |
| 1125 | print "Syncing with origin first by calling git fetch origin" |
| 1126 | system("git fetch origin") |
Simon Hausmann | 10f880f | 2007-05-24 22:28:28 +0200 | [diff] [blame] | 1127 | |
Simon Hausmann | 569d1bd | 2007-03-22 21:34:16 +0100 | [diff] [blame] | 1128 | if len(self.branch) == 0: |
Marius Storm-Olsen | db77555 | 2007-06-07 15:13:59 +0200 | [diff] [blame] | 1129 | self.branch = self.refPrefix + "master" |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 1130 | if gitBranchExists("refs/heads/p4") and self.importIntoRemotes: |
Simon Hausmann | 48df6fd | 2007-05-17 21:18:53 +0200 | [diff] [blame] | 1131 | system("git update-ref %s refs/heads/p4" % self.branch) |
Simon Hausmann | 48df6fd | 2007-05-17 21:18:53 +0200 | [diff] [blame] | 1132 | system("git branch -D p4"); |
Simon Hausmann | faf1bd2 | 2007-05-21 10:05:30 +0200 | [diff] [blame] | 1133 | # create it /after/ importing, when master exists |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 1134 | if not gitBranchExists(self.refPrefix + "HEAD") and self.importIntoRemotes: |
Simon Hausmann | a3c55c0 | 2007-05-27 15:48:01 +0200 | [diff] [blame] | 1135 | system("git symbolic-ref %sHEAD %s" % (self.refPrefix, self.branch)) |
Simon Hausmann | 179caeb | 2007-03-22 22:17:42 +0100 | [diff] [blame] | 1136 | |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1137 | # TODO: should always look at previous commits, |
| 1138 | # merge with previous imports, if possible. |
| 1139 | if args == []: |
Simon Hausmann | d414c74 | 2007-05-25 11:36:42 +0200 | [diff] [blame] | 1140 | if self.hasOrigin: |
Simon Hausmann | 5ca4461 | 2007-08-24 17:44:16 +0200 | [diff] [blame^] | 1141 | createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent) |
Simon Hausmann | abcd790 | 2007-05-24 22:25:36 +0200 | [diff] [blame] | 1142 | self.listExistingP4GitBranches() |
| 1143 | |
| 1144 | if len(self.p4BranchesInGit) > 1: |
| 1145 | if not self.silent: |
| 1146 | print "Importing from/into multiple branches" |
| 1147 | self.detectBranches = True |
Simon Hausmann | 967f72e | 2007-03-23 09:30:41 +0100 | [diff] [blame] | 1148 | |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1149 | if self.verbose: |
| 1150 | print "branches: %s" % self.p4BranchesInGit |
| 1151 | |
| 1152 | p4Change = 0 |
| 1153 | for branch in self.p4BranchesInGit: |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 1154 | logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch) |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1155 | |
| 1156 | settings = extractSettingsGitLog(logMsg) |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1157 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1158 | self.readOptions(settings) |
| 1159 | if (settings.has_key('depot-paths') |
| 1160 | and settings.has_key ('change')): |
| 1161 | change = int(settings['change']) + 1 |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1162 | p4Change = max(p4Change, change) |
| 1163 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1164 | depotPaths = sorted(settings['depot-paths']) |
| 1165 | if self.previousDepotPaths == []: |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1166 | self.previousDepotPaths = depotPaths |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1167 | else: |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1168 | paths = [] |
| 1169 | for (prev, cur) in zip(self.previousDepotPaths, depotPaths): |
Simon Hausmann | 583e170 | 2007-06-07 09:37:13 +0200 | [diff] [blame] | 1170 | for i in range(0, min(len(cur), len(prev))): |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1171 | if cur[i] <> prev[i]: |
Simon Hausmann | 583e170 | 2007-06-07 09:37:13 +0200 | [diff] [blame] | 1172 | i = i - 1 |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1173 | break |
| 1174 | |
Simon Hausmann | 583e170 | 2007-06-07 09:37:13 +0200 | [diff] [blame] | 1175 | paths.append (cur[:i + 1]) |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1176 | |
| 1177 | self.previousDepotPaths = paths |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1178 | |
| 1179 | if p4Change > 0: |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1180 | self.depotPaths = sorted(self.previousDepotPaths) |
Simon Hausmann | d590467 | 2007-05-19 11:07:32 +0200 | [diff] [blame] | 1181 | self.changeRange = "@%s,#head" % p4Change |
Simon Hausmann | 330f53b | 2007-06-07 09:39:51 +0200 | [diff] [blame] | 1182 | if not self.detectBranches: |
| 1183 | self.initialParent = parseRevision(self.branch) |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 1184 | if not self.silent and not self.detectBranches: |
Simon Hausmann | 967f72e | 2007-03-23 09:30:41 +0100 | [diff] [blame] | 1185 | print "Performing incremental import into %s git branch" % self.branch |
Simon Hausmann | 569d1bd | 2007-03-22 21:34:16 +0100 | [diff] [blame] | 1186 | |
Simon Hausmann | f9162f6 | 2007-05-17 09:02:45 +0200 | [diff] [blame] | 1187 | if not self.branch.startswith("refs/"): |
| 1188 | self.branch = "refs/heads/" + self.branch |
Simon Hausmann | 179caeb | 2007-03-22 22:17:42 +0100 | [diff] [blame] | 1189 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1190 | if len(args) == 0 and self.depotPaths: |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1191 | if not self.silent: |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1192 | print "Depot paths: %s" % ' '.join(self.depotPaths) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1193 | else: |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1194 | if self.depotPaths and self.depotPaths != args: |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 1195 | print ("previous import used depot path %s and now %s was specified. " |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1196 | "This doesn't work!" % (' '.join (self.depotPaths), |
| 1197 | ' '.join (args))) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1198 | sys.exit(1) |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1199 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1200 | self.depotPaths = sorted(args) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1201 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1202 | self.revision = "" |
| 1203 | self.users = {} |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1204 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1205 | newPaths = [] |
| 1206 | for p in self.depotPaths: |
| 1207 | if p.find("@") != -1: |
| 1208 | atIdx = p.index("@") |
| 1209 | self.changeRange = p[atIdx:] |
| 1210 | if self.changeRange == "@all": |
| 1211 | self.changeRange = "" |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1212 | elif ',' not in self.changeRange: |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1213 | self.revision = self.changeRange |
| 1214 | self.changeRange = "" |
Han-Wen Nienhuys | 7fcff9d | 2007-07-23 15:56:37 -0700 | [diff] [blame] | 1215 | p = p[:atIdx] |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1216 | elif p.find("#") != -1: |
| 1217 | hashIdx = p.index("#") |
| 1218 | self.revision = p[hashIdx:] |
Han-Wen Nienhuys | 7fcff9d | 2007-07-23 15:56:37 -0700 | [diff] [blame] | 1219 | p = p[:hashIdx] |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1220 | elif self.previousDepotPaths == []: |
| 1221 | self.revision = "#head" |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1222 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1223 | p = re.sub ("\.\.\.$", "", p) |
| 1224 | if not p.endswith("/"): |
| 1225 | p += "/" |
| 1226 | |
| 1227 | newPaths.append(p) |
| 1228 | |
| 1229 | self.depotPaths = newPaths |
| 1230 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1231 | |
Simon Hausmann | b607e71 | 2007-05-20 10:55:54 +0200 | [diff] [blame] | 1232 | self.loadUserMapFromCache() |
Simon Hausmann | cb53e1f | 2007-04-08 00:12:02 +0200 | [diff] [blame] | 1233 | self.labels = {} |
| 1234 | if self.detectLabels: |
| 1235 | self.getLabels(); |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1236 | |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1237 | if self.detectBranches: |
Simon Hausmann | df45092 | 2007-06-08 08:49:22 +0200 | [diff] [blame] | 1238 | ## FIXME - what's a P4 projectName ? |
| 1239 | self.projectName = self.guessProjectName() |
| 1240 | |
| 1241 | if not self.hasOrigin: |
| 1242 | self.getBranchMapping(); |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1243 | if self.verbose: |
| 1244 | print "p4-git branches: %s" % self.p4BranchesInGit |
| 1245 | print "initial parents: %s" % self.initialParents |
| 1246 | for b in self.p4BranchesInGit: |
| 1247 | if b != "master": |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1248 | |
| 1249 | ## FIXME |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1250 | b = b[len(self.projectName):] |
| 1251 | self.createdBranches.add(b) |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1252 | |
Simon Hausmann | f291b4e | 2007-04-14 11:21:50 +0200 | [diff] [blame] | 1253 | self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60)) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1254 | |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 1255 | importProcess = subprocess.Popen(["git", "fast-import"], |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1256 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 1257 | stderr=subprocess.PIPE); |
Simon Hausmann | 0848358 | 2007-05-15 14:31:06 +0200 | [diff] [blame] | 1258 | self.gitOutput = importProcess.stdout |
| 1259 | self.gitStream = importProcess.stdin |
| 1260 | self.gitError = importProcess.stderr |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1261 | |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1262 | if self.revision: |
Simon Hausmann | a9d1a27 | 2007-06-11 23:28:03 +0200 | [diff] [blame] | 1263 | print "Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), self.revision, self.branch) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1264 | |
| 1265 | details = { "user" : "git perforce import user", "time" : int(time.time()) } |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 1266 | details["desc"] = ("Initial import of %s from the state at revision %s" |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1267 | % (' '.join(self.depotPaths), self.revision)) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1268 | details["change"] = self.revision |
| 1269 | newestRevision = 0 |
| 1270 | |
| 1271 | fileCnt = 0 |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1272 | for info in p4CmdList("files " |
| 1273 | + ' '.join(["%s...%s" |
| 1274 | % (p, self.revision) |
| 1275 | for p in self.depotPaths])): |
Han-Wen Nienhuys | 96e07dd | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1276 | |
Han-Wen Nienhuys | d2c6dd3 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1277 | if info['code'] == 'error': |
| 1278 | sys.stderr.write("p4 returned an error: %s\n" |
| 1279 | % info['data']) |
| 1280 | sys.exit(1) |
| 1281 | |
| 1282 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1283 | change = int(info["change"]) |
| 1284 | if change > newestRevision: |
| 1285 | newestRevision = change |
| 1286 | |
| 1287 | if info["action"] == "delete": |
Simon Hausmann | c45b1cf | 2007-04-08 10:13:32 +0200 | [diff] [blame] | 1288 | # don't increase the file cnt, otherwise details["depotFile123"] will have gaps! |
| 1289 | #fileCnt = fileCnt + 1 |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1290 | continue |
| 1291 | |
Han-Wen Nienhuys | 96e07dd | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1292 | for prop in ["depotFile", "rev", "action", "type" ]: |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1293 | details["%s%s" % (prop, fileCnt)] = info[prop] |
| 1294 | |
| 1295 | fileCnt = fileCnt + 1 |
| 1296 | |
| 1297 | details["change"] = newestRevision |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1298 | self.updateOptionDict(details) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1299 | try: |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1300 | self.commit(details, self.extractFilesFromCommit(details), self.branch, self.depotPaths) |
Simon Hausmann | c715706 | 2007-03-20 21:13:49 +0100 | [diff] [blame] | 1301 | except IOError: |
Simon Hausmann | fd4ca86 | 2007-04-13 22:21:10 +0200 | [diff] [blame] | 1302 | 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] | 1303 | print self.gitError.read() |
| 1304 | |
| 1305 | else: |
| 1306 | changes = [] |
| 1307 | |
Simon Hausmann | 0828ab1 | 2007-03-20 20:59:30 +0100 | [diff] [blame] | 1308 | if len(self.changesFile) > 0: |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1309 | output = open(self.changesFile).readlines() |
| 1310 | changeSet = Set() |
| 1311 | for line in output: |
| 1312 | changeSet.add(int(line)) |
| 1313 | |
| 1314 | for change in changeSet: |
| 1315 | changes.append(change) |
| 1316 | |
| 1317 | changes.sort() |
| 1318 | else: |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1319 | if self.verbose: |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1320 | print "Getting p4 changes for %s...%s" % (', '.join(self.depotPaths), |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1321 | self.changeRange) |
| 1322 | assert self.depotPaths |
| 1323 | output = read_pipe_lines("p4 changes " + ' '.join (["%s...%s" % (p, self.changeRange) |
| 1324 | for p in self.depotPaths])) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1325 | |
| 1326 | for line in output: |
| 1327 | changeNum = line.split(" ")[1] |
Reece H. Dunn | 7da660f | 2007-08-13 19:40:50 +0100 | [diff] [blame] | 1328 | changes.append(int(changeNum)) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1329 | |
Han-Wen Nienhuys | a4eba02 | 2007-07-23 15:51:49 -0700 | [diff] [blame] | 1330 | changes.sort() |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1331 | |
Simon Hausmann | 01a9c9c | 2007-05-23 00:07:35 +0200 | [diff] [blame] | 1332 | if len(self.maxChanges) > 0: |
Han-Wen Nienhuys | 7fcff9d | 2007-07-23 15:56:37 -0700 | [diff] [blame] | 1333 | changes = changes[:min(int(self.maxChanges), len(changes))] |
Simon Hausmann | 01a9c9c | 2007-05-23 00:07:35 +0200 | [diff] [blame] | 1334 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1335 | if len(changes) == 0: |
Simon Hausmann | 0828ab1 | 2007-03-20 20:59:30 +0100 | [diff] [blame] | 1336 | if not self.silent: |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 1337 | print "No changes to import!" |
Simon Hausmann | 1f52af6 | 2007-04-08 00:07:02 +0200 | [diff] [blame] | 1338 | return True |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1339 | |
Simon Hausmann | a9d1a27 | 2007-06-11 23:28:03 +0200 | [diff] [blame] | 1340 | if not self.silent and not self.detectBranches: |
| 1341 | print "Import destination: %s" % self.branch |
| 1342 | |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 1343 | self.updatedBranches = set() |
| 1344 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1345 | cnt = 1 |
| 1346 | for change in changes: |
| 1347 | description = p4Cmd("describe %s" % change) |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1348 | self.updateOptionDict(description) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1349 | |
Simon Hausmann | 0828ab1 | 2007-03-20 20:59:30 +0100 | [diff] [blame] | 1350 | if not self.silent: |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 1351 | sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes))) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1352 | sys.stdout.flush() |
| 1353 | cnt = cnt + 1 |
| 1354 | |
| 1355 | try: |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1356 | if self.detectBranches: |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 1357 | branches = self.splitFilesIntoBranches(description) |
Simon Hausmann | d590467 | 2007-05-19 11:07:32 +0200 | [diff] [blame] | 1358 | for branch in branches.keys(): |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1359 | ## HACK --hwn |
| 1360 | branchPrefix = self.depotPaths[0] + branch + "/" |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1361 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1362 | parent = "" |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1363 | |
Simon Hausmann | d590467 | 2007-05-19 11:07:32 +0200 | [diff] [blame] | 1364 | filesForCommit = branches[branch] |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1365 | |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1366 | if self.verbose: |
| 1367 | print "branch is %s" % branch |
| 1368 | |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 1369 | self.updatedBranches.add(branch) |
| 1370 | |
Simon Hausmann | 8f9b2e0 | 2007-05-18 22:13:26 +0200 | [diff] [blame] | 1371 | if branch not in self.createdBranches: |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1372 | self.createdBranches.add(branch) |
Simon Hausmann | 4b97ffb | 2007-05-18 21:45:23 +0200 | [diff] [blame] | 1373 | parent = self.knownBranches[branch] |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1374 | if parent == branch: |
| 1375 | parent = "" |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1376 | elif self.verbose: |
| 1377 | print "parent determined through known branches: %s" % parent |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1378 | |
Simon Hausmann | 8f9b2e0 | 2007-05-18 22:13:26 +0200 | [diff] [blame] | 1379 | # main branch? use master |
| 1380 | if branch == "main": |
| 1381 | branch = "master" |
| 1382 | else: |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1383 | |
| 1384 | ## FIXME |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1385 | branch = self.projectName + branch |
Simon Hausmann | 8f9b2e0 | 2007-05-18 22:13:26 +0200 | [diff] [blame] | 1386 | |
| 1387 | if parent == "main": |
| 1388 | parent = "master" |
| 1389 | elif len(parent) > 0: |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1390 | ## FIXME |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1391 | parent = self.projectName + parent |
Simon Hausmann | 8f9b2e0 | 2007-05-18 22:13:26 +0200 | [diff] [blame] | 1392 | |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 1393 | branch = self.refPrefix + branch |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1394 | if len(parent) > 0: |
Simon Hausmann | a028a98 | 2007-05-23 00:03:08 +0200 | [diff] [blame] | 1395 | parent = self.refPrefix + parent |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1396 | |
| 1397 | if self.verbose: |
| 1398 | print "looking for initial parent for %s; current parent is %s" % (branch, parent) |
| 1399 | |
| 1400 | if len(parent) == 0 and branch in self.initialParents: |
| 1401 | parent = self.initialParents[branch] |
| 1402 | del self.initialParents[branch] |
| 1403 | |
Simon Hausmann | 86fda6a | 2007-06-11 08:54:45 +0200 | [diff] [blame] | 1404 | self.commit(description, filesForCommit, branch, [branchPrefix], parent) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1405 | else: |
Simon Hausmann | 71b112d | 2007-05-19 11:54:11 +0200 | [diff] [blame] | 1406 | files = self.extractFilesFromCommit(description) |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1407 | self.commit(description, files, self.branch, self.depotPaths, |
| 1408 | self.initialParent) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1409 | self.initialParent = "" |
| 1410 | except IOError: |
| 1411 | print self.gitError.read() |
| 1412 | sys.exit(1) |
| 1413 | |
Simon Hausmann | 341dc1c | 2007-05-21 00:39:16 +0200 | [diff] [blame] | 1414 | if not self.silent: |
| 1415 | print "" |
| 1416 | if len(self.updatedBranches) > 0: |
| 1417 | sys.stdout.write("Updated branches: ") |
| 1418 | for b in self.updatedBranches: |
| 1419 | sys.stdout.write("%s " % b) |
| 1420 | sys.stdout.write("\n") |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1421 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1422 | |
| 1423 | self.gitStream.close() |
Simon Hausmann | 29bdbac | 2007-05-19 10:23:12 +0200 | [diff] [blame] | 1424 | if importProcess.wait() != 0: |
| 1425 | die("fast-import failed: %s" % self.gitError.read()) |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1426 | self.gitOutput.close() |
| 1427 | self.gitError.close() |
| 1428 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1429 | return True |
| 1430 | |
Simon Hausmann | 01ce1fe | 2007-04-07 23:46:50 +0200 | [diff] [blame] | 1431 | class P4Rebase(Command): |
| 1432 | def __init__(self): |
| 1433 | Command.__init__(self) |
Simon Hausmann | 0126510 | 2007-05-25 10:36:10 +0200 | [diff] [blame] | 1434 | self.options = [ ] |
Han-Wen Nienhuys | cebdf5a | 2007-05-23 16:53:11 -0300 | [diff] [blame] | 1435 | self.description = ("Fetches the latest revision from perforce and " |
| 1436 | + "rebases the current work (branch) against it") |
Simon Hausmann | 68c4215 | 2007-06-07 12:51:03 +0200 | [diff] [blame] | 1437 | self.verbose = False |
Simon Hausmann | 01ce1fe | 2007-04-07 23:46:50 +0200 | [diff] [blame] | 1438 | |
| 1439 | def run(self, args): |
| 1440 | sync = P4Sync() |
| 1441 | sync.run([]) |
Simon Hausmann | d7e3868 | 2007-06-12 14:34:46 +0200 | [diff] [blame] | 1442 | |
| 1443 | [upstream, settings] = findUpstreamBranchPoint() |
| 1444 | if len(upstream) == 0: |
| 1445 | die("Cannot find upstream branchpoint for rebase") |
| 1446 | |
| 1447 | # the branchpoint may be p4/foo~3, so strip off the parent |
| 1448 | upstream = re.sub("~[0-9]+$", "", upstream) |
| 1449 | |
| 1450 | print "Rebasing the current branch onto %s" % upstream |
Han-Wen Nienhuys | b25b206 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1451 | oldHead = read_pipe("git rev-parse HEAD").strip() |
Simon Hausmann | d7e3868 | 2007-06-12 14:34:46 +0200 | [diff] [blame] | 1452 | system("git rebase %s" % upstream) |
Simon Hausmann | 1f52af6 | 2007-04-08 00:07:02 +0200 | [diff] [blame] | 1453 | system("git diff-tree --stat --summary -M %s HEAD" % oldHead) |
Simon Hausmann | 01ce1fe | 2007-04-07 23:46:50 +0200 | [diff] [blame] | 1454 | return True |
| 1455 | |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1456 | class P4Clone(P4Sync): |
| 1457 | def __init__(self): |
| 1458 | P4Sync.__init__(self) |
| 1459 | self.description = "Creates a new git repository and imports from Perforce into it" |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1460 | self.usage = "usage: %prog [options] //depot/path[@revRange]" |
| 1461 | self.options.append( |
| 1462 | optparse.make_option("--destination", dest="cloneDestination", |
| 1463 | action='store', default=None, |
| 1464 | help="where to leave result of the clone")) |
| 1465 | self.cloneDestination = None |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1466 | self.needsGit = False |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1467 | |
Han-Wen Nienhuys | 6a49f8e | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1468 | def defaultDestination(self, args): |
| 1469 | ## TODO: use common prefix of args? |
| 1470 | depotPath = args[0] |
| 1471 | depotDir = re.sub("(@[^@]*)$", "", depotPath) |
| 1472 | depotDir = re.sub("(#[^#]*)$", "", depotDir) |
| 1473 | depotDir = re.sub(r"\.\.\.$,", "", depotDir) |
| 1474 | depotDir = re.sub(r"/$", "", depotDir) |
| 1475 | return os.path.split(depotDir)[1] |
| 1476 | |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1477 | def run(self, args): |
| 1478 | if len(args) < 1: |
| 1479 | return False |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1480 | |
| 1481 | if self.keepRepoPath and not self.cloneDestination: |
| 1482 | sys.stderr.write("Must specify destination for --keep-path\n") |
| 1483 | sys.exit(1) |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1484 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1485 | depotPaths = args |
Simon Hausmann | 5e100b5 | 2007-06-07 21:12:25 +0200 | [diff] [blame] | 1486 | |
| 1487 | if not self.cloneDestination and len(depotPaths) > 1: |
| 1488 | self.cloneDestination = depotPaths[-1] |
| 1489 | depotPaths = depotPaths[:-1] |
| 1490 | |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1491 | for p in depotPaths: |
| 1492 | if not p.startswith("//"): |
| 1493 | return False |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1494 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1495 | if not self.cloneDestination: |
Marius Storm-Olsen | 98ad4fa | 2007-06-07 15:08:33 +0200 | [diff] [blame] | 1496 | self.cloneDestination = self.defaultDestination(args) |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1497 | |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1498 | print "Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination) |
Kevin Green | c3bf3f1 | 2007-06-11 16:48:07 -0400 | [diff] [blame] | 1499 | if not os.path.exists(self.cloneDestination): |
| 1500 | os.makedirs(self.cloneDestination) |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1501 | os.chdir(self.cloneDestination) |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1502 | system("git init") |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1503 | self.gitdir = os.getcwd() + "/.git" |
Han-Wen Nienhuys | 6326aa5 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1504 | if not P4Sync.run(self, depotPaths): |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1505 | return False |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1506 | if self.branch != "master": |
Simon Hausmann | 8f9b2e0 | 2007-05-18 22:13:26 +0200 | [diff] [blame] | 1507 | if gitBranchExists("refs/remotes/p4/master"): |
| 1508 | system("git branch master refs/remotes/p4/master") |
| 1509 | system("git checkout -f") |
| 1510 | else: |
| 1511 | print "Could not detect main branch. No checkout/master branch created." |
Han-Wen Nienhuys | 86dff6b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1512 | |
Simon Hausmann | f9a3a4f | 2007-04-08 10:08:26 +0200 | [diff] [blame] | 1513 | return True |
| 1514 | |
Simon Hausmann | 09d89de | 2007-06-20 23:10:28 +0200 | [diff] [blame] | 1515 | class P4Branches(Command): |
| 1516 | def __init__(self): |
| 1517 | Command.__init__(self) |
| 1518 | self.options = [ ] |
| 1519 | self.description = ("Shows the git branches that hold imports and their " |
| 1520 | + "corresponding perforce depot paths") |
| 1521 | self.verbose = False |
| 1522 | |
| 1523 | def run(self, args): |
Simon Hausmann | 5ca4461 | 2007-08-24 17:44:16 +0200 | [diff] [blame^] | 1524 | if originP4BranchesExist(): |
| 1525 | createOrUpdateBranchesFromOrigin() |
| 1526 | |
Simon Hausmann | 09d89de | 2007-06-20 23:10:28 +0200 | [diff] [blame] | 1527 | cmdline = "git rev-parse --symbolic " |
| 1528 | cmdline += " --remotes" |
| 1529 | |
| 1530 | for line in read_pipe_lines(cmdline): |
| 1531 | line = line.strip() |
| 1532 | |
| 1533 | if not line.startswith('p4/') or line == "p4/HEAD": |
| 1534 | continue |
| 1535 | branch = line |
| 1536 | |
| 1537 | log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch) |
| 1538 | settings = extractSettingsGitLog(log) |
| 1539 | |
| 1540 | print "%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"]) |
| 1541 | return True |
| 1542 | |
Simon Hausmann | b984733 | 2007-03-20 20:54:23 +0100 | [diff] [blame] | 1543 | class HelpFormatter(optparse.IndentedHelpFormatter): |
| 1544 | def __init__(self): |
| 1545 | optparse.IndentedHelpFormatter.__init__(self) |
| 1546 | |
| 1547 | def format_description(self, description): |
| 1548 | if description: |
| 1549 | return description + "\n" |
| 1550 | else: |
| 1551 | return "" |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1552 | |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 1553 | def printUsage(commands): |
| 1554 | print "usage: %s <command> [options]" % sys.argv[0] |
| 1555 | print "" |
| 1556 | print "valid commands: %s" % ", ".join(commands) |
| 1557 | print "" |
| 1558 | print "Try %s <command> --help for command specific help." % sys.argv[0] |
| 1559 | print "" |
| 1560 | |
| 1561 | commands = { |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1562 | "debug" : P4Debug, |
| 1563 | "submit" : P4Submit, |
| 1564 | "sync" : P4Sync, |
| 1565 | "rebase" : P4Rebase, |
| 1566 | "clone" : P4Clone, |
Simon Hausmann | 09d89de | 2007-06-20 23:10:28 +0200 | [diff] [blame] | 1567 | "rollback" : P4RollBack, |
| 1568 | "branches" : P4Branches |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 1569 | } |
| 1570 | |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 1571 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1572 | def main(): |
| 1573 | if len(sys.argv[1:]) == 0: |
| 1574 | printUsage(commands.keys()) |
| 1575 | sys.exit(2) |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 1576 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1577 | cmd = "" |
| 1578 | cmdName = sys.argv[1] |
| 1579 | try: |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1580 | klass = commands[cmdName] |
| 1581 | cmd = klass() |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1582 | except KeyError: |
| 1583 | print "unknown command %s" % cmdName |
| 1584 | print "" |
| 1585 | printUsage(commands.keys()) |
| 1586 | sys.exit(2) |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1587 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1588 | options = cmd.options |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1589 | cmd.gitdir = os.environ.get("GIT_DIR", None) |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 1590 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1591 | args = sys.argv[2:] |
Simon Hausmann | e20a9e5 | 2007-03-26 00:13:51 +0200 | [diff] [blame] | 1592 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1593 | if len(options) > 0: |
| 1594 | options.append(optparse.make_option("--git-dir", dest="gitdir")) |
Simon Hausmann | e20a9e5 | 2007-03-26 00:13:51 +0200 | [diff] [blame] | 1595 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1596 | parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName), |
| 1597 | options, |
| 1598 | description = cmd.description, |
| 1599 | formatter = HelpFormatter()) |
Simon Hausmann | 86949ee | 2007-03-19 20:59:12 +0100 | [diff] [blame] | 1600 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1601 | (cmd, args) = parser.parse_args(sys.argv[2:], cmd); |
| 1602 | global verbose |
| 1603 | verbose = cmd.verbose |
| 1604 | if cmd.needsGit: |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1605 | if cmd.gitdir == None: |
| 1606 | cmd.gitdir = os.path.abspath(".git") |
| 1607 | if not isValidGitDir(cmd.gitdir): |
| 1608 | cmd.gitdir = read_pipe("git rev-parse --git-dir").strip() |
| 1609 | if os.path.exists(cmd.gitdir): |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1610 | cdup = read_pipe("git rev-parse --show-cdup").strip() |
| 1611 | if len(cdup) > 0: |
| 1612 | os.chdir(cdup); |
| 1613 | |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1614 | if not isValidGitDir(cmd.gitdir): |
| 1615 | if isValidGitDir(cmd.gitdir + "/.git"): |
| 1616 | cmd.gitdir += "/.git" |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1617 | else: |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1618 | die("fatal: cannot locate git repository at %s" % cmd.gitdir) |
Simon Hausmann | 8910ac0 | 2007-03-26 08:18:55 +0200 | [diff] [blame] | 1619 | |
Han-Wen Nienhuys | b86f737 | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1620 | os.environ["GIT_DIR"] = cmd.gitdir |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1621 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1622 | if not cmd.run(args): |
| 1623 | parser.print_help() |
Simon Hausmann | 4f5cf76 | 2007-03-19 22:25:17 +0100 | [diff] [blame] | 1624 | |
Han-Wen Nienhuys | bb6e09b | 2007-05-23 18:49:35 -0300 | [diff] [blame] | 1625 | |
| 1626 | if __name__ == '__main__': |
| 1627 | main() |