PageRenderTime 40ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/git-p4.py

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