PageRenderTime 61ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/git-p4.py

https://bitbucket.org/nbargnesi/git
Python | 3297 lines | 3146 code | 78 blank | 73 comment | 170 complexity | 2bd29f330a41bc7e555643acbb4e1742 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause

Large files files are truncated, but you can click here to view the full file

  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. try:
  26. from subprocess import CalledProcessError
  27. except ImportError:
  28. # from python2.7:subprocess.py
  29. # Exception classes used by this module.
  30. class CalledProcessError(Exception):
  31. """This exception is raised when a process run by check_call() returns
  32. a non-zero exit status. The exit status will be stored in the
  33. returncode attribute."""
  34. def __init__(self, returncode, cmd):
  35. self.returncode = returncode
  36. self.cmd = cmd
  37. def __str__(self):
  38. return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)
  39. verbose = False
  40. # Only labels/tags matching this will be imported/exported
  41. defaultLabelRegexp = r'[a-zA-Z0-9_\-.]+$'
  42. def p4_build_cmd(cmd):
  43. """Build a suitable p4 command line.
  44. This consolidates building and returning a p4 command line into one
  45. location. It means that hooking into the environment, or other configuration
  46. can be done more easily.
  47. """
  48. real_cmd = ["p4"]
  49. user = gitConfig("git-p4.user")
  50. if len(user) > 0:
  51. real_cmd += ["-u",user]
  52. password = gitConfig("git-p4.password")
  53. if len(password) > 0:
  54. real_cmd += ["-P", password]
  55. port = gitConfig("git-p4.port")
  56. if len(port) > 0:
  57. real_cmd += ["-p", port]
  58. host = gitConfig("git-p4.host")
  59. if len(host) > 0:
  60. real_cmd += ["-H", host]
  61. client = gitConfig("git-p4.client")
  62. if len(client) > 0:
  63. real_cmd += ["-c", client]
  64. if isinstance(cmd,basestring):
  65. real_cmd = ' '.join(real_cmd) + ' ' + cmd
  66. else:
  67. real_cmd += cmd
  68. return real_cmd
  69. def chdir(path, is_client_path=False):
  70. """Do chdir to the given path, and set the PWD environment
  71. variable for use by P4. It does not look at getcwd() output.
  72. Since we're not using the shell, it is necessary to set the
  73. PWD environment variable explicitly.
  74. Normally, expand the path to force it to be absolute. This
  75. addresses the use of relative path names inside P4 settings,
  76. e.g. P4CONFIG=.p4config. P4 does not simply open the filename
  77. as given; it looks for .p4config using PWD.
  78. If is_client_path, the path was handed to us directly by p4,
  79. and may be a symbolic link. Do not call os.getcwd() in this
  80. case, because it will cause p4 to think that PWD is not inside
  81. the client path.
  82. """
  83. os.chdir(path)
  84. if not is_client_path:
  85. path = os.getcwd()
  86. os.environ['PWD'] = path
  87. def die(msg):
  88. if verbose:
  89. raise Exception(msg)
  90. else:
  91. sys.stderr.write(msg + "\n")
  92. sys.exit(1)
  93. def write_pipe(c, stdin):
  94. if verbose:
  95. sys.stderr.write('Writing pipe: %s\n' % str(c))
  96. expand = isinstance(c,basestring)
  97. p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand)
  98. pipe = p.stdin
  99. val = pipe.write(stdin)
  100. pipe.close()
  101. if p.wait():
  102. die('Command failed: %s' % str(c))
  103. return val
  104. def p4_write_pipe(c, stdin):
  105. real_cmd = p4_build_cmd(c)
  106. return write_pipe(real_cmd, stdin)
  107. def read_pipe(c, ignore_error=False):
  108. if verbose:
  109. sys.stderr.write('Reading pipe: %s\n' % str(c))
  110. expand = isinstance(c,basestring)
  111. p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
  112. pipe = p.stdout
  113. val = pipe.read()
  114. if p.wait() and not ignore_error:
  115. die('Command failed: %s' % str(c))
  116. return val
  117. def p4_read_pipe(c, ignore_error=False):
  118. real_cmd = p4_build_cmd(c)
  119. return read_pipe(real_cmd, ignore_error)
  120. def read_pipe_lines(c):
  121. if verbose:
  122. sys.stderr.write('Reading pipe: %s\n' % str(c))
  123. expand = isinstance(c, basestring)
  124. p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
  125. pipe = p.stdout
  126. val = pipe.readlines()
  127. if pipe.close() or p.wait():
  128. die('Command failed: %s' % str(c))
  129. return val
  130. def p4_read_pipe_lines(c):
  131. """Specifically invoke p4 on the command supplied. """
  132. real_cmd = p4_build_cmd(c)
  133. return read_pipe_lines(real_cmd)
  134. def p4_has_command(cmd):
  135. """Ask p4 for help on this command. If it returns an error, the
  136. command does not exist in this version of p4."""
  137. real_cmd = p4_build_cmd(["help", cmd])
  138. p = subprocess.Popen(real_cmd, stdout=subprocess.PIPE,
  139. stderr=subprocess.PIPE)
  140. p.communicate()
  141. return p.returncode == 0
  142. def p4_has_move_command():
  143. """See if the move command exists, that it supports -k, and that
  144. it has not been administratively disabled. The arguments
  145. must be correct, but the filenames do not have to exist. Use
  146. ones with wildcards so even if they exist, it will fail."""
  147. if not p4_has_command("move"):
  148. return False
  149. cmd = p4_build_cmd(["move", "-k", "@from", "@to"])
  150. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  151. (out, err) = p.communicate()
  152. # return code will be 1 in either case
  153. if err.find("Invalid option") >= 0:
  154. return False
  155. if err.find("disabled") >= 0:
  156. return False
  157. # assume it failed because @... was invalid changelist
  158. return True
  159. def system(cmd):
  160. expand = isinstance(cmd,basestring)
  161. if verbose:
  162. sys.stderr.write("executing %s\n" % str(cmd))
  163. retcode = subprocess.call(cmd, shell=expand)
  164. if retcode:
  165. raise CalledProcessError(retcode, cmd)
  166. def p4_system(cmd):
  167. """Specifically invoke p4 as the system command. """
  168. real_cmd = p4_build_cmd(cmd)
  169. expand = isinstance(real_cmd, basestring)
  170. retcode = subprocess.call(real_cmd, shell=expand)
  171. if retcode:
  172. raise CalledProcessError(retcode, real_cmd)
  173. _p4_version_string = None
  174. def p4_version_string():
  175. """Read the version string, showing just the last line, which
  176. hopefully is the interesting version bit.
  177. $ p4 -V
  178. Perforce - The Fast Software Configuration Management System.
  179. Copyright 1995-2011 Perforce Software. All rights reserved.
  180. Rev. P4/NTX86/2011.1/393975 (2011/12/16).
  181. """
  182. global _p4_version_string
  183. if not _p4_version_string:
  184. a = p4_read_pipe_lines(["-V"])
  185. _p4_version_string = a[-1].rstrip()
  186. return _p4_version_string
  187. def p4_integrate(src, dest):
  188. p4_system(["integrate", "-Dt", wildcard_encode(src), wildcard_encode(dest)])
  189. def p4_sync(f, *options):
  190. p4_system(["sync"] + list(options) + [wildcard_encode(f)])
  191. def p4_add(f):
  192. # forcibly add file names with wildcards
  193. if wildcard_present(f):
  194. p4_system(["add", "-f", f])
  195. else:
  196. p4_system(["add", f])
  197. def p4_delete(f):
  198. p4_system(["delete", wildcard_encode(f)])
  199. def p4_edit(f):
  200. p4_system(["edit", wildcard_encode(f)])
  201. def p4_revert(f):
  202. p4_system(["revert", wildcard_encode(f)])
  203. def p4_reopen(type, f):
  204. p4_system(["reopen", "-t", type, wildcard_encode(f)])
  205. def p4_move(src, dest):
  206. p4_system(["move", "-k", wildcard_encode(src), wildcard_encode(dest)])
  207. def p4_describe(change):
  208. """Make sure it returns a valid result by checking for
  209. the presence of field "time". Return a dict of the
  210. results."""
  211. ds = p4CmdList(["describe", "-s", str(change)])
  212. if len(ds) != 1:
  213. die("p4 describe -s %d did not return 1 result: %s" % (change, str(ds)))
  214. d = ds[0]
  215. if "p4ExitCode" in d:
  216. die("p4 describe -s %d exited with %d: %s" % (change, d["p4ExitCode"],
  217. str(d)))
  218. if "code" in d:
  219. if d["code"] == "error":
  220. die("p4 describe -s %d returned error code: %s" % (change, str(d)))
  221. if "time" not in d:
  222. die("p4 describe -s %d returned no \"time\": %s" % (change, str(d)))
  223. return d
  224. #
  225. # Canonicalize the p4 type and return a tuple of the
  226. # base type, plus any modifiers. See "p4 help filetypes"
  227. # for a list and explanation.
  228. #
  229. def split_p4_type(p4type):
  230. p4_filetypes_historical = {
  231. "ctempobj": "binary+Sw",
  232. "ctext": "text+C",
  233. "cxtext": "text+Cx",
  234. "ktext": "text+k",
  235. "kxtext": "text+kx",
  236. "ltext": "text+F",
  237. "tempobj": "binary+FSw",
  238. "ubinary": "binary+F",
  239. "uresource": "resource+F",
  240. "uxbinary": "binary+Fx",
  241. "xbinary": "binary+x",
  242. "xltext": "text+Fx",
  243. "xtempobj": "binary+Swx",
  244. "xtext": "text+x",
  245. "xunicode": "unicode+x",
  246. "xutf16": "utf16+x",
  247. }
  248. if p4type in p4_filetypes_historical:
  249. p4type = p4_filetypes_historical[p4type]
  250. mods = ""
  251. s = p4type.split("+")
  252. base = s[0]
  253. mods = ""
  254. if len(s) > 1:
  255. mods = s[1]
  256. return (base, mods)
  257. #
  258. # return the raw p4 type of a file (text, text+ko, etc)
  259. #
  260. def p4_type(f):
  261. results = p4CmdList(["fstat", "-T", "headType", wildcard_encode(f)])
  262. return results[0]['headType']
  263. #
  264. # Given a type base and modifier, return a regexp matching
  265. # the keywords that can be expanded in the file
  266. #
  267. def p4_keywords_regexp_for_type(base, type_mods):
  268. if base in ("text", "unicode", "binary"):
  269. kwords = None
  270. if "ko" in type_mods:
  271. kwords = 'Id|Header'
  272. elif "k" in type_mods:
  273. kwords = 'Id|Header|Author|Date|DateTime|Change|File|Revision'
  274. else:
  275. return None
  276. pattern = r"""
  277. \$ # Starts with a dollar, followed by...
  278. (%s) # one of the keywords, followed by...
  279. (:[^$\n]+)? # possibly an old expansion, followed by...
  280. \$ # another dollar
  281. """ % kwords
  282. return pattern
  283. else:
  284. return None
  285. #
  286. # Given a file, return a regexp matching the possible
  287. # RCS keywords that will be expanded, or None for files
  288. # with kw expansion turned off.
  289. #
  290. def p4_keywords_regexp_for_file(file):
  291. if not os.path.exists(file):
  292. return None
  293. else:
  294. (type_base, type_mods) = split_p4_type(p4_type(file))
  295. return p4_keywords_regexp_for_type(type_base, type_mods)
  296. def setP4ExecBit(file, mode):
  297. # Reopens an already open file and changes the execute bit to match
  298. # the execute bit setting in the passed in mode.
  299. p4Type = "+x"
  300. if not isModeExec(mode):
  301. p4Type = getP4OpenedType(file)
  302. p4Type = re.sub('^([cku]?)x(.*)', '\\1\\2', p4Type)
  303. p4Type = re.sub('(.*?\+.*?)x(.*?)', '\\1\\2', p4Type)
  304. if p4Type[-1] == "+":
  305. p4Type = p4Type[0:-1]
  306. p4_reopen(p4Type, file)
  307. def getP4OpenedType(file):
  308. # Returns the perforce file type for the given file.
  309. result = p4_read_pipe(["opened", wildcard_encode(file)])
  310. match = re.match(".*\((.+)\)\r?$", result)
  311. if match:
  312. return match.group(1)
  313. else:
  314. die("Could not determine file type for %s (result: '%s')" % (file, result))
  315. # Return the set of all p4 labels
  316. def getP4Labels(depotPaths):
  317. labels = set()
  318. if isinstance(depotPaths,basestring):
  319. depotPaths = [depotPaths]
  320. for l in p4CmdList(["labels"] + ["%s..." % p for p in depotPaths]):
  321. label = l['label']
  322. labels.add(label)
  323. return labels
  324. # Return the set of all git tags
  325. def getGitTags():
  326. gitTags = set()
  327. for line in read_pipe_lines(["git", "tag"]):
  328. tag = line.strip()
  329. gitTags.add(tag)
  330. return gitTags
  331. def diffTreePattern():
  332. # This is a simple generator for the diff tree regex pattern. This could be
  333. # a class variable if this and parseDiffTreeEntry were a part of a class.
  334. pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)')
  335. while True:
  336. yield pattern
  337. def parseDiffTreeEntry(entry):
  338. """Parses a single diff tree entry into its component elements.
  339. See git-diff-tree(1) manpage for details about the format of the diff
  340. output. This method returns a dictionary with the following elements:
  341. src_mode - The mode of the source file
  342. dst_mode - The mode of the destination file
  343. src_sha1 - The sha1 for the source file
  344. dst_sha1 - The sha1 fr the destination file
  345. status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc)
  346. status_score - The score for the status (applicable for 'C' and 'R'
  347. statuses). This is None if there is no score.
  348. src - The path for the source file.
  349. dst - The path for the destination file. This is only present for
  350. copy or renames. If it is not present, this is None.
  351. If the pattern is not matched, None is returned."""
  352. match = diffTreePattern().next().match(entry)
  353. if match:
  354. return {
  355. 'src_mode': match.group(1),
  356. 'dst_mode': match.group(2),
  357. 'src_sha1': match.group(3),
  358. 'dst_sha1': match.group(4),
  359. 'status': match.group(5),
  360. 'status_score': match.group(6),
  361. 'src': match.group(7),
  362. 'dst': match.group(10)
  363. }
  364. return None
  365. def isModeExec(mode):
  366. # Returns True if the given git mode represents an executable file,
  367. # otherwise False.
  368. return mode[-3:] == "755"
  369. def isModeExecChanged(src_mode, dst_mode):
  370. return isModeExec(src_mode) != isModeExec(dst_mode)
  371. def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None):
  372. if isinstance(cmd,basestring):
  373. cmd = "-G " + cmd
  374. expand = True
  375. else:
  376. cmd = ["-G"] + cmd
  377. expand = False
  378. cmd = p4_build_cmd(cmd)
  379. if verbose:
  380. sys.stderr.write("Opening pipe: %s\n" % str(cmd))
  381. # Use a temporary file to avoid deadlocks without
  382. # subprocess.communicate(), which would put another copy
  383. # of stdout into memory.
  384. stdin_file = None
  385. if stdin is not None:
  386. stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode)
  387. if isinstance(stdin,basestring):
  388. stdin_file.write(stdin)
  389. else:
  390. for i in stdin:
  391. stdin_file.write(i + '\n')
  392. stdin_file.flush()
  393. stdin_file.seek(0)
  394. p4 = subprocess.Popen(cmd,
  395. shell=expand,
  396. stdin=stdin_file,
  397. stdout=subprocess.PIPE)
  398. result = []
  399. try:
  400. while True:
  401. entry = marshal.load(p4.stdout)
  402. if cb is not None:
  403. cb(entry)
  404. else:
  405. result.append(entry)
  406. except EOFError:
  407. pass
  408. exitCode = p4.wait()
  409. if exitCode != 0:
  410. entry = {}
  411. entry["p4ExitCode"] = exitCode
  412. result.append(entry)
  413. return result
  414. def p4Cmd(cmd):
  415. list = p4CmdList(cmd)
  416. result = {}
  417. for entry in list:
  418. result.update(entry)
  419. return result;
  420. def p4Where(depotPath):
  421. if not depotPath.endswith("/"):
  422. depotPath += "/"
  423. depotPath = depotPath + "..."
  424. outputList = p4CmdList(["where", depotPath])
  425. output = None
  426. for entry in outputList:
  427. if "depotFile" in entry:
  428. if entry["depotFile"] == depotPath:
  429. output = entry
  430. break
  431. elif "data" in entry:
  432. data = entry.get("data")
  433. space = data.find(" ")
  434. if data[:space] == depotPath:
  435. output = entry
  436. break
  437. if output == None:
  438. return ""
  439. if output["code"] == "error":
  440. return ""
  441. clientPath = ""
  442. if "path" in output:
  443. clientPath = output.get("path")
  444. elif "data" in output:
  445. data = output.get("data")
  446. lastSpace = data.rfind(" ")
  447. clientPath = data[lastSpace + 1:]
  448. if clientPath.endswith("..."):
  449. clientPath = clientPath[:-3]
  450. return clientPath
  451. def currentGitBranch():
  452. return read_pipe("git name-rev HEAD").split(" ")[1].strip()
  453. def isValidGitDir(path):
  454. if (os.path.exists(path + "/HEAD")
  455. and os.path.exists(path + "/refs") and os.path.exists(path + "/objects")):
  456. return True;
  457. return False
  458. def parseRevision(ref):
  459. return read_pipe("git rev-parse %s" % ref).strip()
  460. def branchExists(ref):
  461. rev = read_pipe(["git", "rev-parse", "-q", "--verify", ref],
  462. ignore_error=True)
  463. return len(rev) > 0
  464. def extractLogMessageFromGitCommit(commit):
  465. logMessage = ""
  466. ## fixme: title is first line of commit, not 1st paragraph.
  467. foundTitle = False
  468. for log in read_pipe_lines("git cat-file commit %s" % commit):
  469. if not foundTitle:
  470. if len(log) == 1:
  471. foundTitle = True
  472. continue
  473. logMessage += log
  474. return logMessage
  475. def extractSettingsGitLog(log):
  476. values = {}
  477. for line in log.split("\n"):
  478. line = line.strip()
  479. m = re.search (r"^ *\[git-p4: (.*)\]$", line)
  480. if not m:
  481. continue
  482. assignments = m.group(1).split (':')
  483. for a in assignments:
  484. vals = a.split ('=')
  485. key = vals[0].strip()
  486. val = ('='.join (vals[1:])).strip()
  487. if val.endswith ('\"') and val.startswith('"'):
  488. val = val[1:-1]
  489. values[key] = val
  490. paths = values.get("depot-paths")
  491. if not paths:
  492. paths = values.get("depot-path")
  493. if paths:
  494. values['depot-paths'] = paths.split(',')
  495. return values
  496. def gitBranchExists(branch):
  497. proc = subprocess.Popen(["git", "rev-parse", branch],
  498. stderr=subprocess.PIPE, stdout=subprocess.PIPE);
  499. return proc.wait() == 0;
  500. _gitConfig = {}
  501. def gitConfig(key):
  502. if not _gitConfig.has_key(key):
  503. cmd = [ "git", "config", key ]
  504. s = read_pipe(cmd, ignore_error=True)
  505. _gitConfig[key] = s.strip()
  506. return _gitConfig[key]
  507. def gitConfigBool(key):
  508. """Return a bool, using git config --bool. It is True only if the
  509. variable is set to true, and False if set to false or not present
  510. in the config."""
  511. if not _gitConfig.has_key(key):
  512. cmd = [ "git", "config", "--bool", key ]
  513. s = read_pipe(cmd, ignore_error=True)
  514. v = s.strip()
  515. _gitConfig[key] = v == "true"
  516. return _gitConfig[key]
  517. def gitConfigList(key):
  518. if not _gitConfig.has_key(key):
  519. s = read_pipe(["git", "config", "--get-all", key], ignore_error=True)
  520. _gitConfig[key] = s.strip().split(os.linesep)
  521. return _gitConfig[key]
  522. def p4BranchesInGit(branchesAreInRemotes=True):
  523. """Find all the branches whose names start with "p4/", looking
  524. in remotes or heads as specified by the argument. Return
  525. a dictionary of { branch: revision } for each one found.
  526. The branch names are the short names, without any
  527. "p4/" prefix."""
  528. branches = {}
  529. cmdline = "git rev-parse --symbolic "
  530. if branchesAreInRemotes:
  531. cmdline += "--remotes"
  532. else:
  533. cmdline += "--branches"
  534. for line in read_pipe_lines(cmdline):
  535. line = line.strip()
  536. # only import to p4/
  537. if not line.startswith('p4/'):
  538. continue
  539. # special symbolic ref to p4/master
  540. if line == "p4/HEAD":
  541. continue
  542. # strip off p4/ prefix
  543. branch = line[len("p4/"):]
  544. branches[branch] = parseRevision(line)
  545. return branches
  546. def branch_exists(branch):
  547. """Make sure that the given ref name really exists."""
  548. cmd = [ "git", "rev-parse", "--symbolic", "--verify", branch ]
  549. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  550. out, _ = p.communicate()
  551. if p.returncode:
  552. return False
  553. # expect exactly one line of output: the branch name
  554. return out.rstrip() == branch
  555. def findUpstreamBranchPoint(head = "HEAD"):
  556. branches = p4BranchesInGit()
  557. # map from depot-path to branch name
  558. branchByDepotPath = {}
  559. for branch in branches.keys():
  560. tip = branches[branch]
  561. log = extractLogMessageFromGitCommit(tip)
  562. settings = extractSettingsGitLog(log)
  563. if settings.has_key("depot-paths"):
  564. paths = ",".join(settings["depot-paths"])
  565. branchByDepotPath[paths] = "remotes/p4/" + branch
  566. settings = None
  567. parent = 0
  568. while parent < 65535:
  569. commit = head + "~%s" % parent
  570. log = extractLogMessageFromGitCommit(commit)
  571. settings = extractSettingsGitLog(log)
  572. if settings.has_key("depot-paths"):
  573. paths = ",".join(settings["depot-paths"])
  574. if branchByDepotPath.has_key(paths):
  575. return [branchByDepotPath[paths], settings]
  576. parent = parent + 1
  577. return ["", settings]
  578. def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True):
  579. if not silent:
  580. print ("Creating/updating branch(es) in %s based on origin branch(es)"
  581. % localRefPrefix)
  582. originPrefix = "origin/p4/"
  583. for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
  584. line = line.strip()
  585. if (not line.startswith(originPrefix)) or line.endswith("HEAD"):
  586. continue
  587. headName = line[len(originPrefix):]
  588. remoteHead = localRefPrefix + headName
  589. originHead = line
  590. original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))
  591. if (not original.has_key('depot-paths')
  592. or not original.has_key('change')):
  593. continue
  594. update = False
  595. if not gitBranchExists(remoteHead):
  596. if verbose:
  597. print "creating %s" % remoteHead
  598. update = True
  599. else:
  600. settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))
  601. if settings.has_key('change') > 0:
  602. if settings['depot-paths'] == original['depot-paths']:
  603. originP4Change = int(original['change'])
  604. p4Change = int(settings['change'])
  605. if originP4Change > p4Change:
  606. print ("%s (%s) is newer than %s (%s). "
  607. "Updating p4 branch from origin."
  608. % (originHead, originP4Change,
  609. remoteHead, p4Change))
  610. update = True
  611. else:
  612. print ("Ignoring: %s was imported from %s while "
  613. "%s was imported from %s"
  614. % (originHead, ','.join(original['depot-paths']),
  615. remoteHead, ','.join(settings['depot-paths'])))
  616. if update:
  617. system("git update-ref %s %s" % (remoteHead, originHead))
  618. def originP4BranchesExist():
  619. return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
  620. def p4ChangesForPaths(depotPaths, changeRange):
  621. assert depotPaths
  622. cmd = ['changes']
  623. for p in depotPaths:
  624. cmd += ["%s...%s" % (p, changeRange)]
  625. output = p4_read_pipe_lines(cmd)
  626. changes = {}
  627. for line in output:
  628. changeNum = int(line.split(" ")[1])
  629. changes[changeNum] = True
  630. changelist = changes.keys()
  631. changelist.sort()
  632. return changelist
  633. def p4PathStartsWith(path, prefix):
  634. # This method tries to remedy a potential mixed-case issue:
  635. #
  636. # If UserA adds //depot/DirA/file1
  637. # and UserB adds //depot/dira/file2
  638. #
  639. # we may or may not have a problem. If you have core.ignorecase=true,
  640. # we treat DirA and dira as the same directory
  641. if gitConfigBool("core.ignorecase"):
  642. return path.lower().startswith(prefix.lower())
  643. return path.startswith(prefix)
  644. def getClientSpec():
  645. """Look at the p4 client spec, create a View() object that contains
  646. all the mappings, and return it."""
  647. specList = p4CmdList("client -o")
  648. if len(specList) != 1:
  649. die('Output from "client -o" is %d lines, expecting 1' %
  650. len(specList))
  651. # dictionary of all client parameters
  652. entry = specList[0]
  653. # the //client/ name
  654. client_name = entry["Client"]
  655. # just the keys that start with "View"
  656. view_keys = [ k for k in entry.keys() if k.startswith("View") ]
  657. # hold this new View
  658. view = View(client_name)
  659. # append the lines, in order, to the view
  660. for view_num in range(len(view_keys)):
  661. k = "View%d" % view_num
  662. if k not in view_keys:
  663. die("Expected view key %s missing" % k)
  664. view.append(entry[k])
  665. return view
  666. def getClientRoot():
  667. """Grab the client directory."""
  668. output = p4CmdList("client -o")
  669. if len(output) != 1:
  670. die('Output from "client -o" is %d lines, expecting 1' % len(output))
  671. entry = output[0]
  672. if "Root" not in entry:
  673. die('Client has no "Root"')
  674. return entry["Root"]
  675. #
  676. # P4 wildcards are not allowed in filenames. P4 complains
  677. # if you simply add them, but you can force it with "-f", in
  678. # which case it translates them into %xx encoding internally.
  679. #
  680. def wildcard_decode(path):
  681. # Search for and fix just these four characters. Do % last so
  682. # that fixing it does not inadvertently create new %-escapes.
  683. # Cannot have * in a filename in windows; untested as to
  684. # what p4 would do in such a case.
  685. if not platform.system() == "Windows":
  686. path = path.replace("%2A", "*")
  687. path = path.replace("%23", "#") \
  688. .replace("%40", "@") \
  689. .replace("%25", "%")
  690. return path
  691. def wildcard_encode(path):
  692. # do % first to avoid double-encoding the %s introduced here
  693. path = path.replace("%", "%25") \
  694. .replace("*", "%2A") \
  695. .replace("#", "%23") \
  696. .replace("@", "%40")
  697. return path
  698. def wildcard_present(path):
  699. m = re.search("[*#@%]", path)
  700. return m is not None
  701. class Command:
  702. def __init__(self):
  703. self.usage = "usage: %prog [options]"
  704. self.needsGit = True
  705. self.verbose = False
  706. class P4UserMap:
  707. def __init__(self):
  708. self.userMapFromPerforceServer = False
  709. self.myP4UserId = None
  710. def p4UserId(self):
  711. if self.myP4UserId:
  712. return self.myP4UserId
  713. results = p4CmdList("user -o")
  714. for r in results:
  715. if r.has_key('User'):
  716. self.myP4UserId = r['User']
  717. return r['User']
  718. die("Could not find your p4 user id")
  719. def p4UserIsMe(self, p4User):
  720. # return True if the given p4 user is actually me
  721. me = self.p4UserId()
  722. if not p4User or p4User != me:
  723. return False
  724. else:
  725. return True
  726. def getUserCacheFilename(self):
  727. home = os.environ.get("HOME", os.environ.get("USERPROFILE"))
  728. return home + "/.gitp4-usercache.txt"
  729. def getUserMapFromPerforceServer(self):
  730. if self.userMapFromPerforceServer:
  731. return
  732. self.users = {}
  733. self.emails = {}
  734. for output in p4CmdList("users"):
  735. if not output.has_key("User"):
  736. continue
  737. self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
  738. self.emails[output["Email"]] = output["User"]
  739. s = ''
  740. for (key, val) in self.users.items():
  741. s += "%s\t%s\n" % (key.expandtabs(1), val.expandtabs(1))
  742. open(self.getUserCacheFilename(), "wb").write(s)
  743. self.userMapFromPerforceServer = True
  744. def loadUserMapFromCache(self):
  745. self.users = {}
  746. self.userMapFromPerforceServer = False
  747. try:
  748. cache = open(self.getUserCacheFilename(), "rb")
  749. lines = cache.readlines()
  750. cache.close()
  751. for line in lines:
  752. entry = line.strip().split("\t")
  753. self.users[entry[0]] = entry[1]
  754. except IOError:
  755. self.getUserMapFromPerforceServer()
  756. class P4Debug(Command):
  757. def __init__(self):
  758. Command.__init__(self)
  759. self.options = []
  760. self.description = "A tool to debug the output of p4 -G."
  761. self.needsGit = False
  762. def run(self, args):
  763. j = 0
  764. for output in p4CmdList(args):
  765. print 'Element: %d' % j
  766. j += 1
  767. print output
  768. return True
  769. class P4RollBack(Command):
  770. def __init__(self):
  771. Command.__init__(self)
  772. self.options = [
  773. optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true")
  774. ]
  775. self.description = "A tool to debug the multi-branch import. Don't use :)"
  776. self.rollbackLocalBranches = False
  777. def run(self, args):
  778. if len(args) != 1:
  779. return False
  780. maxChange = int(args[0])
  781. if "p4ExitCode" in p4Cmd("changes -m 1"):
  782. die("Problems executing p4");
  783. if self.rollbackLocalBranches:
  784. refPrefix = "refs/heads/"
  785. lines = read_pipe_lines("git rev-parse --symbolic --branches")
  786. else:
  787. refPrefix = "refs/remotes/"
  788. lines = read_pipe_lines("git rev-parse --symbolic --remotes")
  789. for line in lines:
  790. if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"):
  791. line = line.strip()
  792. ref = refPrefix + line
  793. log = extractLogMessageFromGitCommit(ref)
  794. settings = extractSettingsGitLog(log)
  795. depotPaths = settings['depot-paths']
  796. change = settings['change']
  797. changed = False
  798. if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange)
  799. for p in depotPaths]))) == 0:
  800. print "Branch %s did not exist at change %s, deleting." % (ref, maxChange)
  801. system("git update-ref -d %s `git rev-parse %s`" % (ref, ref))
  802. continue
  803. while change and int(change) > maxChange:
  804. changed = True
  805. if self.verbose:
  806. print "%s is at %s ; rewinding towards %s" % (ref, change, maxChange)
  807. system("git update-ref %s \"%s^\"" % (ref, ref))
  808. log = extractLogMessageFromGitCommit(ref)
  809. settings = extractSettingsGitLog(log)
  810. depotPaths = settings['depot-paths']
  811. change = settings['change']
  812. if changed:
  813. print "%s rewound to %s" % (ref, change)
  814. return True
  815. class P4Submit(Command, P4UserMap):
  816. conflict_behavior_choices = ("ask", "skip", "quit")
  817. def __init__(self):
  818. Command.__init__(self)
  819. P4UserMap.__init__(self)
  820. self.options = [
  821. optparse.make_option("--origin", dest="origin"),
  822. optparse.make_option("-M", dest="detectRenames", action="store_true"),
  823. # preserve the user, requires relevant p4 permissions
  824. optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
  825. optparse.make_option("--export-labels", dest="exportLabels", action="store_true"),
  826. optparse.make_option("--dry-run", "-n", dest="dry_run", action="store_true"),
  827. optparse.make_option("--prepare-p4-only", dest="prepare_p4_only", action="store_true"),
  828. optparse.make_option("--conflict", dest="conflict_behavior",
  829. choices=self.conflict_behavior_choices),
  830. optparse.make_option("--branch", dest="branch"),
  831. ]
  832. self.description = "Submit changes from git to the perforce depot."
  833. self.usage += " [name of git branch to submit into perforce depot]"
  834. self.origin = ""
  835. self.detectRenames = False
  836. self.preserveUser = gitConfigBool("git-p4.preserveUser")
  837. self.dry_run = False
  838. self.prepare_p4_only = False
  839. self.conflict_behavior = None
  840. self.isWindows = (platform.system() == "Windows")
  841. self.exportLabels = False
  842. self.p4HasMoveCommand = p4_has_move_command()
  843. self.branch = None
  844. def check(self):
  845. if len(p4CmdList("opened ...")) > 0:
  846. die("You have files opened with perforce! Close them before starting the sync.")
  847. def separate_jobs_from_description(self, message):
  848. """Extract and return a possible Jobs field in the commit
  849. message. It goes into a separate section in the p4 change
  850. specification.
  851. A jobs line starts with "Jobs:" and looks like a new field
  852. in a form. Values are white-space separated on the same
  853. line or on following lines that start with a tab.
  854. This does not parse and extract the full git commit message
  855. like a p4 form. It just sees the Jobs: line as a marker
  856. to pass everything from then on directly into the p4 form,
  857. but outside the description section.
  858. Return a tuple (stripped log message, jobs string)."""
  859. m = re.search(r'^Jobs:', message, re.MULTILINE)
  860. if m is None:
  861. return (message, None)
  862. jobtext = message[m.start():]
  863. stripped_message = message[:m.start()].rstrip()
  864. return (stripped_message, jobtext)
  865. def prepareLogMessage(self, template, message, jobs):
  866. """Edits the template returned from "p4 change -o" to insert
  867. the message in the Description field, and the jobs text in
  868. the Jobs field."""
  869. result = ""
  870. inDescriptionSection = False
  871. for line in template.split("\n"):
  872. if line.startswith("#"):
  873. result += line + "\n"
  874. continue
  875. if inDescriptionSection:
  876. if line.startswith("Files:") or line.startswith("Jobs:"):
  877. inDescriptionSection = False
  878. # insert Jobs section
  879. if jobs:
  880. result += jobs + "\n"
  881. else:
  882. continue
  883. else:
  884. if line.startswith("Description:"):
  885. inDescriptionSection = True
  886. line += "\n"
  887. for messageLine in message.split("\n"):
  888. line += "\t" + messageLine + "\n"
  889. result += line + "\n"
  890. return result
  891. def patchRCSKeywords(self, file, pattern):
  892. # Attempt to zap the RCS keywords in a p4 controlled file matching the given pattern
  893. (handle, outFileName) = tempfile.mkstemp(dir='.')
  894. try:
  895. outFile = os.fdopen(handle, "w+")
  896. inFile = open(file, "r")
  897. regexp = re.compile(pattern, re.VERBOSE)
  898. for line in inFile.readlines():
  899. line = regexp.sub(r'$\1$', line)
  900. outFile.write(line)
  901. inFile.close()
  902. outFile.close()
  903. # Forcibly overwrite the original file
  904. os.unlink(file)
  905. shutil.move(outFileName, file)
  906. except:
  907. # cleanup our temporary file
  908. os.unlink(outFileName)
  909. print "Failed to strip RCS keywords in %s" % file
  910. raise
  911. print "Patched up RCS keywords in %s" % file
  912. def p4UserForCommit(self,id):
  913. # Return the tuple (perforce user,git email) for a given git commit id
  914. self.getUserMapFromPerforceServer()
  915. gitEmail = read_pipe(["git", "log", "--max-count=1",
  916. "--format=%ae", id])
  917. gitEmail = gitEmail.strip()
  918. if not self.emails.has_key(gitEmail):
  919. return (None,gitEmail)
  920. else:
  921. return (self.emails[gitEmail],gitEmail)
  922. def checkValidP4Users(self,commits):
  923. # check if any git authors cannot be mapped to p4 users
  924. for id in commits:
  925. (user,email) = self.p4UserForCommit(id)
  926. if not user:
  927. msg = "Cannot find p4 user for email %s in commit %s." % (email, id)
  928. if gitConfigBool("git-p4.allowMissingP4Users"):
  929. print "%s" % msg
  930. else:
  931. die("Error: %s\nSet git-p4.allowMissingP4Users to true to allow this." % msg)
  932. def lastP4Changelist(self):
  933. # Get back the last changelist number submitted in this client spec. This
  934. # then gets used to patch up the username in the change. If the same
  935. # client spec is being used by multiple processes then this might go
  936. # wrong.
  937. results = p4CmdList("client -o") # find the current client
  938. client = None
  939. for r in results:
  940. if r.has_key('Client'):
  941. client = r['Client']
  942. break
  943. if not client:
  944. die("could not get client spec")
  945. results = p4CmdList(["changes", "-c", client, "-m", "1"])
  946. for r in results:
  947. if r.has_key('change'):
  948. return r['change']
  949. die("Could not get changelist number for last submit - cannot patch up user details")
  950. def modifyChangelistUser(self, changelist, newUser):
  951. # fixup the user field of a changelist after it has been submitted.
  952. changes = p4CmdList("change -o %s" % changelist)
  953. if len(changes) != 1:
  954. die("Bad output from p4 change modifying %s to user %s" %
  955. (changelist, newUser))
  956. c = changes[0]
  957. if c['User'] == newUser: return # nothing to do
  958. c['User'] = newUser
  959. input = marshal.dumps(c)
  960. result = p4CmdList("change -f -i", stdin=input)
  961. for r in result:
  962. if r.has_key('code'):
  963. if r['code'] == 'error':
  964. die("Could not modify user field of changelist %s to %s:%s" % (changelist, newUser, r['data']))
  965. if r.has_key('data'):
  966. print("Updated user field for changelist %s to %s" % (changelist, newUser))
  967. return
  968. die("Could not modify user field of changelist %s to %s" % (changelist, newUser))
  969. def canChangeChangelists(self):
  970. # check to see if we have p4 admin or super-user permissions, either of
  971. # which are required to modify changelists.
  972. results = p4CmdList(["protects", self.depotPath])
  973. for r in results:
  974. if r.has_key('perm'):
  975. if r['perm'] == 'admin':
  976. return 1
  977. if r['perm'] == 'super':
  978. return 1
  979. return 0
  980. def prepareSubmitTemplate(self):
  981. """Run "p4 change -o" to grab a change specification template.
  982. This does not use "p4 -G", as it is nice to keep the submission
  983. template in original order, since a human might edit it.
  984. Remove lines in the Files section that show changes to files
  985. outside the depot path we're committing into."""
  986. template = ""
  987. inFilesSection = False
  988. for line in p4_read_pipe_lines(['change', '-o']):
  989. if line.endswith("\r\n"):
  990. line = line[:-2] + "\n"
  991. if inFilesSection:
  992. if line.startswith("\t"):
  993. # path starts and ends with a tab
  994. path = line[1:]
  995. lastTab = path.rfind("\t")
  996. if lastTab != -1:
  997. path = path[:lastTab]
  998. if not p4PathStartsWith(path, self.depotPath):
  999. continue
  1000. else:
  1001. inFilesSection = False
  1002. else:
  1003. if line.startswith("Files:"):
  1004. inFilesSection = True
  1005. template += line
  1006. return template
  1007. def edit_template(self, template_file):
  1008. """Invoke the editor to let the user change the submission
  1009. message. Return true if okay to continue with the submit."""
  1010. # if configured to skip the editing part, just submit
  1011. if gitConfigBool("git-p4.skipSubmitEdit"):
  1012. return True
  1013. # look at the modification time, to check later if the user saved
  1014. # the file
  1015. mtime = os.stat(template_file).st_mtime
  1016. # invoke the editor
  1017. if os.environ.has_key("P4EDITOR") and (os.environ.get("P4EDITOR") != ""):
  1018. editor = os.environ.get("P4EDITOR")
  1019. else:
  1020. editor = read_pipe("git var GIT_EDITOR").strip()
  1021. system([editor, template_file])
  1022. # If the file was not saved, prompt to see if this patch should
  1023. # be skipped. But skip this verification step if configured so.
  1024. if gitConfigBool("git-p4.skipSubmitEditCheck"):
  1025. return True
  1026. # modification time updated means user saved the file
  1027. if os.stat(template_file).st_mtime > mtime:
  1028. return True
  1029. while True:
  1030. response = raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
  1031. if response == 'y':
  1032. return True
  1033. if response == 'n':
  1034. return False
  1035. def get_diff_description(self, editedFiles, filesToAdd):
  1036. # diff
  1037. if os.environ.has_key("P4DIFF"):
  1038. del(os.environ["P4DIFF"])
  1039. diff = ""
  1040. for editedFile in editedFiles:
  1041. diff += p4_read_pipe(['diff', '-du',
  1042. wildcard_encode(editedFile)])
  1043. # new file diff
  1044. newdiff = ""
  1045. for newFile in filesToAdd:
  1046. newdiff += "==== new file ====\n"
  1047. newdiff += "--- /dev/null\n"
  1048. newdiff += "+++ %s\n" % newFile
  1049. f = open(newFile, "r")
  1050. for line in f.readlines():
  1051. newdiff += "+" + line
  1052. f.close()
  1053. return (diff + newdiff).replace('\r\n', '\n')
  1054. def applyCommit(self, id):
  1055. """Apply one commit, return True if it succeeded."""
  1056. print "Applying", read_pipe(["git", "show", "-s",
  1057. "--format=format:%h %s", id])
  1058. (p4User, gitEmail) = self.p4UserForCommit(id)
  1059. diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (self.diffOpts, id, id))
  1060. filesToAdd = set()
  1061. filesToDelete = set()
  1062. editedFiles = set()
  1063. pureRenameCopy = set()
  1064. filesToChangeExecBit = {}
  1065. for line in diff:
  1066. diff = parseDiffTreeEntry(line)
  1067. modifier = diff['status']
  1068. path = diff['src']
  1069. if modifier == "M":
  1070. p4_edit(path)
  1071. if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
  1072. filesToChangeExecBit[path] = diff['dst_mode']
  1073. editedFiles.add(path)
  1074. elif modifier == "A":
  1075. filesToAdd.add(path)
  1076. filesToChangeExecBit[path] = diff['dst_mode']
  1077. if path in filesToDelete:
  1078. filesToDelete.remove(path)
  1079. elif modifier == "D":
  1080. filesToDelete.add(path)
  1081. if path in filesToAdd:
  1082. filesToAdd.remove(path)
  1083. elif modifier == "C":
  1084. src, dest = diff['src'], diff['dst']
  1085. p4_integrate(src, dest)
  1086. pureRenameCopy.add(dest)
  1087. if diff['src_sha1'] != diff['dst_sha1']:
  1088. p4_edit(dest)
  1089. pureRenameCopy.discard(dest)
  1090. if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
  1091. p4_edit(dest)
  1092. pureRenameCopy.discard(dest)
  1093. filesToChangeExecBit[dest] = diff['dst_mode']
  1094. if self.isWindows:
  1095. # turn off read-only attribute
  1096. os.chmod(dest, stat.S_IWRITE)
  1097. os.unlink(dest)
  1098. editedFiles.add(dest)
  1099. elif modifier == "R":
  1100. src, dest = diff['src'], diff['dst']
  1101. if self.p4HasMoveCommand:
  1102. p4_edit(src) # src must be open before move
  1103. p4_move(src, dest) # opens for (move/delete, move/add)
  1104. else:
  1105. p4_integrate(src, dest)
  1106. if diff['src_sha1'] != diff['dst_sha1']:
  1107. p4_edit(dest)
  1108. else:
  1109. pureRenameCopy.add(dest)
  1110. if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
  1111. if not self.p4HasMoveCommand:
  1112. p4_edit(dest) # with move: already open, writable
  1113. filesToChangeExecBit[dest] = diff['dst_mode']
  1114. if not self.p4HasMoveCommand:
  1115. if self.isWindows:
  1116. os.chmod(dest, stat.S_IWRITE)
  1117. os.unlink(dest)
  1118. filesToDelete.add(src)
  1119. editedFiles.add(dest)
  1120. else:
  1121. die("unknown modifier %s for %s" % (modifier, path))
  1122. diffcmd = "git diff-tree --full-index -p \"%s\"" % (id)
  1123. patchcmd = diffcmd + " | git apply "
  1124. tryPatchCmd = patchcmd + "--check -"
  1125. applyPatchCmd = patchcmd + "--check --apply -"
  1126. patch_succeeded = True
  1127. if os.system(tryPatchCmd) != 0:
  1128. fixed_rcs_keywords = False
  1129. patch_succeeded = False
  1130. print "Unfortunately applying the change failed!"
  1131. # Patch failed, maybe it's just RCS keyword woes. Look through
  1132. # the patch to see if that's possible.
  1133. if gitConfigBool("git-p4.attemptRCSCleanup"):
  1134. file = None
  1135. pattern = None
  1136. kwfiles = {}
  1137. for file in editedFiles | filesToDelete:
  1138. # did this file's delta contain RCS keywords?
  1139. pattern = p4_keywords_regexp_for_file(file)
  1140. if pattern:
  1141. # this file is a possibility...look for RCS keywords.
  1142. regexp = re.compile(pattern, re.VERBOSE)
  1143. for line in read_pipe_lines(["git", "diff", "%s^..%s" % (id, id), file]):
  1144. if regexp.search(line):
  1145. if verbose:
  1146. print "got keyword match on %s in %s in %s" % (pattern, line, file)
  1147. kwfiles[file] = pattern
  1148. break
  1149. for file in kwfiles:
  1150. if verbose:
  1151. print "zapping %s with %s" % (line,pattern)
  1152. # File is being deleted, so not open in p4. Must
  1153. # disable the read-only bit on windows.
  1154. if self.isWindows and file not in editedFiles:
  1155. os.chmod(file, stat.S_IWRITE)
  1156. self.patchRCSKeywords(file, kwfiles[file])
  1157. fixed_rcs_keywords = True
  1158. if fixed_rcs_keywords:
  1159. print "Retrying the patch with RCS keywords cleaned up"
  1160. if os.system(tryPatchCmd) == 0:
  1161. patch_succeeded = True
  1162. if not patch_succeeded:
  1163. for f in editedFiles:
  1164. p4_revert(f)
  1165. return False
  1166. #
  1167. # Apply the patch for real, and do add/delete/+x handling.
  1168. #
  1169. system(applyPatchCmd)
  1170. for f in filesToAdd:
  1171. p4_add(f)
  1172. for f in filesToDelete:
  1173. p4_revert(f)
  1174. p4_delete(f)
  1175. # Set/clear executable bits
  1176. for f in filesToChangeExecBit.keys():
  1177. mode = filesToChangeExecBit[f]
  1178. setP4ExecBit(f, mode)
  1179. #
  1180. # Build p4 change description, starting with the contents
  1181. # of the git commit message.
  1182. #
  1183. logMessage = extractLogMessageFromGitCommit(id)
  1184. logMessage = logMessage.strip()
  1185. (logMessage, jobs) = self.separate_jobs_from_description(logMessage)
  1186. template = self.prepareSubmitTemplate()
  1187. submitTemplate = self.prepareLogMessage(template, logMessage, jobs)
  1188. if self.preserveUser:
  1189. submitTemplate += "\n######## Actual user %s, modified after commit\n" % p4User
  1190. if self.checkAuthorship and not self.p4UserIsMe(p4User):
  1191. submitTemplate += "######## git author %s does not match your p4 account.\n" % gitEmail
  1192. submitTemplate += "######## Use option --preserve-user to modify authorship.\n"
  1193. submitTemplate += "######## Variable git-p4.skipUserNameCheck hides this message.\n"
  1194. separatorLine = "######## everything below this line is just the diff #######\n"
  1195. if not self.prepare_p4_only:
  1196. submitTemplate += separatorLine
  1197. submitTemplate += self.get_diff_description(editedFiles, filesToAdd)
  1198. (handle, fileName) = tempfile.mkstemp()
  1199. tmpFile = os.fdopen(handle, "w+b")
  1200. if self.isWindows:
  1201. submitTemplate = submitTemplate.replace("\n", "\r\n")
  1202. tmpFile.write(submitTemplate)
  1203. tmpFile.close()
  1204. if self.prepare_p4_only:
  1205. #
  1206. # Leave the p4 tree prepared, and the submit template around
  1207. # and let the user decide what to do next
  1208. #
  1209. print
  1210. print "P4 workspace prepared for submission."
  1211. print "To submit or revert, go to client workspace"
  1212. print " " + self.clientPath
  1213. print
  1214. print "To submit, use \"p4 submit\" to write a new description,"
  1215. print "or \"p4 submit -i %s\" to use the one prepared by" \
  1216. " \"git p4\"." % fileName
  1217. print "You can delete the file \"%s\" when finished." % fileName

Large files files are truncated, but you can click here to view the full file