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