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 25import zipfile 26import zlib 27import ctypes 28import errno 29 30try: 31from subprocess import CalledProcessError 32exceptImportError: 33# from python2.7:subprocess.py 34# Exception classes used by this module. 35classCalledProcessError(Exception): 36"""This exception is raised when a process run by check_call() returns 37 a non-zero exit status. The exit status will be stored in the 38 returncode attribute.""" 39def__init__(self, returncode, cmd): 40 self.returncode = returncode 41 self.cmd = cmd 42def__str__(self): 43return"Command '%s' returned non-zero exit status%d"% (self.cmd, self.returncode) 44 45verbose =False 46 47# Only labels/tags matching this will be imported/exported 48defaultLabelRegexp = r'[a-zA-Z0-9_\-.]+$' 49 50# The block size is reduced automatically if required 51defaultBlockSize =1<<20 52 53p4_access_checked =False 54 55defp4_build_cmd(cmd): 56"""Build a suitable p4 command line. 57 58 This consolidates building and returning a p4 command line into one 59 location. It means that hooking into the environment, or other configuration 60 can be done more easily. 61 """ 62 real_cmd = ["p4"] 63 64 user =gitConfig("git-p4.user") 65iflen(user) >0: 66 real_cmd += ["-u",user] 67 68 password =gitConfig("git-p4.password") 69iflen(password) >0: 70 real_cmd += ["-P", password] 71 72 port =gitConfig("git-p4.port") 73iflen(port) >0: 74 real_cmd += ["-p", port] 75 76 host =gitConfig("git-p4.host") 77iflen(host) >0: 78 real_cmd += ["-H", host] 79 80 client =gitConfig("git-p4.client") 81iflen(client) >0: 82 real_cmd += ["-c", client] 83 84 retries =gitConfigInt("git-p4.retries") 85if retries is None: 86# Perform 3 retries by default 87 retries =3 88if retries >0: 89# Provide a way to not pass this option by setting git-p4.retries to 0 90 real_cmd += ["-r",str(retries)] 91 92ifisinstance(cmd,basestring): 93 real_cmd =' '.join(real_cmd) +' '+ cmd 94else: 95 real_cmd += cmd 96 97# now check that we can actually talk to the server 98global p4_access_checked 99if not p4_access_checked: 100 p4_access_checked =True# suppress access checks in p4_check_access itself 101p4_check_access() 102 103return real_cmd 104 105defgit_dir(path): 106""" Return TRUE if the given path is a git directory (/path/to/dir/.git). 107 This won't automatically add ".git" to a directory. 108 """ 109 d =read_pipe(["git","--git-dir", path,"rev-parse","--git-dir"],True).strip() 110if not d orlen(d) ==0: 111return None 112else: 113return d 114 115defchdir(path, is_client_path=False): 116"""Do chdir to the given path, and set the PWD environment 117 variable for use by P4. It does not look at getcwd() output. 118 Since we're not using the shell, it is necessary to set the 119 PWD environment variable explicitly. 120 121 Normally, expand the path to force it to be absolute. This 122 addresses the use of relative path names inside P4 settings, 123 e.g. P4CONFIG=.p4config. P4 does not simply open the filename 124 as given; it looks for .p4config using PWD. 125 126 If is_client_path, the path was handed to us directly by p4, 127 and may be a symbolic link. Do not call os.getcwd() in this 128 case, because it will cause p4 to think that PWD is not inside 129 the client path. 130 """ 131 132 os.chdir(path) 133if not is_client_path: 134 path = os.getcwd() 135 os.environ['PWD'] = path 136 137defcalcDiskFree(): 138"""Return free space in bytes on the disk of the given dirname.""" 139if platform.system() =='Windows': 140 free_bytes = ctypes.c_ulonglong(0) 141 ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(os.getcwd()),None,None, ctypes.pointer(free_bytes)) 142return free_bytes.value 143else: 144 st = os.statvfs(os.getcwd()) 145return st.f_bavail * st.f_frsize 146 147defdie(msg): 148if verbose: 149raiseException(msg) 150else: 151 sys.stderr.write(msg +"\n") 152 sys.exit(1) 153 154defwrite_pipe(c, stdin): 155if verbose: 156 sys.stderr.write('Writing pipe:%s\n'%str(c)) 157 158 expand =isinstance(c,basestring) 159 p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand) 160 pipe = p.stdin 161 val = pipe.write(stdin) 162 pipe.close() 163if p.wait(): 164die('Command failed:%s'%str(c)) 165 166return val 167 168defp4_write_pipe(c, stdin): 169 real_cmd =p4_build_cmd(c) 170returnwrite_pipe(real_cmd, stdin) 171 172defread_pipe_full(c): 173""" Read output from command. Returns a tuple 174 of the return status, stdout text and stderr 175 text. 176 """ 177if verbose: 178 sys.stderr.write('Reading pipe:%s\n'%str(c)) 179 180 expand =isinstance(c,basestring) 181 p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=expand) 182(out, err) = p.communicate() 183return(p.returncode, out, err) 184 185defread_pipe(c, ignore_error=False): 186""" Read output from command. Returns the output text on 187 success. On failure, terminates execution, unless 188 ignore_error is True, when it returns an empty string. 189 """ 190(retcode, out, err) =read_pipe_full(c) 191if retcode !=0: 192if ignore_error: 193 out ="" 194else: 195die('Command failed:%s\nError:%s'% (str(c), err)) 196return out 197 198defread_pipe_text(c): 199""" Read output from a command with trailing whitespace stripped. 200 On error, returns None. 201 """ 202(retcode, out, err) =read_pipe_full(c) 203if retcode !=0: 204return None 205else: 206return out.rstrip() 207 208defp4_read_pipe(c, ignore_error=False): 209 real_cmd =p4_build_cmd(c) 210returnread_pipe(real_cmd, ignore_error) 211 212defread_pipe_lines(c): 213if verbose: 214 sys.stderr.write('Reading pipe:%s\n'%str(c)) 215 216 expand =isinstance(c, basestring) 217 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand) 218 pipe = p.stdout 219 val = pipe.readlines() 220if pipe.close()or p.wait(): 221die('Command failed:%s'%str(c)) 222 223return val 224 225defp4_read_pipe_lines(c): 226"""Specifically invoke p4 on the command supplied. """ 227 real_cmd =p4_build_cmd(c) 228returnread_pipe_lines(real_cmd) 229 230defp4_has_command(cmd): 231"""Ask p4 for help on this command. If it returns an error, the 232 command does not exist in this version of p4.""" 233 real_cmd =p4_build_cmd(["help", cmd]) 234 p = subprocess.Popen(real_cmd, stdout=subprocess.PIPE, 235 stderr=subprocess.PIPE) 236 p.communicate() 237return p.returncode ==0 238 239defp4_has_move_command(): 240"""See if the move command exists, that it supports -k, and that 241 it has not been administratively disabled. The arguments 242 must be correct, but the filenames do not have to exist. Use 243 ones with wildcards so even if they exist, it will fail.""" 244 245if notp4_has_command("move"): 246return False 247 cmd =p4_build_cmd(["move","-k","@from","@to"]) 248 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 249(out, err) = p.communicate() 250# return code will be 1 in either case 251if err.find("Invalid option") >=0: 252return False 253if err.find("disabled") >=0: 254return False 255# assume it failed because @... was invalid changelist 256return True 257 258defsystem(cmd, ignore_error=False): 259 expand =isinstance(cmd,basestring) 260if verbose: 261 sys.stderr.write("executing%s\n"%str(cmd)) 262 retcode = subprocess.call(cmd, shell=expand) 263if retcode and not ignore_error: 264raiseCalledProcessError(retcode, cmd) 265 266return retcode 267 268defp4_system(cmd): 269"""Specifically invoke p4 as the system command. """ 270 real_cmd =p4_build_cmd(cmd) 271 expand =isinstance(real_cmd, basestring) 272 retcode = subprocess.call(real_cmd, shell=expand) 273if retcode: 274raiseCalledProcessError(retcode, real_cmd) 275 276defdie_bad_access(s): 277die("failure accessing depot:{0}".format(s.rstrip())) 278 279defp4_check_access(min_expiration=1): 280""" Check if we can access Perforce - account still logged in 281 """ 282 results =p4CmdList(["login","-s"]) 283 284iflen(results) ==0: 285# should never get here: always get either some results, or a p4ExitCode 286assert("could not parse response from perforce") 287 288 result = results[0] 289 290if'p4ExitCode'in result: 291# p4 returned non-zero status, e.g. P4PORT invalid, or p4 not in path 292die_bad_access("could not run p4") 293 294 code = result.get("code") 295if not code: 296# we get here if we couldn't connect and there was nothing to unmarshal 297die_bad_access("could not connect") 298 299elif code =="stat": 300 expiry = result.get("TicketExpiration") 301if expiry: 302 expiry =int(expiry) 303if expiry > min_expiration: 304# ok to carry on 305return 306else: 307die_bad_access("perforce ticket expires in{0}seconds".format(expiry)) 308 309else: 310# account without a timeout - all ok 311return 312 313elif code =="error": 314 data = result.get("data") 315if data: 316die_bad_access("p4 error:{0}".format(data)) 317else: 318die_bad_access("unknown error") 319else: 320die_bad_access("unknown error code{0}".format(code)) 321 322_p4_version_string =None 323defp4_version_string(): 324"""Read the version string, showing just the last line, which 325 hopefully is the interesting version bit. 326 327 $ p4 -V 328 Perforce - The Fast Software Configuration Management System. 329 Copyright 1995-2011 Perforce Software. All rights reserved. 330 Rev. P4/NTX86/2011.1/393975 (2011/12/16). 331 """ 332global _p4_version_string 333if not _p4_version_string: 334 a =p4_read_pipe_lines(["-V"]) 335 _p4_version_string = a[-1].rstrip() 336return _p4_version_string 337 338defp4_integrate(src, dest): 339p4_system(["integrate","-Dt",wildcard_encode(src),wildcard_encode(dest)]) 340 341defp4_sync(f, *options): 342p4_system(["sync"] +list(options) + [wildcard_encode(f)]) 343 344defp4_add(f): 345# forcibly add file names with wildcards 346ifwildcard_present(f): 347p4_system(["add","-f", f]) 348else: 349p4_system(["add", f]) 350 351defp4_delete(f): 352p4_system(["delete",wildcard_encode(f)]) 353 354defp4_edit(f, *options): 355p4_system(["edit"] +list(options) + [wildcard_encode(f)]) 356 357defp4_revert(f): 358p4_system(["revert",wildcard_encode(f)]) 359 360defp4_reopen(type, f): 361p4_system(["reopen","-t",type,wildcard_encode(f)]) 362 363defp4_reopen_in_change(changelist, files): 364 cmd = ["reopen","-c",str(changelist)] + files 365p4_system(cmd) 366 367defp4_move(src, dest): 368p4_system(["move","-k",wildcard_encode(src),wildcard_encode(dest)]) 369 370defp4_last_change(): 371 results =p4CmdList(["changes","-m","1"], skip_info=True) 372returnint(results[0]['change']) 373 374defp4_describe(change): 375"""Make sure it returns a valid result by checking for 376 the presence of field "time". Return a dict of the 377 results.""" 378 379 ds =p4CmdList(["describe","-s",str(change)], skip_info=True) 380iflen(ds) !=1: 381die("p4 describe -s%ddid not return 1 result:%s"% (change,str(ds))) 382 383 d = ds[0] 384 385if"p4ExitCode"in d: 386die("p4 describe -s%dexited with%d:%s"% (change, d["p4ExitCode"], 387str(d))) 388if"code"in d: 389if d["code"] =="error": 390die("p4 describe -s%dreturned error code:%s"% (change,str(d))) 391 392if"time"not in d: 393die("p4 describe -s%dreturned no\"time\":%s"% (change,str(d))) 394 395return d 396 397# 398# Canonicalize the p4 type and return a tuple of the 399# base type, plus any modifiers. See "p4 help filetypes" 400# for a list and explanation. 401# 402defsplit_p4_type(p4type): 403 404 p4_filetypes_historical = { 405"ctempobj":"binary+Sw", 406"ctext":"text+C", 407"cxtext":"text+Cx", 408"ktext":"text+k", 409"kxtext":"text+kx", 410"ltext":"text+F", 411"tempobj":"binary+FSw", 412"ubinary":"binary+F", 413"uresource":"resource+F", 414"uxbinary":"binary+Fx", 415"xbinary":"binary+x", 416"xltext":"text+Fx", 417"xtempobj":"binary+Swx", 418"xtext":"text+x", 419"xunicode":"unicode+x", 420"xutf16":"utf16+x", 421} 422if p4type in p4_filetypes_historical: 423 p4type = p4_filetypes_historical[p4type] 424 mods ="" 425 s = p4type.split("+") 426 base = s[0] 427 mods ="" 428iflen(s) >1: 429 mods = s[1] 430return(base, mods) 431 432# 433# return the raw p4 type of a file (text, text+ko, etc) 434# 435defp4_type(f): 436 results =p4CmdList(["fstat","-T","headType",wildcard_encode(f)]) 437return results[0]['headType'] 438 439# 440# Given a type base and modifier, return a regexp matching 441# the keywords that can be expanded in the file 442# 443defp4_keywords_regexp_for_type(base, type_mods): 444if base in("text","unicode","binary"): 445 kwords =None 446if"ko"in type_mods: 447 kwords ='Id|Header' 448elif"k"in type_mods: 449 kwords ='Id|Header|Author|Date|DateTime|Change|File|Revision' 450else: 451return None 452 pattern = r""" 453 \$ # Starts with a dollar, followed by... 454 (%s) # one of the keywords, followed by... 455 (:[^$\n]+)? # possibly an old expansion, followed by... 456 \$ # another dollar 457 """% kwords 458return pattern 459else: 460return None 461 462# 463# Given a file, return a regexp matching the possible 464# RCS keywords that will be expanded, or None for files 465# with kw expansion turned off. 466# 467defp4_keywords_regexp_for_file(file): 468if not os.path.exists(file): 469return None 470else: 471(type_base, type_mods) =split_p4_type(p4_type(file)) 472returnp4_keywords_regexp_for_type(type_base, type_mods) 473 474defsetP4ExecBit(file, mode): 475# Reopens an already open file and changes the execute bit to match 476# the execute bit setting in the passed in mode. 477 478 p4Type ="+x" 479 480if notisModeExec(mode): 481 p4Type =getP4OpenedType(file) 482 p4Type = re.sub('^([cku]?)x(.*)','\\1\\2', p4Type) 483 p4Type = re.sub('(.*?\+.*?)x(.*?)','\\1\\2', p4Type) 484if p4Type[-1] =="+": 485 p4Type = p4Type[0:-1] 486 487p4_reopen(p4Type,file) 488 489defgetP4OpenedType(file): 490# Returns the perforce file type for the given file. 491 492 result =p4_read_pipe(["opened",wildcard_encode(file)]) 493 match = re.match(".*\((.+)\)( \*exclusive\*)?\r?$", result) 494if match: 495return match.group(1) 496else: 497die("Could not determine file type for%s(result: '%s')"% (file, result)) 498 499# Return the set of all p4 labels 500defgetP4Labels(depotPaths): 501 labels =set() 502ifisinstance(depotPaths,basestring): 503 depotPaths = [depotPaths] 504 505for l inp4CmdList(["labels"] + ["%s..."% p for p in depotPaths]): 506 label = l['label'] 507 labels.add(label) 508 509return labels 510 511# Return the set of all git tags 512defgetGitTags(): 513 gitTags =set() 514for line inread_pipe_lines(["git","tag"]): 515 tag = line.strip() 516 gitTags.add(tag) 517return gitTags 518 519defdiffTreePattern(): 520# This is a simple generator for the diff tree regex pattern. This could be 521# a class variable if this and parseDiffTreeEntry were a part of a class. 522 pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)') 523while True: 524yield pattern 525 526defparseDiffTreeEntry(entry): 527"""Parses a single diff tree entry into its component elements. 528 529 See git-diff-tree(1) manpage for details about the format of the diff 530 output. This method returns a dictionary with the following elements: 531 532 src_mode - The mode of the source file 533 dst_mode - The mode of the destination file 534 src_sha1 - The sha1 for the source file 535 dst_sha1 - The sha1 fr the destination file 536 status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc) 537 status_score - The score for the status (applicable for 'C' and 'R' 538 statuses). This is None if there is no score. 539 src - The path for the source file. 540 dst - The path for the destination file. This is only present for 541 copy or renames. If it is not present, this is None. 542 543 If the pattern is not matched, None is returned.""" 544 545 match =diffTreePattern().next().match(entry) 546if match: 547return{ 548'src_mode': match.group(1), 549'dst_mode': match.group(2), 550'src_sha1': match.group(3), 551'dst_sha1': match.group(4), 552'status': match.group(5), 553'status_score': match.group(6), 554'src': match.group(7), 555'dst': match.group(10) 556} 557return None 558 559defisModeExec(mode): 560# Returns True if the given git mode represents an executable file, 561# otherwise False. 562return mode[-3:] =="755" 563 564classP4Exception(Exception): 565""" Base class for exceptions from the p4 client """ 566def__init__(self, exit_code): 567 self.p4ExitCode = exit_code 568 569classP4ServerException(P4Exception): 570""" Base class for exceptions where we get some kind of marshalled up result from the server """ 571def__init__(self, exit_code, p4_result): 572super(P4ServerException, self).__init__(exit_code) 573 self.p4_result = p4_result 574 self.code = p4_result[0]['code'] 575 self.data = p4_result[0]['data'] 576 577classP4RequestSizeException(P4ServerException): 578""" One of the maxresults or maxscanrows errors """ 579def__init__(self, exit_code, p4_result, limit): 580super(P4RequestSizeException, self).__init__(exit_code, p4_result) 581 self.limit = limit 582 583defisModeExecChanged(src_mode, dst_mode): 584returnisModeExec(src_mode) !=isModeExec(dst_mode) 585 586defp4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False, 587 errors_as_exceptions=False): 588 589ifisinstance(cmd,basestring): 590 cmd ="-G "+ cmd 591 expand =True 592else: 593 cmd = ["-G"] + cmd 594 expand =False 595 596 cmd =p4_build_cmd(cmd) 597if verbose: 598 sys.stderr.write("Opening pipe:%s\n"%str(cmd)) 599 600# Use a temporary file to avoid deadlocks without 601# subprocess.communicate(), which would put another copy 602# of stdout into memory. 603 stdin_file =None 604if stdin is not None: 605 stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode) 606ifisinstance(stdin,basestring): 607 stdin_file.write(stdin) 608else: 609for i in stdin: 610 stdin_file.write(i +'\n') 611 stdin_file.flush() 612 stdin_file.seek(0) 613 614 p4 = subprocess.Popen(cmd, 615 shell=expand, 616 stdin=stdin_file, 617 stdout=subprocess.PIPE) 618 619 result = [] 620try: 621while True: 622 entry = marshal.load(p4.stdout) 623if skip_info: 624if'code'in entry and entry['code'] =='info': 625continue 626if cb is not None: 627cb(entry) 628else: 629 result.append(entry) 630exceptEOFError: 631pass 632 exitCode = p4.wait() 633if exitCode !=0: 634if errors_as_exceptions: 635iflen(result) >0: 636 data = result[0].get('data') 637if data: 638 m = re.search('Too many rows scanned \(over (\d+)\)', data) 639if not m: 640 m = re.search('Request too large \(over (\d+)\)', data) 641 642if m: 643 limit =int(m.group(1)) 644raiseP4RequestSizeException(exitCode, result, limit) 645 646raiseP4ServerException(exitCode, result) 647else: 648raiseP4Exception(exitCode) 649else: 650 entry = {} 651 entry["p4ExitCode"] = exitCode 652 result.append(entry) 653 654return result 655 656defp4Cmd(cmd): 657list=p4CmdList(cmd) 658 result = {} 659for entry inlist: 660 result.update(entry) 661return result; 662 663defp4Where(depotPath): 664if not depotPath.endswith("/"): 665 depotPath +="/" 666 depotPathLong = depotPath +"..." 667 outputList =p4CmdList(["where", depotPathLong]) 668 output =None 669for entry in outputList: 670if"depotFile"in entry: 671# Search for the base client side depot path, as long as it starts with the branch's P4 path. 672# The base path always ends with "/...". 673if entry["depotFile"].find(depotPath) ==0and entry["depotFile"][-4:] =="/...": 674 output = entry 675break 676elif"data"in entry: 677 data = entry.get("data") 678 space = data.find(" ") 679if data[:space] == depotPath: 680 output = entry 681break 682if output ==None: 683return"" 684if output["code"] =="error": 685return"" 686 clientPath ="" 687if"path"in output: 688 clientPath = output.get("path") 689elif"data"in output: 690 data = output.get("data") 691 lastSpace = data.rfind(" ") 692 clientPath = data[lastSpace +1:] 693 694if clientPath.endswith("..."): 695 clientPath = clientPath[:-3] 696return clientPath 697 698defcurrentGitBranch(): 699returnread_pipe_text(["git","symbolic-ref","--short","-q","HEAD"]) 700 701defisValidGitDir(path): 702returngit_dir(path) !=None 703 704defparseRevision(ref): 705returnread_pipe("git rev-parse%s"% ref).strip() 706 707defbranchExists(ref): 708 rev =read_pipe(["git","rev-parse","-q","--verify", ref], 709 ignore_error=True) 710returnlen(rev) >0 711 712defextractLogMessageFromGitCommit(commit): 713 logMessage ="" 714 715## fixme: title is first line of commit, not 1st paragraph. 716 foundTitle =False 717for log inread_pipe_lines("git cat-file commit%s"% commit): 718if not foundTitle: 719iflen(log) ==1: 720 foundTitle =True 721continue 722 723 logMessage += log 724return logMessage 725 726defextractSettingsGitLog(log): 727 values = {} 728for line in log.split("\n"): 729 line = line.strip() 730 m = re.search(r"^ *\[git-p4: (.*)\]$", line) 731if not m: 732continue 733 734 assignments = m.group(1).split(':') 735for a in assignments: 736 vals = a.split('=') 737 key = vals[0].strip() 738 val = ('='.join(vals[1:])).strip() 739if val.endswith('\"')and val.startswith('"'): 740 val = val[1:-1] 741 742 values[key] = val 743 744 paths = values.get("depot-paths") 745if not paths: 746 paths = values.get("depot-path") 747if paths: 748 values['depot-paths'] = paths.split(',') 749return values 750 751defgitBranchExists(branch): 752 proc = subprocess.Popen(["git","rev-parse", branch], 753 stderr=subprocess.PIPE, stdout=subprocess.PIPE); 754return proc.wait() ==0; 755 756_gitConfig = {} 757 758defgitConfig(key, typeSpecifier=None): 759if not _gitConfig.has_key(key): 760 cmd = ["git","config"] 761if typeSpecifier: 762 cmd += [ typeSpecifier ] 763 cmd += [ key ] 764 s =read_pipe(cmd, ignore_error=True) 765 _gitConfig[key] = s.strip() 766return _gitConfig[key] 767 768defgitConfigBool(key): 769"""Return a bool, using git config --bool. It is True only if the 770 variable is set to true, and False if set to false or not present 771 in the config.""" 772 773if not _gitConfig.has_key(key): 774 _gitConfig[key] =gitConfig(key,'--bool') =="true" 775return _gitConfig[key] 776 777defgitConfigInt(key): 778if not _gitConfig.has_key(key): 779 cmd = ["git","config","--int", key ] 780 s =read_pipe(cmd, ignore_error=True) 781 v = s.strip() 782try: 783 _gitConfig[key] =int(gitConfig(key,'--int')) 784exceptValueError: 785 _gitConfig[key] =None 786return _gitConfig[key] 787 788defgitConfigList(key): 789if not _gitConfig.has_key(key): 790 s =read_pipe(["git","config","--get-all", key], ignore_error=True) 791 _gitConfig[key] = s.strip().splitlines() 792if _gitConfig[key] == ['']: 793 _gitConfig[key] = [] 794return _gitConfig[key] 795 796defp4BranchesInGit(branchesAreInRemotes=True): 797"""Find all the branches whose names start with "p4/", looking 798 in remotes or heads as specified by the argument. Return 799 a dictionary of{ branch: revision }for each one found. 800 The branch names are the short names, without any 801 "p4/" prefix.""" 802 803 branches = {} 804 805 cmdline ="git rev-parse --symbolic " 806if branchesAreInRemotes: 807 cmdline +="--remotes" 808else: 809 cmdline +="--branches" 810 811for line inread_pipe_lines(cmdline): 812 line = line.strip() 813 814# only import to p4/ 815if not line.startswith('p4/'): 816continue 817# special symbolic ref to p4/master 818if line =="p4/HEAD": 819continue 820 821# strip off p4/ prefix 822 branch = line[len("p4/"):] 823 824 branches[branch] =parseRevision(line) 825 826return branches 827 828defbranch_exists(branch): 829"""Make sure that the given ref name really exists.""" 830 831 cmd = ["git","rev-parse","--symbolic","--verify", branch ] 832 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 833 out, _ = p.communicate() 834if p.returncode: 835return False 836# expect exactly one line of output: the branch name 837return out.rstrip() == branch 838 839deffindUpstreamBranchPoint(head ="HEAD"): 840 branches =p4BranchesInGit() 841# map from depot-path to branch name 842 branchByDepotPath = {} 843for branch in branches.keys(): 844 tip = branches[branch] 845 log =extractLogMessageFromGitCommit(tip) 846 settings =extractSettingsGitLog(log) 847if settings.has_key("depot-paths"): 848 paths =",".join(settings["depot-paths"]) 849 branchByDepotPath[paths] ="remotes/p4/"+ branch 850 851 settings =None 852 parent =0 853while parent <65535: 854 commit = head +"~%s"% parent 855 log =extractLogMessageFromGitCommit(commit) 856 settings =extractSettingsGitLog(log) 857if settings.has_key("depot-paths"): 858 paths =",".join(settings["depot-paths"]) 859if branchByDepotPath.has_key(paths): 860return[branchByDepotPath[paths], settings] 861 862 parent = parent +1 863 864return["", settings] 865 866defcreateOrUpdateBranchesFromOrigin(localRefPrefix ="refs/remotes/p4/", silent=True): 867if not silent: 868print("Creating/updating branch(es) in%sbased on origin branch(es)" 869% localRefPrefix) 870 871 originPrefix ="origin/p4/" 872 873for line inread_pipe_lines("git rev-parse --symbolic --remotes"): 874 line = line.strip() 875if(not line.startswith(originPrefix))or line.endswith("HEAD"): 876continue 877 878 headName = line[len(originPrefix):] 879 remoteHead = localRefPrefix + headName 880 originHead = line 881 882 original =extractSettingsGitLog(extractLogMessageFromGitCommit(originHead)) 883if(not original.has_key('depot-paths') 884or not original.has_key('change')): 885continue 886 887 update =False 888if notgitBranchExists(remoteHead): 889if verbose: 890print"creating%s"% remoteHead 891 update =True 892else: 893 settings =extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead)) 894if settings.has_key('change') >0: 895if settings['depot-paths'] == original['depot-paths']: 896 originP4Change =int(original['change']) 897 p4Change =int(settings['change']) 898if originP4Change > p4Change: 899print("%s(%s) is newer than%s(%s). " 900"Updating p4 branch from origin." 901% (originHead, originP4Change, 902 remoteHead, p4Change)) 903 update =True 904else: 905print("Ignoring:%swas imported from%swhile " 906"%swas imported from%s" 907% (originHead,','.join(original['depot-paths']), 908 remoteHead,','.join(settings['depot-paths']))) 909 910if update: 911system("git update-ref%s %s"% (remoteHead, originHead)) 912 913deforiginP4BranchesExist(): 914returngitBranchExists("origin")orgitBranchExists("origin/p4")orgitBranchExists("origin/p4/master") 915 916 917defp4ParseNumericChangeRange(parts): 918 changeStart =int(parts[0][1:]) 919if parts[1] =='#head': 920 changeEnd =p4_last_change() 921else: 922 changeEnd =int(parts[1]) 923 924return(changeStart, changeEnd) 925 926defchooseBlockSize(blockSize): 927if blockSize: 928return blockSize 929else: 930return defaultBlockSize 931 932defp4ChangesForPaths(depotPaths, changeRange, requestedBlockSize): 933assert depotPaths 934 935# Parse the change range into start and end. Try to find integer 936# revision ranges as these can be broken up into blocks to avoid 937# hitting server-side limits (maxrows, maxscanresults). But if 938# that doesn't work, fall back to using the raw revision specifier 939# strings, without using block mode. 940 941if changeRange is None or changeRange =='': 942 changeStart =1 943 changeEnd =p4_last_change() 944 block_size =chooseBlockSize(requestedBlockSize) 945else: 946 parts = changeRange.split(',') 947assertlen(parts) ==2 948try: 949(changeStart, changeEnd) =p4ParseNumericChangeRange(parts) 950 block_size =chooseBlockSize(requestedBlockSize) 951exceptValueError: 952 changeStart = parts[0][1:] 953 changeEnd = parts[1] 954if requestedBlockSize: 955die("cannot use --changes-block-size with non-numeric revisions") 956 block_size =None 957 958 changes =set() 959 960# Retrieve changes a block at a time, to prevent running 961# into a MaxResults/MaxScanRows error from the server. If 962# we _do_ hit one of those errors, turn down the block size 963 964while True: 965 cmd = ['changes'] 966 967if block_size: 968 end =min(changeEnd, changeStart + block_size) 969 revisionRange ="%d,%d"% (changeStart, end) 970else: 971 revisionRange ="%s,%s"% (changeStart, changeEnd) 972 973for p in depotPaths: 974 cmd += ["%s...@%s"% (p, revisionRange)] 975 976# fetch the changes 977try: 978 result =p4CmdList(cmd, errors_as_exceptions=True) 979except P4RequestSizeException as e: 980if not block_size: 981 block_size = e.limit 982elif block_size > e.limit: 983 block_size = e.limit 984else: 985 block_size =max(2, block_size //2) 986 987if verbose:print("block size error, retrying with block size{0}".format(block_size)) 988continue 989except P4Exception as e: 990die('Error retrieving changes description ({0})'.format(e.p4ExitCode)) 991 992# Insert changes in chronological order 993for entry inreversed(result): 994if not entry.has_key('change'): 995continue 996 changes.add(int(entry['change'])) 997 998if not block_size: 999break10001001if end >= changeEnd:1002break10031004 changeStart = end +110051006 changes =sorted(changes)1007return changes10081009defp4PathStartsWith(path, prefix):1010# This method tries to remedy a potential mixed-case issue:1011#1012# If UserA adds //depot/DirA/file11013# and UserB adds //depot/dira/file21014#1015# we may or may not have a problem. If you have core.ignorecase=true,1016# we treat DirA and dira as the same directory1017ifgitConfigBool("core.ignorecase"):1018return path.lower().startswith(prefix.lower())1019return path.startswith(prefix)10201021defgetClientSpec():1022"""Look at the p4 client spec, create a View() object that contains1023 all the mappings, and return it."""10241025 specList =p4CmdList("client -o")1026iflen(specList) !=1:1027die('Output from "client -o" is%dlines, expecting 1'%1028len(specList))10291030# dictionary of all client parameters1031 entry = specList[0]10321033# the //client/ name1034 client_name = entry["Client"]10351036# just the keys that start with "View"1037 view_keys = [ k for k in entry.keys()if k.startswith("View") ]10381039# hold this new View1040 view =View(client_name)10411042# append the lines, in order, to the view1043for view_num inrange(len(view_keys)):1044 k ="View%d"% view_num1045if k not in view_keys:1046die("Expected view key%smissing"% k)1047 view.append(entry[k])10481049return view10501051defgetClientRoot():1052"""Grab the client directory."""10531054 output =p4CmdList("client -o")1055iflen(output) !=1:1056die('Output from "client -o" is%dlines, expecting 1'%len(output))10571058 entry = output[0]1059if"Root"not in entry:1060die('Client has no "Root"')10611062return entry["Root"]10631064#1065# P4 wildcards are not allowed in filenames. P4 complains1066# if you simply add them, but you can force it with "-f", in1067# which case it translates them into %xx encoding internally.1068#1069defwildcard_decode(path):1070# Search for and fix just these four characters. Do % last so1071# that fixing it does not inadvertently create new %-escapes.1072# Cannot have * in a filename in windows; untested as to1073# what p4 would do in such a case.1074if not platform.system() =="Windows":1075 path = path.replace("%2A","*")1076 path = path.replace("%23","#") \1077.replace("%40","@") \1078.replace("%25","%")1079return path10801081defwildcard_encode(path):1082# do % first to avoid double-encoding the %s introduced here1083 path = path.replace("%","%25") \1084.replace("*","%2A") \1085.replace("#","%23") \1086.replace("@","%40")1087return path10881089defwildcard_present(path):1090 m = re.search("[*#@%]", path)1091return m is not None10921093classLargeFileSystem(object):1094"""Base class for large file system support."""10951096def__init__(self, writeToGitStream):1097 self.largeFiles =set()1098 self.writeToGitStream = writeToGitStream10991100defgeneratePointer(self, cloneDestination, contentFile):1101"""Return the content of a pointer file that is stored in Git instead of1102 the actual content."""1103assert False,"Method 'generatePointer' required in "+ self.__class__.__name__11041105defpushFile(self, localLargeFile):1106"""Push the actual content which is not stored in the Git repository to1107 a server."""1108assert False,"Method 'pushFile' required in "+ self.__class__.__name__11091110defhasLargeFileExtension(self, relPath):1111returnreduce(1112lambda a, b: a or b,1113[relPath.endswith('.'+ e)for e ingitConfigList('git-p4.largeFileExtensions')],1114False1115)11161117defgenerateTempFile(self, contents):1118 contentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=False)1119for d in contents:1120 contentFile.write(d)1121 contentFile.close()1122return contentFile.name11231124defexceedsLargeFileThreshold(self, relPath, contents):1125ifgitConfigInt('git-p4.largeFileThreshold'):1126 contentsSize =sum(len(d)for d in contents)1127if contentsSize >gitConfigInt('git-p4.largeFileThreshold'):1128return True1129ifgitConfigInt('git-p4.largeFileCompressedThreshold'):1130 contentsSize =sum(len(d)for d in contents)1131if contentsSize <=gitConfigInt('git-p4.largeFileCompressedThreshold'):1132return False1133 contentTempFile = self.generateTempFile(contents)1134 compressedContentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=False)1135 zf = zipfile.ZipFile(compressedContentFile.name, mode='w')1136 zf.write(contentTempFile, compress_type=zipfile.ZIP_DEFLATED)1137 zf.close()1138 compressedContentsSize = zf.infolist()[0].compress_size1139 os.remove(contentTempFile)1140 os.remove(compressedContentFile.name)1141if compressedContentsSize >gitConfigInt('git-p4.largeFileCompressedThreshold'):1142return True1143return False11441145defaddLargeFile(self, relPath):1146 self.largeFiles.add(relPath)11471148defremoveLargeFile(self, relPath):1149 self.largeFiles.remove(relPath)11501151defisLargeFile(self, relPath):1152return relPath in self.largeFiles11531154defprocessContent(self, git_mode, relPath, contents):1155"""Processes the content of git fast import. This method decides if a1156 file is stored in the large file system and handles all necessary1157 steps."""1158if self.exceedsLargeFileThreshold(relPath, contents)or self.hasLargeFileExtension(relPath):1159 contentTempFile = self.generateTempFile(contents)1160(pointer_git_mode, contents, localLargeFile) = self.generatePointer(contentTempFile)1161if pointer_git_mode:1162 git_mode = pointer_git_mode1163if localLargeFile:1164# Move temp file to final location in large file system1165 largeFileDir = os.path.dirname(localLargeFile)1166if not os.path.isdir(largeFileDir):1167 os.makedirs(largeFileDir)1168 shutil.move(contentTempFile, localLargeFile)1169 self.addLargeFile(relPath)1170ifgitConfigBool('git-p4.largeFilePush'):1171 self.pushFile(localLargeFile)1172if verbose:1173 sys.stderr.write("%smoved to large file system (%s)\n"% (relPath, localLargeFile))1174return(git_mode, contents)11751176classMockLFS(LargeFileSystem):1177"""Mock large file system for testing."""11781179defgeneratePointer(self, contentFile):1180"""The pointer content is the original content prefixed with "pointer-".1181 The local filename of the large file storage is derived from the file content.1182 """1183withopen(contentFile,'r')as f:1184 content =next(f)1185 gitMode ='100644'1186 pointerContents ='pointer-'+ content1187 localLargeFile = os.path.join(os.getcwd(),'.git','mock-storage','local', content[:-1])1188return(gitMode, pointerContents, localLargeFile)11891190defpushFile(self, localLargeFile):1191"""The remote filename of the large file storage is the same as the local1192 one but in a different directory.1193 """1194 remotePath = os.path.join(os.path.dirname(localLargeFile),'..','remote')1195if not os.path.exists(remotePath):1196 os.makedirs(remotePath)1197 shutil.copyfile(localLargeFile, os.path.join(remotePath, os.path.basename(localLargeFile)))11981199classGitLFS(LargeFileSystem):1200"""Git LFS as backend for the git-p4 large file system.1201 See https://git-lfs.github.com/ for details."""12021203def__init__(self, *args):1204 LargeFileSystem.__init__(self, *args)1205 self.baseGitAttributes = []12061207defgeneratePointer(self, contentFile):1208"""Generate a Git LFS pointer for the content. Return LFS Pointer file1209 mode and content which is stored in the Git repository instead of1210 the actual content. Return also the new location of the actual1211 content.1212 """1213if os.path.getsize(contentFile) ==0:1214return(None,'',None)12151216 pointerProcess = subprocess.Popen(1217['git','lfs','pointer','--file='+ contentFile],1218 stdout=subprocess.PIPE1219)1220 pointerFile = pointerProcess.stdout.read()1221if pointerProcess.wait():1222 os.remove(contentFile)1223die('git-lfs pointer command failed. Did you install the extension?')12241225# Git LFS removed the preamble in the output of the 'pointer' command1226# starting from version 1.2.0. Check for the preamble here to support1227# earlier versions.1228# c.f. https://github.com/github/git-lfs/commit/da2935d9a739592bc775c98d8ef4df9c72ea3b431229if pointerFile.startswith('Git LFS pointer for'):1230 pointerFile = re.sub(r'Git LFS pointer for.*\n\n','', pointerFile)12311232 oid = re.search(r'^oid \w+:(\w+)', pointerFile, re.MULTILINE).group(1)1233 localLargeFile = os.path.join(1234 os.getcwd(),1235'.git','lfs','objects', oid[:2], oid[2:4],1236 oid,1237)1238# LFS Spec states that pointer files should not have the executable bit set.1239 gitMode ='100644'1240return(gitMode, pointerFile, localLargeFile)12411242defpushFile(self, localLargeFile):1243 uploadProcess = subprocess.Popen(1244['git','lfs','push','--object-id','origin', os.path.basename(localLargeFile)]1245)1246if uploadProcess.wait():1247die('git-lfs push command failed. Did you define a remote?')12481249defgenerateGitAttributes(self):1250return(1251 self.baseGitAttributes +1252[1253'\n',1254'#\n',1255'# Git LFS (see https://git-lfs.github.com/)\n',1256'#\n',1257] +1258['*.'+ f.replace(' ','[[:space:]]') +' filter=lfs diff=lfs merge=lfs -text\n'1259for f insorted(gitConfigList('git-p4.largeFileExtensions'))1260] +1261['/'+ f.replace(' ','[[:space:]]') +' filter=lfs diff=lfs merge=lfs -text\n'1262for f insorted(self.largeFiles)if not self.hasLargeFileExtension(f)1263]1264)12651266defaddLargeFile(self, relPath):1267 LargeFileSystem.addLargeFile(self, relPath)1268 self.writeToGitStream('100644','.gitattributes', self.generateGitAttributes())12691270defremoveLargeFile(self, relPath):1271 LargeFileSystem.removeLargeFile(self, relPath)1272 self.writeToGitStream('100644','.gitattributes', self.generateGitAttributes())12731274defprocessContent(self, git_mode, relPath, contents):1275if relPath =='.gitattributes':1276 self.baseGitAttributes = contents1277return(git_mode, self.generateGitAttributes())1278else:1279return LargeFileSystem.processContent(self, git_mode, relPath, contents)12801281class Command:1282def__init__(self):1283 self.usage ="usage: %prog [options]"1284 self.needsGit =True1285 self.verbose =False12861287# This is required for the "append" cloneExclude action1288defensure_value(self, attr, value):1289if nothasattr(self, attr)orgetattr(self, attr)is None:1290setattr(self, attr, value)1291returngetattr(self, attr)12921293class P4UserMap:1294def__init__(self):1295 self.userMapFromPerforceServer =False1296 self.myP4UserId =None12971298defp4UserId(self):1299if self.myP4UserId:1300return self.myP4UserId13011302 results =p4CmdList("user -o")1303for r in results:1304if r.has_key('User'):1305 self.myP4UserId = r['User']1306return r['User']1307die("Could not find your p4 user id")13081309defp4UserIsMe(self, p4User):1310# return True if the given p4 user is actually me1311 me = self.p4UserId()1312if not p4User or p4User != me:1313return False1314else:1315return True13161317defgetUserCacheFilename(self):1318 home = os.environ.get("HOME", os.environ.get("USERPROFILE"))1319return home +"/.gitp4-usercache.txt"13201321defgetUserMapFromPerforceServer(self):1322if self.userMapFromPerforceServer:1323return1324 self.users = {}1325 self.emails = {}13261327for output inp4CmdList("users"):1328if not output.has_key("User"):1329continue1330 self.users[output["User"]] = output["FullName"] +" <"+ output["Email"] +">"1331 self.emails[output["Email"]] = output["User"]13321333 mapUserConfigRegex = re.compile(r"^\s*(\S+)\s*=\s*(.+)\s*<(\S+)>\s*$", re.VERBOSE)1334for mapUserConfig ingitConfigList("git-p4.mapUser"):1335 mapUser = mapUserConfigRegex.findall(mapUserConfig)1336if mapUser andlen(mapUser[0]) ==3:1337 user = mapUser[0][0]1338 fullname = mapUser[0][1]1339 email = mapUser[0][2]1340 self.users[user] = fullname +" <"+ email +">"1341 self.emails[email] = user13421343 s =''1344for(key, val)in self.users.items():1345 s +="%s\t%s\n"% (key.expandtabs(1), val.expandtabs(1))13461347open(self.getUserCacheFilename(),"wb").write(s)1348 self.userMapFromPerforceServer =True13491350defloadUserMapFromCache(self):1351 self.users = {}1352 self.userMapFromPerforceServer =False1353try:1354 cache =open(self.getUserCacheFilename(),"rb")1355 lines = cache.readlines()1356 cache.close()1357for line in lines:1358 entry = line.strip().split("\t")1359 self.users[entry[0]] = entry[1]1360exceptIOError:1361 self.getUserMapFromPerforceServer()13621363classP4Debug(Command):1364def__init__(self):1365 Command.__init__(self)1366 self.options = []1367 self.description ="A tool to debug the output of p4 -G."1368 self.needsGit =False13691370defrun(self, args):1371 j =01372for output inp4CmdList(args):1373print'Element:%d'% j1374 j +=11375print output1376return True13771378classP4RollBack(Command):1379def__init__(self):1380 Command.__init__(self)1381 self.options = [1382 optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true")1383]1384 self.description ="A tool to debug the multi-branch import. Don't use :)"1385 self.rollbackLocalBranches =False13861387defrun(self, args):1388iflen(args) !=1:1389return False1390 maxChange =int(args[0])13911392if"p4ExitCode"inp4Cmd("changes -m 1"):1393die("Problems executing p4");13941395if self.rollbackLocalBranches:1396 refPrefix ="refs/heads/"1397 lines =read_pipe_lines("git rev-parse --symbolic --branches")1398else:1399 refPrefix ="refs/remotes/"1400 lines =read_pipe_lines("git rev-parse --symbolic --remotes")14011402for line in lines:1403if self.rollbackLocalBranches or(line.startswith("p4/")and line !="p4/HEAD\n"):1404 line = line.strip()1405 ref = refPrefix + line1406 log =extractLogMessageFromGitCommit(ref)1407 settings =extractSettingsGitLog(log)14081409 depotPaths = settings['depot-paths']1410 change = settings['change']14111412 changed =False14131414iflen(p4Cmd("changes -m 1 "+' '.join(['%s...@%s'% (p, maxChange)1415for p in depotPaths]))) ==0:1416print"Branch%sdid not exist at change%s, deleting."% (ref, maxChange)1417system("git update-ref -d%s`git rev-parse%s`"% (ref, ref))1418continue14191420while change andint(change) > maxChange:1421 changed =True1422if self.verbose:1423print"%sis at%s; rewinding towards%s"% (ref, change, maxChange)1424system("git update-ref%s\"%s^\""% (ref, ref))1425 log =extractLogMessageFromGitCommit(ref)1426 settings =extractSettingsGitLog(log)142714281429 depotPaths = settings['depot-paths']1430 change = settings['change']14311432if changed:1433print"%srewound to%s"% (ref, change)14341435return True14361437classP4Submit(Command, P4UserMap):14381439 conflict_behavior_choices = ("ask","skip","quit")14401441def__init__(self):1442 Command.__init__(self)1443 P4UserMap.__init__(self)1444 self.options = [1445 optparse.make_option("--origin", dest="origin"),1446 optparse.make_option("-M", dest="detectRenames", action="store_true"),1447# preserve the user, requires relevant p4 permissions1448 optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),1449 optparse.make_option("--export-labels", dest="exportLabels", action="store_true"),1450 optparse.make_option("--dry-run","-n", dest="dry_run", action="store_true"),1451 optparse.make_option("--prepare-p4-only", dest="prepare_p4_only", action="store_true"),1452 optparse.make_option("--conflict", dest="conflict_behavior",1453 choices=self.conflict_behavior_choices),1454 optparse.make_option("--branch", dest="branch"),1455 optparse.make_option("--shelve", dest="shelve", action="store_true",1456help="Shelve instead of submit. Shelved files are reverted, "1457"restoring the workspace to the state before the shelve"),1458 optparse.make_option("--update-shelve", dest="update_shelve", action="append",type="int",1459 metavar="CHANGELIST",1460help="update an existing shelved changelist, implies --shelve, "1461"repeat in-order for multiple shelved changelists"),1462 optparse.make_option("--commit", dest="commit", metavar="COMMIT",1463help="submit only the specified commit(s), one commit or xxx..xxx"),1464 optparse.make_option("--disable-rebase", dest="disable_rebase", action="store_true",1465help="Disable rebase after submit is completed. Can be useful if you "1466"work from a local git branch that is not master"),1467 optparse.make_option("--disable-p4sync", dest="disable_p4sync", action="store_true",1468help="Skip Perforce sync of p4/master after submit or shelve"),1469]1470 self.description ="Submit changes from git to the perforce depot."1471 self.usage +=" [name of git branch to submit into perforce depot]"1472 self.origin =""1473 self.detectRenames =False1474 self.preserveUser =gitConfigBool("git-p4.preserveUser")1475 self.dry_run =False1476 self.shelve =False1477 self.update_shelve =list()1478 self.commit =""1479 self.disable_rebase =gitConfigBool("git-p4.disableRebase")1480 self.disable_p4sync =gitConfigBool("git-p4.disableP4Sync")1481 self.prepare_p4_only =False1482 self.conflict_behavior =None1483 self.isWindows = (platform.system() =="Windows")1484 self.exportLabels =False1485 self.p4HasMoveCommand =p4_has_move_command()1486 self.branch =None14871488ifgitConfig('git-p4.largeFileSystem'):1489die("Large file system not supported for git-p4 submit command. Please remove it from config.")14901491defcheck(self):1492iflen(p4CmdList("opened ...")) >0:1493die("You have files opened with perforce! Close them before starting the sync.")14941495defseparate_jobs_from_description(self, message):1496"""Extract and return a possible Jobs field in the commit1497 message. It goes into a separate section in the p4 change1498 specification.14991500 A jobs line starts with "Jobs:" and looks like a new field1501 in a form. Values are white-space separated on the same1502 line or on following lines that start with a tab.15031504 This does not parse and extract the full git commit message1505 like a p4 form. It just sees the Jobs: line as a marker1506 to pass everything from then on directly into the p4 form,1507 but outside the description section.15081509 Return a tuple (stripped log message, jobs string)."""15101511 m = re.search(r'^Jobs:', message, re.MULTILINE)1512if m is None:1513return(message,None)15141515 jobtext = message[m.start():]1516 stripped_message = message[:m.start()].rstrip()1517return(stripped_message, jobtext)15181519defprepareLogMessage(self, template, message, jobs):1520"""Edits the template returned from "p4 change -o" to insert1521 the message in the Description field, and the jobs text in1522 the Jobs field."""1523 result =""15241525 inDescriptionSection =False15261527for line in template.split("\n"):1528if line.startswith("#"):1529 result += line +"\n"1530continue15311532if inDescriptionSection:1533if line.startswith("Files:")or line.startswith("Jobs:"):1534 inDescriptionSection =False1535# insert Jobs section1536if jobs:1537 result += jobs +"\n"1538else:1539continue1540else:1541if line.startswith("Description:"):1542 inDescriptionSection =True1543 line +="\n"1544for messageLine in message.split("\n"):1545 line +="\t"+ messageLine +"\n"15461547 result += line +"\n"15481549return result15501551defpatchRCSKeywords(self,file, pattern):1552# Attempt to zap the RCS keywords in a p4 controlled file matching the given pattern1553(handle, outFileName) = tempfile.mkstemp(dir='.')1554try:1555 outFile = os.fdopen(handle,"w+")1556 inFile =open(file,"r")1557 regexp = re.compile(pattern, re.VERBOSE)1558for line in inFile.readlines():1559 line = regexp.sub(r'$\1$', line)1560 outFile.write(line)1561 inFile.close()1562 outFile.close()1563# Forcibly overwrite the original file1564 os.unlink(file)1565 shutil.move(outFileName,file)1566except:1567# cleanup our temporary file1568 os.unlink(outFileName)1569print"Failed to strip RCS keywords in%s"%file1570raise15711572print"Patched up RCS keywords in%s"%file15731574defp4UserForCommit(self,id):1575# Return the tuple (perforce user,git email) for a given git commit id1576 self.getUserMapFromPerforceServer()1577 gitEmail =read_pipe(["git","log","--max-count=1",1578"--format=%ae",id])1579 gitEmail = gitEmail.strip()1580if not self.emails.has_key(gitEmail):1581return(None,gitEmail)1582else:1583return(self.emails[gitEmail],gitEmail)15841585defcheckValidP4Users(self,commits):1586# check if any git authors cannot be mapped to p4 users1587foridin commits:1588(user,email) = self.p4UserForCommit(id)1589if not user:1590 msg ="Cannot find p4 user for email%sin commit%s."% (email,id)1591ifgitConfigBool("git-p4.allowMissingP4Users"):1592print"%s"% msg1593else:1594die("Error:%s\nSet git-p4.allowMissingP4Users to true to allow this."% msg)15951596deflastP4Changelist(self):1597# Get back the last changelist number submitted in this client spec. This1598# then gets used to patch up the username in the change. If the same1599# client spec is being used by multiple processes then this might go1600# wrong.1601 results =p4CmdList("client -o")# find the current client1602 client =None1603for r in results:1604if r.has_key('Client'):1605 client = r['Client']1606break1607if not client:1608die("could not get client spec")1609 results =p4CmdList(["changes","-c", client,"-m","1"])1610for r in results:1611if r.has_key('change'):1612return r['change']1613die("Could not get changelist number for last submit - cannot patch up user details")16141615defmodifyChangelistUser(self, changelist, newUser):1616# fixup the user field of a changelist after it has been submitted.1617 changes =p4CmdList("change -o%s"% changelist)1618iflen(changes) !=1:1619die("Bad output from p4 change modifying%sto user%s"%1620(changelist, newUser))16211622 c = changes[0]1623if c['User'] == newUser:return# nothing to do1624 c['User'] = newUser1625input= marshal.dumps(c)16261627 result =p4CmdList("change -f -i", stdin=input)1628for r in result:1629if r.has_key('code'):1630if r['code'] =='error':1631die("Could not modify user field of changelist%sto%s:%s"% (changelist, newUser, r['data']))1632if r.has_key('data'):1633print("Updated user field for changelist%sto%s"% (changelist, newUser))1634return1635die("Could not modify user field of changelist%sto%s"% (changelist, newUser))16361637defcanChangeChangelists(self):1638# check to see if we have p4 admin or super-user permissions, either of1639# which are required to modify changelists.1640 results =p4CmdList(["protects", self.depotPath])1641for r in results:1642if r.has_key('perm'):1643if r['perm'] =='admin':1644return11645if r['perm'] =='super':1646return11647return016481649defprepareSubmitTemplate(self, changelist=None):1650"""Run "p4 change -o" to grab a change specification template.1651 This does not use "p4 -G", as it is nice to keep the submission1652 template in original order, since a human might edit it.16531654 Remove lines in the Files section that show changes to files1655 outside the depot path we're committing into."""16561657[upstream, settings] =findUpstreamBranchPoint()16581659 template ="""\1660# A Perforce Change Specification.1661#1662# Change: The change number. 'new' on a new changelist.1663# Date: The date this specification was last modified.1664# Client: The client on which the changelist was created. Read-only.1665# User: The user who created the changelist.1666# Status: Either 'pending' or 'submitted'. Read-only.1667# Type: Either 'public' or 'restricted'. Default is 'public'.1668# Description: Comments about the changelist. Required.1669# Jobs: What opened jobs are to be closed by this changelist.1670# You may delete jobs from this list. (New changelists only.)1671# Files: What opened files from the default changelist are to be added1672# to this changelist. You may delete files from this list.1673# (New changelists only.)1674"""1675 files_list = []1676 inFilesSection =False1677 change_entry =None1678 args = ['change','-o']1679if changelist:1680 args.append(str(changelist))1681for entry inp4CmdList(args):1682if not entry.has_key('code'):1683continue1684if entry['code'] =='stat':1685 change_entry = entry1686break1687if not change_entry:1688die('Failed to decode output of p4 change -o')1689for key, value in change_entry.iteritems():1690if key.startswith('File'):1691if settings.has_key('depot-paths'):1692if not[p for p in settings['depot-paths']1693ifp4PathStartsWith(value, p)]:1694continue1695else:1696if notp4PathStartsWith(value, self.depotPath):1697continue1698 files_list.append(value)1699continue1700# Output in the order expected by prepareLogMessage1701for key in['Change','Client','User','Status','Description','Jobs']:1702if not change_entry.has_key(key):1703continue1704 template +='\n'1705 template += key +':'1706if key =='Description':1707 template +='\n'1708for field_line in change_entry[key].splitlines():1709 template +='\t'+field_line+'\n'1710iflen(files_list) >0:1711 template +='\n'1712 template +='Files:\n'1713for path in files_list:1714 template +='\t'+path+'\n'1715return template17161717defedit_template(self, template_file):1718"""Invoke the editor to let the user change the submission1719 message. Return true if okay to continue with the submit."""17201721# if configured to skip the editing part, just submit1722ifgitConfigBool("git-p4.skipSubmitEdit"):1723return True17241725# look at the modification time, to check later if the user saved1726# the file1727 mtime = os.stat(template_file).st_mtime17281729# invoke the editor1730if os.environ.has_key("P4EDITOR")and(os.environ.get("P4EDITOR") !=""):1731 editor = os.environ.get("P4EDITOR")1732else:1733 editor =read_pipe("git var GIT_EDITOR").strip()1734system(["sh","-c", ('%s"$@"'% editor), editor, template_file])17351736# If the file was not saved, prompt to see if this patch should1737# be skipped. But skip this verification step if configured so.1738ifgitConfigBool("git-p4.skipSubmitEditCheck"):1739return True17401741# modification time updated means user saved the file1742if os.stat(template_file).st_mtime > mtime:1743return True17441745while True:1746 response =raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")1747if response =='y':1748return True1749if response =='n':1750return False17511752defget_diff_description(self, editedFiles, filesToAdd, symlinks):1753# diff1754if os.environ.has_key("P4DIFF"):1755del(os.environ["P4DIFF"])1756 diff =""1757for editedFile in editedFiles:1758 diff +=p4_read_pipe(['diff','-du',1759wildcard_encode(editedFile)])17601761# new file diff1762 newdiff =""1763for newFile in filesToAdd:1764 newdiff +="==== new file ====\n"1765 newdiff +="--- /dev/null\n"1766 newdiff +="+++%s\n"% newFile17671768 is_link = os.path.islink(newFile)1769 expect_link = newFile in symlinks17701771if is_link and expect_link:1772 newdiff +="+%s\n"% os.readlink(newFile)1773else:1774 f =open(newFile,"r")1775for line in f.readlines():1776 newdiff +="+"+ line1777 f.close()17781779return(diff + newdiff).replace('\r\n','\n')17801781defapplyCommit(self,id):1782"""Apply one commit, return True if it succeeded."""17831784print"Applying",read_pipe(["git","show","-s",1785"--format=format:%h%s",id])17861787(p4User, gitEmail) = self.p4UserForCommit(id)17881789 diff =read_pipe_lines("git diff-tree -r%s\"%s^\" \"%s\""% (self.diffOpts,id,id))1790 filesToAdd =set()1791 filesToChangeType =set()1792 filesToDelete =set()1793 editedFiles =set()1794 pureRenameCopy =set()1795 symlinks =set()1796 filesToChangeExecBit = {}1797 all_files =list()17981799for line in diff:1800 diff =parseDiffTreeEntry(line)1801 modifier = diff['status']1802 path = diff['src']1803 all_files.append(path)18041805if modifier =="M":1806p4_edit(path)1807ifisModeExecChanged(diff['src_mode'], diff['dst_mode']):1808 filesToChangeExecBit[path] = diff['dst_mode']1809 editedFiles.add(path)1810elif modifier =="A":1811 filesToAdd.add(path)1812 filesToChangeExecBit[path] = diff['dst_mode']1813if path in filesToDelete:1814 filesToDelete.remove(path)18151816 dst_mode =int(diff['dst_mode'],8)1817if dst_mode ==0120000:1818 symlinks.add(path)18191820elif modifier =="D":1821 filesToDelete.add(path)1822if path in filesToAdd:1823 filesToAdd.remove(path)1824elif modifier =="C":1825 src, dest = diff['src'], diff['dst']1826p4_integrate(src, dest)1827 pureRenameCopy.add(dest)1828if diff['src_sha1'] != diff['dst_sha1']:1829p4_edit(dest)1830 pureRenameCopy.discard(dest)1831ifisModeExecChanged(diff['src_mode'], diff['dst_mode']):1832p4_edit(dest)1833 pureRenameCopy.discard(dest)1834 filesToChangeExecBit[dest] = diff['dst_mode']1835if self.isWindows:1836# turn off read-only attribute1837 os.chmod(dest, stat.S_IWRITE)1838 os.unlink(dest)1839 editedFiles.add(dest)1840elif modifier =="R":1841 src, dest = diff['src'], diff['dst']1842if self.p4HasMoveCommand:1843p4_edit(src)# src must be open before move1844p4_move(src, dest)# opens for (move/delete, move/add)1845else:1846p4_integrate(src, dest)1847if diff['src_sha1'] != diff['dst_sha1']:1848p4_edit(dest)1849else:1850 pureRenameCopy.add(dest)1851ifisModeExecChanged(diff['src_mode'], diff['dst_mode']):1852if not self.p4HasMoveCommand:1853p4_edit(dest)# with move: already open, writable1854 filesToChangeExecBit[dest] = diff['dst_mode']1855if not self.p4HasMoveCommand:1856if self.isWindows:1857 os.chmod(dest, stat.S_IWRITE)1858 os.unlink(dest)1859 filesToDelete.add(src)1860 editedFiles.add(dest)1861elif modifier =="T":1862 filesToChangeType.add(path)1863else:1864die("unknown modifier%sfor%s"% (modifier, path))18651866 diffcmd ="git diff-tree --full-index -p\"%s\""% (id)1867 patchcmd = diffcmd +" | git apply "1868 tryPatchCmd = patchcmd +"--check -"1869 applyPatchCmd = patchcmd +"--check --apply -"1870 patch_succeeded =True18711872if os.system(tryPatchCmd) !=0:1873 fixed_rcs_keywords =False1874 patch_succeeded =False1875print"Unfortunately applying the change failed!"18761877# Patch failed, maybe it's just RCS keyword woes. Look through1878# the patch to see if that's possible.1879ifgitConfigBool("git-p4.attemptRCSCleanup"):1880file=None1881 pattern =None1882 kwfiles = {}1883forfilein editedFiles | filesToDelete:1884# did this file's delta contain RCS keywords?1885 pattern =p4_keywords_regexp_for_file(file)18861887if pattern:1888# this file is a possibility...look for RCS keywords.1889 regexp = re.compile(pattern, re.VERBOSE)1890for line inread_pipe_lines(["git","diff","%s^..%s"% (id,id),file]):1891if regexp.search(line):1892if verbose:1893print"got keyword match on%sin%sin%s"% (pattern, line,file)1894 kwfiles[file] = pattern1895break18961897forfilein kwfiles:1898if verbose:1899print"zapping%swith%s"% (line,pattern)1900# File is being deleted, so not open in p4. Must1901# disable the read-only bit on windows.1902if self.isWindows andfilenot in editedFiles:1903 os.chmod(file, stat.S_IWRITE)1904 self.patchRCSKeywords(file, kwfiles[file])1905 fixed_rcs_keywords =True19061907if fixed_rcs_keywords:1908print"Retrying the patch with RCS keywords cleaned up"1909if os.system(tryPatchCmd) ==0:1910 patch_succeeded =True19111912if not patch_succeeded:1913for f in editedFiles:1914p4_revert(f)1915return False19161917#1918# Apply the patch for real, and do add/delete/+x handling.1919#1920system(applyPatchCmd)19211922for f in filesToChangeType:1923p4_edit(f,"-t","auto")1924for f in filesToAdd:1925p4_add(f)1926for f in filesToDelete:1927p4_revert(f)1928p4_delete(f)19291930# Set/clear executable bits1931for f in filesToChangeExecBit.keys():1932 mode = filesToChangeExecBit[f]1933setP4ExecBit(f, mode)19341935 update_shelve =01936iflen(self.update_shelve) >0:1937 update_shelve = self.update_shelve.pop(0)1938p4_reopen_in_change(update_shelve, all_files)19391940#1941# Build p4 change description, starting with the contents1942# of the git commit message.1943#1944 logMessage =extractLogMessageFromGitCommit(id)1945 logMessage = logMessage.strip()1946(logMessage, jobs) = self.separate_jobs_from_description(logMessage)19471948 template = self.prepareSubmitTemplate(update_shelve)1949 submitTemplate = self.prepareLogMessage(template, logMessage, jobs)19501951if self.preserveUser:1952 submitTemplate +="\n######## Actual user%s, modified after commit\n"% p4User19531954if self.checkAuthorship and not self.p4UserIsMe(p4User):1955 submitTemplate +="######## git author%sdoes not match your p4 account.\n"% gitEmail1956 submitTemplate +="######## Use option --preserve-user to modify authorship.\n"1957 submitTemplate +="######## Variable git-p4.skipUserNameCheck hides this message.\n"19581959 separatorLine ="######## everything below this line is just the diff #######\n"1960if not self.prepare_p4_only:1961 submitTemplate += separatorLine1962 submitTemplate += self.get_diff_description(editedFiles, filesToAdd, symlinks)19631964(handle, fileName) = tempfile.mkstemp()1965 tmpFile = os.fdopen(handle,"w+b")1966if self.isWindows:1967 submitTemplate = submitTemplate.replace("\n","\r\n")1968 tmpFile.write(submitTemplate)1969 tmpFile.close()19701971if self.prepare_p4_only:1972#1973# Leave the p4 tree prepared, and the submit template around1974# and let the user decide what to do next1975#1976print1977print"P4 workspace prepared for submission."1978print"To submit or revert, go to client workspace"1979print" "+ self.clientPath1980print1981print"To submit, use\"p4 submit\"to write a new description,"1982print"or\"p4 submit -i <%s\"to use the one prepared by" \1983"\"git p4\"."% fileName1984print"You can delete the file\"%s\"when finished."% fileName19851986if self.preserveUser and p4User and not self.p4UserIsMe(p4User):1987print"To preserve change ownership by user%s, you must\n" \1988"do\"p4 change -f <change>\"after submitting and\n" \1989"edit the User field."1990if pureRenameCopy:1991print"After submitting, renamed files must be re-synced."1992print"Invoke\"p4 sync -f\"on each of these files:"1993for f in pureRenameCopy:1994print" "+ f19951996print1997print"To revert the changes, use\"p4 revert ...\", and delete"1998print"the submit template file\"%s\""% fileName1999if filesToAdd:2000print"Since the commit adds new files, they must be deleted:"2001for f in filesToAdd:2002print" "+ f2003print2004return True20052006#2007# Let the user edit the change description, then submit it.2008#2009 submitted =False20102011try:2012if self.edit_template(fileName):2013# read the edited message and submit2014 tmpFile =open(fileName,"rb")2015 message = tmpFile.read()2016 tmpFile.close()2017if self.isWindows:2018 message = message.replace("\r\n","\n")2019 submitTemplate = message[:message.index(separatorLine)]20202021if update_shelve:2022p4_write_pipe(['shelve','-r','-i'], submitTemplate)2023elif self.shelve:2024p4_write_pipe(['shelve','-i'], submitTemplate)2025else:2026p4_write_pipe(['submit','-i'], submitTemplate)2027# The rename/copy happened by applying a patch that created a2028# new file. This leaves it writable, which confuses p4.2029for f in pureRenameCopy:2030p4_sync(f,"-f")20312032if self.preserveUser:2033if p4User:2034# Get last changelist number. Cannot easily get it from2035# the submit command output as the output is2036# unmarshalled.2037 changelist = self.lastP4Changelist()2038 self.modifyChangelistUser(changelist, p4User)20392040 submitted =True20412042finally:2043# skip this patch2044if not submitted or self.shelve:2045if self.shelve:2046print("Reverting shelved files.")2047else:2048print("Submission cancelled, undoing p4 changes.")2049for f in editedFiles | filesToDelete:2050p4_revert(f)2051for f in filesToAdd:2052p4_revert(f)2053 os.remove(f)20542055 os.remove(fileName)2056return submitted20572058# Export git tags as p4 labels. Create a p4 label and then tag2059# with that.2060defexportGitTags(self, gitTags):2061 validLabelRegexp =gitConfig("git-p4.labelExportRegexp")2062iflen(validLabelRegexp) ==0:2063 validLabelRegexp = defaultLabelRegexp2064 m = re.compile(validLabelRegexp)20652066for name in gitTags:20672068if not m.match(name):2069if verbose:2070print"tag%sdoes not match regexp%s"% (name, validLabelRegexp)2071continue20722073# Get the p4 commit this corresponds to2074 logMessage =extractLogMessageFromGitCommit(name)2075 values =extractSettingsGitLog(logMessage)20762077if not values.has_key('change'):2078# a tag pointing to something not sent to p4; ignore2079if verbose:2080print"git tag%sdoes not give a p4 commit"% name2081continue2082else:2083 changelist = values['change']20842085# Get the tag details.2086 inHeader =True2087 isAnnotated =False2088 body = []2089for l inread_pipe_lines(["git","cat-file","-p", name]):2090 l = l.strip()2091if inHeader:2092if re.match(r'tag\s+', l):2093 isAnnotated =True2094elif re.match(r'\s*$', l):2095 inHeader =False2096continue2097else:2098 body.append(l)20992100if not isAnnotated:2101 body = ["lightweight tag imported by git p4\n"]21022103# Create the label - use the same view as the client spec we are using2104 clientSpec =getClientSpec()21052106 labelTemplate ="Label:%s\n"% name2107 labelTemplate +="Description:\n"2108for b in body:2109 labelTemplate +="\t"+ b +"\n"2110 labelTemplate +="View:\n"2111for depot_side in clientSpec.mappings:2112 labelTemplate +="\t%s\n"% depot_side21132114if self.dry_run:2115print"Would create p4 label%sfor tag"% name2116elif self.prepare_p4_only:2117print"Not creating p4 label%sfor tag due to option" \2118" --prepare-p4-only"% name2119else:2120p4_write_pipe(["label","-i"], labelTemplate)21212122# Use the label2123p4_system(["tag","-l", name] +2124["%s@%s"% (depot_side, changelist)for depot_side in clientSpec.mappings])21252126if verbose:2127print"created p4 label for tag%s"% name21282129defrun(self, args):2130iflen(args) ==0:2131 self.master =currentGitBranch()2132eliflen(args) ==1:2133 self.master = args[0]2134if notbranchExists(self.master):2135die("Branch%sdoes not exist"% self.master)2136else:2137return False21382139for i in self.update_shelve:2140if i <=0:2141 sys.exit("invalid changelist%d"% i)21422143if self.master:2144 allowSubmit =gitConfig("git-p4.allowSubmit")2145iflen(allowSubmit) >0and not self.master in allowSubmit.split(","):2146die("%sis not in git-p4.allowSubmit"% self.master)21472148[upstream, settings] =findUpstreamBranchPoint()2149 self.depotPath = settings['depot-paths'][0]2150iflen(self.origin) ==0:2151 self.origin = upstream21522153iflen(self.update_shelve) >0:2154 self.shelve =True21552156if self.preserveUser:2157if not self.canChangeChangelists():2158die("Cannot preserve user names without p4 super-user or admin permissions")21592160# if not set from the command line, try the config file2161if self.conflict_behavior is None:2162 val =gitConfig("git-p4.conflict")2163if val:2164if val not in self.conflict_behavior_choices:2165die("Invalid value '%s' for config git-p4.conflict"% val)2166else:2167 val ="ask"2168 self.conflict_behavior = val21692170if self.verbose:2171print"Origin branch is "+ self.origin21722173iflen(self.depotPath) ==0:2174print"Internal error: cannot locate perforce depot path from existing branches"2175 sys.exit(128)21762177 self.useClientSpec =False2178ifgitConfigBool("git-p4.useclientspec"):2179 self.useClientSpec =True2180if self.useClientSpec:2181 self.clientSpecDirs =getClientSpec()21822183# Check for the existence of P4 branches2184 branchesDetected = (len(p4BranchesInGit().keys()) >1)21852186if self.useClientSpec and not branchesDetected:2187# all files are relative to the client spec2188 self.clientPath =getClientRoot()2189else:2190 self.clientPath =p4Where(self.depotPath)21912192if self.clientPath =="":2193die("Error: Cannot locate perforce checkout of%sin client view"% self.depotPath)21942195print"Perforce checkout for depot path%slocated at%s"% (self.depotPath, self.clientPath)2196 self.oldWorkingDirectory = os.getcwd()21972198# ensure the clientPath exists2199 new_client_dir =False2200if not os.path.exists(self.clientPath):2201 new_client_dir =True2202 os.makedirs(self.clientPath)22032204chdir(self.clientPath, is_client_path=True)2205if self.dry_run:2206print"Would synchronize p4 checkout in%s"% self.clientPath2207else:2208print"Synchronizing p4 checkout..."2209if new_client_dir:2210# old one was destroyed, and maybe nobody told p42211p4_sync("...","-f")2212else:2213p4_sync("...")2214 self.check()22152216 commits = []2217if self.master:2218 commitish = self.master2219else:2220 commitish ='HEAD'22212222if self.commit !="":2223if self.commit.find("..") != -1:2224 limits_ish = self.commit.split("..")2225for line inread_pipe_lines(["git","rev-list","--no-merges","%s..%s"% (limits_ish[0], limits_ish[1])]):2226 commits.append(line.strip())2227 commits.reverse()2228else:2229 commits.append(self.commit)2230else:2231for line inread_pipe_lines(["git","rev-list","--no-merges","%s..%s"% (self.origin, commitish)]):2232 commits.append(line.strip())2233 commits.reverse()22342235if self.preserveUser orgitConfigBool("git-p4.skipUserNameCheck"):2236 self.checkAuthorship =False2237else:2238 self.checkAuthorship =True22392240if self.preserveUser:2241 self.checkValidP4Users(commits)22422243#2244# Build up a set of options to be passed to diff when2245# submitting each commit to p4.2246#2247if self.detectRenames:2248# command-line -M arg2249 self.diffOpts ="-M"2250else:2251# If not explicitly set check the config variable2252 detectRenames =gitConfig("git-p4.detectRenames")22532254if detectRenames.lower() =="false"or detectRenames =="":2255 self.diffOpts =""2256elif detectRenames.lower() =="true":2257 self.diffOpts ="-M"2258else:2259 self.diffOpts ="-M%s"% detectRenames22602261# no command-line arg for -C or --find-copies-harder, just2262# config variables2263 detectCopies =gitConfig("git-p4.detectCopies")2264if detectCopies.lower() =="false"or detectCopies =="":2265pass2266elif detectCopies.lower() =="true":2267 self.diffOpts +=" -C"2268else:2269 self.diffOpts +=" -C%s"% detectCopies22702271ifgitConfigBool("git-p4.detectCopiesHarder"):2272 self.diffOpts +=" --find-copies-harder"22732274 num_shelves =len(self.update_shelve)2275if num_shelves >0and num_shelves !=len(commits):2276 sys.exit("number of commits (%d) must match number of shelved changelist (%d)"%2277(len(commits), num_shelves))22782279#2280# Apply the commits, one at a time. On failure, ask if should2281# continue to try the rest of the patches, or quit.2282#2283if self.dry_run:2284print"Would apply"2285 applied = []2286 last =len(commits) -12287for i, commit inenumerate(commits):2288if self.dry_run:2289print" ",read_pipe(["git","show","-s",2290"--format=format:%h%s", commit])2291 ok =True2292else:2293 ok = self.applyCommit(commit)2294if ok:2295 applied.append(commit)2296else:2297if self.prepare_p4_only and i < last:2298print"Processing only the first commit due to option" \2299" --prepare-p4-only"2300break2301if i < last:2302 quit =False2303while True:2304# prompt for what to do, or use the option/variable2305if self.conflict_behavior =="ask":2306print"What do you want to do?"2307 response =raw_input("[s]kip this commit but apply"2308" the rest, or [q]uit? ")2309if not response:2310continue2311elif self.conflict_behavior =="skip":2312 response ="s"2313elif self.conflict_behavior =="quit":2314 response ="q"2315else:2316die("Unknown conflict_behavior '%s'"%2317 self.conflict_behavior)23182319if response[0] =="s":2320print"Skipping this commit, but applying the rest"2321break2322if response[0] =="q":2323print"Quitting"2324 quit =True2325break2326if quit:2327break23282329chdir(self.oldWorkingDirectory)2330 shelved_applied ="shelved"if self.shelve else"applied"2331if self.dry_run:2332pass2333elif self.prepare_p4_only:2334pass2335eliflen(commits) ==len(applied):2336print("All commits{0}!".format(shelved_applied))23372338 sync =P4Sync()2339if self.branch:2340 sync.branch = self.branch2341if self.disable_p4sync:2342 sync.sync_origin_only()2343else:2344 sync.run([])23452346if not self.disable_rebase:2347 rebase =P4Rebase()2348 rebase.rebase()23492350else:2351iflen(applied) ==0:2352print("No commits{0}.".format(shelved_applied))2353else:2354print("{0}only the commits marked with '*':".format(shelved_applied.capitalize()))2355for c in commits:2356if c in applied:2357 star ="*"2358else:2359 star =" "2360print star,read_pipe(["git","show","-s",2361"--format=format:%h%s", c])2362print"You will have to do 'git p4 sync' and rebase."23632364ifgitConfigBool("git-p4.exportLabels"):2365 self.exportLabels =True23662367if self.exportLabels:2368 p4Labels =getP4Labels(self.depotPath)2369 gitTags =getGitTags()23702371 missingGitTags = gitTags - p4Labels2372 self.exportGitTags(missingGitTags)23732374# exit with error unless everything applied perfectly2375iflen(commits) !=len(applied):2376 sys.exit(1)23772378return True23792380classView(object):2381"""Represent a p4 view ("p4 help views"), and map files in a2382 repo according to the view."""23832384def__init__(self, client_name):2385 self.mappings = []2386 self.client_prefix ="//%s/"% client_name2387# cache results of "p4 where" to lookup client file locations2388 self.client_spec_path_cache = {}23892390defappend(self, view_line):2391"""Parse a view line, splitting it into depot and client2392 sides. Append to self.mappings, preserving order. This2393 is only needed for tag creation."""23942395# Split the view line into exactly two words. P4 enforces2396# structure on these lines that simplifies this quite a bit.2397#2398# Either or both words may be double-quoted.2399# Single quotes do not matter.2400# Double-quote marks cannot occur inside the words.2401# A + or - prefix is also inside the quotes.2402# There are no quotes unless they contain a space.2403# The line is already white-space stripped.2404# The two words are separated by a single space.2405#2406if view_line[0] =='"':2407# First word is double quoted. Find its end.2408 close_quote_index = view_line.find('"',1)2409if close_quote_index <=0:2410die("No first-word closing quote found:%s"% view_line)2411 depot_side = view_line[1:close_quote_index]2412# skip closing quote and space2413 rhs_index = close_quote_index +1+12414else:2415 space_index = view_line.find(" ")2416if space_index <=0:2417die("No word-splitting space found:%s"% view_line)2418 depot_side = view_line[0:space_index]2419 rhs_index = space_index +124202421# prefix + means overlay on previous mapping2422if depot_side.startswith("+"):2423 depot_side = depot_side[1:]24242425# prefix - means exclude this path, leave out of mappings2426 exclude =False2427if depot_side.startswith("-"):2428 exclude =True2429 depot_side = depot_side[1:]24302431if not exclude:2432 self.mappings.append(depot_side)24332434defconvert_client_path(self, clientFile):2435# chop off //client/ part to make it relative2436if not clientFile.startswith(self.client_prefix):2437die("No prefix '%s' on clientFile '%s'"%2438(self.client_prefix, clientFile))2439return clientFile[len(self.client_prefix):]24402441defupdate_client_spec_path_cache(self, files):2442""" Caching file paths by "p4 where" batch query """24432444# List depot file paths exclude that already cached2445 fileArgs = [f['path']for f in files if f['path']not in self.client_spec_path_cache]24462447iflen(fileArgs) ==0:2448return# All files in cache24492450 where_result =p4CmdList(["-x","-","where"], stdin=fileArgs)2451for res in where_result:2452if"code"in res and res["code"] =="error":2453# assume error is "... file(s) not in client view"2454continue2455if"clientFile"not in res:2456die("No clientFile in 'p4 where' output")2457if"unmap"in res:2458# it will list all of them, but only one not unmap-ped2459continue2460ifgitConfigBool("core.ignorecase"):2461 res['depotFile'] = res['depotFile'].lower()2462 self.client_spec_path_cache[res['depotFile']] = self.convert_client_path(res["clientFile"])24632464# not found files or unmap files set to ""2465for depotFile in fileArgs:2466ifgitConfigBool("core.ignorecase"):2467 depotFile = depotFile.lower()2468if depotFile not in self.client_spec_path_cache:2469 self.client_spec_path_cache[depotFile] =""24702471defmap_in_client(self, depot_path):2472"""Return the relative location in the client where this2473 depot file should live. Returns "" if the file should2474 not be mapped in the client."""24752476ifgitConfigBool("core.ignorecase"):2477 depot_path = depot_path.lower()24782479if depot_path in self.client_spec_path_cache:2480return self.client_spec_path_cache[depot_path]24812482die("Error:%sis not found in client spec path"% depot_path )2483return""24842485classP4Sync(Command, P4UserMap):2486 delete_actions = ("delete","move/delete","purge")24872488def__init__(self):2489 Command.__init__(self)2490 P4UserMap.__init__(self)2491 self.options = [2492 optparse.make_option("--branch", dest="branch"),2493 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),2494 optparse.make_option("--changesfile", dest="changesFile"),2495 optparse.make_option("--silent", dest="silent", action="store_true"),2496 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),2497 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),2498 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",2499help="Import into refs/heads/ , not refs/remotes"),2500 optparse.make_option("--max-changes", dest="maxChanges",2501help="Maximum number of changes to import"),2502 optparse.make_option("--changes-block-size", dest="changes_block_size",type="int",2503help="Internal block size to use when iteratively calling p4 changes"),2504 optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',2505help="Keep entire BRANCH/DIR/SUBDIR prefix during import"),2506 optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true',2507help="Only sync files that are included in the Perforce Client Spec"),2508 optparse.make_option("-/", dest="cloneExclude",2509 action="append",type="string",2510help="exclude depot path"),2511]2512 self.description ="""Imports from Perforce into a git repository.\n2513 example:2514 //depot/my/project/ -- to import the current head2515 //depot/my/project/@all -- to import everything2516 //depot/my/project/@1,6 -- to import only from revision 1 to 625172518 (a ... is not needed in the path p4 specification, it's added implicitly)"""25192520 self.usage +=" //depot/path[@revRange]"2521 self.silent =False2522 self.createdBranches =set()2523 self.committedChanges =set()2524 self.branch =""2525 self.detectBranches =False2526 self.detectLabels =False2527 self.importLabels =False2528 self.changesFile =""2529 self.syncWithOrigin =True2530 self.importIntoRemotes =True2531 self.maxChanges =""2532 self.changes_block_size =None2533 self.keepRepoPath =False2534 self.depotPaths =None2535 self.p4BranchesInGit = []2536 self.cloneExclude = []2537 self.useClientSpec =False2538 self.useClientSpec_from_options =False2539 self.clientSpecDirs =None2540 self.tempBranches = []2541 self.tempBranchLocation ="refs/git-p4-tmp"2542 self.largeFileSystem =None25432544ifgitConfig('git-p4.largeFileSystem'):2545 largeFileSystemConstructor =globals()[gitConfig('git-p4.largeFileSystem')]2546 self.largeFileSystem =largeFileSystemConstructor(2547lambda git_mode, relPath, contents: self.writeToGitStream(git_mode, relPath, contents)2548)25492550ifgitConfig("git-p4.syncFromOrigin") =="false":2551 self.syncWithOrigin =False25522553# Force a checkpoint in fast-import and wait for it to finish2554defcheckpoint(self):2555 self.gitStream.write("checkpoint\n\n")2556 self.gitStream.write("progress checkpoint\n\n")2557 out = self.gitOutput.readline()2558if self.verbose:2559print"checkpoint finished: "+ out25602561defextractFilesFromCommit(self, commit):2562 self.cloneExclude = [re.sub(r"\.\.\.$","", path)2563for path in self.cloneExclude]2564 files = []2565 fnum =02566while commit.has_key("depotFile%s"% fnum):2567 path = commit["depotFile%s"% fnum]25682569if[p for p in self.cloneExclude2570ifp4PathStartsWith(path, p)]:2571 found =False2572else:2573 found = [p for p in self.depotPaths2574ifp4PathStartsWith(path, p)]2575if not found:2576 fnum = fnum +12577continue25782579file= {}2580file["path"] = path2581file["rev"] = commit["rev%s"% fnum]2582file["action"] = commit["action%s"% fnum]2583file["type"] = commit["type%s"% fnum]2584 files.append(file)2585 fnum = fnum +12586return files25872588defextractJobsFromCommit(self, commit):2589 jobs = []2590 jnum =02591while commit.has_key("job%s"% jnum):2592 job = commit["job%s"% jnum]2593 jobs.append(job)2594 jnum = jnum +12595return jobs25962597defstripRepoPath(self, path, prefixes):2598"""When streaming files, this is called to map a p4 depot path2599 to where it should go in git. The prefixes are either2600 self.depotPaths, or self.branchPrefixes in the case of2601 branch detection."""26022603if self.useClientSpec:2604# branch detection moves files up a level (the branch name)2605# from what client spec interpretation gives2606 path = self.clientSpecDirs.map_in_client(path)2607if self.detectBranches:2608for b in self.knownBranches:2609if path.startswith(b +"/"):2610 path = path[len(b)+1:]26112612elif self.keepRepoPath:2613# Preserve everything in relative path name except leading2614# //depot/; just look at first prefix as they all should2615# be in the same depot.2616 depot = re.sub("^(//[^/]+/).*", r'\1', prefixes[0])2617ifp4PathStartsWith(path, depot):2618 path = path[len(depot):]26192620else:2621for p in prefixes:2622ifp4PathStartsWith(path, p):2623 path = path[len(p):]2624break26252626 path =wildcard_decode(path)2627return path26282629defsplitFilesIntoBranches(self, commit):2630"""Look at each depotFile in the commit to figure out to what2631 branch it belongs."""26322633if self.clientSpecDirs:2634 files = self.extractFilesFromCommit(commit)2635 self.clientSpecDirs.update_client_spec_path_cache(files)26362637 branches = {}2638 fnum =02639while commit.has_key("depotFile%s"% fnum):2640 path = commit["depotFile%s"% fnum]2641 found = [p for p in self.depotPaths2642ifp4PathStartsWith(path, p)]2643if not found:2644 fnum = fnum +12645continue26462647file= {}2648file["path"] = path2649file["rev"] = commit["rev%s"% fnum]2650file["action"] = commit["action%s"% fnum]2651file["type"] = commit["type%s"% fnum]2652 fnum = fnum +126532654# start with the full relative path where this file would2655# go in a p4 client2656if self.useClientSpec:2657 relPath = self.clientSpecDirs.map_in_client(path)2658else:2659 relPath = self.stripRepoPath(path, self.depotPaths)26602661for branch in self.knownBranches.keys():2662# add a trailing slash so that a commit into qt/4.2foo2663# doesn't end up in qt/4.2, e.g.2664if relPath.startswith(branch +"/"):2665if branch not in branches:2666 branches[branch] = []2667 branches[branch].append(file)2668break26692670return branches26712672defwriteToGitStream(self, gitMode, relPath, contents):2673 self.gitStream.write('M%sinline%s\n'% (gitMode, relPath))2674 self.gitStream.write('data%d\n'%sum(len(d)for d in contents))2675for d in contents:2676 self.gitStream.write(d)2677 self.gitStream.write('\n')26782679defencodeWithUTF8(self, path):2680try:2681 path.decode('ascii')2682except:2683 encoding ='utf8'2684ifgitConfig('git-p4.pathEncoding'):2685 encoding =gitConfig('git-p4.pathEncoding')2686 path = path.decode(encoding,'replace').encode('utf8','replace')2687if self.verbose:2688print'Path with non-ASCII characters detected. Used%sto encode:%s'% (encoding, path)2689return path26902691# output one file from the P4 stream2692# - helper for streamP4Files26932694defstreamOneP4File(self,file, contents):2695 relPath = self.stripRepoPath(file['depotFile'], self.branchPrefixes)2696 relPath = self.encodeWithUTF8(relPath)2697if verbose:2698 size =int(self.stream_file['fileSize'])2699 sys.stdout.write('\r%s-->%s(%iMB)\n'% (file['depotFile'], relPath, size/1024/1024))2700 sys.stdout.flush()27012702(type_base, type_mods) =split_p4_type(file["type"])27032704 git_mode ="100644"2705if"x"in type_mods:2706 git_mode ="100755"2707if type_base =="symlink":2708 git_mode ="120000"2709# p4 print on a symlink sometimes contains "target\n";2710# if it does, remove the newline2711 data =''.join(contents)2712if not data:2713# Some version of p4 allowed creating a symlink that pointed2714# to nothing. This causes p4 errors when checking out such2715# a change, and errors here too. Work around it by ignoring2716# the bad symlink; hopefully a future change fixes it.2717print"\nIgnoring empty symlink in%s"%file['depotFile']2718return2719elif data[-1] =='\n':2720 contents = [data[:-1]]2721else:2722 contents = [data]27232724if type_base =="utf16":2725# p4 delivers different text in the python output to -G2726# than it does when using "print -o", or normal p4 client2727# operations. utf16 is converted to ascii or utf8, perhaps.2728# But ascii text saved as -t utf16 is completely mangled.2729# Invoke print -o to get the real contents.2730#2731# On windows, the newlines will always be mangled by print, so put2732# them back too. This is not needed to the cygwin windows version,2733# just the native "NT" type.2734#2735try:2736 text =p4_read_pipe(['print','-q','-o','-','%s@%s'% (file['depotFile'],file['change'])])2737exceptExceptionas e:2738if'Translation of file content failed'instr(e):2739 type_base ='binary'2740else:2741raise e2742else:2743ifp4_version_string().find('/NT') >=0:2744 text = text.replace('\r\n','\n')2745 contents = [ text ]27462747if type_base =="apple":2748# Apple filetype files will be streamed as a concatenation of2749# its appledouble header and the contents. This is useless2750# on both macs and non-macs. If using "print -q -o xx", it2751# will create "xx" with the data, and "%xx" with the header.2752# This is also not very useful.2753#2754# Ideally, someday, this script can learn how to generate2755# appledouble files directly and import those to git, but2756# non-mac machines can never find a use for apple filetype.2757print"\nIgnoring apple filetype file%s"%file['depotFile']2758return27592760# Note that we do not try to de-mangle keywords on utf16 files,2761# even though in theory somebody may want that.2762 pattern =p4_keywords_regexp_for_type(type_base, type_mods)2763if pattern:2764 regexp = re.compile(pattern, re.VERBOSE)2765 text =''.join(contents)2766 text = regexp.sub(r'$\1$', text)2767 contents = [ text ]27682769if self.largeFileSystem:2770(git_mode, contents) = self.largeFileSystem.processContent(git_mode, relPath, contents)27712772 self.writeToGitStream(git_mode, relPath, contents)27732774defstreamOneP4Deletion(self,file):2775 relPath = self.stripRepoPath(file['path'], self.branchPrefixes)2776 relPath = self.encodeWithUTF8(relPath)2777if verbose:2778 sys.stdout.write("delete%s\n"% relPath)2779 sys.stdout.flush()2780 self.gitStream.write("D%s\n"% relPath)27812782if self.largeFileSystem and self.largeFileSystem.isLargeFile(relPath):2783 self.largeFileSystem.removeLargeFile(relPath)27842785# handle another chunk of streaming data2786defstreamP4FilesCb(self, marshalled):27872788# catch p4 errors and complain2789 err =None2790if"code"in marshalled:2791if marshalled["code"] =="error":2792if"data"in marshalled:2793 err = marshalled["data"].rstrip()27942795if not err and'fileSize'in self.stream_file:2796 required_bytes =int((4*int(self.stream_file["fileSize"])) -calcDiskFree())2797if required_bytes >0:2798 err ='Not enough space left on%s! Free at least%iMB.'% (2799 os.getcwd(), required_bytes/1024/10242800)28012802if err:2803 f =None2804if self.stream_have_file_info:2805if"depotFile"in self.stream_file:2806 f = self.stream_file["depotFile"]2807# force a failure in fast-import, else an empty2808# commit will be made2809 self.gitStream.write("\n")2810 self.gitStream.write("die-now\n")2811 self.gitStream.close()2812# ignore errors, but make sure it exits first2813 self.importProcess.wait()2814if f:2815die("Error from p4 print for%s:%s"% (f, err))2816else:2817die("Error from p4 print:%s"% err)28182819if marshalled.has_key('depotFile')and self.stream_have_file_info:2820# start of a new file - output the old one first2821 self.streamOneP4File(self.stream_file, self.stream_contents)2822 self.stream_file = {}2823 self.stream_contents = []2824 self.stream_have_file_info =False28252826# pick up the new file information... for the2827# 'data' field we need to append to our array2828for k in marshalled.keys():2829if k =='data':2830if'streamContentSize'not in self.stream_file:2831 self.stream_file['streamContentSize'] =02832 self.stream_file['streamContentSize'] +=len(marshalled['data'])2833 self.stream_contents.append(marshalled['data'])2834else:2835 self.stream_file[k] = marshalled[k]28362837if(verbose and2838'streamContentSize'in self.stream_file and2839'fileSize'in self.stream_file and2840'depotFile'in self.stream_file):2841 size =int(self.stream_file["fileSize"])2842if size >0:2843 progress =100*self.stream_file['streamContentSize']/size2844 sys.stdout.write('\r%s %d%%(%iMB)'% (self.stream_file['depotFile'], progress,int(size/1024/1024)))2845 sys.stdout.flush()28462847 self.stream_have_file_info =True28482849# Stream directly from "p4 files" into "git fast-import"2850defstreamP4Files(self, files):2851 filesForCommit = []2852 filesToRead = []2853 filesToDelete = []28542855for f in files:2856 filesForCommit.append(f)2857if f['action']in self.delete_actions:2858 filesToDelete.append(f)2859else:2860 filesToRead.append(f)28612862# deleted files...2863for f in filesToDelete:2864 self.streamOneP4Deletion(f)28652866iflen(filesToRead) >0:2867 self.stream_file = {}2868 self.stream_contents = []2869 self.stream_have_file_info =False28702871# curry self argument2872defstreamP4FilesCbSelf(entry):2873 self.streamP4FilesCb(entry)28742875 fileArgs = ['%s#%s'% (f['path'], f['rev'])for f in filesToRead]28762877p4CmdList(["-x","-","print"],2878 stdin=fileArgs,2879 cb=streamP4FilesCbSelf)28802881# do the last chunk2882if self.stream_file.has_key('depotFile'):2883 self.streamOneP4File(self.stream_file, self.stream_contents)28842885defmake_email(self, userid):2886if userid in self.users:2887return self.users[userid]2888else:2889return"%s<a@b>"% userid28902891defstreamTag(self, gitStream, labelName, labelDetails, commit, epoch):2892""" Stream a p4 tag.2893 commit is either a git commit, or a fast-import mark, ":<p4commit>"2894 """28952896if verbose:2897print"writing tag%sfor commit%s"% (labelName, commit)2898 gitStream.write("tag%s\n"% labelName)2899 gitStream.write("from%s\n"% commit)29002901if labelDetails.has_key('Owner'):2902 owner = labelDetails["Owner"]2903else:2904 owner =None29052906# Try to use the owner of the p4 label, or failing that,2907# the current p4 user id.2908if owner:2909 email = self.make_email(owner)2910else:2911 email = self.make_email(self.p4UserId())2912 tagger ="%s %s %s"% (email, epoch, self.tz)29132914 gitStream.write("tagger%s\n"% tagger)29152916print"labelDetails=",labelDetails2917if labelDetails.has_key('Description'):2918 description = labelDetails['Description']2919else:2920 description ='Label from git p4'29212922 gitStream.write("data%d\n"%len(description))2923 gitStream.write(description)2924 gitStream.write("\n")29252926definClientSpec(self, path):2927if not self.clientSpecDirs:2928return True2929 inClientSpec = self.clientSpecDirs.map_in_client(path)2930if not inClientSpec and self.verbose:2931print('Ignoring file outside of client spec:{0}'.format(path))2932return inClientSpec29332934defhasBranchPrefix(self, path):2935if not self.branchPrefixes:2936return True2937 hasPrefix = [p for p in self.branchPrefixes2938ifp4PathStartsWith(path, p)]2939if not hasPrefix and self.verbose:2940print('Ignoring file outside of prefix:{0}'.format(path))2941return hasPrefix29422943defcommit(self, details, files, branch, parent =""):2944 epoch = details["time"]2945 author = details["user"]2946 jobs = self.extractJobsFromCommit(details)29472948if self.verbose:2949print('commit into{0}'.format(branch))29502951if self.clientSpecDirs:2952 self.clientSpecDirs.update_client_spec_path_cache(files)29532954 files = [f for f in files2955if self.inClientSpec(f['path'])and self.hasBranchPrefix(f['path'])]29562957if not files and notgitConfigBool('git-p4.keepEmptyCommits'):2958print('Ignoring revision{0}as it would produce an empty commit.'2959.format(details['change']))2960return29612962 self.gitStream.write("commit%s\n"% branch)2963 self.gitStream.write("mark :%s\n"% details["change"])2964 self.committedChanges.add(int(details["change"]))2965 committer =""2966if author not in self.users:2967 self.getUserMapFromPerforceServer()2968 committer ="%s %s %s"% (self.make_email(author), epoch, self.tz)29692970 self.gitStream.write("committer%s\n"% committer)29712972 self.gitStream.write("data <<EOT\n")2973 self.gitStream.write(details["desc"])2974iflen(jobs) >0:2975 self.gitStream.write("\nJobs:%s"% (' '.join(jobs)))2976 self.gitStream.write("\n[git-p4: depot-paths =\"%s\": change =%s"%2977(','.join(self.branchPrefixes), details["change"]))2978iflen(details['options']) >0:2979 self.gitStream.write(": options =%s"% details['options'])2980 self.gitStream.write("]\nEOT\n\n")29812982iflen(parent) >0:2983if self.verbose:2984print"parent%s"% parent2985 self.gitStream.write("from%s\n"% parent)29862987 self.streamP4Files(files)2988 self.gitStream.write("\n")29892990 change =int(details["change"])29912992if self.labels.has_key(change):2993 label = self.labels[change]2994 labelDetails = label[0]2995 labelRevisions = label[1]2996if self.verbose:2997print"Change%sis labelled%s"% (change, labelDetails)29982999 files =p4CmdList(["files"] + ["%s...@%s"% (p, change)3000for p in self.branchPrefixes])30013002iflen(files) ==len(labelRevisions):30033004 cleanedFiles = {}3005for info in files:3006if info["action"]in self.delete_actions:3007continue3008 cleanedFiles[info["depotFile"]] = info["rev"]30093010if cleanedFiles == labelRevisions:3011 self.streamTag(self.gitStream,'tag_%s'% labelDetails['label'], labelDetails, branch, epoch)30123013else:3014if not self.silent:3015print("Tag%sdoes not match with change%s: files do not match."3016% (labelDetails["label"], change))30173018else:3019if not self.silent:3020print("Tag%sdoes not match with change%s: file count is different."3021% (labelDetails["label"], change))30223023# Build a dictionary of changelists and labels, for "detect-labels" option.3024defgetLabels(self):3025 self.labels = {}30263027 l =p4CmdList(["labels"] + ["%s..."% p for p in self.depotPaths])3028iflen(l) >0and not self.silent:3029print"Finding files belonging to labels in%s"% `self.depotPaths`30303031for output in l:3032 label = output["label"]3033 revisions = {}3034 newestChange =03035if self.verbose:3036print"Querying files for label%s"% label3037forfileinp4CmdList(["files"] +3038["%s...@%s"% (p, label)3039for p in self.depotPaths]):3040 revisions[file["depotFile"]] =file["rev"]3041 change =int(file["change"])3042if change > newestChange:3043 newestChange = change30443045 self.labels[newestChange] = [output, revisions]30463047if self.verbose:3048print"Label changes:%s"% self.labels.keys()30493050# Import p4 labels as git tags. A direct mapping does not3051# exist, so assume that if all the files are at the same revision3052# then we can use that, or it's something more complicated we should3053# just ignore.3054defimportP4Labels(self, stream, p4Labels):3055if verbose:3056print"import p4 labels: "+' '.join(p4Labels)30573058 ignoredP4Labels =gitConfigList("git-p4.ignoredP4Labels")3059 validLabelRegexp =gitConfig("git-p4.labelImportRegexp")3060iflen(validLabelRegexp) ==0:3061 validLabelRegexp = defaultLabelRegexp3062 m = re.compile(validLabelRegexp)30633064for name in p4Labels:3065 commitFound =False30663067if not m.match(name):3068if verbose:3069print"label%sdoes not match regexp%s"% (name,validLabelRegexp)3070continue30713072if name in ignoredP4Labels:3073continue30743075 labelDetails =p4CmdList(['label',"-o", name])[0]30763077# get the most recent changelist for each file in this label3078 change =p4Cmd(["changes","-m","1"] + ["%s...@%s"% (p, name)3079for p in self.depotPaths])30803081if change.has_key('change'):3082# find the corresponding git commit; take the oldest commit3083 changelist =int(change['change'])3084if changelist in self.committedChanges:3085 gitCommit =":%d"% changelist # use a fast-import mark3086 commitFound =True3087else:3088 gitCommit =read_pipe(["git","rev-list","--max-count=1",3089"--reverse",":/\[git-p4:.*change =%d\]"% changelist], ignore_error=True)3090iflen(gitCommit) ==0:3091print"importing label%s: could not find git commit for changelist%d"% (name, changelist)3092else:3093 commitFound =True3094 gitCommit = gitCommit.strip()30953096if commitFound:3097# Convert from p4 time format3098try:3099 tmwhen = time.strptime(labelDetails['Update'],"%Y/%m/%d%H:%M:%S")3100exceptValueError:3101print"Could not convert label time%s"% labelDetails['Update']3102 tmwhen =131033104 when =int(time.mktime(tmwhen))3105 self.streamTag(stream, name, labelDetails, gitCommit, when)3106if verbose:3107print"p4 label%smapped to git commit%s"% (name, gitCommit)3108else:3109if verbose:3110print"Label%shas no changelists - possibly deleted?"% name31113112if not commitFound:3113# We can't import this label; don't try again as it will get very3114# expensive repeatedly fetching all the files for labels that will3115# never be imported. If the label is moved in the future, the3116# ignore will need to be removed manually.3117system(["git","config","--add","git-p4.ignoredP4Labels", name])31183119defguessProjectName(self):3120for p in self.depotPaths:3121if p.endswith("/"):3122 p = p[:-1]3123 p = p[p.strip().rfind("/") +1:]3124if not p.endswith("/"):3125 p +="/"3126return p31273128defgetBranchMapping(self):3129 lostAndFoundBranches =set()31303131 user =gitConfig("git-p4.branchUser")3132iflen(user) >0:3133 command ="branches -u%s"% user3134else:3135 command ="branches"31363137for info inp4CmdList(command):3138 details =p4Cmd(["branch","-o", info["branch"]])3139 viewIdx =03140while details.has_key("View%s"% viewIdx):3141 paths = details["View%s"% viewIdx].split(" ")3142 viewIdx = viewIdx +13143# require standard //depot/foo/... //depot/bar/... mapping3144iflen(paths) !=2or not paths[0].endswith("/...")or not paths[1].endswith("/..."):3145continue3146 source = paths[0]3147 destination = paths[1]3148## HACK3149ifp4PathStartsWith(source, self.depotPaths[0])andp4PathStartsWith(destination, self.depotPaths[0]):3150 source = source[len(self.depotPaths[0]):-4]3151 destination = destination[len(self.depotPaths[0]):-4]31523153if destination in self.knownBranches:3154if not self.silent:3155print"p4 branch%sdefines a mapping from%sto%s"% (info["branch"], source, destination)3156print"but there exists another mapping from%sto%salready!"% (self.knownBranches[destination], destination)3157continue31583159 self.knownBranches[destination] = source31603161 lostAndFoundBranches.discard(destination)31623163if source not in self.knownBranches:3164 lostAndFoundBranches.add(source)31653166# Perforce does not strictly require branches to be defined, so we also3167# check git config for a branch list.3168#3169# Example of branch definition in git config file:3170# [git-p4]3171# branchList=main:branchA3172# branchList=main:branchB3173# branchList=branchA:branchC3174 configBranches =gitConfigList("git-p4.branchList")3175for branch in configBranches:3176if branch:3177(source, destination) = branch.split(":")3178 self.knownBranches[destination] = source31793180 lostAndFoundBranches.discard(destination)31813182if source not in self.knownBranches:3183 lostAndFoundBranches.add(source)318431853186for branch in lostAndFoundBranches:3187 self.knownBranches[branch] = branch31883189defgetBranchMappingFromGitBranches(self):3190 branches =p4BranchesInGit(self.importIntoRemotes)3191for branch in branches.keys():3192if branch =="master":3193 branch ="main"3194else:3195 branch = branch[len(self.projectName):]3196 self.knownBranches[branch] = branch31973198defupdateOptionDict(self, d):3199 option_keys = {}3200if self.keepRepoPath:3201 option_keys['keepRepoPath'] =132023203 d["options"] =' '.join(sorted(option_keys.keys()))32043205defreadOptions(self, d):3206 self.keepRepoPath = (d.has_key('options')3207and('keepRepoPath'in d['options']))32083209defgitRefForBranch(self, branch):3210if branch =="main":3211return self.refPrefix +"master"32123213iflen(branch) <=0:3214return branch32153216return self.refPrefix + self.projectName + branch32173218defgitCommitByP4Change(self, ref, change):3219if self.verbose:3220print"looking in ref "+ ref +" for change%susing bisect..."% change32213222 earliestCommit =""3223 latestCommit =parseRevision(ref)32243225while True:3226if self.verbose:3227print"trying: earliest%slatest%s"% (earliestCommit, latestCommit)3228 next =read_pipe("git rev-list --bisect%s %s"% (latestCommit, earliestCommit)).strip()3229iflen(next) ==0:3230if self.verbose:3231print"argh"3232return""3233 log =extractLogMessageFromGitCommit(next)3234 settings =extractSettingsGitLog(log)3235 currentChange =int(settings['change'])3236if self.verbose:3237print"current change%s"% currentChange32383239if currentChange == change:3240if self.verbose:3241print"found%s"% next3242return next32433244if currentChange < change:3245 earliestCommit ="^%s"% next3246else:3247 latestCommit ="%s"% next32483249return""32503251defimportNewBranch(self, branch, maxChange):3252# make fast-import flush all changes to disk and update the refs using the checkpoint3253# command so that we can try to find the branch parent in the git history3254 self.gitStream.write("checkpoint\n\n");3255 self.gitStream.flush();3256 branchPrefix = self.depotPaths[0] + branch +"/"3257range="@1,%s"% maxChange3258#print "prefix" + branchPrefix3259 changes =p4ChangesForPaths([branchPrefix],range, self.changes_block_size)3260iflen(changes) <=0:3261return False3262 firstChange = changes[0]3263#print "first change in branch: %s" % firstChange3264 sourceBranch = self.knownBranches[branch]3265 sourceDepotPath = self.depotPaths[0] + sourceBranch3266 sourceRef = self.gitRefForBranch(sourceBranch)3267#print "source " + sourceBranch32683269 branchParentChange =int(p4Cmd(["changes","-m","1","%s...@1,%s"% (sourceDepotPath, firstChange)])["change"])3270#print "branch parent: %s" % branchParentChange3271 gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)3272iflen(gitParent) >0:3273 self.initialParents[self.gitRefForBranch(branch)] = gitParent3274#print "parent git commit: %s" % gitParent32753276 self.importChanges(changes)3277return True32783279defsearchParent(self, parent, branch, target):3280 parentFound =False3281for blob inread_pipe_lines(["git","rev-list","--reverse",3282"--no-merges", parent]):3283 blob = blob.strip()3284iflen(read_pipe(["git","diff-tree", blob, target])) ==0:3285 parentFound =True3286if self.verbose:3287print"Found parent of%sin commit%s"% (branch, blob)3288break3289if parentFound:3290return blob3291else:3292return None32933294defimportChanges(self, changes):3295 cnt =13296for change in changes:3297 description =p4_describe(change)3298 self.updateOptionDict(description)32993300if not self.silent:3301 sys.stdout.write("\rImporting revision%s(%s%%)"% (change, cnt *100/len(changes)))3302 sys.stdout.flush()3303 cnt = cnt +133043305try:3306if self.detectBranches:3307 branches = self.splitFilesIntoBranches(description)3308for branch in branches.keys():3309## HACK --hwn3310 branchPrefix = self.depotPaths[0] + branch +"/"3311 self.branchPrefixes = [ branchPrefix ]33123313 parent =""33143315 filesForCommit = branches[branch]33163317if self.verbose:3318print"branch is%s"% branch33193320 self.updatedBranches.add(branch)33213322if branch not in self.createdBranches:3323 self.createdBranches.add(branch)3324 parent = self.knownBranches[branch]3325if parent == branch:3326 parent =""3327else:3328 fullBranch = self.projectName + branch3329if fullBranch not in self.p4BranchesInGit:3330if not self.silent:3331print("\nImporting new branch%s"% fullBranch);3332if self.importNewBranch(branch, change -1):3333 parent =""3334 self.p4BranchesInGit.append(fullBranch)3335if not self.silent:3336print("\nResuming with change%s"% change);33373338if self.verbose:3339print"parent determined through known branches:%s"% parent33403341 branch = self.gitRefForBranch(branch)3342 parent = self.gitRefForBranch(parent)33433344if self.verbose:3345print"looking for initial parent for%s; current parent is%s"% (branch, parent)33463347iflen(parent) ==0and branch in self.initialParents:3348 parent = self.initialParents[branch]3349del self.initialParents[branch]33503351 blob =None3352iflen(parent) >0:3353 tempBranch ="%s/%d"% (self.tempBranchLocation, change)3354if self.verbose:3355print"Creating temporary branch: "+ tempBranch3356 self.commit(description, filesForCommit, tempBranch)3357 self.tempBranches.append(tempBranch)3358 self.checkpoint()3359 blob = self.searchParent(parent, branch, tempBranch)3360if blob:3361 self.commit(description, filesForCommit, branch, blob)3362else:3363if self.verbose:3364print"Parent of%snot found. Committing into head of%s"% (branch, parent)3365 self.commit(description, filesForCommit, branch, parent)3366else:3367 files = self.extractFilesFromCommit(description)3368 self.commit(description, files, self.branch,3369 self.initialParent)3370# only needed once, to connect to the previous commit3371 self.initialParent =""3372exceptIOError:3373print self.gitError.read()3374 sys.exit(1)33753376defsync_origin_only(self):3377if self.syncWithOrigin:3378 self.hasOrigin =originP4BranchesExist()3379if self.hasOrigin:3380if not self.silent:3381print'Syncing with origin first, using "git fetch origin"'3382system("git fetch origin")33833384defimportHeadRevision(self, revision):3385print"Doing initial import of%sfrom revision%sinto%s"% (' '.join(self.depotPaths), revision, self.branch)33863387 details = {}3388 details["user"] ="git perforce import user"3389 details["desc"] = ("Initial import of%sfrom the state at revision%s\n"3390% (' '.join(self.depotPaths), revision))3391 details["change"] = revision3392 newestRevision =033933394 fileCnt =03395 fileArgs = ["%s...%s"% (p,revision)for p in self.depotPaths]33963397for info inp4CmdList(["files"] + fileArgs):33983399if'code'in info and info['code'] =='error':3400 sys.stderr.write("p4 returned an error:%s\n"3401% info['data'])3402if info['data'].find("must refer to client") >=0:3403 sys.stderr.write("This particular p4 error is misleading.\n")3404 sys.stderr.write("Perhaps the depot path was misspelled.\n");3405 sys.stderr.write("Depot path:%s\n"%" ".join(self.depotPaths))3406 sys.exit(1)3407if'p4ExitCode'in info:3408 sys.stderr.write("p4 exitcode:%s\n"% info['p4ExitCode'])3409 sys.exit(1)341034113412 change =int(info["change"])3413if change > newestRevision:3414 newestRevision = change34153416if info["action"]in self.delete_actions:3417# don't increase the file cnt, otherwise details["depotFile123"] will have gaps!3418#fileCnt = fileCnt + 13419continue34203421for prop in["depotFile","rev","action","type"]:3422 details["%s%s"% (prop, fileCnt)] = info[prop]34233424 fileCnt = fileCnt +134253426 details["change"] = newestRevision34273428# Use time from top-most change so that all git p4 clones of3429# the same p4 repo have the same commit SHA1s.3430 res =p4_describe(newestRevision)3431 details["time"] = res["time"]34323433 self.updateOptionDict(details)3434try:3435 self.commit(details, self.extractFilesFromCommit(details), self.branch)3436exceptIOError:3437print"IO error with git fast-import. Is your git version recent enough?"3438print self.gitError.read()343934403441defrun(self, args):3442 self.depotPaths = []3443 self.changeRange =""3444 self.previousDepotPaths = []3445 self.hasOrigin =False34463447# map from branch depot path to parent branch3448 self.knownBranches = {}3449 self.initialParents = {}34503451if self.importIntoRemotes:3452 self.refPrefix ="refs/remotes/p4/"3453else:3454 self.refPrefix ="refs/heads/p4/"34553456 self.sync_origin_only()34573458 branch_arg_given =bool(self.branch)3459iflen(self.branch) ==0:3460 self.branch = self.refPrefix +"master"3461ifgitBranchExists("refs/heads/p4")and self.importIntoRemotes:3462system("git update-ref%srefs/heads/p4"% self.branch)3463system("git branch -D p4")34643465# accept either the command-line option, or the configuration variable3466if self.useClientSpec:3467# will use this after clone to set the variable3468 self.useClientSpec_from_options =True3469else:3470ifgitConfigBool("git-p4.useclientspec"):3471 self.useClientSpec =True3472if self.useClientSpec:3473 self.clientSpecDirs =getClientSpec()34743475# TODO: should always look at previous commits,3476# merge with previous imports, if possible.3477if args == []:3478if self.hasOrigin:3479createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)34803481# branches holds mapping from branch name to sha13482 branches =p4BranchesInGit(self.importIntoRemotes)34833484# restrict to just this one, disabling detect-branches3485if branch_arg_given:3486 short = self.branch.split("/")[-1]3487if short in branches:3488 self.p4BranchesInGit = [ short ]3489else:3490 self.p4BranchesInGit = branches.keys()34913492iflen(self.p4BranchesInGit) >1:3493if not self.silent:3494print"Importing from/into multiple branches"3495 self.detectBranches =True3496for branch in branches.keys():3497 self.initialParents[self.refPrefix + branch] = \3498 branches[branch]34993500if self.verbose:3501print"branches:%s"% self.p4BranchesInGit35023503 p4Change =03504for branch in self.p4BranchesInGit:3505 logMsg =extractLogMessageFromGitCommit(self.refPrefix + branch)35063507 settings =extractSettingsGitLog(logMsg)35083509 self.readOptions(settings)3510if(settings.has_key('depot-paths')3511and settings.has_key('change')):3512 change =int(settings['change']) +13513 p4Change =max(p4Change, change)35143515 depotPaths =sorted(settings['depot-paths'])3516if self.previousDepotPaths == []:3517 self.previousDepotPaths = depotPaths3518else:3519 paths = []3520for(prev, cur)inzip(self.previousDepotPaths, depotPaths):3521 prev_list = prev.split("/")3522 cur_list = cur.split("/")3523for i inrange(0,min(len(cur_list),len(prev_list))):3524if cur_list[i] <> prev_list[i]:3525 i = i -13526break35273528 paths.append("/".join(cur_list[:i +1]))35293530 self.previousDepotPaths = paths35313532if p4Change >0:3533 self.depotPaths =sorted(self.previousDepotPaths)3534 self.changeRange ="@%s,#head"% p4Change3535if not self.silent and not self.detectBranches:3536print"Performing incremental import into%sgit branch"% self.branch35373538# accept multiple ref name abbreviations:3539# refs/foo/bar/branch -> use it exactly3540# p4/branch -> prepend refs/remotes/ or refs/heads/3541# branch -> prepend refs/remotes/p4/ or refs/heads/p4/3542if not self.branch.startswith("refs/"):3543if self.importIntoRemotes:3544 prepend ="refs/remotes/"3545else:3546 prepend ="refs/heads/"3547if not self.branch.startswith("p4/"):3548 prepend +="p4/"3549 self.branch = prepend + self.branch35503551iflen(args) ==0and self.depotPaths:3552if not self.silent:3553print"Depot paths:%s"%' '.join(self.depotPaths)3554else:3555if self.depotPaths and self.depotPaths != args:3556print("previous import used depot path%sand now%swas specified. "3557"This doesn't work!"% (' '.join(self.depotPaths),3558' '.join(args)))3559 sys.exit(1)35603561 self.depotPaths =sorted(args)35623563 revision =""3564 self.users = {}35653566# Make sure no revision specifiers are used when --changesfile3567# is specified.3568 bad_changesfile =False3569iflen(self.changesFile) >0:3570for p in self.depotPaths:3571if p.find("@") >=0or p.find("#") >=0:3572 bad_changesfile =True3573break3574if bad_changesfile:3575die("Option --changesfile is incompatible with revision specifiers")35763577 newPaths = []3578for p in self.depotPaths:3579if p.find("@") != -1:3580 atIdx = p.index("@")3581 self.changeRange = p[atIdx:]3582if self.changeRange =="@all":3583 self.changeRange =""3584elif','not in self.changeRange:3585 revision = self.changeRange3586 self.changeRange =""3587 p = p[:atIdx]3588elif p.find("#") != -1:3589 hashIdx = p.index("#")3590 revision = p[hashIdx:]3591 p = p[:hashIdx]3592elif self.previousDepotPaths == []:3593# pay attention to changesfile, if given, else import3594# the entire p4 tree at the head revision3595iflen(self.changesFile) ==0:3596 revision ="#head"35973598 p = re.sub("\.\.\.$","", p)3599if not p.endswith("/"):3600 p +="/"36013602 newPaths.append(p)36033604 self.depotPaths = newPaths36053606# --detect-branches may change this for each branch3607 self.branchPrefixes = self.depotPaths36083609 self.loadUserMapFromCache()3610 self.labels = {}3611if self.detectLabels:3612 self.getLabels();36133614if self.detectBranches:3615## FIXME - what's a P4 projectName ?3616 self.projectName = self.guessProjectName()36173618if self.hasOrigin:3619 self.getBranchMappingFromGitBranches()3620else:3621 self.getBranchMapping()3622if self.verbose:3623print"p4-git branches:%s"% self.p4BranchesInGit3624print"initial parents:%s"% self.initialParents3625for b in self.p4BranchesInGit:3626if b !="master":36273628## FIXME3629 b = b[len(self.projectName):]3630 self.createdBranches.add(b)36313632 self.tz ="%+03d%02d"% (- time.timezone /3600, ((- time.timezone %3600) /60))36333634 self.importProcess = subprocess.Popen(["git","fast-import"],3635 stdin=subprocess.PIPE,3636 stdout=subprocess.PIPE,3637 stderr=subprocess.PIPE);3638 self.gitOutput = self.importProcess.stdout3639 self.gitStream = self.importProcess.stdin3640 self.gitError = self.importProcess.stderr36413642if revision:3643 self.importHeadRevision(revision)3644else:3645 changes = []36463647iflen(self.changesFile) >0:3648 output =open(self.changesFile).readlines()3649 changeSet =set()3650for line in output:3651 changeSet.add(int(line))36523653for change in changeSet:3654 changes.append(change)36553656 changes.sort()3657else:3658# catch "git p4 sync" with no new branches, in a repo that3659# does not have any existing p4 branches3660iflen(args) ==0:3661if not self.p4BranchesInGit:3662die("No remote p4 branches. Perhaps you never did\"git p4 clone\"in here.")36633664# The default branch is master, unless --branch is used to3665# specify something else. Make sure it exists, or complain3666# nicely about how to use --branch.3667if not self.detectBranches:3668if notbranch_exists(self.branch):3669if branch_arg_given:3670die("Error: branch%sdoes not exist."% self.branch)3671else:3672die("Error: no branch%s; perhaps specify one with --branch."%3673 self.branch)36743675if self.verbose:3676print"Getting p4 changes for%s...%s"% (', '.join(self.depotPaths),3677 self.changeRange)3678 changes =p4ChangesForPaths(self.depotPaths, self.changeRange, self.changes_block_size)36793680iflen(self.maxChanges) >0:3681 changes = changes[:min(int(self.maxChanges),len(changes))]36823683iflen(changes) ==0:3684if not self.silent:3685print"No changes to import!"3686else:3687if not self.silent and not self.detectBranches:3688print"Import destination:%s"% self.branch36893690 self.updatedBranches =set()36913692if not self.detectBranches:3693if args:3694# start a new branch3695 self.initialParent =""3696else:3697# build on a previous revision3698 self.initialParent =parseRevision(self.branch)36993700 self.importChanges(changes)37013702if not self.silent:3703print""3704iflen(self.updatedBranches) >0:3705 sys.stdout.write("Updated branches: ")3706for b in self.updatedBranches:3707 sys.stdout.write("%s"% b)3708 sys.stdout.write("\n")37093710ifgitConfigBool("git-p4.importLabels"):3711 self.importLabels =True37123713if self.importLabels:3714 p4Labels =getP4Labels(self.depotPaths)3715 gitTags =getGitTags()37163717 missingP4Labels = p4Labels - gitTags3718 self.importP4Labels(self.gitStream, missingP4Labels)37193720 self.gitStream.close()3721if self.importProcess.wait() !=0:3722die("fast-import failed:%s"% self.gitError.read())3723 self.gitOutput.close()3724 self.gitError.close()37253726# Cleanup temporary branches created during import3727if self.tempBranches != []:3728for branch in self.tempBranches:3729read_pipe("git update-ref -d%s"% branch)3730 os.rmdir(os.path.join(os.environ.get("GIT_DIR",".git"), self.tempBranchLocation))37313732# Create a symbolic ref p4/HEAD pointing to p4/<branch> to allow3733# a convenient shortcut refname "p4".3734if self.importIntoRemotes:3735 head_ref = self.refPrefix +"HEAD"3736if notgitBranchExists(head_ref)andgitBranchExists(self.branch):3737system(["git","symbolic-ref", head_ref, self.branch])37383739return True37403741classP4Rebase(Command):3742def__init__(self):3743 Command.__init__(self)3744 self.options = [3745 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),3746]3747 self.importLabels =False3748 self.description = ("Fetches the latest revision from perforce and "3749+"rebases the current work (branch) against it")37503751defrun(self, args):3752 sync =P4Sync()3753 sync.importLabels = self.importLabels3754 sync.run([])37553756return self.rebase()37573758defrebase(self):3759if os.system("git update-index --refresh") !=0:3760die("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.");3761iflen(read_pipe("git diff-index HEAD --")) >0:3762die("You have uncommitted changes. Please commit them before rebasing or stash them away with git stash.");37633764[upstream, settings] =findUpstreamBranchPoint()3765iflen(upstream) ==0:3766die("Cannot find upstream branchpoint for rebase")37673768# the branchpoint may be p4/foo~3, so strip off the parent3769 upstream = re.sub("~[0-9]+$","", upstream)37703771print"Rebasing the current branch onto%s"% upstream3772 oldHead =read_pipe("git rev-parse HEAD").strip()3773system("git rebase%s"% upstream)3774system("git diff-tree --stat --summary -M%sHEAD --"% oldHead)3775return True37763777classP4Clone(P4Sync):3778def__init__(self):3779 P4Sync.__init__(self)3780 self.description ="Creates a new git repository and imports from Perforce into it"3781 self.usage ="usage: %prog [options] //depot/path[@revRange]"3782 self.options += [3783 optparse.make_option("--destination", dest="cloneDestination",3784 action='store', default=None,3785help="where to leave result of the clone"),3786 optparse.make_option("--bare", dest="cloneBare",3787 action="store_true", default=False),3788]3789 self.cloneDestination =None3790 self.needsGit =False3791 self.cloneBare =False37923793defdefaultDestination(self, args):3794## TODO: use common prefix of args?3795 depotPath = args[0]3796 depotDir = re.sub("(@[^@]*)$","", depotPath)3797 depotDir = re.sub("(#[^#]*)$","", depotDir)3798 depotDir = re.sub(r"\.\.\.$","", depotDir)3799 depotDir = re.sub(r"/$","", depotDir)3800return os.path.split(depotDir)[1]38013802defrun(self, args):3803iflen(args) <1:3804return False38053806if self.keepRepoPath and not self.cloneDestination:3807 sys.stderr.write("Must specify destination for --keep-path\n")3808 sys.exit(1)38093810 depotPaths = args38113812if not self.cloneDestination andlen(depotPaths) >1:3813 self.cloneDestination = depotPaths[-1]3814 depotPaths = depotPaths[:-1]38153816 self.cloneExclude = ["/"+p for p in self.cloneExclude]3817for p in depotPaths:3818if not p.startswith("//"):3819 sys.stderr.write('Depot paths must start with "//":%s\n'% p)3820return False38213822if not self.cloneDestination:3823 self.cloneDestination = self.defaultDestination(args)38243825print"Importing from%sinto%s"% (', '.join(depotPaths), self.cloneDestination)38263827if not os.path.exists(self.cloneDestination):3828 os.makedirs(self.cloneDestination)3829chdir(self.cloneDestination)38303831 init_cmd = ["git","init"]3832if self.cloneBare:3833 init_cmd.append("--bare")3834 retcode = subprocess.call(init_cmd)3835if retcode:3836raiseCalledProcessError(retcode, init_cmd)38373838if not P4Sync.run(self, depotPaths):3839return False38403841# create a master branch and check out a work tree3842ifgitBranchExists(self.branch):3843system(["git","branch","master", self.branch ])3844if not self.cloneBare:3845system(["git","checkout","-f"])3846else:3847print'Not checking out any branch, use ' \3848'"git checkout -q -b master <branch>"'38493850# auto-set this variable if invoked with --use-client-spec3851if self.useClientSpec_from_options:3852system("git config --bool git-p4.useclientspec true")38533854return True38553856classP4Branches(Command):3857def__init__(self):3858 Command.__init__(self)3859 self.options = [ ]3860 self.description = ("Shows the git branches that hold imports and their "3861+"corresponding perforce depot paths")3862 self.verbose =False38633864defrun(self, args):3865iforiginP4BranchesExist():3866createOrUpdateBranchesFromOrigin()38673868 cmdline ="git rev-parse --symbolic "3869 cmdline +=" --remotes"38703871for line inread_pipe_lines(cmdline):3872 line = line.strip()38733874if not line.startswith('p4/')or line =="p4/HEAD":3875continue3876 branch = line38773878 log =extractLogMessageFromGitCommit("refs/remotes/%s"% branch)3879 settings =extractSettingsGitLog(log)38803881print"%s<=%s(%s)"% (branch,",".join(settings["depot-paths"]), settings["change"])3882return True38833884classHelpFormatter(optparse.IndentedHelpFormatter):3885def__init__(self):3886 optparse.IndentedHelpFormatter.__init__(self)38873888defformat_description(self, description):3889if description:3890return description +"\n"3891else:3892return""38933894defprintUsage(commands):3895print"usage:%s<command> [options]"% sys.argv[0]3896print""3897print"valid commands:%s"%", ".join(commands)3898print""3899print"Try%s<command> --help for command specific help."% sys.argv[0]3900print""39013902commands = {3903"debug": P4Debug,3904"submit": P4Submit,3905"commit": P4Submit,3906"sync": P4Sync,3907"rebase": P4Rebase,3908"clone": P4Clone,3909"rollback": P4RollBack,3910"branches": P4Branches3911}391239133914defmain():3915iflen(sys.argv[1:]) ==0:3916printUsage(commands.keys())3917 sys.exit(2)39183919 cmdName = sys.argv[1]3920try:3921 klass = commands[cmdName]3922 cmd =klass()3923exceptKeyError:3924print"unknown command%s"% cmdName3925print""3926printUsage(commands.keys())3927 sys.exit(2)39283929 options = cmd.options3930 cmd.gitdir = os.environ.get("GIT_DIR",None)39313932 args = sys.argv[2:]39333934 options.append(optparse.make_option("--verbose","-v", dest="verbose", action="store_true"))3935if cmd.needsGit:3936 options.append(optparse.make_option("--git-dir", dest="gitdir"))39373938 parser = optparse.OptionParser(cmd.usage.replace("%prog","%prog "+ cmdName),3939 options,3940 description = cmd.description,3941 formatter =HelpFormatter())39423943(cmd, args) = parser.parse_args(sys.argv[2:], cmd);3944global verbose3945 verbose = cmd.verbose3946if cmd.needsGit:3947if cmd.gitdir ==None:3948 cmd.gitdir = os.path.abspath(".git")3949if notisValidGitDir(cmd.gitdir):3950# "rev-parse --git-dir" without arguments will try $PWD/.git3951 cmd.gitdir =read_pipe("git rev-parse --git-dir").strip()3952if os.path.exists(cmd.gitdir):3953 cdup =read_pipe("git rev-parse --show-cdup").strip()3954iflen(cdup) >0:3955chdir(cdup);39563957if notisValidGitDir(cmd.gitdir):3958ifisValidGitDir(cmd.gitdir +"/.git"):3959 cmd.gitdir +="/.git"3960else:3961die("fatal: cannot locate git repository at%s"% cmd.gitdir)39623963# so git commands invoked from the P4 workspace will succeed3964 os.environ["GIT_DIR"] = cmd.gitdir39653966if not cmd.run(args):3967 parser.print_help()3968 sys.exit(2)396939703971if __name__ =='__main__':3972main()