]> git.ipfire.org Git - thirdparty/git.git/blame - git-p4.py
Merge branch 'nd/gc-auto-background-fix' into maint
[thirdparty/git.git] / git-p4.py
CommitLineData
86949eef
SH
1#!/usr/bin/env python
2#
3# git-p4.py -- A tool for bidirectional operation between a Perforce depot and git.
4#
c8cbbee9
SH
5# Author: Simon Hausmann <simon@lst.de>
6# Copyright: 2007 Simon Hausmann <simon@lst.de>
83dce55a 7# 2007 Trolltech ASA
86949eef
SH
8# License: MIT <http://www.opensource.org/licenses/mit-license.php>
9#
a33faf28
ER
10import sys
11if sys.hexversion < 0x02040000:
12 # The limiter is the subprocess module
13 sys.stderr.write("git-p4: requires Python 2.4 or later.\n")
14 sys.exit(1)
f629fa59
PW
15import os
16import optparse
17import marshal
18import subprocess
19import tempfile
20import time
21import platform
22import re
23import shutil
d20f0f8e 24import stat
8b41a97f 25
a235e85c
BC
26try:
27 from subprocess import CalledProcessError
28except ImportError:
29 # from python2.7:subprocess.py
30 # Exception classes used by this module.
31 class CalledProcessError(Exception):
32 """This exception is raised when a process run by check_call() returns
33 a non-zero exit status. The exit status will be stored in the
34 returncode attribute."""
35 def __init__(self, returncode, cmd):
36 self.returncode = returncode
37 self.cmd = cmd
38 def __str__(self):
39 return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)
40
4addad22 41verbose = False
86949eef 42
06804c76 43# Only labels/tags matching this will be imported/exported
c8942a22 44defaultLabelRegexp = r'[a-zA-Z0-9_\-.]+$'
21a50753 45
1051ef00
LD
46# Grab changes in blocks of this many revisions, unless otherwise requested
47defaultBlockSize = 512
48
21a50753
AK
49def p4_build_cmd(cmd):
50 """Build a suitable p4 command line.
51
52 This consolidates building and returning a p4 command line into one
53 location. It means that hooking into the environment, or other configuration
54 can be done more easily.
55 """
6de040df 56 real_cmd = ["p4"]
abcaf073
AK
57
58 user = gitConfig("git-p4.user")
59 if len(user) > 0:
6de040df 60 real_cmd += ["-u",user]
abcaf073
AK
61
62 password = gitConfig("git-p4.password")
63 if len(password) > 0:
6de040df 64 real_cmd += ["-P", password]
abcaf073
AK
65
66 port = gitConfig("git-p4.port")
67 if len(port) > 0:
6de040df 68 real_cmd += ["-p", port]
abcaf073
AK
69
70 host = gitConfig("git-p4.host")
71 if len(host) > 0:
41799aa2 72 real_cmd += ["-H", host]
abcaf073
AK
73
74 client = gitConfig("git-p4.client")
75 if len(client) > 0:
6de040df 76 real_cmd += ["-c", client]
abcaf073 77
6de040df
LD
78
79 if isinstance(cmd,basestring):
80 real_cmd = ' '.join(real_cmd) + ' ' + cmd
81 else:
82 real_cmd += cmd
21a50753
AK
83 return real_cmd
84
bbd84863
MF
85def chdir(path, is_client_path=False):
86 """Do chdir to the given path, and set the PWD environment
87 variable for use by P4. It does not look at getcwd() output.
88 Since we're not using the shell, it is necessary to set the
89 PWD environment variable explicitly.
90
91 Normally, expand the path to force it to be absolute. This
92 addresses the use of relative path names inside P4 settings,
93 e.g. P4CONFIG=.p4config. P4 does not simply open the filename
94 as given; it looks for .p4config using PWD.
95
96 If is_client_path, the path was handed to us directly by p4,
97 and may be a symbolic link. Do not call os.getcwd() in this
98 case, because it will cause p4 to think that PWD is not inside
99 the client path.
100 """
101
102 os.chdir(path)
103 if not is_client_path:
104 path = os.getcwd()
105 os.environ['PWD'] = path
053fd0c1 106
86dff6b6
HWN
107def die(msg):
108 if verbose:
109 raise Exception(msg)
110 else:
111 sys.stderr.write(msg + "\n")
112 sys.exit(1)
113
6de040df 114def write_pipe(c, stdin):
4addad22 115 if verbose:
6de040df 116 sys.stderr.write('Writing pipe: %s\n' % str(c))
b016d397 117
6de040df
LD
118 expand = isinstance(c,basestring)
119 p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand)
120 pipe = p.stdin
121 val = pipe.write(stdin)
122 pipe.close()
123 if p.wait():
124 die('Command failed: %s' % str(c))
b016d397
HWN
125
126 return val
127
6de040df 128def p4_write_pipe(c, stdin):
d9429194 129 real_cmd = p4_build_cmd(c)
6de040df 130 return write_pipe(real_cmd, stdin)
d9429194 131
4addad22
HWN
132def read_pipe(c, ignore_error=False):
133 if verbose:
6de040df 134 sys.stderr.write('Reading pipe: %s\n' % str(c))
8b41a97f 135
6de040df 136 expand = isinstance(c,basestring)
1f5f3907
LS
137 p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=expand)
138 (out, err) = p.communicate()
139 if p.returncode != 0 and not ignore_error:
140 die('Command failed: %s\nError: %s' % (str(c), err))
141 return out
b016d397 142
d9429194
AK
143def p4_read_pipe(c, ignore_error=False):
144 real_cmd = p4_build_cmd(c)
145 return read_pipe(real_cmd, ignore_error)
b016d397 146
bce4c5fc 147def read_pipe_lines(c):
4addad22 148 if verbose:
6de040df
LD
149 sys.stderr.write('Reading pipe: %s\n' % str(c))
150
151 expand = isinstance(c, basestring)
152 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
153 pipe = p.stdout
b016d397 154 val = pipe.readlines()
6de040df
LD
155 if pipe.close() or p.wait():
156 die('Command failed: %s' % str(c))
b016d397
HWN
157
158 return val
caace111 159
2318121b
AK
160def p4_read_pipe_lines(c):
161 """Specifically invoke p4 on the command supplied. """
155af834 162 real_cmd = p4_build_cmd(c)
2318121b
AK
163 return read_pipe_lines(real_cmd)
164
8e9497c2
GG
165def p4_has_command(cmd):
166 """Ask p4 for help on this command. If it returns an error, the
167 command does not exist in this version of p4."""
168 real_cmd = p4_build_cmd(["help", cmd])
169 p = subprocess.Popen(real_cmd, stdout=subprocess.PIPE,
170 stderr=subprocess.PIPE)
171 p.communicate()
172 return p.returncode == 0
173
249da4c0
PW
174def p4_has_move_command():
175 """See if the move command exists, that it supports -k, and that
176 it has not been administratively disabled. The arguments
177 must be correct, but the filenames do not have to exist. Use
178 ones with wildcards so even if they exist, it will fail."""
179
180 if not p4_has_command("move"):
181 return False
182 cmd = p4_build_cmd(["move", "-k", "@from", "@to"])
183 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
184 (out, err) = p.communicate()
185 # return code will be 1 in either case
186 if err.find("Invalid option") >= 0:
187 return False
188 if err.find("disabled") >= 0:
189 return False
190 # assume it failed because @... was invalid changelist
191 return True
192
6754a299 193def system(cmd):
6de040df 194 expand = isinstance(cmd,basestring)
4addad22 195 if verbose:
6de040df 196 sys.stderr.write("executing %s\n" % str(cmd))
a235e85c
BC
197 retcode = subprocess.call(cmd, shell=expand)
198 if retcode:
199 raise CalledProcessError(retcode, cmd)
6754a299 200
bf9320f1
AK
201def p4_system(cmd):
202 """Specifically invoke p4 as the system command. """
155af834 203 real_cmd = p4_build_cmd(cmd)
6de040df 204 expand = isinstance(real_cmd, basestring)
a235e85c
BC
205 retcode = subprocess.call(real_cmd, shell=expand)
206 if retcode:
207 raise CalledProcessError(retcode, real_cmd)
6de040df 208
7f0e5962
PW
209_p4_version_string = None
210def p4_version_string():
211 """Read the version string, showing just the last line, which
212 hopefully is the interesting version bit.
213
214 $ p4 -V
215 Perforce - The Fast Software Configuration Management System.
216 Copyright 1995-2011 Perforce Software. All rights reserved.
217 Rev. P4/NTX86/2011.1/393975 (2011/12/16).
218 """
219 global _p4_version_string
220 if not _p4_version_string:
221 a = p4_read_pipe_lines(["-V"])
222 _p4_version_string = a[-1].rstrip()
223 return _p4_version_string
224
6de040df 225def p4_integrate(src, dest):
9d7d446a 226 p4_system(["integrate", "-Dt", wildcard_encode(src), wildcard_encode(dest)])
6de040df 227
8d7ec362 228def p4_sync(f, *options):
9d7d446a 229 p4_system(["sync"] + list(options) + [wildcard_encode(f)])
6de040df
LD
230
231def p4_add(f):
9d7d446a
PW
232 # forcibly add file names with wildcards
233 if wildcard_present(f):
234 p4_system(["add", "-f", f])
235 else:
236 p4_system(["add", f])
6de040df
LD
237
238def p4_delete(f):
9d7d446a 239 p4_system(["delete", wildcard_encode(f)])
6de040df
LD
240
241def p4_edit(f):
9d7d446a 242 p4_system(["edit", wildcard_encode(f)])
6de040df
LD
243
244def p4_revert(f):
9d7d446a 245 p4_system(["revert", wildcard_encode(f)])
6de040df 246
9d7d446a
PW
247def p4_reopen(type, f):
248 p4_system(["reopen", "-t", type, wildcard_encode(f)])
bf9320f1 249
8e9497c2
GG
250def p4_move(src, dest):
251 p4_system(["move", "-k", wildcard_encode(src), wildcard_encode(dest)])
252
1051ef00
LD
253def p4_last_change():
254 results = p4CmdList(["changes", "-m", "1"])
255 return int(results[0]['change'])
256
18fa13d0
PW
257def p4_describe(change):
258 """Make sure it returns a valid result by checking for
259 the presence of field "time". Return a dict of the
260 results."""
261
262 ds = p4CmdList(["describe", "-s", str(change)])
263 if len(ds) != 1:
264 die("p4 describe -s %d did not return 1 result: %s" % (change, str(ds)))
265
266 d = ds[0]
267
268 if "p4ExitCode" in d:
269 die("p4 describe -s %d exited with %d: %s" % (change, d["p4ExitCode"],
270 str(d)))
271 if "code" in d:
272 if d["code"] == "error":
273 die("p4 describe -s %d returned error code: %s" % (change, str(d)))
274
275 if "time" not in d:
276 die("p4 describe -s %d returned no \"time\": %s" % (change, str(d)))
277
278 return d
279
9cffb8c8
PW
280#
281# Canonicalize the p4 type and return a tuple of the
282# base type, plus any modifiers. See "p4 help filetypes"
283# for a list and explanation.
284#
285def split_p4_type(p4type):
286
287 p4_filetypes_historical = {
288 "ctempobj": "binary+Sw",
289 "ctext": "text+C",
290 "cxtext": "text+Cx",
291 "ktext": "text+k",
292 "kxtext": "text+kx",
293 "ltext": "text+F",
294 "tempobj": "binary+FSw",
295 "ubinary": "binary+F",
296 "uresource": "resource+F",
297 "uxbinary": "binary+Fx",
298 "xbinary": "binary+x",
299 "xltext": "text+Fx",
300 "xtempobj": "binary+Swx",
301 "xtext": "text+x",
302 "xunicode": "unicode+x",
303 "xutf16": "utf16+x",
304 }
305 if p4type in p4_filetypes_historical:
306 p4type = p4_filetypes_historical[p4type]
307 mods = ""
308 s = p4type.split("+")
309 base = s[0]
310 mods = ""
311 if len(s) > 1:
312 mods = s[1]
313 return (base, mods)
b9fc6ea9 314
60df071c
LD
315#
316# return the raw p4 type of a file (text, text+ko, etc)
317#
79467e61
PW
318def p4_type(f):
319 results = p4CmdList(["fstat", "-T", "headType", wildcard_encode(f)])
60df071c
LD
320 return results[0]['headType']
321
322#
323# Given a type base and modifier, return a regexp matching
324# the keywords that can be expanded in the file
325#
326def p4_keywords_regexp_for_type(base, type_mods):
327 if base in ("text", "unicode", "binary"):
328 kwords = None
329 if "ko" in type_mods:
330 kwords = 'Id|Header'
331 elif "k" in type_mods:
332 kwords = 'Id|Header|Author|Date|DateTime|Change|File|Revision'
333 else:
334 return None
335 pattern = r"""
336 \$ # Starts with a dollar, followed by...
337 (%s) # one of the keywords, followed by...
6b2bf41e 338 (:[^$\n]+)? # possibly an old expansion, followed by...
60df071c
LD
339 \$ # another dollar
340 """ % kwords
341 return pattern
342 else:
343 return None
344
345#
346# Given a file, return a regexp matching the possible
347# RCS keywords that will be expanded, or None for files
348# with kw expansion turned off.
349#
350def p4_keywords_regexp_for_file(file):
351 if not os.path.exists(file):
352 return None
353 else:
354 (type_base, type_mods) = split_p4_type(p4_type(file))
355 return p4_keywords_regexp_for_type(type_base, type_mods)
b9fc6ea9 356
c65b670e
CP
357def setP4ExecBit(file, mode):
358 # Reopens an already open file and changes the execute bit to match
359 # the execute bit setting in the passed in mode.
360
361 p4Type = "+x"
362
363 if not isModeExec(mode):
364 p4Type = getP4OpenedType(file)
365 p4Type = re.sub('^([cku]?)x(.*)', '\\1\\2', p4Type)
366 p4Type = re.sub('(.*?\+.*?)x(.*?)', '\\1\\2', p4Type)
367 if p4Type[-1] == "+":
368 p4Type = p4Type[0:-1]
369
6de040df 370 p4_reopen(p4Type, file)
c65b670e
CP
371
372def getP4OpenedType(file):
373 # Returns the perforce file type for the given file.
374
9d7d446a 375 result = p4_read_pipe(["opened", wildcard_encode(file)])
34a0dbfc 376 match = re.match(".*\((.+)\)( \*exclusive\*)?\r?$", result)
c65b670e
CP
377 if match:
378 return match.group(1)
379 else:
f3e5ae4f 380 die("Could not determine file type for %s (result: '%s')" % (file, result))
c65b670e 381
06804c76
LD
382# Return the set of all p4 labels
383def getP4Labels(depotPaths):
384 labels = set()
385 if isinstance(depotPaths,basestring):
386 depotPaths = [depotPaths]
387
388 for l in p4CmdList(["labels"] + ["%s..." % p for p in depotPaths]):
389 label = l['label']
390 labels.add(label)
391
392 return labels
393
394# Return the set of all git tags
395def getGitTags():
396 gitTags = set()
397 for line in read_pipe_lines(["git", "tag"]):
398 tag = line.strip()
399 gitTags.add(tag)
400 return gitTags
401
b43b0a3c
CP
402def diffTreePattern():
403 # This is a simple generator for the diff tree regex pattern. This could be
404 # a class variable if this and parseDiffTreeEntry were a part of a class.
405 pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)')
406 while True:
407 yield pattern
408
409def parseDiffTreeEntry(entry):
410 """Parses a single diff tree entry into its component elements.
411
412 See git-diff-tree(1) manpage for details about the format of the diff
413 output. This method returns a dictionary with the following elements:
414
415 src_mode - The mode of the source file
416 dst_mode - The mode of the destination file
417 src_sha1 - The sha1 for the source file
418 dst_sha1 - The sha1 fr the destination file
419 status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc)
420 status_score - The score for the status (applicable for 'C' and 'R'
421 statuses). This is None if there is no score.
422 src - The path for the source file.
423 dst - The path for the destination file. This is only present for
424 copy or renames. If it is not present, this is None.
425
426 If the pattern is not matched, None is returned."""
427
428 match = diffTreePattern().next().match(entry)
429 if match:
430 return {
431 'src_mode': match.group(1),
432 'dst_mode': match.group(2),
433 'src_sha1': match.group(3),
434 'dst_sha1': match.group(4),
435 'status': match.group(5),
436 'status_score': match.group(6),
437 'src': match.group(7),
438 'dst': match.group(10)
439 }
440 return None
441
c65b670e
CP
442def isModeExec(mode):
443 # Returns True if the given git mode represents an executable file,
444 # otherwise False.
445 return mode[-3:] == "755"
446
447def isModeExecChanged(src_mode, dst_mode):
448 return isModeExec(src_mode) != isModeExec(dst_mode)
449
b932705b 450def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None):
6de040df
LD
451
452 if isinstance(cmd,basestring):
453 cmd = "-G " + cmd
454 expand = True
455 else:
456 cmd = ["-G"] + cmd
457 expand = False
458
459 cmd = p4_build_cmd(cmd)
6a49f8e2 460 if verbose:
6de040df 461 sys.stderr.write("Opening pipe: %s\n" % str(cmd))
9f90c733
SL
462
463 # Use a temporary file to avoid deadlocks without
464 # subprocess.communicate(), which would put another copy
465 # of stdout into memory.
466 stdin_file = None
467 if stdin is not None:
468 stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode)
6de040df
LD
469 if isinstance(stdin,basestring):
470 stdin_file.write(stdin)
471 else:
472 for i in stdin:
473 stdin_file.write(i + '\n')
9f90c733
SL
474 stdin_file.flush()
475 stdin_file.seek(0)
476
6de040df
LD
477 p4 = subprocess.Popen(cmd,
478 shell=expand,
9f90c733
SL
479 stdin=stdin_file,
480 stdout=subprocess.PIPE)
86949eef
SH
481
482 result = []
483 try:
484 while True:
9f90c733 485 entry = marshal.load(p4.stdout)
c3f6163b
AG
486 if cb is not None:
487 cb(entry)
488 else:
489 result.append(entry)
86949eef
SH
490 except EOFError:
491 pass
9f90c733
SL
492 exitCode = p4.wait()
493 if exitCode != 0:
ac3e0d79
SH
494 entry = {}
495 entry["p4ExitCode"] = exitCode
496 result.append(entry)
86949eef
SH
497
498 return result
499
500def p4Cmd(cmd):
501 list = p4CmdList(cmd)
502 result = {}
503 for entry in list:
504 result.update(entry)
505 return result;
506
cb2c9db5
SH
507def p4Where(depotPath):
508 if not depotPath.endswith("/"):
509 depotPath += "/"
cd884106
VA
510 depotPathLong = depotPath + "..."
511 outputList = p4CmdList(["where", depotPathLong])
7f705dc3
TAL
512 output = None
513 for entry in outputList:
75bc9573 514 if "depotFile" in entry:
cd884106
VA
515 # Search for the base client side depot path, as long as it starts with the branch's P4 path.
516 # The base path always ends with "/...".
517 if entry["depotFile"].find(depotPath) == 0 and entry["depotFile"][-4:] == "/...":
75bc9573
TAL
518 output = entry
519 break
520 elif "data" in entry:
521 data = entry.get("data")
522 space = data.find(" ")
523 if data[:space] == depotPath:
524 output = entry
525 break
7f705dc3
TAL
526 if output == None:
527 return ""
dc524036
SH
528 if output["code"] == "error":
529 return ""
cb2c9db5
SH
530 clientPath = ""
531 if "path" in output:
532 clientPath = output.get("path")
533 elif "data" in output:
534 data = output.get("data")
535 lastSpace = data.rfind(" ")
536 clientPath = data[lastSpace + 1:]
537
538 if clientPath.endswith("..."):
539 clientPath = clientPath[:-3]
540 return clientPath
541
86949eef 542def currentGitBranch():
b25b2065 543 return read_pipe("git name-rev HEAD").split(" ")[1].strip()
86949eef 544
4f5cf76a 545def isValidGitDir(path):
bb6e09b2
HWN
546 if (os.path.exists(path + "/HEAD")
547 and os.path.exists(path + "/refs") and os.path.exists(path + "/objects")):
4f5cf76a
SH
548 return True;
549 return False
550
463e8af6 551def parseRevision(ref):
b25b2065 552 return read_pipe("git rev-parse %s" % ref).strip()
463e8af6 553
28755dba
PW
554def branchExists(ref):
555 rev = read_pipe(["git", "rev-parse", "-q", "--verify", ref],
556 ignore_error=True)
557 return len(rev) > 0
558
6ae8de88
SH
559def extractLogMessageFromGitCommit(commit):
560 logMessage = ""
b016d397
HWN
561
562 ## fixme: title is first line of commit, not 1st paragraph.
6ae8de88 563 foundTitle = False
b016d397 564 for log in read_pipe_lines("git cat-file commit %s" % commit):
6ae8de88
SH
565 if not foundTitle:
566 if len(log) == 1:
1c094184 567 foundTitle = True
6ae8de88
SH
568 continue
569
570 logMessage += log
571 return logMessage
572
bb6e09b2 573def extractSettingsGitLog(log):
6ae8de88
SH
574 values = {}
575 for line in log.split("\n"):
576 line = line.strip()
6326aa58
HWN
577 m = re.search (r"^ *\[git-p4: (.*)\]$", line)
578 if not m:
579 continue
580
581 assignments = m.group(1).split (':')
582 for a in assignments:
583 vals = a.split ('=')
584 key = vals[0].strip()
585 val = ('='.join (vals[1:])).strip()
586 if val.endswith ('\"') and val.startswith('"'):
587 val = val[1:-1]
588
589 values[key] = val
590
845b42cb
SH
591 paths = values.get("depot-paths")
592 if not paths:
593 paths = values.get("depot-path")
a3fdd579
SH
594 if paths:
595 values['depot-paths'] = paths.split(',')
bb6e09b2 596 return values
6ae8de88 597
8136a639 598def gitBranchExists(branch):
bb6e09b2
HWN
599 proc = subprocess.Popen(["git", "rev-parse", branch],
600 stderr=subprocess.PIPE, stdout=subprocess.PIPE);
caace111 601 return proc.wait() == 0;
8136a639 602
36bd8446 603_gitConfig = {}
b345d6c3 604
0d609032 605def gitConfig(key):
36bd8446 606 if not _gitConfig.has_key(key):
0d609032 607 cmd = [ "git", "config", key ]
b345d6c3
PW
608 s = read_pipe(cmd, ignore_error=True)
609 _gitConfig[key] = s.strip()
36bd8446 610 return _gitConfig[key]
01265103 611
0d609032
PW
612def gitConfigBool(key):
613 """Return a bool, using git config --bool. It is True only if the
614 variable is set to true, and False if set to false or not present
615 in the config."""
616
36bd8446 617 if not _gitConfig.has_key(key):
0d609032
PW
618 cmd = [ "git", "config", "--bool", key ]
619 s = read_pipe(cmd, ignore_error=True)
620 v = s.strip()
621 _gitConfig[key] = v == "true"
36bd8446 622 return _gitConfig[key]
01265103 623
7199cf13
VA
624def gitConfigList(key):
625 if not _gitConfig.has_key(key):
2abba301
PW
626 s = read_pipe(["git", "config", "--get-all", key], ignore_error=True)
627 _gitConfig[key] = s.strip().split(os.linesep)
7199cf13
VA
628 return _gitConfig[key]
629
2c8037ed
PW
630def p4BranchesInGit(branchesAreInRemotes=True):
631 """Find all the branches whose names start with "p4/", looking
632 in remotes or heads as specified by the argument. Return
633 a dictionary of { branch: revision } for each one found.
634 The branch names are the short names, without any
635 "p4/" prefix."""
636
062410bb
SH
637 branches = {}
638
639 cmdline = "git rev-parse --symbolic "
640 if branchesAreInRemotes:
2c8037ed 641 cmdline += "--remotes"
062410bb 642 else:
2c8037ed 643 cmdline += "--branches"
062410bb
SH
644
645 for line in read_pipe_lines(cmdline):
646 line = line.strip()
647
2c8037ed
PW
648 # only import to p4/
649 if not line.startswith('p4/'):
650 continue
651 # special symbolic ref to p4/master
652 if line == "p4/HEAD":
062410bb 653 continue
062410bb 654
2c8037ed
PW
655 # strip off p4/ prefix
656 branch = line[len("p4/"):]
062410bb
SH
657
658 branches[branch] = parseRevision(line)
2c8037ed 659
062410bb
SH
660 return branches
661
5a8e84cd
PW
662def branch_exists(branch):
663 """Make sure that the given ref name really exists."""
664
665 cmd = [ "git", "rev-parse", "--symbolic", "--verify", branch ]
666 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
667 out, _ = p.communicate()
668 if p.returncode:
669 return False
670 # expect exactly one line of output: the branch name
671 return out.rstrip() == branch
672
9ceab363 673def findUpstreamBranchPoint(head = "HEAD"):
86506fe5
SH
674 branches = p4BranchesInGit()
675 # map from depot-path to branch name
676 branchByDepotPath = {}
677 for branch in branches.keys():
678 tip = branches[branch]
679 log = extractLogMessageFromGitCommit(tip)
680 settings = extractSettingsGitLog(log)
681 if settings.has_key("depot-paths"):
682 paths = ",".join(settings["depot-paths"])
683 branchByDepotPath[paths] = "remotes/p4/" + branch
684
27d2d811 685 settings = None
27d2d811
SH
686 parent = 0
687 while parent < 65535:
9ceab363 688 commit = head + "~%s" % parent
27d2d811
SH
689 log = extractLogMessageFromGitCommit(commit)
690 settings = extractSettingsGitLog(log)
86506fe5
SH
691 if settings.has_key("depot-paths"):
692 paths = ",".join(settings["depot-paths"])
693 if branchByDepotPath.has_key(paths):
694 return [branchByDepotPath[paths], settings]
27d2d811 695
86506fe5 696 parent = parent + 1
27d2d811 697
86506fe5 698 return ["", settings]
27d2d811 699
5ca44617
SH
700def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True):
701 if not silent:
702 print ("Creating/updating branch(es) in %s based on origin branch(es)"
703 % localRefPrefix)
704
705 originPrefix = "origin/p4/"
706
707 for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
708 line = line.strip()
709 if (not line.startswith(originPrefix)) or line.endswith("HEAD"):
710 continue
711
712 headName = line[len(originPrefix):]
713 remoteHead = localRefPrefix + headName
714 originHead = line
715
716 original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))
717 if (not original.has_key('depot-paths')
718 or not original.has_key('change')):
719 continue
720
721 update = False
722 if not gitBranchExists(remoteHead):
723 if verbose:
724 print "creating %s" % remoteHead
725 update = True
726 else:
727 settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))
728 if settings.has_key('change') > 0:
729 if settings['depot-paths'] == original['depot-paths']:
730 originP4Change = int(original['change'])
731 p4Change = int(settings['change'])
732 if originP4Change > p4Change:
733 print ("%s (%s) is newer than %s (%s). "
734 "Updating p4 branch from origin."
735 % (originHead, originP4Change,
736 remoteHead, p4Change))
737 update = True
738 else:
739 print ("Ignoring: %s was imported from %s while "
740 "%s was imported from %s"
741 % (originHead, ','.join(original['depot-paths']),
742 remoteHead, ','.join(settings['depot-paths'])))
743
744 if update:
745 system("git update-ref %s %s" % (remoteHead, originHead))
746
747def originP4BranchesExist():
748 return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
749
1051ef00
LD
750
751def p4ParseNumericChangeRange(parts):
752 changeStart = int(parts[0][1:])
753 if parts[1] == '#head':
754 changeEnd = p4_last_change()
755 else:
756 changeEnd = int(parts[1])
757
758 return (changeStart, changeEnd)
759
760def chooseBlockSize(blockSize):
761 if blockSize:
762 return blockSize
763 else:
764 return defaultBlockSize
765
766def p4ChangesForPaths(depotPaths, changeRange, requestedBlockSize):
4f6432d8 767 assert depotPaths
96b2d54a 768
1051ef00
LD
769 # Parse the change range into start and end. Try to find integer
770 # revision ranges as these can be broken up into blocks to avoid
771 # hitting server-side limits (maxrows, maxscanresults). But if
772 # that doesn't work, fall back to using the raw revision specifier
773 # strings, without using block mode.
774
96b2d54a 775 if changeRange is None or changeRange == '':
1051ef00
LD
776 changeStart = 1
777 changeEnd = p4_last_change()
778 block_size = chooseBlockSize(requestedBlockSize)
96b2d54a
LS
779 else:
780 parts = changeRange.split(',')
781 assert len(parts) == 2
1051ef00
LD
782 try:
783 (changeStart, changeEnd) = p4ParseNumericChangeRange(parts)
784 block_size = chooseBlockSize(requestedBlockSize)
785 except:
786 changeStart = parts[0][1:]
787 changeEnd = parts[1]
788 if requestedBlockSize:
789 die("cannot use --changes-block-size with non-numeric revisions")
790 block_size = None
4f6432d8 791
96b2d54a 792 # Accumulate change numbers in a dictionary to avoid duplicates
b4b0ba06 793 changes = {}
96b2d54a
LS
794
795 for p in depotPaths:
796 # Retrieve changes a block at a time, to prevent running
1051ef00
LD
797 # into a MaxResults/MaxScanRows error from the server.
798
799 while True:
96b2d54a 800 cmd = ['changes']
1051ef00
LD
801
802 if block_size:
803 end = min(changeEnd, changeStart + block_size)
804 revisionRange = "%d,%d" % (changeStart, end)
805 else:
806 revisionRange = "%s,%s" % (changeStart, changeEnd)
807
808 cmd += ["%s...@%s" % (p, revisionRange)]
809
96b2d54a
LS
810 for line in p4_read_pipe_lines(cmd):
811 changeNum = int(line.split(" ")[1])
96b2d54a 812 changes[changeNum] = True
1051ef00
LD
813
814 if not block_size:
815 break
816
817 if end >= changeEnd:
818 break
819
820 changeStart = end + 1
4f6432d8 821
b4b0ba06
PW
822 changelist = changes.keys()
823 changelist.sort()
824 return changelist
4f6432d8 825
d53de8b9
TAL
826def p4PathStartsWith(path, prefix):
827 # This method tries to remedy a potential mixed-case issue:
828 #
829 # If UserA adds //depot/DirA/file1
830 # and UserB adds //depot/dira/file2
831 #
832 # we may or may not have a problem. If you have core.ignorecase=true,
833 # we treat DirA and dira as the same directory
0d609032 834 if gitConfigBool("core.ignorecase"):
d53de8b9
TAL
835 return path.lower().startswith(prefix.lower())
836 return path.startswith(prefix)
837
543987bd
PW
838def getClientSpec():
839 """Look at the p4 client spec, create a View() object that contains
840 all the mappings, and return it."""
841
842 specList = p4CmdList("client -o")
843 if len(specList) != 1:
844 die('Output from "client -o" is %d lines, expecting 1' %
845 len(specList))
846
847 # dictionary of all client parameters
848 entry = specList[0]
849
9d57c4a6
KS
850 # the //client/ name
851 client_name = entry["Client"]
852
543987bd
PW
853 # just the keys that start with "View"
854 view_keys = [ k for k in entry.keys() if k.startswith("View") ]
855
856 # hold this new View
9d57c4a6 857 view = View(client_name)
543987bd
PW
858
859 # append the lines, in order, to the view
860 for view_num in range(len(view_keys)):
861 k = "View%d" % view_num
862 if k not in view_keys:
863 die("Expected view key %s missing" % k)
864 view.append(entry[k])
865
866 return view
867
868def getClientRoot():
869 """Grab the client directory."""
870
871 output = p4CmdList("client -o")
872 if len(output) != 1:
873 die('Output from "client -o" is %d lines, expecting 1' % len(output))
874
875 entry = output[0]
876 if "Root" not in entry:
877 die('Client has no "Root"')
878
879 return entry["Root"]
880
9d7d446a
PW
881#
882# P4 wildcards are not allowed in filenames. P4 complains
883# if you simply add them, but you can force it with "-f", in
884# which case it translates them into %xx encoding internally.
885#
886def wildcard_decode(path):
887 # Search for and fix just these four characters. Do % last so
888 # that fixing it does not inadvertently create new %-escapes.
889 # Cannot have * in a filename in windows; untested as to
890 # what p4 would do in such a case.
891 if not platform.system() == "Windows":
892 path = path.replace("%2A", "*")
893 path = path.replace("%23", "#") \
894 .replace("%40", "@") \
895 .replace("%25", "%")
896 return path
897
898def wildcard_encode(path):
899 # do % first to avoid double-encoding the %s introduced here
900 path = path.replace("%", "%25") \
901 .replace("*", "%2A") \
902 .replace("#", "%23") \
903 .replace("@", "%40")
904 return path
905
906def wildcard_present(path):
598354c0
BC
907 m = re.search("[*#@%]", path)
908 return m is not None
9d7d446a 909
b984733c
SH
910class Command:
911 def __init__(self):
912 self.usage = "usage: %prog [options]"
8910ac0e 913 self.needsGit = True
6a10b6aa 914 self.verbose = False
b984733c 915
3ea2cfd4
LD
916class P4UserMap:
917 def __init__(self):
918 self.userMapFromPerforceServer = False
affb474f
LD
919 self.myP4UserId = None
920
921 def p4UserId(self):
922 if self.myP4UserId:
923 return self.myP4UserId
924
925 results = p4CmdList("user -o")
926 for r in results:
927 if r.has_key('User'):
928 self.myP4UserId = r['User']
929 return r['User']
930 die("Could not find your p4 user id")
931
932 def p4UserIsMe(self, p4User):
933 # return True if the given p4 user is actually me
934 me = self.p4UserId()
935 if not p4User or p4User != me:
936 return False
937 else:
938 return True
3ea2cfd4
LD
939
940 def getUserCacheFilename(self):
941 home = os.environ.get("HOME", os.environ.get("USERPROFILE"))
942 return home + "/.gitp4-usercache.txt"
943
944 def getUserMapFromPerforceServer(self):
945 if self.userMapFromPerforceServer:
946 return
947 self.users = {}
948 self.emails = {}
949
950 for output in p4CmdList("users"):
951 if not output.has_key("User"):
952 continue
953 self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
954 self.emails[output["Email"]] = output["User"]
955
956
957 s = ''
958 for (key, val) in self.users.items():
959 s += "%s\t%s\n" % (key.expandtabs(1), val.expandtabs(1))
960
961 open(self.getUserCacheFilename(), "wb").write(s)
962 self.userMapFromPerforceServer = True
963
964 def loadUserMapFromCache(self):
965 self.users = {}
966 self.userMapFromPerforceServer = False
967 try:
968 cache = open(self.getUserCacheFilename(), "rb")
969 lines = cache.readlines()
970 cache.close()
971 for line in lines:
972 entry = line.strip().split("\t")
973 self.users[entry[0]] = entry[1]
974 except IOError:
975 self.getUserMapFromPerforceServer()
976
b984733c 977class P4Debug(Command):
86949eef 978 def __init__(self):
6ae8de88 979 Command.__init__(self)
6a10b6aa 980 self.options = []
c8c39116 981 self.description = "A tool to debug the output of p4 -G."
8910ac0e 982 self.needsGit = False
86949eef
SH
983
984 def run(self, args):
b1ce9447 985 j = 0
6de040df 986 for output in p4CmdList(args):
b1ce9447
HWN
987 print 'Element: %d' % j
988 j += 1
86949eef 989 print output
b984733c 990 return True
86949eef 991
5834684d
SH
992class P4RollBack(Command):
993 def __init__(self):
994 Command.__init__(self)
995 self.options = [
0c66a783 996 optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true")
5834684d
SH
997 ]
998 self.description = "A tool to debug the multi-branch import. Don't use :)"
0c66a783 999 self.rollbackLocalBranches = False
5834684d
SH
1000
1001 def run(self, args):
1002 if len(args) != 1:
1003 return False
1004 maxChange = int(args[0])
0c66a783 1005
ad192f28 1006 if "p4ExitCode" in p4Cmd("changes -m 1"):
66a2f523
SH
1007 die("Problems executing p4");
1008
0c66a783
SH
1009 if self.rollbackLocalBranches:
1010 refPrefix = "refs/heads/"
b016d397 1011 lines = read_pipe_lines("git rev-parse --symbolic --branches")
0c66a783
SH
1012 else:
1013 refPrefix = "refs/remotes/"
b016d397 1014 lines = read_pipe_lines("git rev-parse --symbolic --remotes")
0c66a783
SH
1015
1016 for line in lines:
1017 if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"):
b25b2065
HWN
1018 line = line.strip()
1019 ref = refPrefix + line
5834684d 1020 log = extractLogMessageFromGitCommit(ref)
bb6e09b2
HWN
1021 settings = extractSettingsGitLog(log)
1022
1023 depotPaths = settings['depot-paths']
1024 change = settings['change']
1025
5834684d 1026 changed = False
52102d47 1027
6326aa58
HWN
1028 if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange)
1029 for p in depotPaths]))) == 0:
52102d47
SH
1030 print "Branch %s did not exist at change %s, deleting." % (ref, maxChange)
1031 system("git update-ref -d %s `git rev-parse %s`" % (ref, ref))
1032 continue
1033
bb6e09b2 1034 while change and int(change) > maxChange:
5834684d 1035 changed = True
52102d47
SH
1036 if self.verbose:
1037 print "%s is at %s ; rewinding towards %s" % (ref, change, maxChange)
5834684d
SH
1038 system("git update-ref %s \"%s^\"" % (ref, ref))
1039 log = extractLogMessageFromGitCommit(ref)
bb6e09b2
HWN
1040 settings = extractSettingsGitLog(log)
1041
1042
1043 depotPaths = settings['depot-paths']
1044 change = settings['change']
5834684d
SH
1045
1046 if changed:
52102d47 1047 print "%s rewound to %s" % (ref, change)
5834684d
SH
1048
1049 return True
1050
3ea2cfd4 1051class P4Submit(Command, P4UserMap):
6bbfd137
PW
1052
1053 conflict_behavior_choices = ("ask", "skip", "quit")
1054
4f5cf76a 1055 def __init__(self):
b984733c 1056 Command.__init__(self)
3ea2cfd4 1057 P4UserMap.__init__(self)
4f5cf76a 1058 self.options = [
4f5cf76a 1059 optparse.make_option("--origin", dest="origin"),
ae901090 1060 optparse.make_option("-M", dest="detectRenames", action="store_true"),
3ea2cfd4
LD
1061 # preserve the user, requires relevant p4 permissions
1062 optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
06804c76 1063 optparse.make_option("--export-labels", dest="exportLabels", action="store_true"),
ef739f08 1064 optparse.make_option("--dry-run", "-n", dest="dry_run", action="store_true"),
728b7ad8 1065 optparse.make_option("--prepare-p4-only", dest="prepare_p4_only", action="store_true"),
6bbfd137 1066 optparse.make_option("--conflict", dest="conflict_behavior",
44e8d26c
PW
1067 choices=self.conflict_behavior_choices),
1068 optparse.make_option("--branch", dest="branch"),
4f5cf76a
SH
1069 ]
1070 self.description = "Submit changes from git to the perforce depot."
c9b50e63 1071 self.usage += " [name of git branch to submit into perforce depot]"
9512497b 1072 self.origin = ""
ae901090 1073 self.detectRenames = False
0d609032 1074 self.preserveUser = gitConfigBool("git-p4.preserveUser")
ef739f08 1075 self.dry_run = False
728b7ad8 1076 self.prepare_p4_only = False
6bbfd137 1077 self.conflict_behavior = None
f7baba8b 1078 self.isWindows = (platform.system() == "Windows")
06804c76 1079 self.exportLabels = False
249da4c0 1080 self.p4HasMoveCommand = p4_has_move_command()
44e8d26c 1081 self.branch = None
4f5cf76a 1082
4f5cf76a
SH
1083 def check(self):
1084 if len(p4CmdList("opened ...")) > 0:
1085 die("You have files opened with perforce! Close them before starting the sync.")
1086
f19cb0a0
PW
1087 def separate_jobs_from_description(self, message):
1088 """Extract and return a possible Jobs field in the commit
1089 message. It goes into a separate section in the p4 change
1090 specification.
1091
1092 A jobs line starts with "Jobs:" and looks like a new field
1093 in a form. Values are white-space separated on the same
1094 line or on following lines that start with a tab.
1095
1096 This does not parse and extract the full git commit message
1097 like a p4 form. It just sees the Jobs: line as a marker
1098 to pass everything from then on directly into the p4 form,
1099 but outside the description section.
1100
1101 Return a tuple (stripped log message, jobs string)."""
1102
1103 m = re.search(r'^Jobs:', message, re.MULTILINE)
1104 if m is None:
1105 return (message, None)
1106
1107 jobtext = message[m.start():]
1108 stripped_message = message[:m.start()].rstrip()
1109 return (stripped_message, jobtext)
1110
1111 def prepareLogMessage(self, template, message, jobs):
1112 """Edits the template returned from "p4 change -o" to insert
1113 the message in the Description field, and the jobs text in
1114 the Jobs field."""
4f5cf76a
SH
1115 result = ""
1116
edae1e2f
SH
1117 inDescriptionSection = False
1118
4f5cf76a
SH
1119 for line in template.split("\n"):
1120 if line.startswith("#"):
1121 result += line + "\n"
1122 continue
1123
edae1e2f 1124 if inDescriptionSection:
c9dbab04 1125 if line.startswith("Files:") or line.startswith("Jobs:"):
edae1e2f 1126 inDescriptionSection = False
f19cb0a0
PW
1127 # insert Jobs section
1128 if jobs:
1129 result += jobs + "\n"
edae1e2f
SH
1130 else:
1131 continue
1132 else:
1133 if line.startswith("Description:"):
1134 inDescriptionSection = True
1135 line += "\n"
1136 for messageLine in message.split("\n"):
1137 line += "\t" + messageLine + "\n"
1138
1139 result += line + "\n"
4f5cf76a
SH
1140
1141 return result
1142
60df071c
LD
1143 def patchRCSKeywords(self, file, pattern):
1144 # Attempt to zap the RCS keywords in a p4 controlled file matching the given pattern
1145 (handle, outFileName) = tempfile.mkstemp(dir='.')
1146 try:
1147 outFile = os.fdopen(handle, "w+")
1148 inFile = open(file, "r")
1149 regexp = re.compile(pattern, re.VERBOSE)
1150 for line in inFile.readlines():
1151 line = regexp.sub(r'$\1$', line)
1152 outFile.write(line)
1153 inFile.close()
1154 outFile.close()
1155 # Forcibly overwrite the original file
1156 os.unlink(file)
1157 shutil.move(outFileName, file)
1158 except:
1159 # cleanup our temporary file
1160 os.unlink(outFileName)
1161 print "Failed to strip RCS keywords in %s" % file
1162 raise
1163
1164 print "Patched up RCS keywords in %s" % file
1165
3ea2cfd4
LD
1166 def p4UserForCommit(self,id):
1167 # Return the tuple (perforce user,git email) for a given git commit id
1168 self.getUserMapFromPerforceServer()
9bf28855
PW
1169 gitEmail = read_pipe(["git", "log", "--max-count=1",
1170 "--format=%ae", id])
3ea2cfd4
LD
1171 gitEmail = gitEmail.strip()
1172 if not self.emails.has_key(gitEmail):
1173 return (None,gitEmail)
1174 else:
1175 return (self.emails[gitEmail],gitEmail)
1176
1177 def checkValidP4Users(self,commits):
1178 # check if any git authors cannot be mapped to p4 users
1179 for id in commits:
1180 (user,email) = self.p4UserForCommit(id)
1181 if not user:
1182 msg = "Cannot find p4 user for email %s in commit %s." % (email, id)
0d609032 1183 if gitConfigBool("git-p4.allowMissingP4Users"):
3ea2cfd4
LD
1184 print "%s" % msg
1185 else:
1186 die("Error: %s\nSet git-p4.allowMissingP4Users to true to allow this." % msg)
1187
1188 def lastP4Changelist(self):
1189 # Get back the last changelist number submitted in this client spec. This
1190 # then gets used to patch up the username in the change. If the same
1191 # client spec is being used by multiple processes then this might go
1192 # wrong.
1193 results = p4CmdList("client -o") # find the current client
1194 client = None
1195 for r in results:
1196 if r.has_key('Client'):
1197 client = r['Client']
1198 break
1199 if not client:
1200 die("could not get client spec")
6de040df 1201 results = p4CmdList(["changes", "-c", client, "-m", "1"])
3ea2cfd4
LD
1202 for r in results:
1203 if r.has_key('change'):
1204 return r['change']
1205 die("Could not get changelist number for last submit - cannot patch up user details")
1206
1207 def modifyChangelistUser(self, changelist, newUser):
1208 # fixup the user field of a changelist after it has been submitted.
1209 changes = p4CmdList("change -o %s" % changelist)
ecdba36d
LD
1210 if len(changes) != 1:
1211 die("Bad output from p4 change modifying %s to user %s" %
1212 (changelist, newUser))
1213
1214 c = changes[0]
1215 if c['User'] == newUser: return # nothing to do
1216 c['User'] = newUser
1217 input = marshal.dumps(c)
1218
3ea2cfd4
LD
1219 result = p4CmdList("change -f -i", stdin=input)
1220 for r in result:
1221 if r.has_key('code'):
1222 if r['code'] == 'error':
1223 die("Could not modify user field of changelist %s to %s:%s" % (changelist, newUser, r['data']))
1224 if r.has_key('data'):
1225 print("Updated user field for changelist %s to %s" % (changelist, newUser))
1226 return
1227 die("Could not modify user field of changelist %s to %s" % (changelist, newUser))
1228
1229 def canChangeChangelists(self):
1230 # check to see if we have p4 admin or super-user permissions, either of
1231 # which are required to modify changelists.
52a4880b 1232 results = p4CmdList(["protects", self.depotPath])
3ea2cfd4
LD
1233 for r in results:
1234 if r.has_key('perm'):
1235 if r['perm'] == 'admin':
1236 return 1
1237 if r['perm'] == 'super':
1238 return 1
1239 return 0
1240
ea99c3ae 1241 def prepareSubmitTemplate(self):
f19cb0a0
PW
1242 """Run "p4 change -o" to grab a change specification template.
1243 This does not use "p4 -G", as it is nice to keep the submission
1244 template in original order, since a human might edit it.
1245
1246 Remove lines in the Files section that show changes to files
1247 outside the depot path we're committing into."""
1248
ea99c3ae
SH
1249 template = ""
1250 inFilesSection = False
6de040df 1251 for line in p4_read_pipe_lines(['change', '-o']):
f3e5ae4f
MSO
1252 if line.endswith("\r\n"):
1253 line = line[:-2] + "\n"
ea99c3ae
SH
1254 if inFilesSection:
1255 if line.startswith("\t"):
1256 # path starts and ends with a tab
1257 path = line[1:]
1258 lastTab = path.rfind("\t")
1259 if lastTab != -1:
1260 path = path[:lastTab]
d53de8b9 1261 if not p4PathStartsWith(path, self.depotPath):
ea99c3ae
SH
1262 continue
1263 else:
1264 inFilesSection = False
1265 else:
1266 if line.startswith("Files:"):
1267 inFilesSection = True
1268
1269 template += line
1270
1271 return template
1272
7c766e57
PW
1273 def edit_template(self, template_file):
1274 """Invoke the editor to let the user change the submission
1275 message. Return true if okay to continue with the submit."""
1276
1277 # if configured to skip the editing part, just submit
0d609032 1278 if gitConfigBool("git-p4.skipSubmitEdit"):
7c766e57
PW
1279 return True
1280
1281 # look at the modification time, to check later if the user saved
1282 # the file
1283 mtime = os.stat(template_file).st_mtime
1284
1285 # invoke the editor
f95ceaf0 1286 if os.environ.has_key("P4EDITOR") and (os.environ.get("P4EDITOR") != ""):
7c766e57
PW
1287 editor = os.environ.get("P4EDITOR")
1288 else:
1289 editor = read_pipe("git var GIT_EDITOR").strip()
2dade7a7 1290 system(["sh", "-c", ('%s "$@"' % editor), editor, template_file])
7c766e57
PW
1291
1292 # If the file was not saved, prompt to see if this patch should
1293 # be skipped. But skip this verification step if configured so.
0d609032 1294 if gitConfigBool("git-p4.skipSubmitEditCheck"):
7c766e57
PW
1295 return True
1296
d1652049
PW
1297 # modification time updated means user saved the file
1298 if os.stat(template_file).st_mtime > mtime:
1299 return True
1300
1301 while True:
1302 response = raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
1303 if response == 'y':
1304 return True
1305 if response == 'n':
1306 return False
7c766e57 1307
e2a892ee 1308 def get_diff_description(self, editedFiles, filesToAdd):
b4073bb3
MC
1309 # diff
1310 if os.environ.has_key("P4DIFF"):
1311 del(os.environ["P4DIFF"])
1312 diff = ""
1313 for editedFile in editedFiles:
1314 diff += p4_read_pipe(['diff', '-du',
1315 wildcard_encode(editedFile)])
1316
1317 # new file diff
1318 newdiff = ""
1319 for newFile in filesToAdd:
1320 newdiff += "==== new file ====\n"
1321 newdiff += "--- /dev/null\n"
1322 newdiff += "+++ %s\n" % newFile
1323 f = open(newFile, "r")
1324 for line in f.readlines():
1325 newdiff += "+" + line
1326 f.close()
1327
e2a892ee 1328 return (diff + newdiff).replace('\r\n', '\n')
b4073bb3 1329
7cb5cbef 1330 def applyCommit(self, id):
67b0fe2e
PW
1331 """Apply one commit, return True if it succeeded."""
1332
1333 print "Applying", read_pipe(["git", "show", "-s",
1334 "--format=format:%h %s", id])
ae901090 1335
848de9c3 1336 (p4User, gitEmail) = self.p4UserForCommit(id)
3ea2cfd4 1337
84cb0003 1338 diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (self.diffOpts, id, id))
4f5cf76a
SH
1339 filesToAdd = set()
1340 filesToDelete = set()
d336c158 1341 editedFiles = set()
b6ad6dcc 1342 pureRenameCopy = set()
c65b670e 1343 filesToChangeExecBit = {}
60df071c 1344
4f5cf76a 1345 for line in diff:
b43b0a3c
CP
1346 diff = parseDiffTreeEntry(line)
1347 modifier = diff['status']
1348 path = diff['src']
4f5cf76a 1349 if modifier == "M":
6de040df 1350 p4_edit(path)
c65b670e
CP
1351 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
1352 filesToChangeExecBit[path] = diff['dst_mode']
d336c158 1353 editedFiles.add(path)
4f5cf76a
SH
1354 elif modifier == "A":
1355 filesToAdd.add(path)
c65b670e 1356 filesToChangeExecBit[path] = diff['dst_mode']
4f5cf76a
SH
1357 if path in filesToDelete:
1358 filesToDelete.remove(path)
1359 elif modifier == "D":
1360 filesToDelete.add(path)
1361 if path in filesToAdd:
1362 filesToAdd.remove(path)
4fddb41b
VA
1363 elif modifier == "C":
1364 src, dest = diff['src'], diff['dst']
6de040df 1365 p4_integrate(src, dest)
b6ad6dcc 1366 pureRenameCopy.add(dest)
4fddb41b 1367 if diff['src_sha1'] != diff['dst_sha1']:
6de040df 1368 p4_edit(dest)
b6ad6dcc 1369 pureRenameCopy.discard(dest)
4fddb41b 1370 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
6de040df 1371 p4_edit(dest)
b6ad6dcc 1372 pureRenameCopy.discard(dest)
4fddb41b 1373 filesToChangeExecBit[dest] = diff['dst_mode']
d20f0f8e
PW
1374 if self.isWindows:
1375 # turn off read-only attribute
1376 os.chmod(dest, stat.S_IWRITE)
4fddb41b
VA
1377 os.unlink(dest)
1378 editedFiles.add(dest)
d9a5f25b 1379 elif modifier == "R":
b43b0a3c 1380 src, dest = diff['src'], diff['dst']
8e9497c2
GG
1381 if self.p4HasMoveCommand:
1382 p4_edit(src) # src must be open before move
1383 p4_move(src, dest) # opens for (move/delete, move/add)
b6ad6dcc 1384 else:
8e9497c2
GG
1385 p4_integrate(src, dest)
1386 if diff['src_sha1'] != diff['dst_sha1']:
1387 p4_edit(dest)
1388 else:
1389 pureRenameCopy.add(dest)
c65b670e 1390 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
8e9497c2
GG
1391 if not self.p4HasMoveCommand:
1392 p4_edit(dest) # with move: already open, writable
c65b670e 1393 filesToChangeExecBit[dest] = diff['dst_mode']
8e9497c2 1394 if not self.p4HasMoveCommand:
d20f0f8e
PW
1395 if self.isWindows:
1396 os.chmod(dest, stat.S_IWRITE)
8e9497c2
GG
1397 os.unlink(dest)
1398 filesToDelete.add(src)
d9a5f25b 1399 editedFiles.add(dest)
4f5cf76a
SH
1400 else:
1401 die("unknown modifier %s for %s" % (modifier, path))
1402
749b668c 1403 diffcmd = "git diff-tree --full-index -p \"%s\"" % (id)
47a130b7 1404 patchcmd = diffcmd + " | git apply "
c1b296b9
SH
1405 tryPatchCmd = patchcmd + "--check -"
1406 applyPatchCmd = patchcmd + "--check --apply -"
60df071c 1407 patch_succeeded = True
51a2640a 1408
47a130b7 1409 if os.system(tryPatchCmd) != 0:
60df071c
LD
1410 fixed_rcs_keywords = False
1411 patch_succeeded = False
51a2640a 1412 print "Unfortunately applying the change failed!"
60df071c
LD
1413
1414 # Patch failed, maybe it's just RCS keyword woes. Look through
1415 # the patch to see if that's possible.
0d609032 1416 if gitConfigBool("git-p4.attemptRCSCleanup"):
60df071c
LD
1417 file = None
1418 pattern = None
1419 kwfiles = {}
1420 for file in editedFiles | filesToDelete:
1421 # did this file's delta contain RCS keywords?
1422 pattern = p4_keywords_regexp_for_file(file)
1423
1424 if pattern:
1425 # this file is a possibility...look for RCS keywords.
1426 regexp = re.compile(pattern, re.VERBOSE)
1427 for line in read_pipe_lines(["git", "diff", "%s^..%s" % (id, id), file]):
1428 if regexp.search(line):
1429 if verbose:
1430 print "got keyword match on %s in %s in %s" % (pattern, line, file)
1431 kwfiles[file] = pattern
1432 break
1433
1434 for file in kwfiles:
1435 if verbose:
1436 print "zapping %s with %s" % (line,pattern)
d20f0f8e
PW
1437 # File is being deleted, so not open in p4. Must
1438 # disable the read-only bit on windows.
1439 if self.isWindows and file not in editedFiles:
1440 os.chmod(file, stat.S_IWRITE)
60df071c
LD
1441 self.patchRCSKeywords(file, kwfiles[file])
1442 fixed_rcs_keywords = True
1443
1444 if fixed_rcs_keywords:
1445 print "Retrying the patch with RCS keywords cleaned up"
1446 if os.system(tryPatchCmd) == 0:
1447 patch_succeeded = True
1448
1449 if not patch_succeeded:
7e5dd9f2
PW
1450 for f in editedFiles:
1451 p4_revert(f)
7e5dd9f2 1452 return False
51a2640a 1453
55ac2ed6
PW
1454 #
1455 # Apply the patch for real, and do add/delete/+x handling.
1456 #
47a130b7 1457 system(applyPatchCmd)
4f5cf76a
SH
1458
1459 for f in filesToAdd:
6de040df 1460 p4_add(f)
4f5cf76a 1461 for f in filesToDelete:
6de040df
LD
1462 p4_revert(f)
1463 p4_delete(f)
4f5cf76a 1464
c65b670e
CP
1465 # Set/clear executable bits
1466 for f in filesToChangeExecBit.keys():
1467 mode = filesToChangeExecBit[f]
1468 setP4ExecBit(f, mode)
1469
55ac2ed6
PW
1470 #
1471 # Build p4 change description, starting with the contents
1472 # of the git commit message.
1473 #
0e36f2d7 1474 logMessage = extractLogMessageFromGitCommit(id)
0e36f2d7 1475 logMessage = logMessage.strip()
f19cb0a0 1476 (logMessage, jobs) = self.separate_jobs_from_description(logMessage)
4f5cf76a 1477
ea99c3ae 1478 template = self.prepareSubmitTemplate()
f19cb0a0 1479 submitTemplate = self.prepareLogMessage(template, logMessage, jobs)
ecdba36d 1480
c47178d4 1481 if self.preserveUser:
55ac2ed6 1482 submitTemplate += "\n######## Actual user %s, modified after commit\n" % p4User
c47178d4 1483
55ac2ed6
PW
1484 if self.checkAuthorship and not self.p4UserIsMe(p4User):
1485 submitTemplate += "######## git author %s does not match your p4 account.\n" % gitEmail
1486 submitTemplate += "######## Use option --preserve-user to modify authorship.\n"
1487 submitTemplate += "######## Variable git-p4.skipUserNameCheck hides this message.\n"
c47178d4 1488
55ac2ed6 1489 separatorLine = "######## everything below this line is just the diff #######\n"
b4073bb3
MC
1490 if not self.prepare_p4_only:
1491 submitTemplate += separatorLine
e2a892ee 1492 submitTemplate += self.get_diff_description(editedFiles, filesToAdd)
55ac2ed6 1493
c47178d4 1494 (handle, fileName) = tempfile.mkstemp()
e2a892ee 1495 tmpFile = os.fdopen(handle, "w+b")
c47178d4
PW
1496 if self.isWindows:
1497 submitTemplate = submitTemplate.replace("\n", "\r\n")
b4073bb3 1498 tmpFile.write(submitTemplate)
c47178d4
PW
1499 tmpFile.close()
1500
728b7ad8
PW
1501 if self.prepare_p4_only:
1502 #
1503 # Leave the p4 tree prepared, and the submit template around
1504 # and let the user decide what to do next
1505 #
1506 print
1507 print "P4 workspace prepared for submission."
1508 print "To submit or revert, go to client workspace"
1509 print " " + self.clientPath
1510 print
1511 print "To submit, use \"p4 submit\" to write a new description,"
10de86d0 1512 print "or \"p4 submit -i <%s\" to use the one prepared by" \
728b7ad8
PW
1513 " \"git p4\"." % fileName
1514 print "You can delete the file \"%s\" when finished." % fileName
1515
1516 if self.preserveUser and p4User and not self.p4UserIsMe(p4User):
1517 print "To preserve change ownership by user %s, you must\n" \
1518 "do \"p4 change -f <change>\" after submitting and\n" \
1519 "edit the User field."
1520 if pureRenameCopy:
1521 print "After submitting, renamed files must be re-synced."
1522 print "Invoke \"p4 sync -f\" on each of these files:"
1523 for f in pureRenameCopy:
1524 print " " + f
1525
1526 print
1527 print "To revert the changes, use \"p4 revert ...\", and delete"
1528 print "the submit template file \"%s\"" % fileName
1529 if filesToAdd:
1530 print "Since the commit adds new files, they must be deleted:"
1531 for f in filesToAdd:
1532 print " " + f
1533 print
1534 return True
1535
55ac2ed6
PW
1536 #
1537 # Let the user edit the change description, then submit it.
1538 #
c47178d4
PW
1539 if self.edit_template(fileName):
1540 # read the edited message and submit
5a41c16a 1541 ret = True
c47178d4
PW
1542 tmpFile = open(fileName, "rb")
1543 message = tmpFile.read()
e96e400f 1544 tmpFile.close()
c47178d4 1545 if self.isWindows:
e2a892ee
MC
1546 message = message.replace("\r\n", "\n")
1547 submitTemplate = message[:message.index(separatorLine)]
c47178d4 1548 p4_write_pipe(['submit', '-i'], submitTemplate)
e96e400f 1549
c47178d4
PW
1550 if self.preserveUser:
1551 if p4User:
1552 # Get last changelist number. Cannot easily get it from
1553 # the submit command output as the output is
1554 # unmarshalled.
1555 changelist = self.lastP4Changelist()
1556 self.modifyChangelistUser(changelist, p4User)
1557
1558 # The rename/copy happened by applying a patch that created a
1559 # new file. This leaves it writable, which confuses p4.
1560 for f in pureRenameCopy:
1561 p4_sync(f, "-f")
cdc7e388 1562
4f5cf76a 1563 else:
c47178d4 1564 # skip this patch
5a41c16a 1565 ret = False
c47178d4
PW
1566 print "Submission cancelled, undoing p4 changes."
1567 for f in editedFiles:
1568 p4_revert(f)
1569 for f in filesToAdd:
1570 p4_revert(f)
1571 os.remove(f)
df9c5453
PW
1572 for f in filesToDelete:
1573 p4_revert(f)
c47178d4
PW
1574
1575 os.remove(fileName)
5a41c16a 1576 return ret
4f5cf76a 1577
06804c76
LD
1578 # Export git tags as p4 labels. Create a p4 label and then tag
1579 # with that.
1580 def exportGitTags(self, gitTags):
c8942a22
LD
1581 validLabelRegexp = gitConfig("git-p4.labelExportRegexp")
1582 if len(validLabelRegexp) == 0:
1583 validLabelRegexp = defaultLabelRegexp
1584 m = re.compile(validLabelRegexp)
06804c76
LD
1585
1586 for name in gitTags:
1587
1588 if not m.match(name):
1589 if verbose:
05a3cec5 1590 print "tag %s does not match regexp %s" % (name, validLabelRegexp)
06804c76
LD
1591 continue
1592
1593 # Get the p4 commit this corresponds to
c8942a22
LD
1594 logMessage = extractLogMessageFromGitCommit(name)
1595 values = extractSettingsGitLog(logMessage)
06804c76 1596
c8942a22 1597 if not values.has_key('change'):
06804c76
LD
1598 # a tag pointing to something not sent to p4; ignore
1599 if verbose:
1600 print "git tag %s does not give a p4 commit" % name
1601 continue
c8942a22
LD
1602 else:
1603 changelist = values['change']
06804c76
LD
1604
1605 # Get the tag details.
1606 inHeader = True
1607 isAnnotated = False
1608 body = []
1609 for l in read_pipe_lines(["git", "cat-file", "-p", name]):
1610 l = l.strip()
1611 if inHeader:
1612 if re.match(r'tag\s+', l):
1613 isAnnotated = True
1614 elif re.match(r'\s*$', l):
1615 inHeader = False
1616 continue
1617 else:
1618 body.append(l)
1619
1620 if not isAnnotated:
1621 body = ["lightweight tag imported by git p4\n"]
1622
1623 # Create the label - use the same view as the client spec we are using
1624 clientSpec = getClientSpec()
1625
1626 labelTemplate = "Label: %s\n" % name
1627 labelTemplate += "Description:\n"
1628 for b in body:
1629 labelTemplate += "\t" + b + "\n"
1630 labelTemplate += "View:\n"
9d57c4a6
KS
1631 for depot_side in clientSpec.mappings:
1632 labelTemplate += "\t%s\n" % depot_side
06804c76 1633
ef739f08
PW
1634 if self.dry_run:
1635 print "Would create p4 label %s for tag" % name
728b7ad8
PW
1636 elif self.prepare_p4_only:
1637 print "Not creating p4 label %s for tag due to option" \
1638 " --prepare-p4-only" % name
ef739f08
PW
1639 else:
1640 p4_write_pipe(["label", "-i"], labelTemplate)
06804c76 1641
ef739f08
PW
1642 # Use the label
1643 p4_system(["tag", "-l", name] +
9d57c4a6 1644 ["%s@%s" % (depot_side, changelist) for depot_side in clientSpec.mappings])
06804c76 1645
ef739f08
PW
1646 if verbose:
1647 print "created p4 label for tag %s" % name
06804c76 1648
4f5cf76a 1649 def run(self, args):
c9b50e63
SH
1650 if len(args) == 0:
1651 self.master = currentGitBranch()
4280e533 1652 if len(self.master) == 0 or not gitBranchExists("refs/heads/%s" % self.master):
c9b50e63
SH
1653 die("Detecting current git branch failed!")
1654 elif len(args) == 1:
1655 self.master = args[0]
28755dba
PW
1656 if not branchExists(self.master):
1657 die("Branch %s does not exist" % self.master)
c9b50e63
SH
1658 else:
1659 return False
1660
4c2d5d72
JX
1661 allowSubmit = gitConfig("git-p4.allowSubmit")
1662 if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","):
1663 die("%s is not in git-p4.allowSubmit" % self.master)
1664
27d2d811 1665 [upstream, settings] = findUpstreamBranchPoint()
ea99c3ae 1666 self.depotPath = settings['depot-paths'][0]
27d2d811
SH
1667 if len(self.origin) == 0:
1668 self.origin = upstream
a3fdd579 1669
3ea2cfd4
LD
1670 if self.preserveUser:
1671 if not self.canChangeChangelists():
1672 die("Cannot preserve user names without p4 super-user or admin permissions")
1673
6bbfd137
PW
1674 # if not set from the command line, try the config file
1675 if self.conflict_behavior is None:
1676 val = gitConfig("git-p4.conflict")
1677 if val:
1678 if val not in self.conflict_behavior_choices:
1679 die("Invalid value '%s' for config git-p4.conflict" % val)
1680 else:
1681 val = "ask"
1682 self.conflict_behavior = val
1683
a3fdd579
SH
1684 if self.verbose:
1685 print "Origin branch is " + self.origin
9512497b 1686
ea99c3ae 1687 if len(self.depotPath) == 0:
9512497b
SH
1688 print "Internal error: cannot locate perforce depot path from existing branches"
1689 sys.exit(128)
1690
543987bd 1691 self.useClientSpec = False
0d609032 1692 if gitConfigBool("git-p4.useclientspec"):
543987bd
PW
1693 self.useClientSpec = True
1694 if self.useClientSpec:
1695 self.clientSpecDirs = getClientSpec()
9512497b 1696
cd884106
VA
1697 # Check for the existance of P4 branches
1698 branchesDetected = (len(p4BranchesInGit().keys()) > 1)
1699
1700 if self.useClientSpec and not branchesDetected:
543987bd
PW
1701 # all files are relative to the client spec
1702 self.clientPath = getClientRoot()
1703 else:
1704 self.clientPath = p4Where(self.depotPath)
9512497b 1705
543987bd
PW
1706 if self.clientPath == "":
1707 die("Error: Cannot locate perforce checkout of %s in client view" % self.depotPath)
9512497b 1708
ea99c3ae 1709 print "Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath)
7944f142 1710 self.oldWorkingDirectory = os.getcwd()
c1b296b9 1711
0591cfa8 1712 # ensure the clientPath exists
8d7ec362 1713 new_client_dir = False
0591cfa8 1714 if not os.path.exists(self.clientPath):
8d7ec362 1715 new_client_dir = True
0591cfa8
GG
1716 os.makedirs(self.clientPath)
1717
bbd84863 1718 chdir(self.clientPath, is_client_path=True)
ef739f08
PW
1719 if self.dry_run:
1720 print "Would synchronize p4 checkout in %s" % self.clientPath
8d7ec362 1721 else:
ef739f08
PW
1722 print "Synchronizing p4 checkout..."
1723 if new_client_dir:
1724 # old one was destroyed, and maybe nobody told p4
1725 p4_sync("...", "-f")
1726 else:
1727 p4_sync("...")
4f5cf76a 1728 self.check()
4f5cf76a 1729
4c750c0d 1730 commits = []
c7d34884 1731 for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, self.master)]):
4c750c0d
SH
1732 commits.append(line.strip())
1733 commits.reverse()
4f5cf76a 1734
0d609032 1735 if self.preserveUser or gitConfigBool("git-p4.skipUserNameCheck"):
848de9c3
LD
1736 self.checkAuthorship = False
1737 else:
1738 self.checkAuthorship = True
1739
3ea2cfd4
LD
1740 if self.preserveUser:
1741 self.checkValidP4Users(commits)
1742
84cb0003
GG
1743 #
1744 # Build up a set of options to be passed to diff when
1745 # submitting each commit to p4.
1746 #
1747 if self.detectRenames:
1748 # command-line -M arg
1749 self.diffOpts = "-M"
1750 else:
1751 # If not explicitly set check the config variable
1752 detectRenames = gitConfig("git-p4.detectRenames")
1753
1754 if detectRenames.lower() == "false" or detectRenames == "":
1755 self.diffOpts = ""
1756 elif detectRenames.lower() == "true":
1757 self.diffOpts = "-M"
1758 else:
1759 self.diffOpts = "-M%s" % detectRenames
1760
1761 # no command-line arg for -C or --find-copies-harder, just
1762 # config variables
1763 detectCopies = gitConfig("git-p4.detectCopies")
1764 if detectCopies.lower() == "false" or detectCopies == "":
1765 pass
1766 elif detectCopies.lower() == "true":
1767 self.diffOpts += " -C"
1768 else:
1769 self.diffOpts += " -C%s" % detectCopies
1770
0d609032 1771 if gitConfigBool("git-p4.detectCopiesHarder"):
84cb0003
GG
1772 self.diffOpts += " --find-copies-harder"
1773
7e5dd9f2
PW
1774 #
1775 # Apply the commits, one at a time. On failure, ask if should
1776 # continue to try the rest of the patches, or quit.
1777 #
ef739f08
PW
1778 if self.dry_run:
1779 print "Would apply"
67b0fe2e 1780 applied = []
7e5dd9f2
PW
1781 last = len(commits) - 1
1782 for i, commit in enumerate(commits):
ef739f08
PW
1783 if self.dry_run:
1784 print " ", read_pipe(["git", "show", "-s",
1785 "--format=format:%h %s", commit])
1786 ok = True
1787 else:
1788 ok = self.applyCommit(commit)
67b0fe2e
PW
1789 if ok:
1790 applied.append(commit)
7e5dd9f2 1791 else:
728b7ad8
PW
1792 if self.prepare_p4_only and i < last:
1793 print "Processing only the first commit due to option" \
1794 " --prepare-p4-only"
1795 break
7e5dd9f2
PW
1796 if i < last:
1797 quit = False
1798 while True:
6bbfd137
PW
1799 # prompt for what to do, or use the option/variable
1800 if self.conflict_behavior == "ask":
1801 print "What do you want to do?"
1802 response = raw_input("[s]kip this commit but apply"
1803 " the rest, or [q]uit? ")
1804 if not response:
1805 continue
1806 elif self.conflict_behavior == "skip":
1807 response = "s"
1808 elif self.conflict_behavior == "quit":
1809 response = "q"
1810 else:
1811 die("Unknown conflict_behavior '%s'" %
1812 self.conflict_behavior)
1813
7e5dd9f2
PW
1814 if response[0] == "s":
1815 print "Skipping this commit, but applying the rest"
1816 break
1817 if response[0] == "q":
1818 print "Quitting"
1819 quit = True
1820 break
1821 if quit:
1822 break
4f5cf76a 1823
67b0fe2e 1824 chdir(self.oldWorkingDirectory)
4f5cf76a 1825
ef739f08
PW
1826 if self.dry_run:
1827 pass
728b7ad8
PW
1828 elif self.prepare_p4_only:
1829 pass
ef739f08 1830 elif len(commits) == len(applied):
67b0fe2e 1831 print "All commits applied!"
14594f4b 1832
4c750c0d 1833 sync = P4Sync()
44e8d26c
PW
1834 if self.branch:
1835 sync.branch = self.branch
4c750c0d 1836 sync.run([])
14594f4b 1837
4c750c0d
SH
1838 rebase = P4Rebase()
1839 rebase.rebase()
4f5cf76a 1840
67b0fe2e
PW
1841 else:
1842 if len(applied) == 0:
1843 print "No commits applied."
1844 else:
1845 print "Applied only the commits marked with '*':"
1846 for c in commits:
1847 if c in applied:
1848 star = "*"
1849 else:
1850 star = " "
1851 print star, read_pipe(["git", "show", "-s",
1852 "--format=format:%h %s", c])
1853 print "You will have to do 'git p4 sync' and rebase."
1854
0d609032 1855 if gitConfigBool("git-p4.exportLabels"):
06dcd152 1856 self.exportLabels = True
06804c76
LD
1857
1858 if self.exportLabels:
1859 p4Labels = getP4Labels(self.depotPath)
1860 gitTags = getGitTags()
1861
1862 missingGitTags = gitTags - p4Labels
1863 self.exportGitTags(missingGitTags)
1864
98e023de 1865 # exit with error unless everything applied perfectly
67b0fe2e
PW
1866 if len(commits) != len(applied):
1867 sys.exit(1)
1868
b984733c
SH
1869 return True
1870
ecb7cf98
PW
1871class View(object):
1872 """Represent a p4 view ("p4 help views"), and map files in a
1873 repo according to the view."""
1874
9d57c4a6 1875 def __init__(self, client_name):
ecb7cf98 1876 self.mappings = []
9d57c4a6
KS
1877 self.client_prefix = "//%s/" % client_name
1878 # cache results of "p4 where" to lookup client file locations
1879 self.client_spec_path_cache = {}
ecb7cf98
PW
1880
1881 def append(self, view_line):
1882 """Parse a view line, splitting it into depot and client
9d57c4a6
KS
1883 sides. Append to self.mappings, preserving order. This
1884 is only needed for tag creation."""
ecb7cf98
PW
1885
1886 # Split the view line into exactly two words. P4 enforces
1887 # structure on these lines that simplifies this quite a bit.
1888 #
1889 # Either or both words may be double-quoted.
1890 # Single quotes do not matter.
1891 # Double-quote marks cannot occur inside the words.
1892 # A + or - prefix is also inside the quotes.
1893 # There are no quotes unless they contain a space.
1894 # The line is already white-space stripped.
1895 # The two words are separated by a single space.
1896 #
1897 if view_line[0] == '"':
1898 # First word is double quoted. Find its end.
1899 close_quote_index = view_line.find('"', 1)
1900 if close_quote_index <= 0:
1901 die("No first-word closing quote found: %s" % view_line)
1902 depot_side = view_line[1:close_quote_index]
1903 # skip closing quote and space
1904 rhs_index = close_quote_index + 1 + 1
1905 else:
1906 space_index = view_line.find(" ")
1907 if space_index <= 0:
1908 die("No word-splitting space found: %s" % view_line)
1909 depot_side = view_line[0:space_index]
1910 rhs_index = space_index + 1
1911
ecb7cf98 1912 # prefix + means overlay on previous mapping
ecb7cf98 1913 if depot_side.startswith("+"):
ecb7cf98
PW
1914 depot_side = depot_side[1:]
1915
9d57c4a6 1916 # prefix - means exclude this path, leave out of mappings
ecb7cf98
PW
1917 exclude = False
1918 if depot_side.startswith("-"):
1919 exclude = True
1920 depot_side = depot_side[1:]
1921
9d57c4a6
KS
1922 if not exclude:
1923 self.mappings.append(depot_side)
ecb7cf98 1924
9d57c4a6
KS
1925 def convert_client_path(self, clientFile):
1926 # chop off //client/ part to make it relative
1927 if not clientFile.startswith(self.client_prefix):
1928 die("No prefix '%s' on clientFile '%s'" %
1929 (self.client_prefix, clientFile))
1930 return clientFile[len(self.client_prefix):]
ecb7cf98 1931
9d57c4a6
KS
1932 def update_client_spec_path_cache(self, files):
1933 """ Caching file paths by "p4 where" batch query """
ecb7cf98 1934
9d57c4a6
KS
1935 # List depot file paths exclude that already cached
1936 fileArgs = [f['path'] for f in files if f['path'] not in self.client_spec_path_cache]
ecb7cf98 1937
9d57c4a6
KS
1938 if len(fileArgs) == 0:
1939 return # All files in cache
ecb7cf98 1940
9d57c4a6
KS
1941 where_result = p4CmdList(["-x", "-", "where"], stdin=fileArgs)
1942 for res in where_result:
1943 if "code" in res and res["code"] == "error":
1944 # assume error is "... file(s) not in client view"
1945 continue
1946 if "clientFile" not in res:
20005443 1947 die("No clientFile in 'p4 where' output")
9d57c4a6
KS
1948 if "unmap" in res:
1949 # it will list all of them, but only one not unmap-ped
1950 continue
a0a50d87
LS
1951 if gitConfigBool("core.ignorecase"):
1952 res['depotFile'] = res['depotFile'].lower()
9d57c4a6 1953 self.client_spec_path_cache[res['depotFile']] = self.convert_client_path(res["clientFile"])
ecb7cf98 1954
9d57c4a6
KS
1955 # not found files or unmap files set to ""
1956 for depotFile in fileArgs:
a0a50d87
LS
1957 if gitConfigBool("core.ignorecase"):
1958 depotFile = depotFile.lower()
9d57c4a6
KS
1959 if depotFile not in self.client_spec_path_cache:
1960 self.client_spec_path_cache[depotFile] = ""
ecb7cf98 1961
9d57c4a6
KS
1962 def map_in_client(self, depot_path):
1963 """Return the relative location in the client where this
1964 depot file should live. Returns "" if the file should
1965 not be mapped in the client."""
ecb7cf98 1966
a0a50d87
LS
1967 if gitConfigBool("core.ignorecase"):
1968 depot_path = depot_path.lower()
1969
9d57c4a6
KS
1970 if depot_path in self.client_spec_path_cache:
1971 return self.client_spec_path_cache[depot_path]
1972
1973 die( "Error: %s is not found in client spec path" % depot_path )
1974 return ""
ecb7cf98 1975
3ea2cfd4 1976class P4Sync(Command, P4UserMap):
56c09345
PW
1977 delete_actions = ( "delete", "move/delete", "purge" )
1978
b984733c
SH
1979 def __init__(self):
1980 Command.__init__(self)
3ea2cfd4 1981 P4UserMap.__init__(self)
b984733c
SH
1982 self.options = [
1983 optparse.make_option("--branch", dest="branch"),
1984 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
1985 optparse.make_option("--changesfile", dest="changesFile"),
1986 optparse.make_option("--silent", dest="silent", action="store_true"),
ef48f909 1987 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
06804c76 1988 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
d2c6dd30
HWN
1989 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",
1990 help="Import into refs/heads/ , not refs/remotes"),
96b2d54a
LS
1991 optparse.make_option("--max-changes", dest="maxChanges",
1992 help="Maximum number of changes to import"),
1993 optparse.make_option("--changes-block-size", dest="changes_block_size", type="int",
1994 help="Internal block size to use when iteratively calling p4 changes"),
86dff6b6 1995 optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',
3a70cdfa
TAL
1996 help="Keep entire BRANCH/DIR/SUBDIR prefix during import"),
1997 optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true',
51334bb0
LD
1998 help="Only sync files that are included in the Perforce Client Spec"),
1999 optparse.make_option("-/", dest="cloneExclude",
2000 action="append", type="string",
2001 help="exclude depot path"),
b984733c
SH
2002 ]
2003 self.description = """Imports from Perforce into a git repository.\n
2004 example:
2005 //depot/my/project/ -- to import the current head
2006 //depot/my/project/@all -- to import everything
2007 //depot/my/project/@1,6 -- to import only from revision 1 to 6
2008
2009 (a ... is not needed in the path p4 specification, it's added implicitly)"""
2010
2011 self.usage += " //depot/path[@revRange]"
b984733c 2012 self.silent = False
1d7367dc
RG
2013 self.createdBranches = set()
2014 self.committedChanges = set()
569d1bd4 2015 self.branch = ""
b984733c 2016 self.detectBranches = False
cb53e1f8 2017 self.detectLabels = False
06804c76 2018 self.importLabels = False
b984733c 2019 self.changesFile = ""
01265103 2020 self.syncWithOrigin = True
a028a98e 2021 self.importIntoRemotes = True
01a9c9c5 2022 self.maxChanges = ""
1051ef00 2023 self.changes_block_size = None
8b41a97f 2024 self.keepRepoPath = False
6326aa58 2025 self.depotPaths = None
3c699645 2026 self.p4BranchesInGit = []
354081d5 2027 self.cloneExclude = []
3a70cdfa 2028 self.useClientSpec = False
a93d33ee 2029 self.useClientSpec_from_options = False
ecb7cf98 2030 self.clientSpecDirs = None
fed23693
VA
2031 self.tempBranches = []
2032 self.tempBranchLocation = "git-p4-tmp"
b984733c 2033
01265103
SH
2034 if gitConfig("git-p4.syncFromOrigin") == "false":
2035 self.syncWithOrigin = False
2036
51334bb0
LD
2037 # This is required for the "append" cloneExclude action
2038 def ensure_value(self, attr, value):
2039 if not hasattr(self, attr) or getattr(self, attr) is None:
2040 setattr(self, attr, value)
2041 return getattr(self, attr)
2042
fed23693
VA
2043 # Force a checkpoint in fast-import and wait for it to finish
2044 def checkpoint(self):
2045 self.gitStream.write("checkpoint\n\n")
2046 self.gitStream.write("progress checkpoint\n\n")
2047 out = self.gitOutput.readline()
2048 if self.verbose:
2049 print "checkpoint finished: " + out
2050
b984733c 2051 def extractFilesFromCommit(self, commit):
354081d5
TT
2052 self.cloneExclude = [re.sub(r"\.\.\.$", "", path)
2053 for path in self.cloneExclude]
b984733c
SH
2054 files = []
2055 fnum = 0
2056 while commit.has_key("depotFile%s" % fnum):
2057 path = commit["depotFile%s" % fnum]
6326aa58 2058
354081d5 2059 if [p for p in self.cloneExclude
d53de8b9 2060 if p4PathStartsWith(path, p)]:
354081d5
TT
2061 found = False
2062 else:
2063 found = [p for p in self.depotPaths
d53de8b9 2064 if p4PathStartsWith(path, p)]
6326aa58 2065 if not found:
b984733c
SH
2066 fnum = fnum + 1
2067 continue
2068
2069 file = {}
2070 file["path"] = path
2071 file["rev"] = commit["rev%s" % fnum]
2072 file["action"] = commit["action%s" % fnum]
2073 file["type"] = commit["type%s" % fnum]
2074 files.append(file)
2075 fnum = fnum + 1
2076 return files
2077
6326aa58 2078 def stripRepoPath(self, path, prefixes):
21ef5df4
PW
2079 """When streaming files, this is called to map a p4 depot path
2080 to where it should go in git. The prefixes are either
2081 self.depotPaths, or self.branchPrefixes in the case of
2082 branch detection."""
2083
3952710b 2084 if self.useClientSpec:
21ef5df4
PW
2085 # branch detection moves files up a level (the branch name)
2086 # from what client spec interpretation gives
0d1696ef 2087 path = self.clientSpecDirs.map_in_client(path)
21ef5df4
PW
2088 if self.detectBranches:
2089 for b in self.knownBranches:
2090 if path.startswith(b + "/"):
2091 path = path[len(b)+1:]
2092
2093 elif self.keepRepoPath:
2094 # Preserve everything in relative path name except leading
2095 # //depot/; just look at first prefix as they all should
2096 # be in the same depot.
2097 depot = re.sub("^(//[^/]+/).*", r'\1', prefixes[0])
2098 if p4PathStartsWith(path, depot):
2099 path = path[len(depot):]
3952710b 2100
0d1696ef 2101 else:
0d1696ef
PW
2102 for p in prefixes:
2103 if p4PathStartsWith(path, p):
2104 path = path[len(p):]
21ef5df4 2105 break
8b41a97f 2106
0d1696ef 2107 path = wildcard_decode(path)
6326aa58 2108 return path
6754a299 2109
71b112d4 2110 def splitFilesIntoBranches(self, commit):
21ef5df4
PW
2111 """Look at each depotFile in the commit to figure out to what
2112 branch it belongs."""
2113
9d57c4a6
KS
2114 if self.clientSpecDirs:
2115 files = self.extractFilesFromCommit(commit)
2116 self.clientSpecDirs.update_client_spec_path_cache(files)
2117
d5904674 2118 branches = {}
71b112d4
SH
2119 fnum = 0
2120 while commit.has_key("depotFile%s" % fnum):
2121 path = commit["depotFile%s" % fnum]
6326aa58 2122 found = [p for p in self.depotPaths
d53de8b9 2123 if p4PathStartsWith(path, p)]
6326aa58 2124 if not found:
71b112d4
SH
2125 fnum = fnum + 1
2126 continue
2127
2128 file = {}
2129 file["path"] = path
2130 file["rev"] = commit["rev%s" % fnum]
2131 file["action"] = commit["action%s" % fnum]
2132 file["type"] = commit["type%s" % fnum]
2133 fnum = fnum + 1
2134
21ef5df4
PW
2135 # start with the full relative path where this file would
2136 # go in a p4 client
2137 if self.useClientSpec:
2138 relPath = self.clientSpecDirs.map_in_client(path)
2139 else:
2140 relPath = self.stripRepoPath(path, self.depotPaths)
b984733c 2141
4b97ffb1 2142 for branch in self.knownBranches.keys():
21ef5df4
PW
2143 # add a trailing slash so that a commit into qt/4.2foo
2144 # doesn't end up in qt/4.2, e.g.
6754a299 2145 if relPath.startswith(branch + "/"):
d5904674
SH
2146 if branch not in branches:
2147 branches[branch] = []
71b112d4 2148 branches[branch].append(file)
6555b2cc 2149 break
b984733c
SH
2150
2151 return branches
2152
b932705b
LD
2153 # output one file from the P4 stream
2154 # - helper for streamP4Files
2155
2156 def streamOneP4File(self, file, contents):
b932705b
LD
2157 relPath = self.stripRepoPath(file['depotFile'], self.branchPrefixes)
2158 if verbose:
2159 sys.stderr.write("%s\n" % relPath)
2160
9cffb8c8
PW
2161 (type_base, type_mods) = split_p4_type(file["type"])
2162
2163 git_mode = "100644"
2164 if "x" in type_mods:
2165 git_mode = "100755"
2166 if type_base == "symlink":
2167 git_mode = "120000"
1292df11
AJ
2168 # p4 print on a symlink sometimes contains "target\n";
2169 # if it does, remove the newline
b39c3612 2170 data = ''.join(contents)
40f846c3
PW
2171 if not data:
2172 # Some version of p4 allowed creating a symlink that pointed
2173 # to nothing. This causes p4 errors when checking out such
2174 # a change, and errors here too. Work around it by ignoring
2175 # the bad symlink; hopefully a future change fixes it.
2176 print "\nIgnoring empty symlink in %s" % file['depotFile']
2177 return
2178 elif data[-1] == '\n':
1292df11
AJ
2179 contents = [data[:-1]]
2180 else:
2181 contents = [data]
b932705b 2182
9cffb8c8 2183 if type_base == "utf16":
55aa5714
PW
2184 # p4 delivers different text in the python output to -G
2185 # than it does when using "print -o", or normal p4 client
2186 # operations. utf16 is converted to ascii or utf8, perhaps.
2187 # But ascii text saved as -t utf16 is completely mangled.
2188 # Invoke print -o to get the real contents.
7f0e5962
PW
2189 #
2190 # On windows, the newlines will always be mangled by print, so put
2191 # them back too. This is not needed to the cygwin windows version,
2192 # just the native "NT" type.
2193 #
1f5f3907
LS
2194 try:
2195 text = p4_read_pipe(['print', '-q', '-o', '-', '%s@%s' % (file['depotFile'], file['change'])])
2196 except Exception as e:
2197 if 'Translation of file content failed' in str(e):
2198 type_base = 'binary'
2199 else:
2200 raise e
2201 else:
2202 if p4_version_string().find('/NT') >= 0:
2203 text = text.replace('\r\n', '\n')
2204 contents = [ text ]
55aa5714 2205
9f7ef0ea
PW
2206 if type_base == "apple":
2207 # Apple filetype files will be streamed as a concatenation of
2208 # its appledouble header and the contents. This is useless
2209 # on both macs and non-macs. If using "print -q -o xx", it
2210 # will create "xx" with the data, and "%xx" with the header.
2211 # This is also not very useful.
2212 #
2213 # Ideally, someday, this script can learn how to generate
2214 # appledouble files directly and import those to git, but
2215 # non-mac machines can never find a use for apple filetype.
2216 print "\nIgnoring apple filetype file %s" % file['depotFile']
2217 return
2218
55aa5714
PW
2219 # Note that we do not try to de-mangle keywords on utf16 files,
2220 # even though in theory somebody may want that.
60df071c
LD
2221 pattern = p4_keywords_regexp_for_type(type_base, type_mods)
2222 if pattern:
2223 regexp = re.compile(pattern, re.VERBOSE)
2224 text = ''.join(contents)
2225 text = regexp.sub(r'$\1$', text)
2226 contents = [ text ]
b932705b 2227
9cffb8c8 2228 self.gitStream.write("M %s inline %s\n" % (git_mode, relPath))
b932705b
LD
2229
2230 # total length...
2231 length = 0
2232 for d in contents:
2233 length = length + len(d)
2234
2235 self.gitStream.write("data %d\n" % length)
2236 for d in contents:
2237 self.gitStream.write(d)
2238 self.gitStream.write("\n")
2239
2240 def streamOneP4Deletion(self, file):
2241 relPath = self.stripRepoPath(file['path'], self.branchPrefixes)
2242 if verbose:
2243 sys.stderr.write("delete %s\n" % relPath)
2244 self.gitStream.write("D %s\n" % relPath)
2245
2246 # handle another chunk of streaming data
2247 def streamP4FilesCb(self, marshalled):
2248
78189bea
PW
2249 # catch p4 errors and complain
2250 err = None
2251 if "code" in marshalled:
2252 if marshalled["code"] == "error":
2253 if "data" in marshalled:
2254 err = marshalled["data"].rstrip()
2255 if err:
2256 f = None
2257 if self.stream_have_file_info:
2258 if "depotFile" in self.stream_file:
2259 f = self.stream_file["depotFile"]
2260 # force a failure in fast-import, else an empty
2261 # commit will be made
2262 self.gitStream.write("\n")
2263 self.gitStream.write("die-now\n")
2264 self.gitStream.close()
2265 # ignore errors, but make sure it exits first
2266 self.importProcess.wait()
2267 if f:
2268 die("Error from p4 print for %s: %s" % (f, err))
2269 else:
2270 die("Error from p4 print: %s" % err)
2271
c3f6163b
AG
2272 if marshalled.has_key('depotFile') and self.stream_have_file_info:
2273 # start of a new file - output the old one first
2274 self.streamOneP4File(self.stream_file, self.stream_contents)
2275 self.stream_file = {}
2276 self.stream_contents = []
2277 self.stream_have_file_info = False
b932705b 2278
c3f6163b
AG
2279 # pick up the new file information... for the
2280 # 'data' field we need to append to our array
2281 for k in marshalled.keys():
2282 if k == 'data':
2283 self.stream_contents.append(marshalled['data'])
2284 else:
2285 self.stream_file[k] = marshalled[k]
b932705b 2286
c3f6163b 2287 self.stream_have_file_info = True
b932705b
LD
2288
2289 # Stream directly from "p4 files" into "git fast-import"
2290 def streamP4Files(self, files):
30b5940b
SH
2291 filesForCommit = []
2292 filesToRead = []
b932705b 2293 filesToDelete = []
30b5940b 2294
3a70cdfa 2295 for f in files:
ecb7cf98
PW
2296 # if using a client spec, only add the files that have
2297 # a path in the client
2298 if self.clientSpecDirs:
2299 if self.clientSpecDirs.map_in_client(f['path']) == "":
2300 continue
3a70cdfa 2301
ecb7cf98
PW
2302 filesForCommit.append(f)
2303 if f['action'] in self.delete_actions:
2304 filesToDelete.append(f)
2305 else:
2306 filesToRead.append(f)
6a49f8e2 2307
b932705b
LD
2308 # deleted files...
2309 for f in filesToDelete:
2310 self.streamOneP4Deletion(f)
1b9a4684 2311
b932705b
LD
2312 if len(filesToRead) > 0:
2313 self.stream_file = {}
2314 self.stream_contents = []
2315 self.stream_have_file_info = False
8ff45f2a 2316
c3f6163b
AG
2317 # curry self argument
2318 def streamP4FilesCbSelf(entry):
2319 self.streamP4FilesCb(entry)
6a49f8e2 2320
6de040df
LD
2321 fileArgs = ['%s#%s' % (f['path'], f['rev']) for f in filesToRead]
2322
2323 p4CmdList(["-x", "-", "print"],
2324 stdin=fileArgs,
2325 cb=streamP4FilesCbSelf)
30b5940b 2326
b932705b
LD
2327 # do the last chunk
2328 if self.stream_file.has_key('depotFile'):
2329 self.streamOneP4File(self.stream_file, self.stream_contents)
6a49f8e2 2330
affb474f
LD
2331 def make_email(self, userid):
2332 if userid in self.users:
2333 return self.users[userid]
2334 else:
2335 return "%s <a@b>" % userid
2336
06804c76 2337 def streamTag(self, gitStream, labelName, labelDetails, commit, epoch):
b43702ac
LD
2338 """ Stream a p4 tag.
2339 commit is either a git commit, or a fast-import mark, ":<p4commit>"
2340 """
2341
06804c76
LD
2342 if verbose:
2343 print "writing tag %s for commit %s" % (labelName, commit)
2344 gitStream.write("tag %s\n" % labelName)
2345 gitStream.write("from %s\n" % commit)
2346
2347 if labelDetails.has_key('Owner'):
2348 owner = labelDetails["Owner"]
2349 else:
2350 owner = None
2351
2352 # Try to use the owner of the p4 label, or failing that,
2353 # the current p4 user id.
2354 if owner:
2355 email = self.make_email(owner)
2356 else:
2357 email = self.make_email(self.p4UserId())
2358 tagger = "%s %s %s" % (email, epoch, self.tz)
2359
2360 gitStream.write("tagger %s\n" % tagger)
2361
2362 print "labelDetails=",labelDetails
2363 if labelDetails.has_key('Description'):
2364 description = labelDetails['Description']
2365 else:
2366 description = 'Label from git p4'
2367
2368 gitStream.write("data %d\n" % len(description))
2369 gitStream.write(description)
2370 gitStream.write("\n")
2371
e63231e5 2372 def commit(self, details, files, branch, parent = ""):
b984733c
SH
2373 epoch = details["time"]
2374 author = details["user"]
2375
4b97ffb1
SH
2376 if self.verbose:
2377 print "commit into %s" % branch
2378
96e07dd2
HWN
2379 # start with reading files; if that fails, we should not
2380 # create a commit.
2381 new_files = []
2382 for f in files:
e63231e5 2383 if [p for p in self.branchPrefixes if p4PathStartsWith(f['path'], p)]:
96e07dd2
HWN
2384 new_files.append (f)
2385 else:
afa1dd9a 2386 sys.stderr.write("Ignoring file outside of prefix: %s\n" % f['path'])
96e07dd2 2387
9d57c4a6
KS
2388 if self.clientSpecDirs:
2389 self.clientSpecDirs.update_client_spec_path_cache(files)
2390
b984733c 2391 self.gitStream.write("commit %s\n" % branch)
b43702ac 2392 self.gitStream.write("mark :%s\n" % details["change"])
b984733c
SH
2393 self.committedChanges.add(int(details["change"]))
2394 committer = ""
b607e71e
SH
2395 if author not in self.users:
2396 self.getUserMapFromPerforceServer()
affb474f 2397 committer = "%s %s %s" % (self.make_email(author), epoch, self.tz)
b984733c
SH
2398
2399 self.gitStream.write("committer %s\n" % committer)
2400
2401 self.gitStream.write("data <<EOT\n")
2402 self.gitStream.write(details["desc"])
e63231e5
PW
2403 self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s" %
2404 (','.join(self.branchPrefixes), details["change"]))
6581de09
SH
2405 if len(details['options']) > 0:
2406 self.gitStream.write(": options = %s" % details['options'])
2407 self.gitStream.write("]\nEOT\n\n")
b984733c
SH
2408
2409 if len(parent) > 0:
4b97ffb1
SH
2410 if self.verbose:
2411 print "parent %s" % parent
b984733c
SH
2412 self.gitStream.write("from %s\n" % parent)
2413
b932705b 2414 self.streamP4Files(new_files)
b984733c
SH
2415 self.gitStream.write("\n")
2416
1f4ba1cb
SH
2417 change = int(details["change"])
2418
9bda3a85 2419 if self.labels.has_key(change):
1f4ba1cb
SH
2420 label = self.labels[change]
2421 labelDetails = label[0]
2422 labelRevisions = label[1]
71b112d4
SH
2423 if self.verbose:
2424 print "Change %s is labelled %s" % (change, labelDetails)
1f4ba1cb 2425
6de040df 2426 files = p4CmdList(["files"] + ["%s...@%s" % (p, change)
e63231e5 2427 for p in self.branchPrefixes])
1f4ba1cb
SH
2428
2429 if len(files) == len(labelRevisions):
2430
2431 cleanedFiles = {}
2432 for info in files:
56c09345 2433 if info["action"] in self.delete_actions:
1f4ba1cb
SH
2434 continue
2435 cleanedFiles[info["depotFile"]] = info["rev"]
2436
2437 if cleanedFiles == labelRevisions:
06804c76 2438 self.streamTag(self.gitStream, 'tag_%s' % labelDetails['label'], labelDetails, branch, epoch)
1f4ba1cb
SH
2439
2440 else:
a46668fa 2441 if not self.silent:
cebdf5af
HWN
2442 print ("Tag %s does not match with change %s: files do not match."
2443 % (labelDetails["label"], change))
1f4ba1cb
SH
2444
2445 else:
a46668fa 2446 if not self.silent:
cebdf5af
HWN
2447 print ("Tag %s does not match with change %s: file count is different."
2448 % (labelDetails["label"], change))
b984733c 2449
06804c76 2450 # Build a dictionary of changelists and labels, for "detect-labels" option.
1f4ba1cb
SH
2451 def getLabels(self):
2452 self.labels = {}
2453
52a4880b 2454 l = p4CmdList(["labels"] + ["%s..." % p for p in self.depotPaths])
10c3211b 2455 if len(l) > 0 and not self.silent:
183f8436 2456 print "Finding files belonging to labels in %s" % `self.depotPaths`
01ce1fe9
SH
2457
2458 for output in l:
1f4ba1cb
SH
2459 label = output["label"]
2460 revisions = {}
2461 newestChange = 0
71b112d4
SH
2462 if self.verbose:
2463 print "Querying files for label %s" % label
6de040df
LD
2464 for file in p4CmdList(["files"] +
2465 ["%s...@%s" % (p, label)
2466 for p in self.depotPaths]):
1f4ba1cb
SH
2467 revisions[file["depotFile"]] = file["rev"]
2468 change = int(file["change"])
2469 if change > newestChange:
2470 newestChange = change
2471
9bda3a85
SH
2472 self.labels[newestChange] = [output, revisions]
2473
2474 if self.verbose:
2475 print "Label changes: %s" % self.labels.keys()
1f4ba1cb 2476
06804c76
LD
2477 # Import p4 labels as git tags. A direct mapping does not
2478 # exist, so assume that if all the files are at the same revision
2479 # then we can use that, or it's something more complicated we should
2480 # just ignore.
2481 def importP4Labels(self, stream, p4Labels):
2482 if verbose:
2483 print "import p4 labels: " + ' '.join(p4Labels)
2484
2485 ignoredP4Labels = gitConfigList("git-p4.ignoredP4Labels")
c8942a22 2486 validLabelRegexp = gitConfig("git-p4.labelImportRegexp")
06804c76
LD
2487 if len(validLabelRegexp) == 0:
2488 validLabelRegexp = defaultLabelRegexp
2489 m = re.compile(validLabelRegexp)
2490
2491 for name in p4Labels:
2492 commitFound = False
2493
2494 if not m.match(name):
2495 if verbose:
2496 print "label %s does not match regexp %s" % (name,validLabelRegexp)
2497 continue
2498
2499 if name in ignoredP4Labels:
2500 continue
2501
2502 labelDetails = p4CmdList(['label', "-o", name])[0]
2503
2504 # get the most recent changelist for each file in this label
2505 change = p4Cmd(["changes", "-m", "1"] + ["%s...@%s" % (p, name)
2506 for p in self.depotPaths])
2507
2508 if change.has_key('change'):
2509 # find the corresponding git commit; take the oldest commit
2510 changelist = int(change['change'])
b43702ac
LD
2511 if changelist in self.committedChanges:
2512 gitCommit = ":%d" % changelist # use a fast-import mark
06804c76 2513 commitFound = True
b43702ac
LD
2514 else:
2515 gitCommit = read_pipe(["git", "rev-list", "--max-count=1",
2516 "--reverse", ":/\[git-p4:.*change = %d\]" % changelist], ignore_error=True)
2517 if len(gitCommit) == 0:
2518 print "importing label %s: could not find git commit for changelist %d" % (name, changelist)
2519 else:
2520 commitFound = True
2521 gitCommit = gitCommit.strip()
2522
2523 if commitFound:
06804c76
LD
2524 # Convert from p4 time format
2525 try:
2526 tmwhen = time.strptime(labelDetails['Update'], "%Y/%m/%d %H:%M:%S")
2527 except ValueError:
a4e9054c 2528 print "Could not convert label time %s" % labelDetails['Update']
06804c76
LD
2529 tmwhen = 1
2530
2531 when = int(time.mktime(tmwhen))
2532 self.streamTag(stream, name, labelDetails, gitCommit, when)
2533 if verbose:
2534 print "p4 label %s mapped to git commit %s" % (name, gitCommit)
2535 else:
2536 if verbose:
2537 print "Label %s has no changelists - possibly deleted?" % name
2538
2539 if not commitFound:
2540 # We can't import this label; don't try again as it will get very
2541 # expensive repeatedly fetching all the files for labels that will
2542 # never be imported. If the label is moved in the future, the
2543 # ignore will need to be removed manually.
2544 system(["git", "config", "--add", "git-p4.ignoredP4Labels", name])
2545
86dff6b6
HWN
2546 def guessProjectName(self):
2547 for p in self.depotPaths:
6e5295c4
SH
2548 if p.endswith("/"):
2549 p = p[:-1]
2550 p = p[p.strip().rfind("/") + 1:]
2551 if not p.endswith("/"):
2552 p += "/"
2553 return p
86dff6b6 2554
4b97ffb1 2555 def getBranchMapping(self):
6555b2cc
SH
2556 lostAndFoundBranches = set()
2557
8ace74c0
VA
2558 user = gitConfig("git-p4.branchUser")
2559 if len(user) > 0:
2560 command = "branches -u %s" % user
2561 else:
2562 command = "branches"
2563
2564 for info in p4CmdList(command):
52a4880b 2565 details = p4Cmd(["branch", "-o", info["branch"]])
4b97ffb1
SH
2566 viewIdx = 0
2567 while details.has_key("View%s" % viewIdx):
2568 paths = details["View%s" % viewIdx].split(" ")
2569 viewIdx = viewIdx + 1
2570 # require standard //depot/foo/... //depot/bar/... mapping
2571 if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."):
2572 continue
2573 source = paths[0]
2574 destination = paths[1]
6509e19c 2575 ## HACK
d53de8b9 2576 if p4PathStartsWith(source, self.depotPaths[0]) and p4PathStartsWith(destination, self.depotPaths[0]):
6509e19c
SH
2577 source = source[len(self.depotPaths[0]):-4]
2578 destination = destination[len(self.depotPaths[0]):-4]
6555b2cc 2579
1a2edf4e
SH
2580 if destination in self.knownBranches:
2581 if not self.silent:
2582 print "p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination)
2583 print "but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination)
2584 continue
2585
6555b2cc
SH
2586 self.knownBranches[destination] = source
2587
2588 lostAndFoundBranches.discard(destination)
2589
29bdbac1 2590 if source not in self.knownBranches:
6555b2cc
SH
2591 lostAndFoundBranches.add(source)
2592
7199cf13
VA
2593 # Perforce does not strictly require branches to be defined, so we also
2594 # check git config for a branch list.
2595 #
2596 # Example of branch definition in git config file:
2597 # [git-p4]
2598 # branchList=main:branchA
2599 # branchList=main:branchB
2600 # branchList=branchA:branchC
2601 configBranches = gitConfigList("git-p4.branchList")
2602 for branch in configBranches:
2603 if branch:
2604 (source, destination) = branch.split(":")
2605 self.knownBranches[destination] = source
2606
2607 lostAndFoundBranches.discard(destination)
2608
2609 if source not in self.knownBranches:
2610 lostAndFoundBranches.add(source)
2611
6555b2cc
SH
2612
2613 for branch in lostAndFoundBranches:
2614 self.knownBranches[branch] = branch
29bdbac1 2615
38f9f5ec
SH
2616 def getBranchMappingFromGitBranches(self):
2617 branches = p4BranchesInGit(self.importIntoRemotes)
2618 for branch in branches.keys():
2619 if branch == "master":
2620 branch = "main"
2621 else:
2622 branch = branch[len(self.projectName):]
2623 self.knownBranches[branch] = branch
2624
bb6e09b2
HWN
2625 def updateOptionDict(self, d):
2626 option_keys = {}
2627 if self.keepRepoPath:
2628 option_keys['keepRepoPath'] = 1
2629
2630 d["options"] = ' '.join(sorted(option_keys.keys()))
2631
2632 def readOptions(self, d):
2633 self.keepRepoPath = (d.has_key('options')
2634 and ('keepRepoPath' in d['options']))
6326aa58 2635
8134f69c
SH
2636 def gitRefForBranch(self, branch):
2637 if branch == "main":
2638 return self.refPrefix + "master"
2639
2640 if len(branch) <= 0:
2641 return branch
2642
2643 return self.refPrefix + self.projectName + branch
2644
1ca3d710
SH
2645 def gitCommitByP4Change(self, ref, change):
2646 if self.verbose:
2647 print "looking in ref " + ref + " for change %s using bisect..." % change
2648
2649 earliestCommit = ""
2650 latestCommit = parseRevision(ref)
2651
2652 while True:
2653 if self.verbose:
2654 print "trying: earliest %s latest %s" % (earliestCommit, latestCommit)
2655 next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip()
2656 if len(next) == 0:
2657 if self.verbose:
2658 print "argh"
2659 return ""
2660 log = extractLogMessageFromGitCommit(next)
2661 settings = extractSettingsGitLog(log)
2662 currentChange = int(settings['change'])
2663 if self.verbose:
2664 print "current change %s" % currentChange
2665
2666 if currentChange == change:
2667 if self.verbose:
2668 print "found %s" % next
2669 return next
2670
2671 if currentChange < change:
2672 earliestCommit = "^%s" % next
2673 else:
2674 latestCommit = "%s" % next
2675
2676 return ""
2677
2678 def importNewBranch(self, branch, maxChange):
2679 # make fast-import flush all changes to disk and update the refs using the checkpoint
2680 # command so that we can try to find the branch parent in the git history
2681 self.gitStream.write("checkpoint\n\n");
2682 self.gitStream.flush();
2683 branchPrefix = self.depotPaths[0] + branch + "/"
2684 range = "@1,%s" % maxChange
2685 #print "prefix" + branchPrefix
96b2d54a 2686 changes = p4ChangesForPaths([branchPrefix], range, self.changes_block_size)
1ca3d710
SH
2687 if len(changes) <= 0:
2688 return False
2689 firstChange = changes[0]
2690 #print "first change in branch: %s" % firstChange
2691 sourceBranch = self.knownBranches[branch]
2692 sourceDepotPath = self.depotPaths[0] + sourceBranch
2693 sourceRef = self.gitRefForBranch(sourceBranch)
2694 #print "source " + sourceBranch
2695
52a4880b 2696 branchParentChange = int(p4Cmd(["changes", "-m", "1", "%s...@1,%s" % (sourceDepotPath, firstChange)])["change"])
1ca3d710
SH
2697 #print "branch parent: %s" % branchParentChange
2698 gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)
2699 if len(gitParent) > 0:
2700 self.initialParents[self.gitRefForBranch(branch)] = gitParent
2701 #print "parent git commit: %s" % gitParent
2702
2703 self.importChanges(changes)
2704 return True
2705
fed23693
VA
2706 def searchParent(self, parent, branch, target):
2707 parentFound = False
c7d34884
PW
2708 for blob in read_pipe_lines(["git", "rev-list", "--reverse",
2709 "--no-merges", parent]):
fed23693
VA
2710 blob = blob.strip()
2711 if len(read_pipe(["git", "diff-tree", blob, target])) == 0:
2712 parentFound = True
2713 if self.verbose:
2714 print "Found parent of %s in commit %s" % (branch, blob)
2715 break
2716 if parentFound:
2717 return blob
2718 else:
2719 return None
2720
e87f37ae
SH
2721 def importChanges(self, changes):
2722 cnt = 1
2723 for change in changes:
18fa13d0 2724 description = p4_describe(change)
e87f37ae
SH
2725 self.updateOptionDict(description)
2726
2727 if not self.silent:
2728 sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
2729 sys.stdout.flush()
2730 cnt = cnt + 1
2731
2732 try:
2733 if self.detectBranches:
2734 branches = self.splitFilesIntoBranches(description)
2735 for branch in branches.keys():
2736 ## HACK --hwn
2737 branchPrefix = self.depotPaths[0] + branch + "/"
e63231e5 2738 self.branchPrefixes = [ branchPrefix ]
e87f37ae
SH
2739
2740 parent = ""
2741
2742 filesForCommit = branches[branch]
2743
2744 if self.verbose:
2745 print "branch is %s" % branch
2746
2747 self.updatedBranches.add(branch)
2748
2749 if branch not in self.createdBranches:
2750 self.createdBranches.add(branch)
2751 parent = self.knownBranches[branch]
2752 if parent == branch:
2753 parent = ""
1ca3d710
SH
2754 else:
2755 fullBranch = self.projectName + branch
2756 if fullBranch not in self.p4BranchesInGit:
2757 if not self.silent:
2758 print("\n Importing new branch %s" % fullBranch);
2759 if self.importNewBranch(branch, change - 1):
2760 parent = ""
2761 self.p4BranchesInGit.append(fullBranch)
2762 if not self.silent:
2763 print("\n Resuming with change %s" % change);
2764
2765 if self.verbose:
2766 print "parent determined through known branches: %s" % parent
e87f37ae 2767
8134f69c
SH
2768 branch = self.gitRefForBranch(branch)
2769 parent = self.gitRefForBranch(parent)
e87f37ae
SH
2770
2771 if self.verbose:
2772 print "looking for initial parent for %s; current parent is %s" % (branch, parent)
2773
2774 if len(parent) == 0 and branch in self.initialParents:
2775 parent = self.initialParents[branch]
2776 del self.initialParents[branch]
2777
fed23693
VA
2778 blob = None
2779 if len(parent) > 0:
4f9273d2 2780 tempBranch = "%s/%d" % (self.tempBranchLocation, change)
fed23693
VA
2781 if self.verbose:
2782 print "Creating temporary branch: " + tempBranch
e63231e5 2783 self.commit(description, filesForCommit, tempBranch)
fed23693
VA
2784 self.tempBranches.append(tempBranch)
2785 self.checkpoint()
2786 blob = self.searchParent(parent, branch, tempBranch)
2787 if blob:
e63231e5 2788 self.commit(description, filesForCommit, branch, blob)
fed23693
VA
2789 else:
2790 if self.verbose:
2791 print "Parent of %s not found. Committing into head of %s" % (branch, parent)
e63231e5 2792 self.commit(description, filesForCommit, branch, parent)
e87f37ae
SH
2793 else:
2794 files = self.extractFilesFromCommit(description)
e63231e5 2795 self.commit(description, files, self.branch,
e87f37ae 2796 self.initialParent)
47497844 2797 # only needed once, to connect to the previous commit
e87f37ae
SH
2798 self.initialParent = ""
2799 except IOError:
2800 print self.gitError.read()
2801 sys.exit(1)
2802
c208a243
SH
2803 def importHeadRevision(self, revision):
2804 print "Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch)
2805
4e2e6ce4
PW
2806 details = {}
2807 details["user"] = "git perforce import user"
1494fcbb 2808 details["desc"] = ("Initial import of %s from the state at revision %s\n"
c208a243
SH
2809 % (' '.join(self.depotPaths), revision))
2810 details["change"] = revision
2811 newestRevision = 0
2812
2813 fileCnt = 0
6de040df
LD
2814 fileArgs = ["%s...%s" % (p,revision) for p in self.depotPaths]
2815
2816 for info in p4CmdList(["files"] + fileArgs):
c208a243 2817
68b28593 2818 if 'code' in info and info['code'] == 'error':
c208a243
SH
2819 sys.stderr.write("p4 returned an error: %s\n"
2820 % info['data'])
d88e707f
PW
2821 if info['data'].find("must refer to client") >= 0:
2822 sys.stderr.write("This particular p4 error is misleading.\n")
2823 sys.stderr.write("Perhaps the depot path was misspelled.\n");
2824 sys.stderr.write("Depot path: %s\n" % " ".join(self.depotPaths))
c208a243 2825 sys.exit(1)
68b28593
PW
2826 if 'p4ExitCode' in info:
2827 sys.stderr.write("p4 exitcode: %s\n" % info['p4ExitCode'])
c208a243
SH
2828 sys.exit(1)
2829
2830
2831 change = int(info["change"])
2832 if change > newestRevision:
2833 newestRevision = change
2834
56c09345 2835 if info["action"] in self.delete_actions:
c208a243
SH
2836 # don't increase the file cnt, otherwise details["depotFile123"] will have gaps!
2837 #fileCnt = fileCnt + 1
2838 continue
2839
2840 for prop in ["depotFile", "rev", "action", "type" ]:
2841 details["%s%s" % (prop, fileCnt)] = info[prop]
2842
2843 fileCnt = fileCnt + 1
2844
2845 details["change"] = newestRevision
4e2e6ce4 2846
9dcb9f24 2847 # Use time from top-most change so that all git p4 clones of
4e2e6ce4 2848 # the same p4 repo have the same commit SHA1s.
18fa13d0
PW
2849 res = p4_describe(newestRevision)
2850 details["time"] = res["time"]
4e2e6ce4 2851
c208a243
SH
2852 self.updateOptionDict(details)
2853 try:
e63231e5 2854 self.commit(details, self.extractFilesFromCommit(details), self.branch)
c208a243
SH
2855 except IOError:
2856 print "IO error with git fast-import. Is your git version recent enough?"
2857 print self.gitError.read()
2858
2859
b984733c 2860 def run(self, args):
6326aa58 2861 self.depotPaths = []
179caebf 2862 self.changeRange = ""
6326aa58 2863 self.previousDepotPaths = []
991a2de4 2864 self.hasOrigin = False
ce6f33c8 2865
29bdbac1
SH
2866 # map from branch depot path to parent branch
2867 self.knownBranches = {}
2868 self.initialParents = {}
2869
a028a98e
SH
2870 if self.importIntoRemotes:
2871 self.refPrefix = "refs/remotes/p4/"
2872 else:
db775559 2873 self.refPrefix = "refs/heads/p4/"
a028a98e 2874
991a2de4
PW
2875 if self.syncWithOrigin:
2876 self.hasOrigin = originP4BranchesExist()
2877 if self.hasOrigin:
2878 if not self.silent:
2879 print 'Syncing with origin first, using "git fetch origin"'
2880 system("git fetch origin")
10f880f8 2881
5a8e84cd 2882 branch_arg_given = bool(self.branch)
569d1bd4 2883 if len(self.branch) == 0:
db775559 2884 self.branch = self.refPrefix + "master"
a028a98e 2885 if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
48df6fd8 2886 system("git update-ref %s refs/heads/p4" % self.branch)
55d12437 2887 system("git branch -D p4")
967f72e2 2888
a93d33ee
PW
2889 # accept either the command-line option, or the configuration variable
2890 if self.useClientSpec:
2891 # will use this after clone to set the variable
2892 self.useClientSpec_from_options = True
2893 else:
0d609032 2894 if gitConfigBool("git-p4.useclientspec"):
09fca77b
PW
2895 self.useClientSpec = True
2896 if self.useClientSpec:
543987bd 2897 self.clientSpecDirs = getClientSpec()
3a70cdfa 2898
6a49f8e2
HWN
2899 # TODO: should always look at previous commits,
2900 # merge with previous imports, if possible.
2901 if args == []:
d414c74a 2902 if self.hasOrigin:
5ca44617 2903 createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)
3b650fc9
PW
2904
2905 # branches holds mapping from branch name to sha1
2906 branches = p4BranchesInGit(self.importIntoRemotes)
8c9e8b6e
PW
2907
2908 # restrict to just this one, disabling detect-branches
2909 if branch_arg_given:
2910 short = self.branch.split("/")[-1]
2911 if short in branches:
2912 self.p4BranchesInGit = [ short ]
2913 else:
2914 self.p4BranchesInGit = branches.keys()
abcd790f
SH
2915
2916 if len(self.p4BranchesInGit) > 1:
2917 if not self.silent:
2918 print "Importing from/into multiple branches"
2919 self.detectBranches = True
8c9e8b6e
PW
2920 for branch in branches.keys():
2921 self.initialParents[self.refPrefix + branch] = \
2922 branches[branch]
967f72e2 2923
29bdbac1
SH
2924 if self.verbose:
2925 print "branches: %s" % self.p4BranchesInGit
2926
2927 p4Change = 0
2928 for branch in self.p4BranchesInGit:
cebdf5af 2929 logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch)
bb6e09b2
HWN
2930
2931 settings = extractSettingsGitLog(logMsg)
29bdbac1 2932
bb6e09b2
HWN
2933 self.readOptions(settings)
2934 if (settings.has_key('depot-paths')
2935 and settings.has_key ('change')):
2936 change = int(settings['change']) + 1
29bdbac1
SH
2937 p4Change = max(p4Change, change)
2938
bb6e09b2
HWN
2939 depotPaths = sorted(settings['depot-paths'])
2940 if self.previousDepotPaths == []:
6326aa58 2941 self.previousDepotPaths = depotPaths
29bdbac1 2942 else:
6326aa58
HWN
2943 paths = []
2944 for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
04d277b3
VA
2945 prev_list = prev.split("/")
2946 cur_list = cur.split("/")
2947 for i in range(0, min(len(cur_list), len(prev_list))):
2948 if cur_list[i] <> prev_list[i]:
583e1707 2949 i = i - 1
6326aa58
HWN
2950 break
2951
04d277b3 2952 paths.append ("/".join(cur_list[:i + 1]))
6326aa58
HWN
2953
2954 self.previousDepotPaths = paths
29bdbac1
SH
2955
2956 if p4Change > 0:
bb6e09b2 2957 self.depotPaths = sorted(self.previousDepotPaths)
d5904674 2958 self.changeRange = "@%s,#head" % p4Change
341dc1c1 2959 if not self.silent and not self.detectBranches:
967f72e2 2960 print "Performing incremental import into %s git branch" % self.branch
569d1bd4 2961
40d69ac3
PW
2962 # accept multiple ref name abbreviations:
2963 # refs/foo/bar/branch -> use it exactly
2964 # p4/branch -> prepend refs/remotes/ or refs/heads/
2965 # branch -> prepend refs/remotes/p4/ or refs/heads/p4/
f9162f6a 2966 if not self.branch.startswith("refs/"):
40d69ac3
PW
2967 if self.importIntoRemotes:
2968 prepend = "refs/remotes/"
2969 else:
2970 prepend = "refs/heads/"
2971 if not self.branch.startswith("p4/"):
2972 prepend += "p4/"
2973 self.branch = prepend + self.branch
179caebf 2974
6326aa58 2975 if len(args) == 0 and self.depotPaths:
b984733c 2976 if not self.silent:
6326aa58 2977 print "Depot paths: %s" % ' '.join(self.depotPaths)
b984733c 2978 else:
6326aa58 2979 if self.depotPaths and self.depotPaths != args:
cebdf5af 2980 print ("previous import used depot path %s and now %s was specified. "
6326aa58
HWN
2981 "This doesn't work!" % (' '.join (self.depotPaths),
2982 ' '.join (args)))
b984733c 2983 sys.exit(1)
6326aa58 2984
bb6e09b2 2985 self.depotPaths = sorted(args)
b984733c 2986
1c49fc19 2987 revision = ""
b984733c 2988 self.users = {}
b984733c 2989
58c8bc7c
PW
2990 # Make sure no revision specifiers are used when --changesfile
2991 # is specified.
2992 bad_changesfile = False
2993 if len(self.changesFile) > 0:
2994 for p in self.depotPaths:
2995 if p.find("@") >= 0 or p.find("#") >= 0:
2996 bad_changesfile = True
2997 break
2998 if bad_changesfile:
2999 die("Option --changesfile is incompatible with revision specifiers")
3000
6326aa58
HWN
3001 newPaths = []
3002 for p in self.depotPaths:
3003 if p.find("@") != -1:
3004 atIdx = p.index("@")
3005 self.changeRange = p[atIdx:]
3006 if self.changeRange == "@all":
3007 self.changeRange = ""
6a49f8e2 3008 elif ',' not in self.changeRange:
1c49fc19 3009 revision = self.changeRange
6326aa58 3010 self.changeRange = ""
7fcff9de 3011 p = p[:atIdx]
6326aa58
HWN
3012 elif p.find("#") != -1:
3013 hashIdx = p.index("#")
1c49fc19 3014 revision = p[hashIdx:]
7fcff9de 3015 p = p[:hashIdx]
6326aa58 3016 elif self.previousDepotPaths == []:
58c8bc7c
PW
3017 # pay attention to changesfile, if given, else import
3018 # the entire p4 tree at the head revision
3019 if len(self.changesFile) == 0:
3020 revision = "#head"
6326aa58
HWN
3021
3022 p = re.sub ("\.\.\.$", "", p)
3023 if not p.endswith("/"):
3024 p += "/"
3025
3026 newPaths.append(p)
3027
3028 self.depotPaths = newPaths
3029
e63231e5
PW
3030 # --detect-branches may change this for each branch
3031 self.branchPrefixes = self.depotPaths
3032
b607e71e 3033 self.loadUserMapFromCache()
cb53e1f8
SH
3034 self.labels = {}
3035 if self.detectLabels:
3036 self.getLabels();
b984733c 3037
4b97ffb1 3038 if self.detectBranches:
df450923
SH
3039 ## FIXME - what's a P4 projectName ?
3040 self.projectName = self.guessProjectName()
3041
38f9f5ec
SH
3042 if self.hasOrigin:
3043 self.getBranchMappingFromGitBranches()
3044 else:
3045 self.getBranchMapping()
29bdbac1
SH
3046 if self.verbose:
3047 print "p4-git branches: %s" % self.p4BranchesInGit
3048 print "initial parents: %s" % self.initialParents
3049 for b in self.p4BranchesInGit:
3050 if b != "master":
6326aa58
HWN
3051
3052 ## FIXME
29bdbac1
SH
3053 b = b[len(self.projectName):]
3054 self.createdBranches.add(b)
4b97ffb1 3055
f291b4e3 3056 self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
b984733c 3057
78189bea
PW
3058 self.importProcess = subprocess.Popen(["git", "fast-import"],
3059 stdin=subprocess.PIPE,
3060 stdout=subprocess.PIPE,
3061 stderr=subprocess.PIPE);
3062 self.gitOutput = self.importProcess.stdout
3063 self.gitStream = self.importProcess.stdin
3064 self.gitError = self.importProcess.stderr
b984733c 3065
1c49fc19 3066 if revision:
c208a243 3067 self.importHeadRevision(revision)
b984733c
SH
3068 else:
3069 changes = []
3070
0828ab14 3071 if len(self.changesFile) > 0:
b984733c 3072 output = open(self.changesFile).readlines()
1d7367dc 3073 changeSet = set()
b984733c
SH
3074 for line in output:
3075 changeSet.add(int(line))
3076
3077 for change in changeSet:
3078 changes.append(change)
3079
3080 changes.sort()
3081 else:
9dcb9f24
PW
3082 # catch "git p4 sync" with no new branches, in a repo that
3083 # does not have any existing p4 branches
5a8e84cd
PW
3084 if len(args) == 0:
3085 if not self.p4BranchesInGit:
3086 die("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.")
3087
3088 # The default branch is master, unless --branch is used to
3089 # specify something else. Make sure it exists, or complain
3090 # nicely about how to use --branch.
3091 if not self.detectBranches:
3092 if not branch_exists(self.branch):
3093 if branch_arg_given:
3094 die("Error: branch %s does not exist." % self.branch)
3095 else:
3096 die("Error: no branch %s; perhaps specify one with --branch." %
3097 self.branch)
3098
29bdbac1 3099 if self.verbose:
86dff6b6 3100 print "Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
6326aa58 3101 self.changeRange)
96b2d54a 3102 changes = p4ChangesForPaths(self.depotPaths, self.changeRange, self.changes_block_size)
b984733c 3103
01a9c9c5 3104 if len(self.maxChanges) > 0:
7fcff9de 3105 changes = changes[:min(int(self.maxChanges), len(changes))]
01a9c9c5 3106
b984733c 3107 if len(changes) == 0:
0828ab14 3108 if not self.silent:
341dc1c1 3109 print "No changes to import!"
06804c76
LD
3110 else:
3111 if not self.silent and not self.detectBranches:
3112 print "Import destination: %s" % self.branch
3113
3114 self.updatedBranches = set()
b984733c 3115
47497844
PW
3116 if not self.detectBranches:
3117 if args:
3118 # start a new branch
3119 self.initialParent = ""
3120 else:
3121 # build on a previous revision
3122 self.initialParent = parseRevision(self.branch)
3123
06804c76 3124 self.importChanges(changes)
a9d1a27a 3125
06804c76
LD
3126 if not self.silent:
3127 print ""
3128 if len(self.updatedBranches) > 0:
3129 sys.stdout.write("Updated branches: ")
3130 for b in self.updatedBranches:
3131 sys.stdout.write("%s " % b)
3132 sys.stdout.write("\n")
341dc1c1 3133
0d609032 3134 if gitConfigBool("git-p4.importLabels"):
06dcd152 3135 self.importLabels = True
b984733c 3136
06804c76
LD
3137 if self.importLabels:
3138 p4Labels = getP4Labels(self.depotPaths)
3139 gitTags = getGitTags()
3140
3141 missingP4Labels = p4Labels - gitTags
3142 self.importP4Labels(self.gitStream, missingP4Labels)
b984733c 3143
b984733c 3144 self.gitStream.close()
78189bea 3145 if self.importProcess.wait() != 0:
29bdbac1 3146 die("fast-import failed: %s" % self.gitError.read())
b984733c
SH
3147 self.gitOutput.close()
3148 self.gitError.close()
3149
fed23693
VA
3150 # Cleanup temporary branches created during import
3151 if self.tempBranches != []:
3152 for branch in self.tempBranches:
3153 read_pipe("git update-ref -d %s" % branch)
3154 os.rmdir(os.path.join(os.environ.get("GIT_DIR", ".git"), self.tempBranchLocation))
3155
55d12437
PW
3156 # Create a symbolic ref p4/HEAD pointing to p4/<branch> to allow
3157 # a convenient shortcut refname "p4".
3158 if self.importIntoRemotes:
3159 head_ref = self.refPrefix + "HEAD"
3160 if not gitBranchExists(head_ref) and gitBranchExists(self.branch):
3161 system(["git", "symbolic-ref", head_ref, self.branch])
3162
b984733c
SH
3163 return True
3164
01ce1fe9
SH
3165class P4Rebase(Command):
3166 def __init__(self):
3167 Command.__init__(self)
06804c76
LD
3168 self.options = [
3169 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
06804c76 3170 ]
06804c76 3171 self.importLabels = False
cebdf5af
HWN
3172 self.description = ("Fetches the latest revision from perforce and "
3173 + "rebases the current work (branch) against it")
01ce1fe9
SH
3174
3175 def run(self, args):
3176 sync = P4Sync()
06804c76 3177 sync.importLabels = self.importLabels
01ce1fe9 3178 sync.run([])
d7e3868c 3179
14594f4b
SH
3180 return self.rebase()
3181
3182 def rebase(self):
36ee4ee4
SH
3183 if os.system("git update-index --refresh") != 0:
3184 die("Some files in your working directory are modified and different than what is in your index. You can use git update-index <filename> to bring the index up-to-date or stash away all your changes with git stash.");
3185 if len(read_pipe("git diff-index HEAD --")) > 0:
f7e604ed 3186 die("You have uncommitted changes. Please commit them before rebasing or stash them away with git stash.");
36ee4ee4 3187
d7e3868c
SH
3188 [upstream, settings] = findUpstreamBranchPoint()
3189 if len(upstream) == 0:
3190 die("Cannot find upstream branchpoint for rebase")
3191
3192 # the branchpoint may be p4/foo~3, so strip off the parent
3193 upstream = re.sub("~[0-9]+$", "", upstream)
3194
3195 print "Rebasing the current branch onto %s" % upstream
b25b2065 3196 oldHead = read_pipe("git rev-parse HEAD").strip()
d7e3868c 3197 system("git rebase %s" % upstream)
4e49d95e 3198 system("git diff-tree --stat --summary -M %s HEAD --" % oldHead)
01ce1fe9
SH
3199 return True
3200
f9a3a4f7
SH
3201class P4Clone(P4Sync):
3202 def __init__(self):
3203 P4Sync.__init__(self)
3204 self.description = "Creates a new git repository and imports from Perforce into it"
bb6e09b2 3205 self.usage = "usage: %prog [options] //depot/path[@revRange]"
354081d5 3206 self.options += [
bb6e09b2
HWN
3207 optparse.make_option("--destination", dest="cloneDestination",
3208 action='store', default=None,
354081d5 3209 help="where to leave result of the clone"),
38200076
PW
3210 optparse.make_option("--bare", dest="cloneBare",
3211 action="store_true", default=False),
354081d5 3212 ]
bb6e09b2 3213 self.cloneDestination = None
f9a3a4f7 3214 self.needsGit = False
38200076 3215 self.cloneBare = False
f9a3a4f7 3216
6a49f8e2
HWN
3217 def defaultDestination(self, args):
3218 ## TODO: use common prefix of args?
3219 depotPath = args[0]
3220 depotDir = re.sub("(@[^@]*)$", "", depotPath)
3221 depotDir = re.sub("(#[^#]*)$", "", depotDir)
053d9e43 3222 depotDir = re.sub(r"\.\.\.$", "", depotDir)
6a49f8e2
HWN
3223 depotDir = re.sub(r"/$", "", depotDir)
3224 return os.path.split(depotDir)[1]
3225
f9a3a4f7
SH
3226 def run(self, args):
3227 if len(args) < 1:
3228 return False
bb6e09b2
HWN
3229
3230 if self.keepRepoPath and not self.cloneDestination:
3231 sys.stderr.write("Must specify destination for --keep-path\n")
3232 sys.exit(1)
f9a3a4f7 3233
6326aa58 3234 depotPaths = args
5e100b5c
SH
3235
3236 if not self.cloneDestination and len(depotPaths) > 1:
3237 self.cloneDestination = depotPaths[-1]
3238 depotPaths = depotPaths[:-1]
3239
354081d5 3240 self.cloneExclude = ["/"+p for p in self.cloneExclude]
6326aa58
HWN
3241 for p in depotPaths:
3242 if not p.startswith("//"):
0f487d30 3243 sys.stderr.write('Depot paths must start with "//": %s\n' % p)
6326aa58 3244 return False
f9a3a4f7 3245
bb6e09b2 3246 if not self.cloneDestination:
98ad4faf 3247 self.cloneDestination = self.defaultDestination(args)
f9a3a4f7 3248
86dff6b6 3249 print "Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination)
38200076 3250
c3bf3f13
KG
3251 if not os.path.exists(self.cloneDestination):
3252 os.makedirs(self.cloneDestination)
053fd0c1 3253 chdir(self.cloneDestination)
38200076
PW
3254
3255 init_cmd = [ "git", "init" ]
3256 if self.cloneBare:
3257 init_cmd.append("--bare")
a235e85c
BC
3258 retcode = subprocess.call(init_cmd)
3259 if retcode:
3260 raise CalledProcessError(retcode, init_cmd)
38200076 3261
6326aa58 3262 if not P4Sync.run(self, depotPaths):
f9a3a4f7 3263 return False
c595956d
PW
3264
3265 # create a master branch and check out a work tree
3266 if gitBranchExists(self.branch):
3267 system([ "git", "branch", "master", self.branch ])
3268 if not self.cloneBare:
3269 system([ "git", "checkout", "-f" ])
3270 else:
3271 print 'Not checking out any branch, use ' \
3272 '"git checkout -q -b master <branch>"'
86dff6b6 3273
a93d33ee
PW
3274 # auto-set this variable if invoked with --use-client-spec
3275 if self.useClientSpec_from_options:
3276 system("git config --bool git-p4.useclientspec true")
3277
f9a3a4f7
SH
3278 return True
3279
09d89de2
SH
3280class P4Branches(Command):
3281 def __init__(self):
3282 Command.__init__(self)
3283 self.options = [ ]
3284 self.description = ("Shows the git branches that hold imports and their "
3285 + "corresponding perforce depot paths")
3286 self.verbose = False
3287
3288 def run(self, args):
5ca44617
SH
3289 if originP4BranchesExist():
3290 createOrUpdateBranchesFromOrigin()
3291
09d89de2
SH
3292 cmdline = "git rev-parse --symbolic "
3293 cmdline += " --remotes"
3294
3295 for line in read_pipe_lines(cmdline):
3296 line = line.strip()
3297
3298 if not line.startswith('p4/') or line == "p4/HEAD":
3299 continue
3300 branch = line
3301
3302 log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch)
3303 settings = extractSettingsGitLog(log)
3304
3305 print "%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"])
3306 return True
3307
b984733c
SH
3308class HelpFormatter(optparse.IndentedHelpFormatter):
3309 def __init__(self):
3310 optparse.IndentedHelpFormatter.__init__(self)
3311
3312 def format_description(self, description):
3313 if description:
3314 return description + "\n"
3315 else:
3316 return ""
4f5cf76a 3317
86949eef
SH
3318def printUsage(commands):
3319 print "usage: %s <command> [options]" % sys.argv[0]
3320 print ""
3321 print "valid commands: %s" % ", ".join(commands)
3322 print ""
3323 print "Try %s <command> --help for command specific help." % sys.argv[0]
3324 print ""
3325
3326commands = {
b86f7378
HWN
3327 "debug" : P4Debug,
3328 "submit" : P4Submit,
a9834f58 3329 "commit" : P4Submit,
b86f7378
HWN
3330 "sync" : P4Sync,
3331 "rebase" : P4Rebase,
3332 "clone" : P4Clone,
09d89de2
SH
3333 "rollback" : P4RollBack,
3334 "branches" : P4Branches
86949eef
SH
3335}
3336
86949eef 3337
bb6e09b2
HWN
3338def main():
3339 if len(sys.argv[1:]) == 0:
3340 printUsage(commands.keys())
3341 sys.exit(2)
4f5cf76a 3342
bb6e09b2
HWN
3343 cmdName = sys.argv[1]
3344 try:
b86f7378
HWN
3345 klass = commands[cmdName]
3346 cmd = klass()
bb6e09b2
HWN
3347 except KeyError:
3348 print "unknown command %s" % cmdName
3349 print ""
3350 printUsage(commands.keys())
3351 sys.exit(2)
3352
3353 options = cmd.options
b86f7378 3354 cmd.gitdir = os.environ.get("GIT_DIR", None)
bb6e09b2
HWN
3355
3356 args = sys.argv[2:]
3357
b0ccc80d 3358 options.append(optparse.make_option("--verbose", "-v", dest="verbose", action="store_true"))
6a10b6aa
LD
3359 if cmd.needsGit:
3360 options.append(optparse.make_option("--git-dir", dest="gitdir"))
bb6e09b2 3361
6a10b6aa
LD
3362 parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
3363 options,
3364 description = cmd.description,
3365 formatter = HelpFormatter())
bb6e09b2 3366
6a10b6aa 3367 (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
bb6e09b2
HWN
3368 global verbose
3369 verbose = cmd.verbose
3370 if cmd.needsGit:
b86f7378
HWN
3371 if cmd.gitdir == None:
3372 cmd.gitdir = os.path.abspath(".git")
3373 if not isValidGitDir(cmd.gitdir):
3374 cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
3375 if os.path.exists(cmd.gitdir):
bb6e09b2
HWN
3376 cdup = read_pipe("git rev-parse --show-cdup").strip()
3377 if len(cdup) > 0:
053fd0c1 3378 chdir(cdup);
e20a9e53 3379
b86f7378
HWN
3380 if not isValidGitDir(cmd.gitdir):
3381 if isValidGitDir(cmd.gitdir + "/.git"):
3382 cmd.gitdir += "/.git"
bb6e09b2 3383 else:
b86f7378 3384 die("fatal: cannot locate git repository at %s" % cmd.gitdir)
e20a9e53 3385
b86f7378 3386 os.environ["GIT_DIR"] = cmd.gitdir
86949eef 3387
bb6e09b2
HWN
3388 if not cmd.run(args):
3389 parser.print_help()
09fca77b 3390 sys.exit(2)
4f5cf76a 3391
4f5cf76a 3392
bb6e09b2
HWN
3393if __name__ == '__main__':
3394 main()