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# 10import sys 11if sys.hexversion <0x02040000: 12# The limiter is the subprocess module 13 sys.stderr.write("git-p4: requires Python 2.4 or later.\n") 14 sys.exit(1) 15import os 16import optparse 17import marshal 18import subprocess 19import tempfile 20import time 21import platform 22import re 23import shutil 24import stat 25 26try: 27from subprocess import CalledProcessError 28exceptImportError: 29# from python2.7:subprocess.py 30# Exception classes used by this module. 31classCalledProcessError(Exception): 32"""This exception is raised when a process run by check_call() returns 33 a non-zero exit status. The exit status will be stored in the 34 returncode attribute.""" 35def__init__(self, returncode, cmd): 36 self.returncode = returncode 37 self.cmd = cmd 38def__str__(self): 39return"Command '%s' returned non-zero exit status%d"% (self.cmd, self.returncode) 40 41verbose =False 42 43# Only labels/tags matching this will be imported/exported 44defaultLabelRegexp = r'[a-zA-Z0-9_\-.]+$' 45 46# Grab changes in blocks of this many revisions, unless otherwise requested 47defaultBlockSize =512 48 49defp4_build_cmd(cmd): 50"""Build a suitable p4 command line. 51 52 This consolidates building and returning a p4 command line into one 53 location. It means that hooking into the environment, or other configuration 54 can be done more easily. 55 """ 56 real_cmd = ["p4"] 57 58 user =gitConfig("git-p4.user") 59iflen(user) >0: 60 real_cmd += ["-u",user] 61 62 password =gitConfig("git-p4.password") 63iflen(password) >0: 64 real_cmd += ["-P", password] 65 66 port =gitConfig("git-p4.port") 67iflen(port) >0: 68 real_cmd += ["-p", port] 69 70 host =gitConfig("git-p4.host") 71iflen(host) >0: 72 real_cmd += ["-H", host] 73 74 client =gitConfig("git-p4.client") 75iflen(client) >0: 76 real_cmd += ["-c", client] 77 78 79ifisinstance(cmd,basestring): 80 real_cmd =' '.join(real_cmd) +' '+ cmd 81else: 82 real_cmd += cmd 83return real_cmd 84 85defchdir(path, is_client_path=False): 86"""Do chdir to the given path, and set the PWD environment 87 variable for use by P4. It does not look at getcwd() output. 88 Since we're not using the shell, it is necessary to set the 89 PWD environment variable explicitly. 90 91 Normally, expand the path to force it to be absolute. This 92 addresses the use of relative path names inside P4 settings, 93 e.g. P4CONFIG=.p4config. P4 does not simply open the filename 94 as given; it looks for .p4config using PWD. 95 96 If is_client_path, the path was handed to us directly by p4, 97 and may be a symbolic link. Do not call os.getcwd() in this 98 case, because it will cause p4 to think that PWD is not inside 99 the client path. 100 """ 101 102 os.chdir(path) 103if not is_client_path: 104 path = os.getcwd() 105 os.environ['PWD'] = path 106 107defdie(msg): 108if verbose: 109raiseException(msg) 110else: 111 sys.stderr.write(msg +"\n") 112 sys.exit(1) 113 114defwrite_pipe(c, stdin): 115if verbose: 116 sys.stderr.write('Writing pipe:%s\n'%str(c)) 117 118 expand =isinstance(c,basestring) 119 p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand) 120 pipe = p.stdin 121 val = pipe.write(stdin) 122 pipe.close() 123if p.wait(): 124die('Command failed:%s'%str(c)) 125 126return val 127 128defp4_write_pipe(c, stdin): 129 real_cmd =p4_build_cmd(c) 130returnwrite_pipe(real_cmd, stdin) 131 132defread_pipe(c, ignore_error=False): 133if verbose: 134 sys.stderr.write('Reading pipe:%s\n'%str(c)) 135 136 expand =isinstance(c,basestring) 137 p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=expand) 138(out, err) = p.communicate() 139if p.returncode !=0and not ignore_error: 140die('Command failed:%s\nError:%s'% (str(c), err)) 141return out 142 143defp4_read_pipe(c, ignore_error=False): 144 real_cmd =p4_build_cmd(c) 145returnread_pipe(real_cmd, ignore_error) 146 147defread_pipe_lines(c): 148if verbose: 149 sys.stderr.write('Reading pipe:%s\n'%str(c)) 150 151 expand =isinstance(c, basestring) 152 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand) 153 pipe = p.stdout 154 val = pipe.readlines() 155if pipe.close()or p.wait(): 156die('Command failed:%s'%str(c)) 157 158return val 159 160defp4_read_pipe_lines(c): 161"""Specifically invoke p4 on the command supplied. """ 162 real_cmd =p4_build_cmd(c) 163returnread_pipe_lines(real_cmd) 164 165defp4_has_command(cmd): 166"""Ask p4 for help on this command. If it returns an error, the 167 command does not exist in this version of p4.""" 168 real_cmd =p4_build_cmd(["help", cmd]) 169 p = subprocess.Popen(real_cmd, stdout=subprocess.PIPE, 170 stderr=subprocess.PIPE) 171 p.communicate() 172return p.returncode ==0 173 174defp4_has_move_command(): 175"""See if the move command exists, that it supports -k, and that 176 it has not been administratively disabled. The arguments 177 must be correct, but the filenames do not have to exist. Use 178 ones with wildcards so even if they exist, it will fail.""" 179 180if notp4_has_command("move"): 181return False 182 cmd =p4_build_cmd(["move","-k","@from","@to"]) 183 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 184(out, err) = p.communicate() 185# return code will be 1 in either case 186if err.find("Invalid option") >=0: 187return False 188if err.find("disabled") >=0: 189return False 190# assume it failed because @... was invalid changelist 191return True 192 193defsystem(cmd): 194 expand =isinstance(cmd,basestring) 195if verbose: 196 sys.stderr.write("executing%s\n"%str(cmd)) 197 retcode = subprocess.call(cmd, shell=expand) 198if retcode: 199raiseCalledProcessError(retcode, cmd) 200 201defp4_system(cmd): 202"""Specifically invoke p4 as the system command. """ 203 real_cmd =p4_build_cmd(cmd) 204 expand =isinstance(real_cmd, basestring) 205 retcode = subprocess.call(real_cmd, shell=expand) 206if retcode: 207raiseCalledProcessError(retcode, real_cmd) 208 209_p4_version_string =None 210defp4_version_string(): 211"""Read the version string, showing just the last line, which 212 hopefully is the interesting version bit. 213 214 $ p4 -V 215 Perforce - The Fast Software Configuration Management System. 216 Copyright 1995-2011 Perforce Software. All rights reserved. 217 Rev. P4/NTX86/2011.1/393975 (2011/12/16). 218 """ 219global _p4_version_string 220if not _p4_version_string: 221 a =p4_read_pipe_lines(["-V"]) 222 _p4_version_string = a[-1].rstrip() 223return _p4_version_string 224 225defp4_integrate(src, dest): 226p4_system(["integrate","-Dt",wildcard_encode(src),wildcard_encode(dest)]) 227 228defp4_sync(f, *options): 229p4_system(["sync"] +list(options) + [wildcard_encode(f)]) 230 231defp4_add(f): 232# forcibly add file names with wildcards 233ifwildcard_present(f): 234p4_system(["add","-f", f]) 235else: 236p4_system(["add", f]) 237 238defp4_delete(f): 239p4_system(["delete",wildcard_encode(f)]) 240 241defp4_edit(f): 242p4_system(["edit",wildcard_encode(f)]) 243 244defp4_revert(f): 245p4_system(["revert",wildcard_encode(f)]) 246 247defp4_reopen(type, f): 248p4_system(["reopen","-t",type,wildcard_encode(f)]) 249 250defp4_move(src, dest): 251p4_system(["move","-k",wildcard_encode(src),wildcard_encode(dest)]) 252 253defp4_last_change(): 254 results =p4CmdList(["changes","-m","1"]) 255returnint(results[0]['change']) 256 257defp4_describe(change): 258"""Make sure it returns a valid result by checking for 259 the presence of field "time". Return a dict of the 260 results.""" 261 262 ds =p4CmdList(["describe","-s",str(change)]) 263iflen(ds) !=1: 264die("p4 describe -s%ddid not return 1 result:%s"% (change,str(ds))) 265 266 d = ds[0] 267 268if"p4ExitCode"in d: 269die("p4 describe -s%dexited with%d:%s"% (change, d["p4ExitCode"], 270str(d))) 271if"code"in d: 272if d["code"] =="error": 273die("p4 describe -s%dreturned error code:%s"% (change,str(d))) 274 275if"time"not in d: 276die("p4 describe -s%dreturned no\"time\":%s"% (change,str(d))) 277 278return d 279 280# 281# Canonicalize the p4 type and return a tuple of the 282# base type, plus any modifiers. See "p4 help filetypes" 283# for a list and explanation. 284# 285defsplit_p4_type(p4type): 286 287 p4_filetypes_historical = { 288"ctempobj":"binary+Sw", 289"ctext":"text+C", 290"cxtext":"text+Cx", 291"ktext":"text+k", 292"kxtext":"text+kx", 293"ltext":"text+F", 294"tempobj":"binary+FSw", 295"ubinary":"binary+F", 296"uresource":"resource+F", 297"uxbinary":"binary+Fx", 298"xbinary":"binary+x", 299"xltext":"text+Fx", 300"xtempobj":"binary+Swx", 301"xtext":"text+x", 302"xunicode":"unicode+x", 303"xutf16":"utf16+x", 304} 305if p4type in p4_filetypes_historical: 306 p4type = p4_filetypes_historical[p4type] 307 mods ="" 308 s = p4type.split("+") 309 base = s[0] 310 mods ="" 311iflen(s) >1: 312 mods = s[1] 313return(base, mods) 314 315# 316# return the raw p4 type of a file (text, text+ko, etc) 317# 318defp4_type(f): 319 results =p4CmdList(["fstat","-T","headType",wildcard_encode(f)]) 320return results[0]['headType'] 321 322# 323# Given a type base and modifier, return a regexp matching 324# the keywords that can be expanded in the file 325# 326defp4_keywords_regexp_for_type(base, type_mods): 327if base in("text","unicode","binary"): 328 kwords =None 329if"ko"in type_mods: 330 kwords ='Id|Header' 331elif"k"in type_mods: 332 kwords ='Id|Header|Author|Date|DateTime|Change|File|Revision' 333else: 334return None 335 pattern = r""" 336 \$ # Starts with a dollar, followed by... 337 (%s) # one of the keywords, followed by... 338 (:[^$\n]+)? # possibly an old expansion, followed by... 339 \$ # another dollar 340 """% kwords 341return pattern 342else: 343return None 344 345# 346# Given a file, return a regexp matching the possible 347# RCS keywords that will be expanded, or None for files 348# with kw expansion turned off. 349# 350defp4_keywords_regexp_for_file(file): 351if not os.path.exists(file): 352return None 353else: 354(type_base, type_mods) =split_p4_type(p4_type(file)) 355returnp4_keywords_regexp_for_type(type_base, type_mods) 356 357defsetP4ExecBit(file, mode): 358# Reopens an already open file and changes the execute bit to match 359# the execute bit setting in the passed in mode. 360 361 p4Type ="+x" 362 363if notisModeExec(mode): 364 p4Type =getP4OpenedType(file) 365 p4Type = re.sub('^([cku]?)x(.*)','\\1\\2', p4Type) 366 p4Type = re.sub('(.*?\+.*?)x(.*?)','\\1\\2', p4Type) 367if p4Type[-1] =="+": 368 p4Type = p4Type[0:-1] 369 370p4_reopen(p4Type,file) 371 372defgetP4OpenedType(file): 373# Returns the perforce file type for the given file. 374 375 result =p4_read_pipe(["opened",wildcard_encode(file)]) 376 match = re.match(".*\((.+)\)( \*exclusive\*)?\r?$", result) 377if match: 378return match.group(1) 379else: 380die("Could not determine file type for%s(result: '%s')"% (file, result)) 381 382# Return the set of all p4 labels 383defgetP4Labels(depotPaths): 384 labels =set() 385ifisinstance(depotPaths,basestring): 386 depotPaths = [depotPaths] 387 388for l inp4CmdList(["labels"] + ["%s..."% p for p in depotPaths]): 389 label = l['label'] 390 labels.add(label) 391 392return labels 393 394# Return the set of all git tags 395defgetGitTags(): 396 gitTags =set() 397for line inread_pipe_lines(["git","tag"]): 398 tag = line.strip() 399 gitTags.add(tag) 400return gitTags 401 402defdiffTreePattern(): 403# This is a simple generator for the diff tree regex pattern. This could be 404# a class variable if this and parseDiffTreeEntry were a part of a class. 405 pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)') 406while True: 407yield pattern 408 409defparseDiffTreeEntry(entry): 410"""Parses a single diff tree entry into its component elements. 411 412 See git-diff-tree(1) manpage for details about the format of the diff 413 output. This method returns a dictionary with the following elements: 414 415 src_mode - The mode of the source file 416 dst_mode - The mode of the destination file 417 src_sha1 - The sha1 for the source file 418 dst_sha1 - The sha1 fr the destination file 419 status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc) 420 status_score - The score for the status (applicable for 'C' and 'R' 421 statuses). This is None if there is no score. 422 src - The path for the source file. 423 dst - The path for the destination file. This is only present for 424 copy or renames. If it is not present, this is None. 425 426 If the pattern is not matched, None is returned.""" 427 428 match =diffTreePattern().next().match(entry) 429if match: 430return{ 431'src_mode': match.group(1), 432'dst_mode': match.group(2), 433'src_sha1': match.group(3), 434'dst_sha1': match.group(4), 435'status': match.group(5), 436'status_score': match.group(6), 437'src': match.group(7), 438'dst': match.group(10) 439} 440return None 441 442defisModeExec(mode): 443# Returns True if the given git mode represents an executable file, 444# otherwise False. 445return mode[-3:] =="755" 446 447defisModeExecChanged(src_mode, dst_mode): 448returnisModeExec(src_mode) !=isModeExec(dst_mode) 449 450defp4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None): 451 452ifisinstance(cmd,basestring): 453 cmd ="-G "+ cmd 454 expand =True 455else: 456 cmd = ["-G"] + cmd 457 expand =False 458 459 cmd =p4_build_cmd(cmd) 460if verbose: 461 sys.stderr.write("Opening pipe:%s\n"%str(cmd)) 462 463# Use a temporary file to avoid deadlocks without 464# subprocess.communicate(), which would put another copy 465# of stdout into memory. 466 stdin_file =None 467if stdin is not None: 468 stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode) 469ifisinstance(stdin,basestring): 470 stdin_file.write(stdin) 471else: 472for i in stdin: 473 stdin_file.write(i +'\n') 474 stdin_file.flush() 475 stdin_file.seek(0) 476 477 p4 = subprocess.Popen(cmd, 478 shell=expand, 479 stdin=stdin_file, 480 stdout=subprocess.PIPE) 481 482 result = [] 483try: 484while True: 485 entry = marshal.load(p4.stdout) 486if cb is not None: 487cb(entry) 488else: 489 result.append(entry) 490exceptEOFError: 491pass 492 exitCode = p4.wait() 493if exitCode !=0: 494 entry = {} 495 entry["p4ExitCode"] = exitCode 496 result.append(entry) 497 498return result 499 500defp4Cmd(cmd): 501list=p4CmdList(cmd) 502 result = {} 503for entry inlist: 504 result.update(entry) 505return result; 506 507defp4Where(depotPath): 508if not depotPath.endswith("/"): 509 depotPath +="/" 510 depotPathLong = depotPath +"..." 511 outputList =p4CmdList(["where", depotPathLong]) 512 output =None 513for entry in outputList: 514if"depotFile"in entry: 515# Search for the base client side depot path, as long as it starts with the branch's P4 path. 516# The base path always ends with "/...". 517if entry["depotFile"].find(depotPath) ==0and entry["depotFile"][-4:] =="/...": 518 output = entry 519break 520elif"data"in entry: 521 data = entry.get("data") 522 space = data.find(" ") 523if data[:space] == depotPath: 524 output = entry 525break 526if output ==None: 527return"" 528if output["code"] =="error": 529return"" 530 clientPath ="" 531if"path"in output: 532 clientPath = output.get("path") 533elif"data"in output: 534 data = output.get("data") 535 lastSpace = data.rfind(" ") 536 clientPath = data[lastSpace +1:] 537 538if clientPath.endswith("..."): 539 clientPath = clientPath[:-3] 540return clientPath 541 542defcurrentGitBranch(): 543returnread_pipe("git name-rev HEAD").split(" ")[1].strip() 544 545defisValidGitDir(path): 546if(os.path.exists(path +"/HEAD") 547and os.path.exists(path +"/refs")and os.path.exists(path +"/objects")): 548return True; 549return False 550 551defparseRevision(ref): 552returnread_pipe("git rev-parse%s"% ref).strip() 553 554defbranchExists(ref): 555 rev =read_pipe(["git","rev-parse","-q","--verify", ref], 556 ignore_error=True) 557returnlen(rev) >0 558 559defextractLogMessageFromGitCommit(commit): 560 logMessage ="" 561 562## fixme: title is first line of commit, not 1st paragraph. 563 foundTitle =False 564for log inread_pipe_lines("git cat-file commit%s"% commit): 565if not foundTitle: 566iflen(log) ==1: 567 foundTitle =True 568continue 569 570 logMessage += log 571return logMessage 572 573defextractSettingsGitLog(log): 574 values = {} 575for line in log.split("\n"): 576 line = line.strip() 577 m = re.search(r"^ *\[git-p4: (.*)\]$", line) 578if not m: 579continue 580 581 assignments = m.group(1).split(':') 582for a in assignments: 583 vals = a.split('=') 584 key = vals[0].strip() 585 val = ('='.join(vals[1:])).strip() 586if val.endswith('\"')and val.startswith('"'): 587 val = val[1:-1] 588 589 values[key] = val 590 591 paths = values.get("depot-paths") 592if not paths: 593 paths = values.get("depot-path") 594if paths: 595 values['depot-paths'] = paths.split(',') 596return values 597 598defgitBranchExists(branch): 599 proc = subprocess.Popen(["git","rev-parse", branch], 600 stderr=subprocess.PIPE, stdout=subprocess.PIPE); 601return proc.wait() ==0; 602 603_gitConfig = {} 604 605defgitConfig(key): 606if not _gitConfig.has_key(key): 607 cmd = ["git","config", key ] 608 s =read_pipe(cmd, ignore_error=True) 609 _gitConfig[key] = s.strip() 610return _gitConfig[key] 611 612defgitConfigBool(key): 613"""Return a bool, using git config --bool. It is True only if the 614 variable is set to true, and False if set to false or not present 615 in the config.""" 616 617if not _gitConfig.has_key(key): 618 cmd = ["git","config","--bool", key ] 619 s =read_pipe(cmd, ignore_error=True) 620 v = s.strip() 621 _gitConfig[key] = v =="true" 622return _gitConfig[key] 623 624defgitConfigList(key): 625if not _gitConfig.has_key(key): 626 s =read_pipe(["git","config","--get-all", key], ignore_error=True) 627 _gitConfig[key] = s.strip().split(os.linesep) 628return _gitConfig[key] 629 630defp4BranchesInGit(branchesAreInRemotes=True): 631"""Find all the branches whose names start with "p4/", looking 632 in remotes or heads as specified by the argument. Return 633 a dictionary of{ branch: revision }for each one found. 634 The branch names are the short names, without any 635 "p4/" prefix.""" 636 637 branches = {} 638 639 cmdline ="git rev-parse --symbolic " 640if branchesAreInRemotes: 641 cmdline +="--remotes" 642else: 643 cmdline +="--branches" 644 645for line inread_pipe_lines(cmdline): 646 line = line.strip() 647 648# only import to p4/ 649if not line.startswith('p4/'): 650continue 651# special symbolic ref to p4/master 652if line =="p4/HEAD": 653continue 654 655# strip off p4/ prefix 656 branch = line[len("p4/"):] 657 658 branches[branch] =parseRevision(line) 659 660return branches 661 662defbranch_exists(branch): 663"""Make sure that the given ref name really exists.""" 664 665 cmd = ["git","rev-parse","--symbolic","--verify", branch ] 666 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 667 out, _ = p.communicate() 668if p.returncode: 669return False 670# expect exactly one line of output: the branch name 671return out.rstrip() == branch 672 673deffindUpstreamBranchPoint(head ="HEAD"): 674 branches =p4BranchesInGit() 675# map from depot-path to branch name 676 branchByDepotPath = {} 677for branch in branches.keys(): 678 tip = branches[branch] 679 log =extractLogMessageFromGitCommit(tip) 680 settings =extractSettingsGitLog(log) 681if settings.has_key("depot-paths"): 682 paths =",".join(settings["depot-paths"]) 683 branchByDepotPath[paths] ="remotes/p4/"+ branch 684 685 settings =None 686 parent =0 687while parent <65535: 688 commit = head +"~%s"% parent 689 log =extractLogMessageFromGitCommit(commit) 690 settings =extractSettingsGitLog(log) 691if settings.has_key("depot-paths"): 692 paths =",".join(settings["depot-paths"]) 693if branchByDepotPath.has_key(paths): 694return[branchByDepotPath[paths], settings] 695 696 parent = parent +1 697 698return["", settings] 699 700defcreateOrUpdateBranchesFromOrigin(localRefPrefix ="refs/remotes/p4/", silent=True): 701if not silent: 702print("Creating/updating branch(es) in%sbased on origin branch(es)" 703% localRefPrefix) 704 705 originPrefix ="origin/p4/" 706 707for line inread_pipe_lines("git rev-parse --symbolic --remotes"): 708 line = line.strip() 709if(not line.startswith(originPrefix))or line.endswith("HEAD"): 710continue 711 712 headName = line[len(originPrefix):] 713 remoteHead = localRefPrefix + headName 714 originHead = line 715 716 original =extractSettingsGitLog(extractLogMessageFromGitCommit(originHead)) 717if(not original.has_key('depot-paths') 718or not original.has_key('change')): 719continue 720 721 update =False 722if notgitBranchExists(remoteHead): 723if verbose: 724print"creating%s"% remoteHead 725 update =True 726else: 727 settings =extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead)) 728if settings.has_key('change') >0: 729if settings['depot-paths'] == original['depot-paths']: 730 originP4Change =int(original['change']) 731 p4Change =int(settings['change']) 732if originP4Change > p4Change: 733print("%s(%s) is newer than%s(%s). " 734"Updating p4 branch from origin." 735% (originHead, originP4Change, 736 remoteHead, p4Change)) 737 update =True 738else: 739print("Ignoring:%swas imported from%swhile " 740"%swas imported from%s" 741% (originHead,','.join(original['depot-paths']), 742 remoteHead,','.join(settings['depot-paths']))) 743 744if update: 745system("git update-ref%s %s"% (remoteHead, originHead)) 746 747deforiginP4BranchesExist(): 748returngitBranchExists("origin")orgitBranchExists("origin/p4")orgitBranchExists("origin/p4/master") 749 750 751defp4ParseNumericChangeRange(parts): 752 changeStart =int(parts[0][1:]) 753if parts[1] =='#head': 754 changeEnd =p4_last_change() 755else: 756 changeEnd =int(parts[1]) 757 758return(changeStart, changeEnd) 759 760defchooseBlockSize(blockSize): 761if blockSize: 762return blockSize 763else: 764return defaultBlockSize 765 766defp4ChangesForPaths(depotPaths, changeRange, requestedBlockSize): 767assert depotPaths 768 769# Parse the change range into start and end. Try to find integer 770# revision ranges as these can be broken up into blocks to avoid 771# hitting server-side limits (maxrows, maxscanresults). But if 772# that doesn't work, fall back to using the raw revision specifier 773# strings, without using block mode. 774 775if changeRange is None or changeRange =='': 776 changeStart =1 777 changeEnd =p4_last_change() 778 block_size =chooseBlockSize(requestedBlockSize) 779else: 780 parts = changeRange.split(',') 781assertlen(parts) ==2 782try: 783(changeStart, changeEnd) =p4ParseNumericChangeRange(parts) 784 block_size =chooseBlockSize(requestedBlockSize) 785except: 786 changeStart = parts[0][1:] 787 changeEnd = parts[1] 788if requestedBlockSize: 789die("cannot use --changes-block-size with non-numeric revisions") 790 block_size =None 791 792# Accumulate change numbers in a dictionary to avoid duplicates 793 changes = {} 794 795for p in depotPaths: 796# Retrieve changes a block at a time, to prevent running 797# into a MaxResults/MaxScanRows error from the server. 798 799while True: 800 cmd = ['changes'] 801 802if block_size: 803 end =min(changeEnd, changeStart + block_size) 804 revisionRange ="%d,%d"% (changeStart, end) 805else: 806 revisionRange ="%s,%s"% (changeStart, changeEnd) 807 808 cmd += ["%s...@%s"% (p, revisionRange)] 809 810for line inp4_read_pipe_lines(cmd): 811 changeNum =int(line.split(" ")[1]) 812 changes[changeNum] =True 813 814if not block_size: 815break 816 817if end >= changeEnd: 818break 819 820 changeStart = end +1 821 822 changelist = changes.keys() 823 changelist.sort() 824return changelist 825 826defp4PathStartsWith(path, prefix): 827# This method tries to remedy a potential mixed-case issue: 828# 829# If UserA adds //depot/DirA/file1 830# and UserB adds //depot/dira/file2 831# 832# we may or may not have a problem. If you have core.ignorecase=true, 833# we treat DirA and dira as the same directory 834ifgitConfigBool("core.ignorecase"): 835return path.lower().startswith(prefix.lower()) 836return path.startswith(prefix) 837 838defgetClientSpec(): 839"""Look at the p4 client spec, create a View() object that contains 840 all the mappings, and return it.""" 841 842 specList =p4CmdList("client -o") 843iflen(specList) !=1: 844die('Output from "client -o" is%dlines, expecting 1'% 845len(specList)) 846 847# dictionary of all client parameters 848 entry = specList[0] 849 850# the //client/ name 851 client_name = entry["Client"] 852 853# just the keys that start with "View" 854 view_keys = [ k for k in entry.keys()if k.startswith("View") ] 855 856# hold this new View 857 view =View(client_name) 858 859# append the lines, in order, to the view 860for view_num inrange(len(view_keys)): 861 k ="View%d"% view_num 862if k not in view_keys: 863die("Expected view key%smissing"% k) 864 view.append(entry[k]) 865 866return view 867 868defgetClientRoot(): 869"""Grab the client directory.""" 870 871 output =p4CmdList("client -o") 872iflen(output) !=1: 873die('Output from "client -o" is%dlines, expecting 1'%len(output)) 874 875 entry = output[0] 876if"Root"not in entry: 877die('Client has no "Root"') 878 879return entry["Root"] 880 881# 882# P4 wildcards are not allowed in filenames. P4 complains 883# if you simply add them, but you can force it with "-f", in 884# which case it translates them into %xx encoding internally. 885# 886defwildcard_decode(path): 887# Search for and fix just these four characters. Do % last so 888# that fixing it does not inadvertently create new %-escapes. 889# Cannot have * in a filename in windows; untested as to 890# what p4 would do in such a case. 891if not platform.system() =="Windows": 892 path = path.replace("%2A","*") 893 path = path.replace("%23","#") \ 894.replace("%40","@") \ 895.replace("%25","%") 896return path 897 898defwildcard_encode(path): 899# do % first to avoid double-encoding the %s introduced here 900 path = path.replace("%","%25") \ 901.replace("*","%2A") \ 902.replace("#","%23") \ 903.replace("@","%40") 904return path 905 906defwildcard_present(path): 907 m = re.search("[*#@%]", path) 908return m is not None 909 910class Command: 911def__init__(self): 912 self.usage ="usage: %prog [options]" 913 self.needsGit =True 914 self.verbose =False 915 916class P4UserMap: 917def__init__(self): 918 self.userMapFromPerforceServer =False 919 self.myP4UserId =None 920 921defp4UserId(self): 922if self.myP4UserId: 923return self.myP4UserId 924 925 results =p4CmdList("user -o") 926for r in results: 927if r.has_key('User'): 928 self.myP4UserId = r['User'] 929return r['User'] 930die("Could not find your p4 user id") 931 932defp4UserIsMe(self, p4User): 933# return True if the given p4 user is actually me 934 me = self.p4UserId() 935if not p4User or p4User != me: 936return False 937else: 938return True 939 940defgetUserCacheFilename(self): 941 home = os.environ.get("HOME", os.environ.get("USERPROFILE")) 942return home +"/.gitp4-usercache.txt" 943 944defgetUserMapFromPerforceServer(self): 945if self.userMapFromPerforceServer: 946return 947 self.users = {} 948 self.emails = {} 949 950for output inp4CmdList("users"): 951if not output.has_key("User"): 952continue 953 self.users[output["User"]] = output["FullName"] +" <"+ output["Email"] +">" 954 self.emails[output["Email"]] = output["User"] 955 956 957 s ='' 958for(key, val)in self.users.items(): 959 s +="%s\t%s\n"% (key.expandtabs(1), val.expandtabs(1)) 960 961open(self.getUserCacheFilename(),"wb").write(s) 962 self.userMapFromPerforceServer =True 963 964defloadUserMapFromCache(self): 965 self.users = {} 966 self.userMapFromPerforceServer =False 967try: 968 cache =open(self.getUserCacheFilename(),"rb") 969 lines = cache.readlines() 970 cache.close() 971for line in lines: 972 entry = line.strip().split("\t") 973 self.users[entry[0]] = entry[1] 974exceptIOError: 975 self.getUserMapFromPerforceServer() 976 977classP4Debug(Command): 978def__init__(self): 979 Command.__init__(self) 980 self.options = [] 981 self.description ="A tool to debug the output of p4 -G." 982 self.needsGit =False 983 984defrun(self, args): 985 j =0 986for output inp4CmdList(args): 987print'Element:%d'% j 988 j +=1 989print output 990return True 991 992classP4RollBack(Command): 993def__init__(self): 994 Command.__init__(self) 995 self.options = [ 996 optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true") 997] 998 self.description ="A tool to debug the multi-branch import. Don't use :)" 999 self.rollbackLocalBranches =False10001001defrun(self, args):1002iflen(args) !=1:1003return False1004 maxChange =int(args[0])10051006if"p4ExitCode"inp4Cmd("changes -m 1"):1007die("Problems executing p4");10081009if self.rollbackLocalBranches:1010 refPrefix ="refs/heads/"1011 lines =read_pipe_lines("git rev-parse --symbolic --branches")1012else:1013 refPrefix ="refs/remotes/"1014 lines =read_pipe_lines("git rev-parse --symbolic --remotes")10151016for line in lines:1017if self.rollbackLocalBranches or(line.startswith("p4/")and line !="p4/HEAD\n"):1018 line = line.strip()1019 ref = refPrefix + line1020 log =extractLogMessageFromGitCommit(ref)1021 settings =extractSettingsGitLog(log)10221023 depotPaths = settings['depot-paths']1024 change = settings['change']10251026 changed =False10271028iflen(p4Cmd("changes -m 1 "+' '.join(['%s...@%s'% (p, maxChange)1029for p in depotPaths]))) ==0:1030print"Branch%sdid not exist at change%s, deleting."% (ref, maxChange)1031system("git update-ref -d%s`git rev-parse%s`"% (ref, ref))1032continue10331034while change andint(change) > maxChange:1035 changed =True1036if self.verbose:1037print"%sis at%s; rewinding towards%s"% (ref, change, maxChange)1038system("git update-ref%s\"%s^\""% (ref, ref))1039 log =extractLogMessageFromGitCommit(ref)1040 settings =extractSettingsGitLog(log)104110421043 depotPaths = settings['depot-paths']1044 change = settings['change']10451046if changed:1047print"%srewound to%s"% (ref, change)10481049return True10501051classP4Submit(Command, P4UserMap):10521053 conflict_behavior_choices = ("ask","skip","quit")10541055def__init__(self):1056 Command.__init__(self)1057 P4UserMap.__init__(self)1058 self.options = [1059 optparse.make_option("--origin", dest="origin"),1060 optparse.make_option("-M", dest="detectRenames", action="store_true"),1061# preserve the user, requires relevant p4 permissions1062 optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),1063 optparse.make_option("--export-labels", dest="exportLabels", action="store_true"),1064 optparse.make_option("--dry-run","-n", dest="dry_run", action="store_true"),1065 optparse.make_option("--prepare-p4-only", dest="prepare_p4_only", action="store_true"),1066 optparse.make_option("--conflict", dest="conflict_behavior",1067 choices=self.conflict_behavior_choices),1068 optparse.make_option("--branch", dest="branch"),1069]1070 self.description ="Submit changes from git to the perforce depot."1071 self.usage +=" [name of git branch to submit into perforce depot]"1072 self.origin =""1073 self.detectRenames =False1074 self.preserveUser =gitConfigBool("git-p4.preserveUser")1075 self.dry_run =False1076 self.prepare_p4_only =False1077 self.conflict_behavior =None1078 self.isWindows = (platform.system() =="Windows")1079 self.exportLabels =False1080 self.p4HasMoveCommand =p4_has_move_command()1081 self.branch =None10821083defcheck(self):1084iflen(p4CmdList("opened ...")) >0:1085die("You have files opened with perforce! Close them before starting the sync.")10861087defseparate_jobs_from_description(self, message):1088"""Extract and return a possible Jobs field in the commit1089 message. It goes into a separate section in the p4 change1090 specification.10911092 A jobs line starts with "Jobs:" and looks like a new field1093 in a form. Values are white-space separated on the same1094 line or on following lines that start with a tab.10951096 This does not parse and extract the full git commit message1097 like a p4 form. It just sees the Jobs: line as a marker1098 to pass everything from then on directly into the p4 form,1099 but outside the description section.11001101 Return a tuple (stripped log message, jobs string)."""11021103 m = re.search(r'^Jobs:', message, re.MULTILINE)1104if m is None:1105return(message,None)11061107 jobtext = message[m.start():]1108 stripped_message = message[:m.start()].rstrip()1109return(stripped_message, jobtext)11101111defprepareLogMessage(self, template, message, jobs):1112"""Edits the template returned from "p4 change -o" to insert1113 the message in the Description field, and the jobs text in1114 the Jobs field."""1115 result =""11161117 inDescriptionSection =False11181119for line in template.split("\n"):1120if line.startswith("#"):1121 result += line +"\n"1122continue11231124if inDescriptionSection:1125if line.startswith("Files:")or line.startswith("Jobs:"):1126 inDescriptionSection =False1127# insert Jobs section1128if jobs:1129 result += jobs +"\n"1130else:1131continue1132else:1133if line.startswith("Description:"):1134 inDescriptionSection =True1135 line +="\n"1136for messageLine in message.split("\n"):1137 line +="\t"+ messageLine +"\n"11381139 result += line +"\n"11401141return result11421143defpatchRCSKeywords(self,file, pattern):1144# Attempt to zap the RCS keywords in a p4 controlled file matching the given pattern1145(handle, outFileName) = tempfile.mkstemp(dir='.')1146try:1147 outFile = os.fdopen(handle,"w+")1148 inFile =open(file,"r")1149 regexp = re.compile(pattern, re.VERBOSE)1150for line in inFile.readlines():1151 line = regexp.sub(r'$\1$', line)1152 outFile.write(line)1153 inFile.close()1154 outFile.close()1155# Forcibly overwrite the original file1156 os.unlink(file)1157 shutil.move(outFileName,file)1158except:1159# cleanup our temporary file1160 os.unlink(outFileName)1161print"Failed to strip RCS keywords in%s"%file1162raise11631164print"Patched up RCS keywords in%s"%file11651166defp4UserForCommit(self,id):1167# Return the tuple (perforce user,git email) for a given git commit id1168 self.getUserMapFromPerforceServer()1169 gitEmail =read_pipe(["git","log","--max-count=1",1170"--format=%ae",id])1171 gitEmail = gitEmail.strip()1172if not self.emails.has_key(gitEmail):1173return(None,gitEmail)1174else:1175return(self.emails[gitEmail],gitEmail)11761177defcheckValidP4Users(self,commits):1178# check if any git authors cannot be mapped to p4 users1179foridin commits:1180(user,email) = self.p4UserForCommit(id)1181if not user:1182 msg ="Cannot find p4 user for email%sin commit%s."% (email,id)1183ifgitConfigBool("git-p4.allowMissingP4Users"):1184print"%s"% msg1185else:1186die("Error:%s\nSet git-p4.allowMissingP4Users to true to allow this."% msg)11871188deflastP4Changelist(self):1189# Get back the last changelist number submitted in this client spec. This1190# then gets used to patch up the username in the change. If the same1191# client spec is being used by multiple processes then this might go1192# wrong.1193 results =p4CmdList("client -o")# find the current client1194 client =None1195for r in results:1196if r.has_key('Client'):1197 client = r['Client']1198break1199if not client:1200die("could not get client spec")1201 results =p4CmdList(["changes","-c", client,"-m","1"])1202for r in results:1203if r.has_key('change'):1204return r['change']1205die("Could not get changelist number for last submit - cannot patch up user details")12061207defmodifyChangelistUser(self, changelist, newUser):1208# fixup the user field of a changelist after it has been submitted.1209 changes =p4CmdList("change -o%s"% changelist)1210iflen(changes) !=1:1211die("Bad output from p4 change modifying%sto user%s"%1212(changelist, newUser))12131214 c = changes[0]1215if c['User'] == newUser:return# nothing to do1216 c['User'] = newUser1217input= marshal.dumps(c)12181219 result =p4CmdList("change -f -i", stdin=input)1220for r in result:1221if r.has_key('code'):1222if r['code'] =='error':1223die("Could not modify user field of changelist%sto%s:%s"% (changelist, newUser, r['data']))1224if r.has_key('data'):1225print("Updated user field for changelist%sto%s"% (changelist, newUser))1226return1227die("Could not modify user field of changelist%sto%s"% (changelist, newUser))12281229defcanChangeChangelists(self):1230# check to see if we have p4 admin or super-user permissions, either of1231# which are required to modify changelists.1232 results =p4CmdList(["protects", self.depotPath])1233for r in results:1234if r.has_key('perm'):1235if r['perm'] =='admin':1236return11237if r['perm'] =='super':1238return11239return012401241defprepareSubmitTemplate(self):1242"""Run "p4 change -o" to grab a change specification template.1243 This does not use "p4 -G", as it is nice to keep the submission1244 template in original order, since a human might edit it.12451246 Remove lines in the Files section that show changes to files1247 outside the depot path we're committing into."""12481249 template =""1250 inFilesSection =False1251for line inp4_read_pipe_lines(['change','-o']):1252if line.endswith("\r\n"):1253 line = line[:-2] +"\n"1254if inFilesSection:1255if line.startswith("\t"):1256# path starts and ends with a tab1257 path = line[1:]1258 lastTab = path.rfind("\t")1259if lastTab != -1:1260 path = path[:lastTab]1261if notp4PathStartsWith(path, self.depotPath):1262continue1263else:1264 inFilesSection =False1265else:1266if line.startswith("Files:"):1267 inFilesSection =True12681269 template += line12701271return template12721273defedit_template(self, template_file):1274"""Invoke the editor to let the user change the submission1275 message. Return true if okay to continue with the submit."""12761277# if configured to skip the editing part, just submit1278ifgitConfigBool("git-p4.skipSubmitEdit"):1279return True12801281# look at the modification time, to check later if the user saved1282# the file1283 mtime = os.stat(template_file).st_mtime12841285# invoke the editor1286if os.environ.has_key("P4EDITOR")and(os.environ.get("P4EDITOR") !=""):1287 editor = os.environ.get("P4EDITOR")1288else:1289 editor =read_pipe("git var GIT_EDITOR").strip()1290system(["sh","-c", ('%s"$@"'% editor), editor, template_file])12911292# If the file was not saved, prompt to see if this patch should1293# be skipped. But skip this verification step if configured so.1294ifgitConfigBool("git-p4.skipSubmitEditCheck"):1295return True12961297# modification time updated means user saved the file1298if os.stat(template_file).st_mtime > mtime:1299return True13001301while True:1302 response =raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")1303if response =='y':1304return True1305if response =='n':1306return False13071308defget_diff_description(self, editedFiles, filesToAdd):1309# diff1310if os.environ.has_key("P4DIFF"):1311del(os.environ["P4DIFF"])1312 diff =""1313for editedFile in editedFiles:1314 diff +=p4_read_pipe(['diff','-du',1315wildcard_encode(editedFile)])13161317# new file diff1318 newdiff =""1319for newFile in filesToAdd:1320 newdiff +="==== new file ====\n"1321 newdiff +="--- /dev/null\n"1322 newdiff +="+++%s\n"% newFile1323 f =open(newFile,"r")1324for line in f.readlines():1325 newdiff +="+"+ line1326 f.close()13271328return(diff + newdiff).replace('\r\n','\n')13291330defapplyCommit(self,id):1331"""Apply one commit, return True if it succeeded."""13321333print"Applying",read_pipe(["git","show","-s",1334"--format=format:%h%s",id])13351336(p4User, gitEmail) = self.p4UserForCommit(id)13371338 diff =read_pipe_lines("git diff-tree -r%s\"%s^\" \"%s\""% (self.diffOpts,id,id))1339 filesToAdd =set()1340 filesToDelete =set()1341 editedFiles =set()1342 pureRenameCopy =set()1343 filesToChangeExecBit = {}13441345for line in diff:1346 diff =parseDiffTreeEntry(line)1347 modifier = diff['status']1348 path = diff['src']1349if modifier =="M":1350p4_edit(path)1351ifisModeExecChanged(diff['src_mode'], diff['dst_mode']):1352 filesToChangeExecBit[path] = diff['dst_mode']1353 editedFiles.add(path)1354elif modifier =="A":1355 filesToAdd.add(path)1356 filesToChangeExecBit[path] = diff['dst_mode']1357if path in filesToDelete:1358 filesToDelete.remove(path)1359elif modifier =="D":1360 filesToDelete.add(path)1361if path in filesToAdd:1362 filesToAdd.remove(path)1363elif modifier =="C":1364 src, dest = diff['src'], diff['dst']1365p4_integrate(src, dest)1366 pureRenameCopy.add(dest)1367if diff['src_sha1'] != diff['dst_sha1']:1368p4_edit(dest)1369 pureRenameCopy.discard(dest)1370ifisModeExecChanged(diff['src_mode'], diff['dst_mode']):1371p4_edit(dest)1372 pureRenameCopy.discard(dest)1373 filesToChangeExecBit[dest] = diff['dst_mode']1374if self.isWindows:1375# turn off read-only attribute1376 os.chmod(dest, stat.S_IWRITE)1377 os.unlink(dest)1378 editedFiles.add(dest)1379elif modifier =="R":1380 src, dest = diff['src'], diff['dst']1381if self.p4HasMoveCommand:1382p4_edit(src)# src must be open before move1383p4_move(src, dest)# opens for (move/delete, move/add)1384else:1385p4_integrate(src, dest)1386if diff['src_sha1'] != diff['dst_sha1']:1387p4_edit(dest)1388else:1389 pureRenameCopy.add(dest)1390ifisModeExecChanged(diff['src_mode'], diff['dst_mode']):1391if not self.p4HasMoveCommand:1392p4_edit(dest)# with move: already open, writable1393 filesToChangeExecBit[dest] = diff['dst_mode']1394if not self.p4HasMoveCommand:1395if self.isWindows:1396 os.chmod(dest, stat.S_IWRITE)1397 os.unlink(dest)1398 filesToDelete.add(src)1399 editedFiles.add(dest)1400else:1401die("unknown modifier%sfor%s"% (modifier, path))14021403 diffcmd ="git diff-tree --full-index -p\"%s\""% (id)1404 patchcmd = diffcmd +" | git apply "1405 tryPatchCmd = patchcmd +"--check -"1406 applyPatchCmd = patchcmd +"--check --apply -"1407 patch_succeeded =True14081409if os.system(tryPatchCmd) !=0:1410 fixed_rcs_keywords =False1411 patch_succeeded =False1412print"Unfortunately applying the change failed!"14131414# Patch failed, maybe it's just RCS keyword woes. Look through1415# the patch to see if that's possible.1416ifgitConfigBool("git-p4.attemptRCSCleanup"):1417file=None1418 pattern =None1419 kwfiles = {}1420forfilein editedFiles | filesToDelete:1421# did this file's delta contain RCS keywords?1422 pattern =p4_keywords_regexp_for_file(file)14231424if pattern:1425# this file is a possibility...look for RCS keywords.1426 regexp = re.compile(pattern, re.VERBOSE)1427for line inread_pipe_lines(["git","diff","%s^..%s"% (id,id),file]):1428if regexp.search(line):1429if verbose:1430print"got keyword match on%sin%sin%s"% (pattern, line,file)1431 kwfiles[file] = pattern1432break14331434forfilein kwfiles:1435if verbose:1436print"zapping%swith%s"% (line,pattern)1437# File is being deleted, so not open in p4. Must1438# disable the read-only bit on windows.1439if self.isWindows andfilenot in editedFiles:1440 os.chmod(file, stat.S_IWRITE)1441 self.patchRCSKeywords(file, kwfiles[file])1442 fixed_rcs_keywords =True14431444if fixed_rcs_keywords:1445print"Retrying the patch with RCS keywords cleaned up"1446if os.system(tryPatchCmd) ==0:1447 patch_succeeded =True14481449if not patch_succeeded:1450for f in editedFiles:1451p4_revert(f)1452return False14531454#1455# Apply the patch for real, and do add/delete/+x handling.1456#1457system(applyPatchCmd)14581459for f in filesToAdd:1460p4_add(f)1461for f in filesToDelete:1462p4_revert(f)1463p4_delete(f)14641465# Set/clear executable bits1466for f in filesToChangeExecBit.keys():1467 mode = filesToChangeExecBit[f]1468setP4ExecBit(f, mode)14691470#1471# Build p4 change description, starting with the contents1472# of the git commit message.1473#1474 logMessage =extractLogMessageFromGitCommit(id)1475 logMessage = logMessage.strip()1476(logMessage, jobs) = self.separate_jobs_from_description(logMessage)14771478 template = self.prepareSubmitTemplate()1479 submitTemplate = self.prepareLogMessage(template, logMessage, jobs)14801481if self.preserveUser:1482 submitTemplate +="\n######## Actual user%s, modified after commit\n"% p4User14831484if self.checkAuthorship and not self.p4UserIsMe(p4User):1485 submitTemplate +="######## git author%sdoes not match your p4 account.\n"% gitEmail1486 submitTemplate +="######## Use option --preserve-user to modify authorship.\n"1487 submitTemplate +="######## Variable git-p4.skipUserNameCheck hides this message.\n"14881489 separatorLine ="######## everything below this line is just the diff #######\n"1490if not self.prepare_p4_only:1491 submitTemplate += separatorLine1492 submitTemplate += self.get_diff_description(editedFiles, filesToAdd)14931494(handle, fileName) = tempfile.mkstemp()1495 tmpFile = os.fdopen(handle,"w+b")1496if self.isWindows:1497 submitTemplate = submitTemplate.replace("\n","\r\n")1498 tmpFile.write(submitTemplate)1499 tmpFile.close()15001501if self.prepare_p4_only:1502#1503# Leave the p4 tree prepared, and the submit template around1504# and let the user decide what to do next1505#1506print1507print"P4 workspace prepared for submission."1508print"To submit or revert, go to client workspace"1509print" "+ self.clientPath1510print1511print"To submit, use\"p4 submit\"to write a new description,"1512print"or\"p4 submit -i <%s\"to use the one prepared by" \1513"\"git p4\"."% fileName1514print"You can delete the file\"%s\"when finished."% fileName15151516if self.preserveUser and p4User and not self.p4UserIsMe(p4User):1517print"To preserve change ownership by user%s, you must\n" \1518"do\"p4 change -f <change>\"after submitting and\n" \1519"edit the User field."1520if pureRenameCopy:1521print"After submitting, renamed files must be re-synced."1522print"Invoke\"p4 sync -f\"on each of these files:"1523for f in pureRenameCopy:1524print" "+ f15251526print1527print"To revert the changes, use\"p4 revert ...\", and delete"1528print"the submit template file\"%s\""% fileName1529if filesToAdd:1530print"Since the commit adds new files, they must be deleted:"1531for f in filesToAdd:1532print" "+ f1533print1534return True15351536#1537# Let the user edit the change description, then submit it.1538#1539if self.edit_template(fileName):1540# read the edited message and submit1541 ret =True1542 tmpFile =open(fileName,"rb")1543 message = tmpFile.read()1544 tmpFile.close()1545if self.isWindows:1546 message = message.replace("\r\n","\n")1547 submitTemplate = message[:message.index(separatorLine)]1548p4_write_pipe(['submit','-i'], submitTemplate)15491550if self.preserveUser:1551if p4User:1552# Get last changelist number. Cannot easily get it from1553# the submit command output as the output is1554# unmarshalled.1555 changelist = self.lastP4Changelist()1556 self.modifyChangelistUser(changelist, p4User)15571558# The rename/copy happened by applying a patch that created a1559# new file. This leaves it writable, which confuses p4.1560for f in pureRenameCopy:1561p4_sync(f,"-f")15621563else:1564# skip this patch1565 ret =False1566print"Submission cancelled, undoing p4 changes."1567for f in editedFiles:1568p4_revert(f)1569for f in filesToAdd:1570p4_revert(f)1571 os.remove(f)1572for f in filesToDelete:1573p4_revert(f)15741575 os.remove(fileName)1576return ret15771578# Export git tags as p4 labels. Create a p4 label and then tag1579# with that.1580defexportGitTags(self, gitTags):1581 validLabelRegexp =gitConfig("git-p4.labelExportRegexp")1582iflen(validLabelRegexp) ==0:1583 validLabelRegexp = defaultLabelRegexp1584 m = re.compile(validLabelRegexp)15851586for name in gitTags:15871588if not m.match(name):1589if verbose:1590print"tag%sdoes not match regexp%s"% (name, validLabelRegexp)1591continue15921593# Get the p4 commit this corresponds to1594 logMessage =extractLogMessageFromGitCommit(name)1595 values =extractSettingsGitLog(logMessage)15961597if not values.has_key('change'):1598# a tag pointing to something not sent to p4; ignore1599if verbose:1600print"git tag%sdoes not give a p4 commit"% name1601continue1602else:1603 changelist = values['change']16041605# Get the tag details.1606 inHeader =True1607 isAnnotated =False1608 body = []1609for l inread_pipe_lines(["git","cat-file","-p", name]):1610 l = l.strip()1611if inHeader:1612if re.match(r'tag\s+', l):1613 isAnnotated =True1614elif re.match(r'\s*$', l):1615 inHeader =False1616continue1617else:1618 body.append(l)16191620if not isAnnotated:1621 body = ["lightweight tag imported by git p4\n"]16221623# Create the label - use the same view as the client spec we are using1624 clientSpec =getClientSpec()16251626 labelTemplate ="Label:%s\n"% name1627 labelTemplate +="Description:\n"1628for b in body:1629 labelTemplate +="\t"+ b +"\n"1630 labelTemplate +="View:\n"1631for depot_side in clientSpec.mappings:1632 labelTemplate +="\t%s\n"% depot_side16331634if self.dry_run:1635print"Would create p4 label%sfor tag"% name1636elif self.prepare_p4_only:1637print"Not creating p4 label%sfor tag due to option" \1638" --prepare-p4-only"% name1639else:1640p4_write_pipe(["label","-i"], labelTemplate)16411642# Use the label1643p4_system(["tag","-l", name] +1644["%s@%s"% (depot_side, changelist)for depot_side in clientSpec.mappings])16451646if verbose:1647print"created p4 label for tag%s"% name16481649defrun(self, args):1650iflen(args) ==0:1651 self.master =currentGitBranch()1652iflen(self.master) ==0or notgitBranchExists("refs/heads/%s"% self.master):1653die("Detecting current git branch failed!")1654eliflen(args) ==1:1655 self.master = args[0]1656if notbranchExists(self.master):1657die("Branch%sdoes not exist"% self.master)1658else:1659return False16601661 allowSubmit =gitConfig("git-p4.allowSubmit")1662iflen(allowSubmit) >0and not self.master in allowSubmit.split(","):1663die("%sis not in git-p4.allowSubmit"% self.master)16641665[upstream, settings] =findUpstreamBranchPoint()1666 self.depotPath = settings['depot-paths'][0]1667iflen(self.origin) ==0:1668 self.origin = upstream16691670if self.preserveUser:1671if not self.canChangeChangelists():1672die("Cannot preserve user names without p4 super-user or admin permissions")16731674# if not set from the command line, try the config file1675if self.conflict_behavior is None:1676 val =gitConfig("git-p4.conflict")1677if val:1678if val not in self.conflict_behavior_choices:1679die("Invalid value '%s' for config git-p4.conflict"% val)1680else:1681 val ="ask"1682 self.conflict_behavior = val16831684if self.verbose:1685print"Origin branch is "+ self.origin16861687iflen(self.depotPath) ==0:1688print"Internal error: cannot locate perforce depot path from existing branches"1689 sys.exit(128)16901691 self.useClientSpec =False1692ifgitConfigBool("git-p4.useclientspec"):1693 self.useClientSpec =True1694if self.useClientSpec:1695 self.clientSpecDirs =getClientSpec()16961697# Check for the existance of P4 branches1698 branchesDetected = (len(p4BranchesInGit().keys()) >1)16991700if self.useClientSpec and not branchesDetected:1701# all files are relative to the client spec1702 self.clientPath =getClientRoot()1703else:1704 self.clientPath =p4Where(self.depotPath)17051706if self.clientPath =="":1707die("Error: Cannot locate perforce checkout of%sin client view"% self.depotPath)17081709print"Perforce checkout for depot path%slocated at%s"% (self.depotPath, self.clientPath)1710 self.oldWorkingDirectory = os.getcwd()17111712# ensure the clientPath exists1713 new_client_dir =False1714if not os.path.exists(self.clientPath):1715 new_client_dir =True1716 os.makedirs(self.clientPath)17171718chdir(self.clientPath, is_client_path=True)1719if self.dry_run:1720print"Would synchronize p4 checkout in%s"% self.clientPath1721else:1722print"Synchronizing p4 checkout..."1723if new_client_dir:1724# old one was destroyed, and maybe nobody told p41725p4_sync("...","-f")1726else:1727p4_sync("...")1728 self.check()17291730 commits = []1731for line inread_pipe_lines(["git","rev-list","--no-merges","%s..%s"% (self.origin, self.master)]):1732 commits.append(line.strip())1733 commits.reverse()17341735if self.preserveUser orgitConfigBool("git-p4.skipUserNameCheck"):1736 self.checkAuthorship =False1737else:1738 self.checkAuthorship =True17391740if self.preserveUser:1741 self.checkValidP4Users(commits)17421743#1744# Build up a set of options to be passed to diff when1745# submitting each commit to p4.1746#1747if self.detectRenames:1748# command-line -M arg1749 self.diffOpts ="-M"1750else:1751# If not explicitly set check the config variable1752 detectRenames =gitConfig("git-p4.detectRenames")17531754if detectRenames.lower() =="false"or detectRenames =="":1755 self.diffOpts =""1756elif detectRenames.lower() =="true":1757 self.diffOpts ="-M"1758else:1759 self.diffOpts ="-M%s"% detectRenames17601761# no command-line arg for -C or --find-copies-harder, just1762# config variables1763 detectCopies =gitConfig("git-p4.detectCopies")1764if detectCopies.lower() =="false"or detectCopies =="":1765pass1766elif detectCopies.lower() =="true":1767 self.diffOpts +=" -C"1768else:1769 self.diffOpts +=" -C%s"% detectCopies17701771ifgitConfigBool("git-p4.detectCopiesHarder"):1772 self.diffOpts +=" --find-copies-harder"17731774#1775# Apply the commits, one at a time. On failure, ask if should1776# continue to try the rest of the patches, or quit.1777#1778if self.dry_run:1779print"Would apply"1780 applied = []1781 last =len(commits) -11782for i, commit inenumerate(commits):1783if self.dry_run:1784print" ",read_pipe(["git","show","-s",1785"--format=format:%h%s", commit])1786 ok =True1787else:1788 ok = self.applyCommit(commit)1789if ok:1790 applied.append(commit)1791else:1792if self.prepare_p4_only and i < last:1793print"Processing only the first commit due to option" \1794" --prepare-p4-only"1795break1796if i < last:1797 quit =False1798while True:1799# prompt for what to do, or use the option/variable1800if self.conflict_behavior =="ask":1801print"What do you want to do?"1802 response =raw_input("[s]kip this commit but apply"1803" the rest, or [q]uit? ")1804if not response:1805continue1806elif self.conflict_behavior =="skip":1807 response ="s"1808elif self.conflict_behavior =="quit":1809 response ="q"1810else:1811die("Unknown conflict_behavior '%s'"%1812 self.conflict_behavior)18131814if response[0] =="s":1815print"Skipping this commit, but applying the rest"1816break1817if response[0] =="q":1818print"Quitting"1819 quit =True1820break1821if quit:1822break18231824chdir(self.oldWorkingDirectory)18251826if self.dry_run:1827pass1828elif self.prepare_p4_only:1829pass1830eliflen(commits) ==len(applied):1831print"All commits applied!"18321833 sync =P4Sync()1834if self.branch:1835 sync.branch = self.branch1836 sync.run([])18371838 rebase =P4Rebase()1839 rebase.rebase()18401841else:1842iflen(applied) ==0:1843print"No commits applied."1844else:1845print"Applied only the commits marked with '*':"1846for c in commits:1847if c in applied:1848 star ="*"1849else:1850 star =" "1851print star,read_pipe(["git","show","-s",1852"--format=format:%h%s", c])1853print"You will have to do 'git p4 sync' and rebase."18541855ifgitConfigBool("git-p4.exportLabels"):1856 self.exportLabels =True18571858if self.exportLabels:1859 p4Labels =getP4Labels(self.depotPath)1860 gitTags =getGitTags()18611862 missingGitTags = gitTags - p4Labels1863 self.exportGitTags(missingGitTags)18641865# exit with error unless everything applied perfectly1866iflen(commits) !=len(applied):1867 sys.exit(1)18681869return True18701871classView(object):1872"""Represent a p4 view ("p4 help views"), and map files in a1873 repo according to the view."""18741875def__init__(self, client_name):1876 self.mappings = []1877 self.client_prefix ="//%s/"% client_name1878# cache results of "p4 where" to lookup client file locations1879 self.client_spec_path_cache = {}18801881defappend(self, view_line):1882"""Parse a view line, splitting it into depot and client1883 sides. Append to self.mappings, preserving order. This1884 is only needed for tag creation."""18851886# Split the view line into exactly two words. P4 enforces1887# structure on these lines that simplifies this quite a bit.1888#1889# Either or both words may be double-quoted.1890# Single quotes do not matter.1891# Double-quote marks cannot occur inside the words.1892# A + or - prefix is also inside the quotes.1893# There are no quotes unless they contain a space.1894# The line is already white-space stripped.1895# The two words are separated by a single space.1896#1897if view_line[0] =='"':1898# First word is double quoted. Find its end.1899 close_quote_index = view_line.find('"',1)1900if close_quote_index <=0:1901die("No first-word closing quote found:%s"% view_line)1902 depot_side = view_line[1:close_quote_index]1903# skip closing quote and space1904 rhs_index = close_quote_index +1+11905else:1906 space_index = view_line.find(" ")1907if space_index <=0:1908die("No word-splitting space found:%s"% view_line)1909 depot_side = view_line[0:space_index]1910 rhs_index = space_index +119111912# prefix + means overlay on previous mapping1913if depot_side.startswith("+"):1914 depot_side = depot_side[1:]19151916# prefix - means exclude this path, leave out of mappings1917 exclude =False1918if depot_side.startswith("-"):1919 exclude =True1920 depot_side = depot_side[1:]19211922if not exclude:1923 self.mappings.append(depot_side)19241925defconvert_client_path(self, clientFile):1926# chop off //client/ part to make it relative1927if not clientFile.startswith(self.client_prefix):1928die("No prefix '%s' on clientFile '%s'"%1929(self.client_prefix, clientFile))1930return clientFile[len(self.client_prefix):]19311932defupdate_client_spec_path_cache(self, files):1933""" Caching file paths by "p4 where" batch query """19341935# List depot file paths exclude that already cached1936 fileArgs = [f['path']for f in files if f['path']not in self.client_spec_path_cache]19371938iflen(fileArgs) ==0:1939return# All files in cache19401941 where_result =p4CmdList(["-x","-","where"], stdin=fileArgs)1942for res in where_result:1943if"code"in res and res["code"] =="error":1944# assume error is "... file(s) not in client view"1945continue1946if"clientFile"not in res:1947die("No clientFile in 'p4 where' output")1948if"unmap"in res:1949# it will list all of them, but only one not unmap-ped1950continue1951ifgitConfigBool("core.ignorecase"):1952 res['depotFile'] = res['depotFile'].lower()1953 self.client_spec_path_cache[res['depotFile']] = self.convert_client_path(res["clientFile"])19541955# not found files or unmap files set to ""1956for depotFile in fileArgs:1957ifgitConfigBool("core.ignorecase"):1958 depotFile = depotFile.lower()1959if depotFile not in self.client_spec_path_cache:1960 self.client_spec_path_cache[depotFile] =""19611962defmap_in_client(self, depot_path):1963"""Return the relative location in the client where this1964 depot file should live. Returns "" if the file should1965 not be mapped in the client."""19661967ifgitConfigBool("core.ignorecase"):1968 depot_path = depot_path.lower()19691970if depot_path in self.client_spec_path_cache:1971return self.client_spec_path_cache[depot_path]19721973die("Error:%sis not found in client spec path"% depot_path )1974return""19751976classP4Sync(Command, P4UserMap):1977 delete_actions = ("delete","move/delete","purge")19781979def__init__(self):1980 Command.__init__(self)1981 P4UserMap.__init__(self)1982 self.options = [1983 optparse.make_option("--branch", dest="branch"),1984 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),1985 optparse.make_option("--changesfile", dest="changesFile"),1986 optparse.make_option("--silent", dest="silent", action="store_true"),1987 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),1988 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),1989 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",1990help="Import into refs/heads/ , not refs/remotes"),1991 optparse.make_option("--max-changes", dest="maxChanges",1992help="Maximum number of changes to import"),1993 optparse.make_option("--changes-block-size", dest="changes_block_size",type="int",1994help="Internal block size to use when iteratively calling p4 changes"),1995 optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',1996help="Keep entire BRANCH/DIR/SUBDIR prefix during import"),1997 optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true',1998help="Only sync files that are included in the Perforce Client Spec"),1999 optparse.make_option("-/", dest="cloneExclude",2000 action="append",type="string",2001help="exclude depot path"),2002]2003 self.description ="""Imports from Perforce into a git repository.\n2004 example:2005 //depot/my/project/ -- to import the current head2006 //depot/my/project/@all -- to import everything2007 //depot/my/project/@1,6 -- to import only from revision 1 to 620082009 (a ... is not needed in the path p4 specification, it's added implicitly)"""20102011 self.usage +=" //depot/path[@revRange]"2012 self.silent =False2013 self.createdBranches =set()2014 self.committedChanges =set()2015 self.branch =""2016 self.detectBranches =False2017 self.detectLabels =False2018 self.importLabels =False2019 self.changesFile =""2020 self.syncWithOrigin =True2021 self.importIntoRemotes =True2022 self.maxChanges =""2023 self.changes_block_size =None2024 self.keepRepoPath =False2025 self.depotPaths =None2026 self.p4BranchesInGit = []2027 self.cloneExclude = []2028 self.useClientSpec =False2029 self.useClientSpec_from_options =False2030 self.clientSpecDirs =None2031 self.tempBranches = []2032 self.tempBranchLocation ="git-p4-tmp"20332034ifgitConfig("git-p4.syncFromOrigin") =="false":2035 self.syncWithOrigin =False20362037# This is required for the "append" cloneExclude action2038defensure_value(self, attr, value):2039if nothasattr(self, attr)orgetattr(self, attr)is None:2040setattr(self, attr, value)2041returngetattr(self, attr)20422043# Force a checkpoint in fast-import and wait for it to finish2044defcheckpoint(self):2045 self.gitStream.write("checkpoint\n\n")2046 self.gitStream.write("progress checkpoint\n\n")2047 out = self.gitOutput.readline()2048if self.verbose:2049print"checkpoint finished: "+ out20502051defextractFilesFromCommit(self, commit):2052 self.cloneExclude = [re.sub(r"\.\.\.$","", path)2053for path in self.cloneExclude]2054 files = []2055 fnum =02056while commit.has_key("depotFile%s"% fnum):2057 path = commit["depotFile%s"% fnum]20582059if[p for p in self.cloneExclude2060ifp4PathStartsWith(path, p)]:2061 found =False2062else:2063 found = [p for p in self.depotPaths2064ifp4PathStartsWith(path, p)]2065if not found:2066 fnum = fnum +12067continue20682069file= {}2070file["path"] = path2071file["rev"] = commit["rev%s"% fnum]2072file["action"] = commit["action%s"% fnum]2073file["type"] = commit["type%s"% fnum]2074 files.append(file)2075 fnum = fnum +12076return files20772078defstripRepoPath(self, path, prefixes):2079"""When streaming files, this is called to map a p4 depot path2080 to where it should go in git. The prefixes are either2081 self.depotPaths, or self.branchPrefixes in the case of2082 branch detection."""20832084if self.useClientSpec:2085# branch detection moves files up a level (the branch name)2086# from what client spec interpretation gives2087 path = self.clientSpecDirs.map_in_client(path)2088if self.detectBranches:2089for b in self.knownBranches:2090if path.startswith(b +"/"):2091 path = path[len(b)+1:]20922093elif self.keepRepoPath:2094# Preserve everything in relative path name except leading2095# //depot/; just look at first prefix as they all should2096# be in the same depot.2097 depot = re.sub("^(//[^/]+/).*", r'\1', prefixes[0])2098ifp4PathStartsWith(path, depot):2099 path = path[len(depot):]21002101else:2102for p in prefixes:2103ifp4PathStartsWith(path, p):2104 path = path[len(p):]2105break21062107 path =wildcard_decode(path)2108return path21092110defsplitFilesIntoBranches(self, commit):2111"""Look at each depotFile in the commit to figure out to what2112 branch it belongs."""21132114if self.clientSpecDirs:2115 files = self.extractFilesFromCommit(commit)2116 self.clientSpecDirs.update_client_spec_path_cache(files)21172118 branches = {}2119 fnum =02120while commit.has_key("depotFile%s"% fnum):2121 path = commit["depotFile%s"% fnum]2122 found = [p for p in self.depotPaths2123ifp4PathStartsWith(path, p)]2124if not found:2125 fnum = fnum +12126continue21272128file= {}2129file["path"] = path2130file["rev"] = commit["rev%s"% fnum]2131file["action"] = commit["action%s"% fnum]2132file["type"] = commit["type%s"% fnum]2133 fnum = fnum +121342135# start with the full relative path where this file would2136# go in a p4 client2137if self.useClientSpec:2138 relPath = self.clientSpecDirs.map_in_client(path)2139else:2140 relPath = self.stripRepoPath(path, self.depotPaths)21412142for branch in self.knownBranches.keys():2143# add a trailing slash so that a commit into qt/4.2foo2144# doesn't end up in qt/4.2, e.g.2145if relPath.startswith(branch +"/"):2146if branch not in branches:2147 branches[branch] = []2148 branches[branch].append(file)2149break21502151return branches21522153# output one file from the P4 stream2154# - helper for streamP4Files21552156defstreamOneP4File(self,file, contents):2157 relPath = self.stripRepoPath(file['depotFile'], self.branchPrefixes)2158if verbose:2159 sys.stderr.write("%s\n"% relPath)21602161(type_base, type_mods) =split_p4_type(file["type"])21622163 git_mode ="100644"2164if"x"in type_mods:2165 git_mode ="100755"2166if type_base =="symlink":2167 git_mode ="120000"2168# p4 print on a symlink sometimes contains "target\n";2169# if it does, remove the newline2170 data =''.join(contents)2171if not data:2172# Some version of p4 allowed creating a symlink that pointed2173# to nothing. This causes p4 errors when checking out such2174# a change, and errors here too. Work around it by ignoring2175# the bad symlink; hopefully a future change fixes it.2176print"\nIgnoring empty symlink in%s"%file['depotFile']2177return2178elif data[-1] =='\n':2179 contents = [data[:-1]]2180else:2181 contents = [data]21822183if type_base =="utf16":2184# p4 delivers different text in the python output to -G2185# than it does when using "print -o", or normal p4 client2186# operations. utf16 is converted to ascii or utf8, perhaps.2187# But ascii text saved as -t utf16 is completely mangled.2188# Invoke print -o to get the real contents.2189#2190# On windows, the newlines will always be mangled by print, so put2191# them back too. This is not needed to the cygwin windows version,2192# just the native "NT" type.2193#2194try:2195 text =p4_read_pipe(['print','-q','-o','-','%s@%s'% (file['depotFile'],file['change'])])2196exceptExceptionas e:2197if'Translation of file content failed'instr(e):2198 type_base ='binary'2199else:2200raise e2201else:2202ifp4_version_string().find('/NT') >=0:2203 text = text.replace('\r\n','\n')2204 contents = [ text ]22052206if type_base =="apple":2207# Apple filetype files will be streamed as a concatenation of2208# its appledouble header and the contents. This is useless2209# on both macs and non-macs. If using "print -q -o xx", it2210# will create "xx" with the data, and "%xx" with the header.2211# This is also not very useful.2212#2213# Ideally, someday, this script can learn how to generate2214# appledouble files directly and import those to git, but2215# non-mac machines can never find a use for apple filetype.2216print"\nIgnoring apple filetype file%s"%file['depotFile']2217return22182219# Note that we do not try to de-mangle keywords on utf16 files,2220# even though in theory somebody may want that.2221 pattern =p4_keywords_regexp_for_type(type_base, type_mods)2222if pattern:2223 regexp = re.compile(pattern, re.VERBOSE)2224 text =''.join(contents)2225 text = regexp.sub(r'$\1$', text)2226 contents = [ text ]22272228try:2229 relPath.decode('ascii')2230except:2231 encoding ='utf8'2232ifgitConfig('git-p4.pathEncoding'):2233 encoding =gitConfig('git-p4.pathEncoding')2234 relPath = relPath.decode(encoding,'replace').encode('utf8','replace')2235if self.verbose:2236print'Path with non-ASCII characters detected. Used%sto encode:%s'% (encoding, relPath)22372238 self.gitStream.write("M%sinline%s\n"% (git_mode, relPath))22392240# total length...2241 length =02242for d in contents:2243 length = length +len(d)22442245 self.gitStream.write("data%d\n"% length)2246for d in contents:2247 self.gitStream.write(d)2248 self.gitStream.write("\n")22492250defstreamOneP4Deletion(self,file):2251 relPath = self.stripRepoPath(file['path'], self.branchPrefixes)2252if verbose:2253 sys.stderr.write("delete%s\n"% relPath)2254 self.gitStream.write("D%s\n"% relPath)22552256# handle another chunk of streaming data2257defstreamP4FilesCb(self, marshalled):22582259# catch p4 errors and complain2260 err =None2261if"code"in marshalled:2262if marshalled["code"] =="error":2263if"data"in marshalled:2264 err = marshalled["data"].rstrip()2265if err:2266 f =None2267if self.stream_have_file_info:2268if"depotFile"in self.stream_file:2269 f = self.stream_file["depotFile"]2270# force a failure in fast-import, else an empty2271# commit will be made2272 self.gitStream.write("\n")2273 self.gitStream.write("die-now\n")2274 self.gitStream.close()2275# ignore errors, but make sure it exits first2276 self.importProcess.wait()2277if f:2278die("Error from p4 print for%s:%s"% (f, err))2279else:2280die("Error from p4 print:%s"% err)22812282if marshalled.has_key('depotFile')and self.stream_have_file_info:2283# start of a new file - output the old one first2284 self.streamOneP4File(self.stream_file, self.stream_contents)2285 self.stream_file = {}2286 self.stream_contents = []2287 self.stream_have_file_info =False22882289# pick up the new file information... for the2290# 'data' field we need to append to our array2291for k in marshalled.keys():2292if k =='data':2293 self.stream_contents.append(marshalled['data'])2294else:2295 self.stream_file[k] = marshalled[k]22962297 self.stream_have_file_info =True22982299# Stream directly from "p4 files" into "git fast-import"2300defstreamP4Files(self, files):2301 filesForCommit = []2302 filesToRead = []2303 filesToDelete = []23042305for f in files:2306# if using a client spec, only add the files that have2307# a path in the client2308if self.clientSpecDirs:2309if self.clientSpecDirs.map_in_client(f['path']) =="":2310continue23112312 filesForCommit.append(f)2313if f['action']in self.delete_actions:2314 filesToDelete.append(f)2315else:2316 filesToRead.append(f)23172318# deleted files...2319for f in filesToDelete:2320 self.streamOneP4Deletion(f)23212322iflen(filesToRead) >0:2323 self.stream_file = {}2324 self.stream_contents = []2325 self.stream_have_file_info =False23262327# curry self argument2328defstreamP4FilesCbSelf(entry):2329 self.streamP4FilesCb(entry)23302331 fileArgs = ['%s#%s'% (f['path'], f['rev'])for f in filesToRead]23322333p4CmdList(["-x","-","print"],2334 stdin=fileArgs,2335 cb=streamP4FilesCbSelf)23362337# do the last chunk2338if self.stream_file.has_key('depotFile'):2339 self.streamOneP4File(self.stream_file, self.stream_contents)23402341defmake_email(self, userid):2342if userid in self.users:2343return self.users[userid]2344else:2345return"%s<a@b>"% userid23462347defstreamTag(self, gitStream, labelName, labelDetails, commit, epoch):2348""" Stream a p4 tag.2349 commit is either a git commit, or a fast-import mark, ":<p4commit>"2350 """23512352if verbose:2353print"writing tag%sfor commit%s"% (labelName, commit)2354 gitStream.write("tag%s\n"% labelName)2355 gitStream.write("from%s\n"% commit)23562357if labelDetails.has_key('Owner'):2358 owner = labelDetails["Owner"]2359else:2360 owner =None23612362# Try to use the owner of the p4 label, or failing that,2363# the current p4 user id.2364if owner:2365 email = self.make_email(owner)2366else:2367 email = self.make_email(self.p4UserId())2368 tagger ="%s %s %s"% (email, epoch, self.tz)23692370 gitStream.write("tagger%s\n"% tagger)23712372print"labelDetails=",labelDetails2373if labelDetails.has_key('Description'):2374 description = labelDetails['Description']2375else:2376 description ='Label from git p4'23772378 gitStream.write("data%d\n"%len(description))2379 gitStream.write(description)2380 gitStream.write("\n")23812382defcommit(self, details, files, branch, parent =""):2383 epoch = details["time"]2384 author = details["user"]23852386if self.verbose:2387print"commit into%s"% branch23882389# start with reading files; if that fails, we should not2390# create a commit.2391 new_files = []2392for f in files:2393if[p for p in self.branchPrefixes ifp4PathStartsWith(f['path'], p)]:2394 new_files.append(f)2395else:2396 sys.stderr.write("Ignoring file outside of prefix:%s\n"% f['path'])23972398if self.clientSpecDirs:2399 self.clientSpecDirs.update_client_spec_path_cache(files)24002401 self.gitStream.write("commit%s\n"% branch)2402 self.gitStream.write("mark :%s\n"% details["change"])2403 self.committedChanges.add(int(details["change"]))2404 committer =""2405if author not in self.users:2406 self.getUserMapFromPerforceServer()2407 committer ="%s %s %s"% (self.make_email(author), epoch, self.tz)24082409 self.gitStream.write("committer%s\n"% committer)24102411 self.gitStream.write("data <<EOT\n")2412 self.gitStream.write(details["desc"])2413 self.gitStream.write("\n[git-p4: depot-paths =\"%s\": change =%s"%2414(','.join(self.branchPrefixes), details["change"]))2415iflen(details['options']) >0:2416 self.gitStream.write(": options =%s"% details['options'])2417 self.gitStream.write("]\nEOT\n\n")24182419iflen(parent) >0:2420if self.verbose:2421print"parent%s"% parent2422 self.gitStream.write("from%s\n"% parent)24232424 self.streamP4Files(new_files)2425 self.gitStream.write("\n")24262427 change =int(details["change"])24282429if self.labels.has_key(change):2430 label = self.labels[change]2431 labelDetails = label[0]2432 labelRevisions = label[1]2433if self.verbose:2434print"Change%sis labelled%s"% (change, labelDetails)24352436 files =p4CmdList(["files"] + ["%s...@%s"% (p, change)2437for p in self.branchPrefixes])24382439iflen(files) ==len(labelRevisions):24402441 cleanedFiles = {}2442for info in files:2443if info["action"]in self.delete_actions:2444continue2445 cleanedFiles[info["depotFile"]] = info["rev"]24462447if cleanedFiles == labelRevisions:2448 self.streamTag(self.gitStream,'tag_%s'% labelDetails['label'], labelDetails, branch, epoch)24492450else:2451if not self.silent:2452print("Tag%sdoes not match with change%s: files do not match."2453% (labelDetails["label"], change))24542455else:2456if not self.silent:2457print("Tag%sdoes not match with change%s: file count is different."2458% (labelDetails["label"], change))24592460# Build a dictionary of changelists and labels, for "detect-labels" option.2461defgetLabels(self):2462 self.labels = {}24632464 l =p4CmdList(["labels"] + ["%s..."% p for p in self.depotPaths])2465iflen(l) >0and not self.silent:2466print"Finding files belonging to labels in%s"% `self.depotPaths`24672468for output in l:2469 label = output["label"]2470 revisions = {}2471 newestChange =02472if self.verbose:2473print"Querying files for label%s"% label2474forfileinp4CmdList(["files"] +2475["%s...@%s"% (p, label)2476for p in self.depotPaths]):2477 revisions[file["depotFile"]] =file["rev"]2478 change =int(file["change"])2479if change > newestChange:2480 newestChange = change24812482 self.labels[newestChange] = [output, revisions]24832484if self.verbose:2485print"Label changes:%s"% self.labels.keys()24862487# Import p4 labels as git tags. A direct mapping does not2488# exist, so assume that if all the files are at the same revision2489# then we can use that, or it's something more complicated we should2490# just ignore.2491defimportP4Labels(self, stream, p4Labels):2492if verbose:2493print"import p4 labels: "+' '.join(p4Labels)24942495 ignoredP4Labels =gitConfigList("git-p4.ignoredP4Labels")2496 validLabelRegexp =gitConfig("git-p4.labelImportRegexp")2497iflen(validLabelRegexp) ==0:2498 validLabelRegexp = defaultLabelRegexp2499 m = re.compile(validLabelRegexp)25002501for name in p4Labels:2502 commitFound =False25032504if not m.match(name):2505if verbose:2506print"label%sdoes not match regexp%s"% (name,validLabelRegexp)2507continue25082509if name in ignoredP4Labels:2510continue25112512 labelDetails =p4CmdList(['label',"-o", name])[0]25132514# get the most recent changelist for each file in this label2515 change =p4Cmd(["changes","-m","1"] + ["%s...@%s"% (p, name)2516for p in self.depotPaths])25172518if change.has_key('change'):2519# find the corresponding git commit; take the oldest commit2520 changelist =int(change['change'])2521if changelist in self.committedChanges:2522 gitCommit =":%d"% changelist # use a fast-import mark2523 commitFound =True2524else:2525 gitCommit =read_pipe(["git","rev-list","--max-count=1",2526"--reverse",":/\[git-p4:.*change =%d\]"% changelist], ignore_error=True)2527iflen(gitCommit) ==0:2528print"importing label%s: could not find git commit for changelist%d"% (name, changelist)2529else:2530 commitFound =True2531 gitCommit = gitCommit.strip()25322533if commitFound:2534# Convert from p4 time format2535try:2536 tmwhen = time.strptime(labelDetails['Update'],"%Y/%m/%d%H:%M:%S")2537exceptValueError:2538print"Could not convert label time%s"% labelDetails['Update']2539 tmwhen =125402541 when =int(time.mktime(tmwhen))2542 self.streamTag(stream, name, labelDetails, gitCommit, when)2543if verbose:2544print"p4 label%smapped to git commit%s"% (name, gitCommit)2545else:2546if verbose:2547print"Label%shas no changelists - possibly deleted?"% name25482549if not commitFound:2550# We can't import this label; don't try again as it will get very2551# expensive repeatedly fetching all the files for labels that will2552# never be imported. If the label is moved in the future, the2553# ignore will need to be removed manually.2554system(["git","config","--add","git-p4.ignoredP4Labels", name])25552556defguessProjectName(self):2557for p in self.depotPaths:2558if p.endswith("/"):2559 p = p[:-1]2560 p = p[p.strip().rfind("/") +1:]2561if not p.endswith("/"):2562 p +="/"2563return p25642565defgetBranchMapping(self):2566 lostAndFoundBranches =set()25672568 user =gitConfig("git-p4.branchUser")2569iflen(user) >0:2570 command ="branches -u%s"% user2571else:2572 command ="branches"25732574for info inp4CmdList(command):2575 details =p4Cmd(["branch","-o", info["branch"]])2576 viewIdx =02577while details.has_key("View%s"% viewIdx):2578 paths = details["View%s"% viewIdx].split(" ")2579 viewIdx = viewIdx +12580# require standard //depot/foo/... //depot/bar/... mapping2581iflen(paths) !=2or not paths[0].endswith("/...")or not paths[1].endswith("/..."):2582continue2583 source = paths[0]2584 destination = paths[1]2585## HACK2586ifp4PathStartsWith(source, self.depotPaths[0])andp4PathStartsWith(destination, self.depotPaths[0]):2587 source = source[len(self.depotPaths[0]):-4]2588 destination = destination[len(self.depotPaths[0]):-4]25892590if destination in self.knownBranches:2591if not self.silent:2592print"p4 branch%sdefines a mapping from%sto%s"% (info["branch"], source, destination)2593print"but there exists another mapping from%sto%salready!"% (self.knownBranches[destination], destination)2594continue25952596 self.knownBranches[destination] = source25972598 lostAndFoundBranches.discard(destination)25992600if source not in self.knownBranches:2601 lostAndFoundBranches.add(source)26022603# Perforce does not strictly require branches to be defined, so we also2604# check git config for a branch list.2605#2606# Example of branch definition in git config file:2607# [git-p4]2608# branchList=main:branchA2609# branchList=main:branchB2610# branchList=branchA:branchC2611 configBranches =gitConfigList("git-p4.branchList")2612for branch in configBranches:2613if branch:2614(source, destination) = branch.split(":")2615 self.knownBranches[destination] = source26162617 lostAndFoundBranches.discard(destination)26182619if source not in self.knownBranches:2620 lostAndFoundBranches.add(source)262126222623for branch in lostAndFoundBranches:2624 self.knownBranches[branch] = branch26252626defgetBranchMappingFromGitBranches(self):2627 branches =p4BranchesInGit(self.importIntoRemotes)2628for branch in branches.keys():2629if branch =="master":2630 branch ="main"2631else:2632 branch = branch[len(self.projectName):]2633 self.knownBranches[branch] = branch26342635defupdateOptionDict(self, d):2636 option_keys = {}2637if self.keepRepoPath:2638 option_keys['keepRepoPath'] =126392640 d["options"] =' '.join(sorted(option_keys.keys()))26412642defreadOptions(self, d):2643 self.keepRepoPath = (d.has_key('options')2644and('keepRepoPath'in d['options']))26452646defgitRefForBranch(self, branch):2647if branch =="main":2648return self.refPrefix +"master"26492650iflen(branch) <=0:2651return branch26522653return self.refPrefix + self.projectName + branch26542655defgitCommitByP4Change(self, ref, change):2656if self.verbose:2657print"looking in ref "+ ref +" for change%susing bisect..."% change26582659 earliestCommit =""2660 latestCommit =parseRevision(ref)26612662while True:2663if self.verbose:2664print"trying: earliest%slatest%s"% (earliestCommit, latestCommit)2665 next =read_pipe("git rev-list --bisect%s %s"% (latestCommit, earliestCommit)).strip()2666iflen(next) ==0:2667if self.verbose:2668print"argh"2669return""2670 log =extractLogMessageFromGitCommit(next)2671 settings =extractSettingsGitLog(log)2672 currentChange =int(settings['change'])2673if self.verbose:2674print"current change%s"% currentChange26752676if currentChange == change:2677if self.verbose:2678print"found%s"% next2679return next26802681if currentChange < change:2682 earliestCommit ="^%s"% next2683else:2684 latestCommit ="%s"% next26852686return""26872688defimportNewBranch(self, branch, maxChange):2689# make fast-import flush all changes to disk and update the refs using the checkpoint2690# command so that we can try to find the branch parent in the git history2691 self.gitStream.write("checkpoint\n\n");2692 self.gitStream.flush();2693 branchPrefix = self.depotPaths[0] + branch +"/"2694range="@1,%s"% maxChange2695#print "prefix" + branchPrefix2696 changes =p4ChangesForPaths([branchPrefix],range, self.changes_block_size)2697iflen(changes) <=0:2698return False2699 firstChange = changes[0]2700#print "first change in branch: %s" % firstChange2701 sourceBranch = self.knownBranches[branch]2702 sourceDepotPath = self.depotPaths[0] + sourceBranch2703 sourceRef = self.gitRefForBranch(sourceBranch)2704#print "source " + sourceBranch27052706 branchParentChange =int(p4Cmd(["changes","-m","1","%s...@1,%s"% (sourceDepotPath, firstChange)])["change"])2707#print "branch parent: %s" % branchParentChange2708 gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)2709iflen(gitParent) >0:2710 self.initialParents[self.gitRefForBranch(branch)] = gitParent2711#print "parent git commit: %s" % gitParent27122713 self.importChanges(changes)2714return True27152716defsearchParent(self, parent, branch, target):2717 parentFound =False2718for blob inread_pipe_lines(["git","rev-list","--reverse",2719"--no-merges", parent]):2720 blob = blob.strip()2721iflen(read_pipe(["git","diff-tree", blob, target])) ==0:2722 parentFound =True2723if self.verbose:2724print"Found parent of%sin commit%s"% (branch, blob)2725break2726if parentFound:2727return blob2728else:2729return None27302731defimportChanges(self, changes):2732 cnt =12733for change in changes:2734 description =p4_describe(change)2735 self.updateOptionDict(description)27362737if not self.silent:2738 sys.stdout.write("\rImporting revision%s(%s%%)"% (change, cnt *100/len(changes)))2739 sys.stdout.flush()2740 cnt = cnt +127412742try:2743if self.detectBranches:2744 branches = self.splitFilesIntoBranches(description)2745for branch in branches.keys():2746## HACK --hwn2747 branchPrefix = self.depotPaths[0] + branch +"/"2748 self.branchPrefixes = [ branchPrefix ]27492750 parent =""27512752 filesForCommit = branches[branch]27532754if self.verbose:2755print"branch is%s"% branch27562757 self.updatedBranches.add(branch)27582759if branch not in self.createdBranches:2760 self.createdBranches.add(branch)2761 parent = self.knownBranches[branch]2762if parent == branch:2763 parent =""2764else:2765 fullBranch = self.projectName + branch2766if fullBranch not in self.p4BranchesInGit:2767if not self.silent:2768print("\nImporting new branch%s"% fullBranch);2769if self.importNewBranch(branch, change -1):2770 parent =""2771 self.p4BranchesInGit.append(fullBranch)2772if not self.silent:2773print("\nResuming with change%s"% change);27742775if self.verbose:2776print"parent determined through known branches:%s"% parent27772778 branch = self.gitRefForBranch(branch)2779 parent = self.gitRefForBranch(parent)27802781if self.verbose:2782print"looking for initial parent for%s; current parent is%s"% (branch, parent)27832784iflen(parent) ==0and branch in self.initialParents:2785 parent = self.initialParents[branch]2786del self.initialParents[branch]27872788 blob =None2789iflen(parent) >0:2790 tempBranch ="%s/%d"% (self.tempBranchLocation, change)2791if self.verbose:2792print"Creating temporary branch: "+ tempBranch2793 self.commit(description, filesForCommit, tempBranch)2794 self.tempBranches.append(tempBranch)2795 self.checkpoint()2796 blob = self.searchParent(parent, branch, tempBranch)2797if blob:2798 self.commit(description, filesForCommit, branch, blob)2799else:2800if self.verbose:2801print"Parent of%snot found. Committing into head of%s"% (branch, parent)2802 self.commit(description, filesForCommit, branch, parent)2803else:2804 files = self.extractFilesFromCommit(description)2805 self.commit(description, files, self.branch,2806 self.initialParent)2807# only needed once, to connect to the previous commit2808 self.initialParent =""2809exceptIOError:2810print self.gitError.read()2811 sys.exit(1)28122813defimportHeadRevision(self, revision):2814print"Doing initial import of%sfrom revision%sinto%s"% (' '.join(self.depotPaths), revision, self.branch)28152816 details = {}2817 details["user"] ="git perforce import user"2818 details["desc"] = ("Initial import of%sfrom the state at revision%s\n"2819% (' '.join(self.depotPaths), revision))2820 details["change"] = revision2821 newestRevision =028222823 fileCnt =02824 fileArgs = ["%s...%s"% (p,revision)for p in self.depotPaths]28252826for info inp4CmdList(["files"] + fileArgs):28272828if'code'in info and info['code'] =='error':2829 sys.stderr.write("p4 returned an error:%s\n"2830% info['data'])2831if info['data'].find("must refer to client") >=0:2832 sys.stderr.write("This particular p4 error is misleading.\n")2833 sys.stderr.write("Perhaps the depot path was misspelled.\n");2834 sys.stderr.write("Depot path:%s\n"%" ".join(self.depotPaths))2835 sys.exit(1)2836if'p4ExitCode'in info:2837 sys.stderr.write("p4 exitcode:%s\n"% info['p4ExitCode'])2838 sys.exit(1)283928402841 change =int(info["change"])2842if change > newestRevision:2843 newestRevision = change28442845if info["action"]in self.delete_actions:2846# don't increase the file cnt, otherwise details["depotFile123"] will have gaps!2847#fileCnt = fileCnt + 12848continue28492850for prop in["depotFile","rev","action","type"]:2851 details["%s%s"% (prop, fileCnt)] = info[prop]28522853 fileCnt = fileCnt +128542855 details["change"] = newestRevision28562857# Use time from top-most change so that all git p4 clones of2858# the same p4 repo have the same commit SHA1s.2859 res =p4_describe(newestRevision)2860 details["time"] = res["time"]28612862 self.updateOptionDict(details)2863try:2864 self.commit(details, self.extractFilesFromCommit(details), self.branch)2865exceptIOError:2866print"IO error with git fast-import. Is your git version recent enough?"2867print self.gitError.read()286828692870defrun(self, args):2871 self.depotPaths = []2872 self.changeRange =""2873 self.previousDepotPaths = []2874 self.hasOrigin =False28752876# map from branch depot path to parent branch2877 self.knownBranches = {}2878 self.initialParents = {}28792880if self.importIntoRemotes:2881 self.refPrefix ="refs/remotes/p4/"2882else:2883 self.refPrefix ="refs/heads/p4/"28842885if self.syncWithOrigin:2886 self.hasOrigin =originP4BranchesExist()2887if self.hasOrigin:2888if not self.silent:2889print'Syncing with origin first, using "git fetch origin"'2890system("git fetch origin")28912892 branch_arg_given =bool(self.branch)2893iflen(self.branch) ==0:2894 self.branch = self.refPrefix +"master"2895ifgitBranchExists("refs/heads/p4")and self.importIntoRemotes:2896system("git update-ref%srefs/heads/p4"% self.branch)2897system("git branch -D p4")28982899# accept either the command-line option, or the configuration variable2900if self.useClientSpec:2901# will use this after clone to set the variable2902 self.useClientSpec_from_options =True2903else:2904ifgitConfigBool("git-p4.useclientspec"):2905 self.useClientSpec =True2906if self.useClientSpec:2907 self.clientSpecDirs =getClientSpec()29082909# TODO: should always look at previous commits,2910# merge with previous imports, if possible.2911if args == []:2912if self.hasOrigin:2913createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)29142915# branches holds mapping from branch name to sha12916 branches =p4BranchesInGit(self.importIntoRemotes)29172918# restrict to just this one, disabling detect-branches2919if branch_arg_given:2920 short = self.branch.split("/")[-1]2921if short in branches:2922 self.p4BranchesInGit = [ short ]2923else:2924 self.p4BranchesInGit = branches.keys()29252926iflen(self.p4BranchesInGit) >1:2927if not self.silent:2928print"Importing from/into multiple branches"2929 self.detectBranches =True2930for branch in branches.keys():2931 self.initialParents[self.refPrefix + branch] = \2932 branches[branch]29332934if self.verbose:2935print"branches:%s"% self.p4BranchesInGit29362937 p4Change =02938for branch in self.p4BranchesInGit:2939 logMsg =extractLogMessageFromGitCommit(self.refPrefix + branch)29402941 settings =extractSettingsGitLog(logMsg)29422943 self.readOptions(settings)2944if(settings.has_key('depot-paths')2945and settings.has_key('change')):2946 change =int(settings['change']) +12947 p4Change =max(p4Change, change)29482949 depotPaths =sorted(settings['depot-paths'])2950if self.previousDepotPaths == []:2951 self.previousDepotPaths = depotPaths2952else:2953 paths = []2954for(prev, cur)inzip(self.previousDepotPaths, depotPaths):2955 prev_list = prev.split("/")2956 cur_list = cur.split("/")2957for i inrange(0,min(len(cur_list),len(prev_list))):2958if cur_list[i] <> prev_list[i]:2959 i = i -12960break29612962 paths.append("/".join(cur_list[:i +1]))29632964 self.previousDepotPaths = paths29652966if p4Change >0:2967 self.depotPaths =sorted(self.previousDepotPaths)2968 self.changeRange ="@%s,#head"% p4Change2969if not self.silent and not self.detectBranches:2970print"Performing incremental import into%sgit branch"% self.branch29712972# accept multiple ref name abbreviations:2973# refs/foo/bar/branch -> use it exactly2974# p4/branch -> prepend refs/remotes/ or refs/heads/2975# branch -> prepend refs/remotes/p4/ or refs/heads/p4/2976if not self.branch.startswith("refs/"):2977if self.importIntoRemotes:2978 prepend ="refs/remotes/"2979else:2980 prepend ="refs/heads/"2981if not self.branch.startswith("p4/"):2982 prepend +="p4/"2983 self.branch = prepend + self.branch29842985iflen(args) ==0and self.depotPaths:2986if not self.silent:2987print"Depot paths:%s"%' '.join(self.depotPaths)2988else:2989if self.depotPaths and self.depotPaths != args:2990print("previous import used depot path%sand now%swas specified. "2991"This doesn't work!"% (' '.join(self.depotPaths),2992' '.join(args)))2993 sys.exit(1)29942995 self.depotPaths =sorted(args)29962997 revision =""2998 self.users = {}29993000# Make sure no revision specifiers are used when --changesfile3001# is specified.3002 bad_changesfile =False3003iflen(self.changesFile) >0:3004for p in self.depotPaths:3005if p.find("@") >=0or p.find("#") >=0:3006 bad_changesfile =True3007break3008if bad_changesfile:3009die("Option --changesfile is incompatible with revision specifiers")30103011 newPaths = []3012for p in self.depotPaths:3013if p.find("@") != -1:3014 atIdx = p.index("@")3015 self.changeRange = p[atIdx:]3016if self.changeRange =="@all":3017 self.changeRange =""3018elif','not in self.changeRange:3019 revision = self.changeRange3020 self.changeRange =""3021 p = p[:atIdx]3022elif p.find("#") != -1:3023 hashIdx = p.index("#")3024 revision = p[hashIdx:]3025 p = p[:hashIdx]3026elif self.previousDepotPaths == []:3027# pay attention to changesfile, if given, else import3028# the entire p4 tree at the head revision3029iflen(self.changesFile) ==0:3030 revision ="#head"30313032 p = re.sub("\.\.\.$","", p)3033if not p.endswith("/"):3034 p +="/"30353036 newPaths.append(p)30373038 self.depotPaths = newPaths30393040# --detect-branches may change this for each branch3041 self.branchPrefixes = self.depotPaths30423043 self.loadUserMapFromCache()3044 self.labels = {}3045if self.detectLabels:3046 self.getLabels();30473048if self.detectBranches:3049## FIXME - what's a P4 projectName ?3050 self.projectName = self.guessProjectName()30513052if self.hasOrigin:3053 self.getBranchMappingFromGitBranches()3054else:3055 self.getBranchMapping()3056if self.verbose:3057print"p4-git branches:%s"% self.p4BranchesInGit3058print"initial parents:%s"% self.initialParents3059for b in self.p4BranchesInGit:3060if b !="master":30613062## FIXME3063 b = b[len(self.projectName):]3064 self.createdBranches.add(b)30653066 self.tz ="%+03d%02d"% (- time.timezone /3600, ((- time.timezone %3600) /60))30673068 self.importProcess = subprocess.Popen(["git","fast-import"],3069 stdin=subprocess.PIPE,3070 stdout=subprocess.PIPE,3071 stderr=subprocess.PIPE);3072 self.gitOutput = self.importProcess.stdout3073 self.gitStream = self.importProcess.stdin3074 self.gitError = self.importProcess.stderr30753076if revision:3077 self.importHeadRevision(revision)3078else:3079 changes = []30803081iflen(self.changesFile) >0:3082 output =open(self.changesFile).readlines()3083 changeSet =set()3084for line in output:3085 changeSet.add(int(line))30863087for change in changeSet:3088 changes.append(change)30893090 changes.sort()3091else:3092# catch "git p4 sync" with no new branches, in a repo that3093# does not have any existing p4 branches3094iflen(args) ==0:3095if not self.p4BranchesInGit:3096die("No remote p4 branches. Perhaps you never did\"git p4 clone\"in here.")30973098# The default branch is master, unless --branch is used to3099# specify something else. Make sure it exists, or complain3100# nicely about how to use --branch.3101if not self.detectBranches:3102if notbranch_exists(self.branch):3103if branch_arg_given:3104die("Error: branch%sdoes not exist."% self.branch)3105else:3106die("Error: no branch%s; perhaps specify one with --branch."%3107 self.branch)31083109if self.verbose:3110print"Getting p4 changes for%s...%s"% (', '.join(self.depotPaths),3111 self.changeRange)3112 changes =p4ChangesForPaths(self.depotPaths, self.changeRange, self.changes_block_size)31133114iflen(self.maxChanges) >0:3115 changes = changes[:min(int(self.maxChanges),len(changes))]31163117iflen(changes) ==0:3118if not self.silent:3119print"No changes to import!"3120else:3121if not self.silent and not self.detectBranches:3122print"Import destination:%s"% self.branch31233124 self.updatedBranches =set()31253126if not self.detectBranches:3127if args:3128# start a new branch3129 self.initialParent =""3130else:3131# build on a previous revision3132 self.initialParent =parseRevision(self.branch)31333134 self.importChanges(changes)31353136if not self.silent:3137print""3138iflen(self.updatedBranches) >0:3139 sys.stdout.write("Updated branches: ")3140for b in self.updatedBranches:3141 sys.stdout.write("%s"% b)3142 sys.stdout.write("\n")31433144ifgitConfigBool("git-p4.importLabels"):3145 self.importLabels =True31463147if self.importLabels:3148 p4Labels =getP4Labels(self.depotPaths)3149 gitTags =getGitTags()31503151 missingP4Labels = p4Labels - gitTags3152 self.importP4Labels(self.gitStream, missingP4Labels)31533154 self.gitStream.close()3155if self.importProcess.wait() !=0:3156die("fast-import failed:%s"% self.gitError.read())3157 self.gitOutput.close()3158 self.gitError.close()31593160# Cleanup temporary branches created during import3161if self.tempBranches != []:3162for branch in self.tempBranches:3163read_pipe("git update-ref -d%s"% branch)3164 os.rmdir(os.path.join(os.environ.get("GIT_DIR",".git"), self.tempBranchLocation))31653166# Create a symbolic ref p4/HEAD pointing to p4/<branch> to allow3167# a convenient shortcut refname "p4".3168if self.importIntoRemotes:3169 head_ref = self.refPrefix +"HEAD"3170if notgitBranchExists(head_ref)andgitBranchExists(self.branch):3171system(["git","symbolic-ref", head_ref, self.branch])31723173return True31743175classP4Rebase(Command):3176def__init__(self):3177 Command.__init__(self)3178 self.options = [3179 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),3180]3181 self.importLabels =False3182 self.description = ("Fetches the latest revision from perforce and "3183+"rebases the current work (branch) against it")31843185defrun(self, args):3186 sync =P4Sync()3187 sync.importLabels = self.importLabels3188 sync.run([])31893190return self.rebase()31913192defrebase(self):3193if os.system("git update-index --refresh") !=0:3194die("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.");3195iflen(read_pipe("git diff-index HEAD --")) >0:3196die("You have uncommitted changes. Please commit them before rebasing or stash them away with git stash.");31973198[upstream, settings] =findUpstreamBranchPoint()3199iflen(upstream) ==0:3200die("Cannot find upstream branchpoint for rebase")32013202# the branchpoint may be p4/foo~3, so strip off the parent3203 upstream = re.sub("~[0-9]+$","", upstream)32043205print"Rebasing the current branch onto%s"% upstream3206 oldHead =read_pipe("git rev-parse HEAD").strip()3207system("git rebase%s"% upstream)3208system("git diff-tree --stat --summary -M%sHEAD --"% oldHead)3209return True32103211classP4Clone(P4Sync):3212def__init__(self):3213 P4Sync.__init__(self)3214 self.description ="Creates a new git repository and imports from Perforce into it"3215 self.usage ="usage: %prog [options] //depot/path[@revRange]"3216 self.options += [3217 optparse.make_option("--destination", dest="cloneDestination",3218 action='store', default=None,3219help="where to leave result of the clone"),3220 optparse.make_option("--bare", dest="cloneBare",3221 action="store_true", default=False),3222]3223 self.cloneDestination =None3224 self.needsGit =False3225 self.cloneBare =False32263227defdefaultDestination(self, args):3228## TODO: use common prefix of args?3229 depotPath = args[0]3230 depotDir = re.sub("(@[^@]*)$","", depotPath)3231 depotDir = re.sub("(#[^#]*)$","", depotDir)3232 depotDir = re.sub(r"\.\.\.$","", depotDir)3233 depotDir = re.sub(r"/$","", depotDir)3234return os.path.split(depotDir)[1]32353236defrun(self, args):3237iflen(args) <1:3238return False32393240if self.keepRepoPath and not self.cloneDestination:3241 sys.stderr.write("Must specify destination for --keep-path\n")3242 sys.exit(1)32433244 depotPaths = args32453246if not self.cloneDestination andlen(depotPaths) >1:3247 self.cloneDestination = depotPaths[-1]3248 depotPaths = depotPaths[:-1]32493250 self.cloneExclude = ["/"+p for p in self.cloneExclude]3251for p in depotPaths:3252if not p.startswith("//"):3253 sys.stderr.write('Depot paths must start with "//":%s\n'% p)3254return False32553256if not self.cloneDestination:3257 self.cloneDestination = self.defaultDestination(args)32583259print"Importing from%sinto%s"% (', '.join(depotPaths), self.cloneDestination)32603261if not os.path.exists(self.cloneDestination):3262 os.makedirs(self.cloneDestination)3263chdir(self.cloneDestination)32643265 init_cmd = ["git","init"]3266if self.cloneBare:3267 init_cmd.append("--bare")3268 retcode = subprocess.call(init_cmd)3269if retcode:3270raiseCalledProcessError(retcode, init_cmd)32713272if not P4Sync.run(self, depotPaths):3273return False32743275# create a master branch and check out a work tree3276ifgitBranchExists(self.branch):3277system(["git","branch","master", self.branch ])3278if not self.cloneBare:3279system(["git","checkout","-f"])3280else:3281print'Not checking out any branch, use ' \3282'"git checkout -q -b master <branch>"'32833284# auto-set this variable if invoked with --use-client-spec3285if self.useClientSpec_from_options:3286system("git config --bool git-p4.useclientspec true")32873288return True32893290classP4Branches(Command):3291def__init__(self):3292 Command.__init__(self)3293 self.options = [ ]3294 self.description = ("Shows the git branches that hold imports and their "3295+"corresponding perforce depot paths")3296 self.verbose =False32973298defrun(self, args):3299iforiginP4BranchesExist():3300createOrUpdateBranchesFromOrigin()33013302 cmdline ="git rev-parse --symbolic "3303 cmdline +=" --remotes"33043305for line inread_pipe_lines(cmdline):3306 line = line.strip()33073308if not line.startswith('p4/')or line =="p4/HEAD":3309continue3310 branch = line33113312 log =extractLogMessageFromGitCommit("refs/remotes/%s"% branch)3313 settings =extractSettingsGitLog(log)33143315print"%s<=%s(%s)"% (branch,",".join(settings["depot-paths"]), settings["change"])3316return True33173318classHelpFormatter(optparse.IndentedHelpFormatter):3319def__init__(self):3320 optparse.IndentedHelpFormatter.__init__(self)33213322defformat_description(self, description):3323if description:3324return description +"\n"3325else:3326return""33273328defprintUsage(commands):3329print"usage:%s<command> [options]"% sys.argv[0]3330print""3331print"valid commands:%s"%", ".join(commands)3332print""3333print"Try%s<command> --help for command specific help."% sys.argv[0]3334print""33353336commands = {3337"debug": P4Debug,3338"submit": P4Submit,3339"commit": P4Submit,3340"sync": P4Sync,3341"rebase": P4Rebase,3342"clone": P4Clone,3343"rollback": P4RollBack,3344"branches": P4Branches3345}334633473348defmain():3349iflen(sys.argv[1:]) ==0:3350printUsage(commands.keys())3351 sys.exit(2)33523353 cmdName = sys.argv[1]3354try:3355 klass = commands[cmdName]3356 cmd =klass()3357exceptKeyError:3358print"unknown command%s"% cmdName3359print""3360printUsage(commands.keys())3361 sys.exit(2)33623363 options = cmd.options3364 cmd.gitdir = os.environ.get("GIT_DIR",None)33653366 args = sys.argv[2:]33673368 options.append(optparse.make_option("--verbose","-v", dest="verbose", action="store_true"))3369if cmd.needsGit:3370 options.append(optparse.make_option("--git-dir", dest="gitdir"))33713372 parser = optparse.OptionParser(cmd.usage.replace("%prog","%prog "+ cmdName),3373 options,3374 description = cmd.description,3375 formatter =HelpFormatter())33763377(cmd, args) = parser.parse_args(sys.argv[2:], cmd);3378global verbose3379 verbose = cmd.verbose3380if cmd.needsGit:3381if cmd.gitdir ==None:3382 cmd.gitdir = os.path.abspath(".git")3383if notisValidGitDir(cmd.gitdir):3384 cmd.gitdir =read_pipe("git rev-parse --git-dir").strip()3385if os.path.exists(cmd.gitdir):3386 cdup =read_pipe("git rev-parse --show-cdup").strip()3387iflen(cdup) >0:3388chdir(cdup);33893390if notisValidGitDir(cmd.gitdir):3391ifisValidGitDir(cmd.gitdir +"/.git"):3392 cmd.gitdir +="/.git"3393else:3394die("fatal: cannot locate git repository at%s"% cmd.gitdir)33953396 os.environ["GIT_DIR"] = cmd.gitdir33973398if not cmd.run(args):3399 parser.print_help()3400 sys.exit(2)340134023403if __name__ =='__main__':3404main()