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