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 24 25verbose =False 26 27# Only labels/tags matching this will be imported/exported 28defaultLabelRegexp = r'[a-zA-Z0-9_\-.]+$' 29 30defp4_build_cmd(cmd): 31"""Build a suitable p4 command line. 32 33 This consolidates building and returning a p4 command line into one 34 location. It means that hooking into the environment, or other configuration 35 can be done more easily. 36 """ 37 real_cmd = ["p4"] 38 39 user =gitConfig("git-p4.user") 40iflen(user) >0: 41 real_cmd += ["-u",user] 42 43 password =gitConfig("git-p4.password") 44iflen(password) >0: 45 real_cmd += ["-P", password] 46 47 port =gitConfig("git-p4.port") 48iflen(port) >0: 49 real_cmd += ["-p", port] 50 51 host =gitConfig("git-p4.host") 52iflen(host) >0: 53 real_cmd += ["-H", host] 54 55 client =gitConfig("git-p4.client") 56iflen(client) >0: 57 real_cmd += ["-c", client] 58 59 60ifisinstance(cmd,basestring): 61 real_cmd =' '.join(real_cmd) +' '+ cmd 62else: 63 real_cmd += cmd 64return real_cmd 65 66defchdir(dir): 67# P4 uses the PWD environment variable rather than getcwd(). Since we're 68# not using the shell, we have to set it ourselves. This path could 69# be relative, so go there first, then figure out where we ended up. 70 os.chdir(dir) 71 os.environ['PWD'] = os.getcwd() 72 73defdie(msg): 74if verbose: 75raiseException(msg) 76else: 77 sys.stderr.write(msg +"\n") 78 sys.exit(1) 79 80defwrite_pipe(c, stdin): 81if verbose: 82 sys.stderr.write('Writing pipe:%s\n'%str(c)) 83 84 expand =isinstance(c,basestring) 85 p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand) 86 pipe = p.stdin 87 val = pipe.write(stdin) 88 pipe.close() 89if p.wait(): 90die('Command failed:%s'%str(c)) 91 92return val 93 94defp4_write_pipe(c, stdin): 95 real_cmd =p4_build_cmd(c) 96returnwrite_pipe(real_cmd, stdin) 97 98defread_pipe(c, ignore_error=False): 99if verbose: 100 sys.stderr.write('Reading pipe:%s\n'%str(c)) 101 102 expand =isinstance(c,basestring) 103 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand) 104 pipe = p.stdout 105 val = pipe.read() 106if p.wait()and not ignore_error: 107die('Command failed:%s'%str(c)) 108 109return val 110 111defp4_read_pipe(c, ignore_error=False): 112 real_cmd =p4_build_cmd(c) 113returnread_pipe(real_cmd, ignore_error) 114 115defread_pipe_lines(c): 116if verbose: 117 sys.stderr.write('Reading pipe:%s\n'%str(c)) 118 119 expand =isinstance(c, basestring) 120 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand) 121 pipe = p.stdout 122 val = pipe.readlines() 123if pipe.close()or p.wait(): 124die('Command failed:%s'%str(c)) 125 126return val 127 128defp4_read_pipe_lines(c): 129"""Specifically invoke p4 on the command supplied. """ 130 real_cmd =p4_build_cmd(c) 131returnread_pipe_lines(real_cmd) 132 133defp4_has_command(cmd): 134"""Ask p4 for help on this command. If it returns an error, the 135 command does not exist in this version of p4.""" 136 real_cmd =p4_build_cmd(["help", cmd]) 137 p = subprocess.Popen(real_cmd, stdout=subprocess.PIPE, 138 stderr=subprocess.PIPE) 139 p.communicate() 140return p.returncode ==0 141 142defp4_has_move_command(): 143"""See if the move command exists, that it supports -k, and that 144 it has not been administratively disabled. The arguments 145 must be correct, but the filenames do not have to exist. Use 146 ones with wildcards so even if they exist, it will fail.""" 147 148if notp4_has_command("move"): 149return False 150 cmd =p4_build_cmd(["move","-k","@from","@to"]) 151 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 152(out, err) = p.communicate() 153# return code will be 1 in either case 154if err.find("Invalid option") >=0: 155return False 156if err.find("disabled") >=0: 157return False 158# assume it failed because @... was invalid changelist 159return True 160 161defsystem(cmd): 162 expand =isinstance(cmd,basestring) 163if verbose: 164 sys.stderr.write("executing%s\n"%str(cmd)) 165 subprocess.check_call(cmd, shell=expand) 166 167defp4_system(cmd): 168"""Specifically invoke p4 as the system command. """ 169 real_cmd =p4_build_cmd(cmd) 170 expand =isinstance(real_cmd, basestring) 171 subprocess.check_call(real_cmd, shell=expand) 172 173_p4_version_string =None 174defp4_version_string(): 175"""Read the version string, showing just the last line, which 176 hopefully is the interesting version bit. 177 178 $ p4 -V 179 Perforce - The Fast Software Configuration Management System. 180 Copyright 1995-2011 Perforce Software. All rights reserved. 181 Rev. P4/NTX86/2011.1/393975 (2011/12/16). 182 """ 183global _p4_version_string 184if not _p4_version_string: 185 a =p4_read_pipe_lines(["-V"]) 186 _p4_version_string = a[-1].rstrip() 187return _p4_version_string 188 189defp4_integrate(src, dest): 190p4_system(["integrate","-Dt",wildcard_encode(src),wildcard_encode(dest)]) 191 192defp4_sync(f, *options): 193p4_system(["sync"] +list(options) + [wildcard_encode(f)]) 194 195defp4_add(f): 196# forcibly add file names with wildcards 197ifwildcard_present(f): 198p4_system(["add","-f", f]) 199else: 200p4_system(["add", f]) 201 202defp4_delete(f): 203p4_system(["delete",wildcard_encode(f)]) 204 205defp4_edit(f): 206p4_system(["edit",wildcard_encode(f)]) 207 208defp4_revert(f): 209p4_system(["revert",wildcard_encode(f)]) 210 211defp4_reopen(type, f): 212p4_system(["reopen","-t",type,wildcard_encode(f)]) 213 214defp4_move(src, dest): 215p4_system(["move","-k",wildcard_encode(src),wildcard_encode(dest)]) 216 217defp4_describe(change): 218"""Make sure it returns a valid result by checking for 219 the presence of field "time". Return a dict of the 220 results.""" 221 222 ds =p4CmdList(["describe","-s",str(change)]) 223iflen(ds) !=1: 224die("p4 describe -s%ddid not return 1 result:%s"% (change,str(ds))) 225 226 d = ds[0] 227 228if"p4ExitCode"in d: 229die("p4 describe -s%dexited with%d:%s"% (change, d["p4ExitCode"], 230str(d))) 231if"code"in d: 232if d["code"] =="error": 233die("p4 describe -s%dreturned error code:%s"% (change,str(d))) 234 235if"time"not in d: 236die("p4 describe -s%dreturned no\"time\":%s"% (change,str(d))) 237 238return d 239 240# 241# Canonicalize the p4 type and return a tuple of the 242# base type, plus any modifiers. See "p4 help filetypes" 243# for a list and explanation. 244# 245defsplit_p4_type(p4type): 246 247 p4_filetypes_historical = { 248"ctempobj":"binary+Sw", 249"ctext":"text+C", 250"cxtext":"text+Cx", 251"ktext":"text+k", 252"kxtext":"text+kx", 253"ltext":"text+F", 254"tempobj":"binary+FSw", 255"ubinary":"binary+F", 256"uresource":"resource+F", 257"uxbinary":"binary+Fx", 258"xbinary":"binary+x", 259"xltext":"text+Fx", 260"xtempobj":"binary+Swx", 261"xtext":"text+x", 262"xunicode":"unicode+x", 263"xutf16":"utf16+x", 264} 265if p4type in p4_filetypes_historical: 266 p4type = p4_filetypes_historical[p4type] 267 mods ="" 268 s = p4type.split("+") 269 base = s[0] 270 mods ="" 271iflen(s) >1: 272 mods = s[1] 273return(base, mods) 274 275# 276# return the raw p4 type of a file (text, text+ko, etc) 277# 278defp4_type(file): 279 results =p4CmdList(["fstat","-T","headType",file]) 280return results[0]['headType'] 281 282# 283# Given a type base and modifier, return a regexp matching 284# the keywords that can be expanded in the file 285# 286defp4_keywords_regexp_for_type(base, type_mods): 287if base in("text","unicode","binary"): 288 kwords =None 289if"ko"in type_mods: 290 kwords ='Id|Header' 291elif"k"in type_mods: 292 kwords ='Id|Header|Author|Date|DateTime|Change|File|Revision' 293else: 294return None 295 pattern = r""" 296 \$ # Starts with a dollar, followed by... 297 (%s) # one of the keywords, followed by... 298 (:[^$\n]+)? # possibly an old expansion, followed by... 299 \$ # another dollar 300 """% kwords 301return pattern 302else: 303return None 304 305# 306# Given a file, return a regexp matching the possible 307# RCS keywords that will be expanded, or None for files 308# with kw expansion turned off. 309# 310defp4_keywords_regexp_for_file(file): 311if not os.path.exists(file): 312return None 313else: 314(type_base, type_mods) =split_p4_type(p4_type(file)) 315returnp4_keywords_regexp_for_type(type_base, type_mods) 316 317defsetP4ExecBit(file, mode): 318# Reopens an already open file and changes the execute bit to match 319# the execute bit setting in the passed in mode. 320 321 p4Type ="+x" 322 323if notisModeExec(mode): 324 p4Type =getP4OpenedType(file) 325 p4Type = re.sub('^([cku]?)x(.*)','\\1\\2', p4Type) 326 p4Type = re.sub('(.*?\+.*?)x(.*?)','\\1\\2', p4Type) 327if p4Type[-1] =="+": 328 p4Type = p4Type[0:-1] 329 330p4_reopen(p4Type,file) 331 332defgetP4OpenedType(file): 333# Returns the perforce file type for the given file. 334 335 result =p4_read_pipe(["opened",wildcard_encode(file)]) 336 match = re.match(".*\((.+)\)\r?$", result) 337if match: 338return match.group(1) 339else: 340die("Could not determine file type for%s(result: '%s')"% (file, result)) 341 342# Return the set of all p4 labels 343defgetP4Labels(depotPaths): 344 labels =set() 345ifisinstance(depotPaths,basestring): 346 depotPaths = [depotPaths] 347 348for l inp4CmdList(["labels"] + ["%s..."% p for p in depotPaths]): 349 label = l['label'] 350 labels.add(label) 351 352return labels 353 354# Return the set of all git tags 355defgetGitTags(): 356 gitTags =set() 357for line inread_pipe_lines(["git","tag"]): 358 tag = line.strip() 359 gitTags.add(tag) 360return gitTags 361 362defdiffTreePattern(): 363# This is a simple generator for the diff tree regex pattern. This could be 364# a class variable if this and parseDiffTreeEntry were a part of a class. 365 pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)') 366while True: 367yield pattern 368 369defparseDiffTreeEntry(entry): 370"""Parses a single diff tree entry into its component elements. 371 372 See git-diff-tree(1) manpage for details about the format of the diff 373 output. This method returns a dictionary with the following elements: 374 375 src_mode - The mode of the source file 376 dst_mode - The mode of the destination file 377 src_sha1 - The sha1 for the source file 378 dst_sha1 - The sha1 fr the destination file 379 status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc) 380 status_score - The score for the status (applicable for 'C' and 'R' 381 statuses). This is None if there is no score. 382 src - The path for the source file. 383 dst - The path for the destination file. This is only present for 384 copy or renames. If it is not present, this is None. 385 386 If the pattern is not matched, None is returned.""" 387 388 match =diffTreePattern().next().match(entry) 389if match: 390return{ 391'src_mode': match.group(1), 392'dst_mode': match.group(2), 393'src_sha1': match.group(3), 394'dst_sha1': match.group(4), 395'status': match.group(5), 396'status_score': match.group(6), 397'src': match.group(7), 398'dst': match.group(10) 399} 400return None 401 402defisModeExec(mode): 403# Returns True if the given git mode represents an executable file, 404# otherwise False. 405return mode[-3:] =="755" 406 407defisModeExecChanged(src_mode, dst_mode): 408returnisModeExec(src_mode) !=isModeExec(dst_mode) 409 410defp4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None): 411 412ifisinstance(cmd,basestring): 413 cmd ="-G "+ cmd 414 expand =True 415else: 416 cmd = ["-G"] + cmd 417 expand =False 418 419 cmd =p4_build_cmd(cmd) 420if verbose: 421 sys.stderr.write("Opening pipe:%s\n"%str(cmd)) 422 423# Use a temporary file to avoid deadlocks without 424# subprocess.communicate(), which would put another copy 425# of stdout into memory. 426 stdin_file =None 427if stdin is not None: 428 stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode) 429ifisinstance(stdin,basestring): 430 stdin_file.write(stdin) 431else: 432for i in stdin: 433 stdin_file.write(i +'\n') 434 stdin_file.flush() 435 stdin_file.seek(0) 436 437 p4 = subprocess.Popen(cmd, 438 shell=expand, 439 stdin=stdin_file, 440 stdout=subprocess.PIPE) 441 442 result = [] 443try: 444while True: 445 entry = marshal.load(p4.stdout) 446if cb is not None: 447cb(entry) 448else: 449 result.append(entry) 450exceptEOFError: 451pass 452 exitCode = p4.wait() 453if exitCode !=0: 454 entry = {} 455 entry["p4ExitCode"] = exitCode 456 result.append(entry) 457 458return result 459 460defp4Cmd(cmd): 461list=p4CmdList(cmd) 462 result = {} 463for entry inlist: 464 result.update(entry) 465return result; 466 467defp4Where(depotPath): 468if not depotPath.endswith("/"): 469 depotPath +="/" 470 depotPath = depotPath +"..." 471 outputList =p4CmdList(["where", depotPath]) 472 output =None 473for entry in outputList: 474if"depotFile"in entry: 475if entry["depotFile"] == depotPath: 476 output = entry 477break 478elif"data"in entry: 479 data = entry.get("data") 480 space = data.find(" ") 481if data[:space] == depotPath: 482 output = entry 483break 484if output ==None: 485return"" 486if output["code"] =="error": 487return"" 488 clientPath ="" 489if"path"in output: 490 clientPath = output.get("path") 491elif"data"in output: 492 data = output.get("data") 493 lastSpace = data.rfind(" ") 494 clientPath = data[lastSpace +1:] 495 496if clientPath.endswith("..."): 497 clientPath = clientPath[:-3] 498return clientPath 499 500defcurrentGitBranch(): 501returnread_pipe("git name-rev HEAD").split(" ")[1].strip() 502 503defisValidGitDir(path): 504if(os.path.exists(path +"/HEAD") 505and os.path.exists(path +"/refs")and os.path.exists(path +"/objects")): 506return True; 507return False 508 509defparseRevision(ref): 510returnread_pipe("git rev-parse%s"% ref).strip() 511 512defbranchExists(ref): 513 rev =read_pipe(["git","rev-parse","-q","--verify", ref], 514 ignore_error=True) 515returnlen(rev) >0 516 517defextractLogMessageFromGitCommit(commit): 518 logMessage ="" 519 520## fixme: title is first line of commit, not 1st paragraph. 521 foundTitle =False 522for log inread_pipe_lines("git cat-file commit%s"% commit): 523if not foundTitle: 524iflen(log) ==1: 525 foundTitle =True 526continue 527 528 logMessage += log 529return logMessage 530 531defextractSettingsGitLog(log): 532 values = {} 533for line in log.split("\n"): 534 line = line.strip() 535 m = re.search(r"^ *\[git-p4: (.*)\]$", line) 536if not m: 537continue 538 539 assignments = m.group(1).split(':') 540for a in assignments: 541 vals = a.split('=') 542 key = vals[0].strip() 543 val = ('='.join(vals[1:])).strip() 544if val.endswith('\"')and val.startswith('"'): 545 val = val[1:-1] 546 547 values[key] = val 548 549 paths = values.get("depot-paths") 550if not paths: 551 paths = values.get("depot-path") 552if paths: 553 values['depot-paths'] = paths.split(',') 554return values 555 556defgitBranchExists(branch): 557 proc = subprocess.Popen(["git","rev-parse", branch], 558 stderr=subprocess.PIPE, stdout=subprocess.PIPE); 559return proc.wait() ==0; 560 561_gitConfig = {} 562defgitConfig(key, args =None):# set args to "--bool", for instance 563if not _gitConfig.has_key(key): 564 argsFilter ="" 565if args !=None: 566 argsFilter ="%s"% args 567 cmd ="git config%s%s"% (argsFilter, key) 568 _gitConfig[key] =read_pipe(cmd, ignore_error=True).strip() 569return _gitConfig[key] 570 571defgitConfigList(key): 572if not _gitConfig.has_key(key): 573 _gitConfig[key] =read_pipe("git config --get-all%s"% key, ignore_error=True).strip().split(os.linesep) 574return _gitConfig[key] 575 576defp4BranchesInGit(branchesAreInRemotes=True): 577"""Find all the branches whose names start with "p4/", looking 578 in remotes or heads as specified by the argument. Return 579 a dictionary of{ branch: revision }for each one found. 580 The branch names are the short names, without any 581 "p4/" prefix.""" 582 583 branches = {} 584 585 cmdline ="git rev-parse --symbolic " 586if branchesAreInRemotes: 587 cmdline +="--remotes" 588else: 589 cmdline +="--branches" 590 591for line inread_pipe_lines(cmdline): 592 line = line.strip() 593 594# only import to p4/ 595if not line.startswith('p4/'): 596continue 597# special symbolic ref to p4/master 598if line =="p4/HEAD": 599continue 600 601# strip off p4/ prefix 602 branch = line[len("p4/"):] 603 604 branches[branch] =parseRevision(line) 605 606return branches 607 608defbranch_exists(branch): 609"""Make sure that the given ref name really exists.""" 610 611 cmd = ["git","rev-parse","--symbolic","--verify", branch ] 612 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 613 out, _ = p.communicate() 614if p.returncode: 615return False 616# expect exactly one line of output: the branch name 617return out.rstrip() == branch 618 619deffindUpstreamBranchPoint(head ="HEAD"): 620 branches =p4BranchesInGit() 621# map from depot-path to branch name 622 branchByDepotPath = {} 623for branch in branches.keys(): 624 tip = branches[branch] 625 log =extractLogMessageFromGitCommit(tip) 626 settings =extractSettingsGitLog(log) 627if settings.has_key("depot-paths"): 628 paths =",".join(settings["depot-paths"]) 629 branchByDepotPath[paths] ="remotes/p4/"+ branch 630 631 settings =None 632 parent =0 633while parent <65535: 634 commit = head +"~%s"% parent 635 log =extractLogMessageFromGitCommit(commit) 636 settings =extractSettingsGitLog(log) 637if settings.has_key("depot-paths"): 638 paths =",".join(settings["depot-paths"]) 639if branchByDepotPath.has_key(paths): 640return[branchByDepotPath[paths], settings] 641 642 parent = parent +1 643 644return["", settings] 645 646defcreateOrUpdateBranchesFromOrigin(localRefPrefix ="refs/remotes/p4/", silent=True): 647if not silent: 648print("Creating/updating branch(es) in%sbased on origin branch(es)" 649% localRefPrefix) 650 651 originPrefix ="origin/p4/" 652 653for line inread_pipe_lines("git rev-parse --symbolic --remotes"): 654 line = line.strip() 655if(not line.startswith(originPrefix))or line.endswith("HEAD"): 656continue 657 658 headName = line[len(originPrefix):] 659 remoteHead = localRefPrefix + headName 660 originHead = line 661 662 original =extractSettingsGitLog(extractLogMessageFromGitCommit(originHead)) 663if(not original.has_key('depot-paths') 664or not original.has_key('change')): 665continue 666 667 update =False 668if notgitBranchExists(remoteHead): 669if verbose: 670print"creating%s"% remoteHead 671 update =True 672else: 673 settings =extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead)) 674if settings.has_key('change') >0: 675if settings['depot-paths'] == original['depot-paths']: 676 originP4Change =int(original['change']) 677 p4Change =int(settings['change']) 678if originP4Change > p4Change: 679print("%s(%s) is newer than%s(%s). " 680"Updating p4 branch from origin." 681% (originHead, originP4Change, 682 remoteHead, p4Change)) 683 update =True 684else: 685print("Ignoring:%swas imported from%swhile " 686"%swas imported from%s" 687% (originHead,','.join(original['depot-paths']), 688 remoteHead,','.join(settings['depot-paths']))) 689 690if update: 691system("git update-ref%s %s"% (remoteHead, originHead)) 692 693deforiginP4BranchesExist(): 694returngitBranchExists("origin")orgitBranchExists("origin/p4")orgitBranchExists("origin/p4/master") 695 696defp4ChangesForPaths(depotPaths, changeRange): 697assert depotPaths 698 cmd = ['changes'] 699for p in depotPaths: 700 cmd += ["%s...%s"% (p, changeRange)] 701 output =p4_read_pipe_lines(cmd) 702 703 changes = {} 704for line in output: 705 changeNum =int(line.split(" ")[1]) 706 changes[changeNum] =True 707 708 changelist = changes.keys() 709 changelist.sort() 710return changelist 711 712defp4PathStartsWith(path, prefix): 713# This method tries to remedy a potential mixed-case issue: 714# 715# If UserA adds //depot/DirA/file1 716# and UserB adds //depot/dira/file2 717# 718# we may or may not have a problem. If you have core.ignorecase=true, 719# we treat DirA and dira as the same directory 720 ignorecase =gitConfig("core.ignorecase","--bool") =="true" 721if ignorecase: 722return path.lower().startswith(prefix.lower()) 723return path.startswith(prefix) 724 725defgetClientSpec(): 726"""Look at the p4 client spec, create a View() object that contains 727 all the mappings, and return it.""" 728 729 specList =p4CmdList("client -o") 730iflen(specList) !=1: 731die('Output from "client -o" is%dlines, expecting 1'% 732len(specList)) 733 734# dictionary of all client parameters 735 entry = specList[0] 736 737# just the keys that start with "View" 738 view_keys = [ k for k in entry.keys()if k.startswith("View") ] 739 740# hold this new View 741 view =View() 742 743# append the lines, in order, to the view 744for view_num inrange(len(view_keys)): 745 k ="View%d"% view_num 746if k not in view_keys: 747die("Expected view key%smissing"% k) 748 view.append(entry[k]) 749 750return view 751 752defgetClientRoot(): 753"""Grab the client directory.""" 754 755 output =p4CmdList("client -o") 756iflen(output) !=1: 757die('Output from "client -o" is%dlines, expecting 1'%len(output)) 758 759 entry = output[0] 760if"Root"not in entry: 761die('Client has no "Root"') 762 763return entry["Root"] 764 765# 766# P4 wildcards are not allowed in filenames. P4 complains 767# if you simply add them, but you can force it with "-f", in 768# which case it translates them into %xx encoding internally. 769# 770defwildcard_decode(path): 771# Search for and fix just these four characters. Do % last so 772# that fixing it does not inadvertently create new %-escapes. 773# Cannot have * in a filename in windows; untested as to 774# what p4 would do in such a case. 775if not platform.system() =="Windows": 776 path = path.replace("%2A","*") 777 path = path.replace("%23","#") \ 778.replace("%40","@") \ 779.replace("%25","%") 780return path 781 782defwildcard_encode(path): 783# do % first to avoid double-encoding the %s introduced here 784 path = path.replace("%","%25") \ 785.replace("*","%2A") \ 786.replace("#","%23") \ 787.replace("@","%40") 788return path 789 790defwildcard_present(path): 791return path.translate(None,"*#@%") != path 792 793class Command: 794def__init__(self): 795 self.usage ="usage: %prog [options]" 796 self.needsGit =True 797 self.verbose =False 798 799class P4UserMap: 800def__init__(self): 801 self.userMapFromPerforceServer =False 802 self.myP4UserId =None 803 804defp4UserId(self): 805if self.myP4UserId: 806return self.myP4UserId 807 808 results =p4CmdList("user -o") 809for r in results: 810if r.has_key('User'): 811 self.myP4UserId = r['User'] 812return r['User'] 813die("Could not find your p4 user id") 814 815defp4UserIsMe(self, p4User): 816# return True if the given p4 user is actually me 817 me = self.p4UserId() 818if not p4User or p4User != me: 819return False 820else: 821return True 822 823defgetUserCacheFilename(self): 824 home = os.environ.get("HOME", os.environ.get("USERPROFILE")) 825return home +"/.gitp4-usercache.txt" 826 827defgetUserMapFromPerforceServer(self): 828if self.userMapFromPerforceServer: 829return 830 self.users = {} 831 self.emails = {} 832 833for output inp4CmdList("users"): 834if not output.has_key("User"): 835continue 836 self.users[output["User"]] = output["FullName"] +" <"+ output["Email"] +">" 837 self.emails[output["Email"]] = output["User"] 838 839 840 s ='' 841for(key, val)in self.users.items(): 842 s +="%s\t%s\n"% (key.expandtabs(1), val.expandtabs(1)) 843 844open(self.getUserCacheFilename(),"wb").write(s) 845 self.userMapFromPerforceServer =True 846 847defloadUserMapFromCache(self): 848 self.users = {} 849 self.userMapFromPerforceServer =False 850try: 851 cache =open(self.getUserCacheFilename(),"rb") 852 lines = cache.readlines() 853 cache.close() 854for line in lines: 855 entry = line.strip().split("\t") 856 self.users[entry[0]] = entry[1] 857exceptIOError: 858 self.getUserMapFromPerforceServer() 859 860classP4Debug(Command): 861def__init__(self): 862 Command.__init__(self) 863 self.options = [] 864 self.description ="A tool to debug the output of p4 -G." 865 self.needsGit =False 866 867defrun(self, args): 868 j =0 869for output inp4CmdList(args): 870print'Element:%d'% j 871 j +=1 872print output 873return True 874 875classP4RollBack(Command): 876def__init__(self): 877 Command.__init__(self) 878 self.options = [ 879 optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true") 880] 881 self.description ="A tool to debug the multi-branch import. Don't use :)" 882 self.rollbackLocalBranches =False 883 884defrun(self, args): 885iflen(args) !=1: 886return False 887 maxChange =int(args[0]) 888 889if"p4ExitCode"inp4Cmd("changes -m 1"): 890die("Problems executing p4"); 891 892if self.rollbackLocalBranches: 893 refPrefix ="refs/heads/" 894 lines =read_pipe_lines("git rev-parse --symbolic --branches") 895else: 896 refPrefix ="refs/remotes/" 897 lines =read_pipe_lines("git rev-parse --symbolic --remotes") 898 899for line in lines: 900if self.rollbackLocalBranches or(line.startswith("p4/")and line !="p4/HEAD\n"): 901 line = line.strip() 902 ref = refPrefix + line 903 log =extractLogMessageFromGitCommit(ref) 904 settings =extractSettingsGitLog(log) 905 906 depotPaths = settings['depot-paths'] 907 change = settings['change'] 908 909 changed =False 910 911iflen(p4Cmd("changes -m 1 "+' '.join(['%s...@%s'% (p, maxChange) 912for p in depotPaths]))) ==0: 913print"Branch%sdid not exist at change%s, deleting."% (ref, maxChange) 914system("git update-ref -d%s`git rev-parse%s`"% (ref, ref)) 915continue 916 917while change andint(change) > maxChange: 918 changed =True 919if self.verbose: 920print"%sis at%s; rewinding towards%s"% (ref, change, maxChange) 921system("git update-ref%s\"%s^\""% (ref, ref)) 922 log =extractLogMessageFromGitCommit(ref) 923 settings =extractSettingsGitLog(log) 924 925 926 depotPaths = settings['depot-paths'] 927 change = settings['change'] 928 929if changed: 930print"%srewound to%s"% (ref, change) 931 932return True 933 934classP4Submit(Command, P4UserMap): 935 936 conflict_behavior_choices = ("ask","skip","quit") 937 938def__init__(self): 939 Command.__init__(self) 940 P4UserMap.__init__(self) 941 self.options = [ 942 optparse.make_option("--origin", dest="origin"), 943 optparse.make_option("-M", dest="detectRenames", action="store_true"), 944# preserve the user, requires relevant p4 permissions 945 optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"), 946 optparse.make_option("--export-labels", dest="exportLabels", action="store_true"), 947 optparse.make_option("--dry-run","-n", dest="dry_run", action="store_true"), 948 optparse.make_option("--prepare-p4-only", dest="prepare_p4_only", action="store_true"), 949 optparse.make_option("--conflict", dest="conflict_behavior", 950 choices=self.conflict_behavior_choices), 951 optparse.make_option("--branch", dest="branch"), 952] 953 self.description ="Submit changes from git to the perforce depot." 954 self.usage +=" [name of git branch to submit into perforce depot]" 955 self.origin ="" 956 self.detectRenames =False 957 self.preserveUser =gitConfig("git-p4.preserveUser").lower() =="true" 958 self.dry_run =False 959 self.prepare_p4_only =False 960 self.conflict_behavior =None 961 self.isWindows = (platform.system() =="Windows") 962 self.exportLabels =False 963 self.p4HasMoveCommand =p4_has_move_command() 964 self.branch =None 965 966defcheck(self): 967iflen(p4CmdList("opened ...")) >0: 968die("You have files opened with perforce! Close them before starting the sync.") 969 970defseparate_jobs_from_description(self, message): 971"""Extract and return a possible Jobs field in the commit 972 message. It goes into a separate section in the p4 change 973 specification. 974 975 A jobs line starts with "Jobs:" and looks like a new field 976 in a form. Values are white-space separated on the same 977 line or on following lines that start with a tab. 978 979 This does not parse and extract the full git commit message 980 like a p4 form. It just sees the Jobs: line as a marker 981 to pass everything from then on directly into the p4 form, 982 but outside the description section. 983 984 Return a tuple (stripped log message, jobs string).""" 985 986 m = re.search(r'^Jobs:', message, re.MULTILINE) 987if m is None: 988return(message,None) 989 990 jobtext = message[m.start():] 991 stripped_message = message[:m.start()].rstrip() 992return(stripped_message, jobtext) 993 994defprepareLogMessage(self, template, message, jobs): 995"""Edits the template returned from "p4 change -o" to insert 996 the message in the Description field, and the jobs text in 997 the Jobs field.""" 998 result ="" 9991000 inDescriptionSection =False10011002for line in template.split("\n"):1003if line.startswith("#"):1004 result += line +"\n"1005continue10061007if inDescriptionSection:1008if line.startswith("Files:")or line.startswith("Jobs:"):1009 inDescriptionSection =False1010# insert Jobs section1011if jobs:1012 result += jobs +"\n"1013else:1014continue1015else:1016if line.startswith("Description:"):1017 inDescriptionSection =True1018 line +="\n"1019for messageLine in message.split("\n"):1020 line +="\t"+ messageLine +"\n"10211022 result += line +"\n"10231024return result10251026defpatchRCSKeywords(self,file, pattern):1027# Attempt to zap the RCS keywords in a p4 controlled file matching the given pattern1028(handle, outFileName) = tempfile.mkstemp(dir='.')1029try:1030 outFile = os.fdopen(handle,"w+")1031 inFile =open(file,"r")1032 regexp = re.compile(pattern, re.VERBOSE)1033for line in inFile.readlines():1034 line = regexp.sub(r'$\1$', line)1035 outFile.write(line)1036 inFile.close()1037 outFile.close()1038# Forcibly overwrite the original file1039 os.unlink(file)1040 shutil.move(outFileName,file)1041except:1042# cleanup our temporary file1043 os.unlink(outFileName)1044print"Failed to strip RCS keywords in%s"%file1045raise10461047print"Patched up RCS keywords in%s"%file10481049defp4UserForCommit(self,id):1050# Return the tuple (perforce user,git email) for a given git commit id1051 self.getUserMapFromPerforceServer()1052 gitEmail =read_pipe("git log --max-count=1 --format='%%ae'%s"%id)1053 gitEmail = gitEmail.strip()1054if not self.emails.has_key(gitEmail):1055return(None,gitEmail)1056else:1057return(self.emails[gitEmail],gitEmail)10581059defcheckValidP4Users(self,commits):1060# check if any git authors cannot be mapped to p4 users1061foridin commits:1062(user,email) = self.p4UserForCommit(id)1063if not user:1064 msg ="Cannot find p4 user for email%sin commit%s."% (email,id)1065ifgitConfig('git-p4.allowMissingP4Users').lower() =="true":1066print"%s"% msg1067else:1068die("Error:%s\nSet git-p4.allowMissingP4Users to true to allow this."% msg)10691070deflastP4Changelist(self):1071# Get back the last changelist number submitted in this client spec. This1072# then gets used to patch up the username in the change. If the same1073# client spec is being used by multiple processes then this might go1074# wrong.1075 results =p4CmdList("client -o")# find the current client1076 client =None1077for r in results:1078if r.has_key('Client'):1079 client = r['Client']1080break1081if not client:1082die("could not get client spec")1083 results =p4CmdList(["changes","-c", client,"-m","1"])1084for r in results:1085if r.has_key('change'):1086return r['change']1087die("Could not get changelist number for last submit - cannot patch up user details")10881089defmodifyChangelistUser(self, changelist, newUser):1090# fixup the user field of a changelist after it has been submitted.1091 changes =p4CmdList("change -o%s"% changelist)1092iflen(changes) !=1:1093die("Bad output from p4 change modifying%sto user%s"%1094(changelist, newUser))10951096 c = changes[0]1097if c['User'] == newUser:return# nothing to do1098 c['User'] = newUser1099input= marshal.dumps(c)11001101 result =p4CmdList("change -f -i", stdin=input)1102for r in result:1103if r.has_key('code'):1104if r['code'] =='error':1105die("Could not modify user field of changelist%sto%s:%s"% (changelist, newUser, r['data']))1106if r.has_key('data'):1107print("Updated user field for changelist%sto%s"% (changelist, newUser))1108return1109die("Could not modify user field of changelist%sto%s"% (changelist, newUser))11101111defcanChangeChangelists(self):1112# check to see if we have p4 admin or super-user permissions, either of1113# which are required to modify changelists.1114 results =p4CmdList(["protects", self.depotPath])1115for r in results:1116if r.has_key('perm'):1117if r['perm'] =='admin':1118return11119if r['perm'] =='super':1120return11121return011221123defprepareSubmitTemplate(self):1124"""Run "p4 change -o" to grab a change specification template.1125 This does not use "p4 -G", as it is nice to keep the submission1126 template in original order, since a human might edit it.11271128 Remove lines in the Files section that show changes to files1129 outside the depot path we're committing into."""11301131 template =""1132 inFilesSection =False1133for line inp4_read_pipe_lines(['change','-o']):1134if line.endswith("\r\n"):1135 line = line[:-2] +"\n"1136if inFilesSection:1137if line.startswith("\t"):1138# path starts and ends with a tab1139 path = line[1:]1140 lastTab = path.rfind("\t")1141if lastTab != -1:1142 path = path[:lastTab]1143if notp4PathStartsWith(path, self.depotPath):1144continue1145else:1146 inFilesSection =False1147else:1148if line.startswith("Files:"):1149 inFilesSection =True11501151 template += line11521153return template11541155defedit_template(self, template_file):1156"""Invoke the editor to let the user change the submission1157 message. Return true if okay to continue with the submit."""11581159# if configured to skip the editing part, just submit1160ifgitConfig("git-p4.skipSubmitEdit") =="true":1161return True11621163# look at the modification time, to check later if the user saved1164# the file1165 mtime = os.stat(template_file).st_mtime11661167# invoke the editor1168if os.environ.has_key("P4EDITOR")and(os.environ.get("P4EDITOR") !=""):1169 editor = os.environ.get("P4EDITOR")1170else:1171 editor =read_pipe("git var GIT_EDITOR").strip()1172system(editor +" "+ template_file)11731174# If the file was not saved, prompt to see if this patch should1175# be skipped. But skip this verification step if configured so.1176ifgitConfig("git-p4.skipSubmitEditCheck") =="true":1177return True11781179# modification time updated means user saved the file1180if os.stat(template_file).st_mtime > mtime:1181return True11821183while True:1184 response =raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")1185if response =='y':1186return True1187if response =='n':1188return False11891190defapplyCommit(self,id):1191"""Apply one commit, return True if it succeeded."""11921193print"Applying",read_pipe(["git","show","-s",1194"--format=format:%h%s",id])11951196(p4User, gitEmail) = self.p4UserForCommit(id)11971198 diff =read_pipe_lines("git diff-tree -r%s\"%s^\" \"%s\""% (self.diffOpts,id,id))1199 filesToAdd =set()1200 filesToDelete =set()1201 editedFiles =set()1202 pureRenameCopy =set()1203 filesToChangeExecBit = {}12041205for line in diff:1206 diff =parseDiffTreeEntry(line)1207 modifier = diff['status']1208 path = diff['src']1209if modifier =="M":1210p4_edit(path)1211ifisModeExecChanged(diff['src_mode'], diff['dst_mode']):1212 filesToChangeExecBit[path] = diff['dst_mode']1213 editedFiles.add(path)1214elif modifier =="A":1215 filesToAdd.add(path)1216 filesToChangeExecBit[path] = diff['dst_mode']1217if path in filesToDelete:1218 filesToDelete.remove(path)1219elif modifier =="D":1220 filesToDelete.add(path)1221if path in filesToAdd:1222 filesToAdd.remove(path)1223elif modifier =="C":1224 src, dest = diff['src'], diff['dst']1225p4_integrate(src, dest)1226 pureRenameCopy.add(dest)1227if diff['src_sha1'] != diff['dst_sha1']:1228p4_edit(dest)1229 pureRenameCopy.discard(dest)1230ifisModeExecChanged(diff['src_mode'], diff['dst_mode']):1231p4_edit(dest)1232 pureRenameCopy.discard(dest)1233 filesToChangeExecBit[dest] = diff['dst_mode']1234 os.unlink(dest)1235 editedFiles.add(dest)1236elif modifier =="R":1237 src, dest = diff['src'], diff['dst']1238if self.p4HasMoveCommand:1239p4_edit(src)# src must be open before move1240p4_move(src, dest)# opens for (move/delete, move/add)1241else:1242p4_integrate(src, dest)1243if diff['src_sha1'] != diff['dst_sha1']:1244p4_edit(dest)1245else:1246 pureRenameCopy.add(dest)1247ifisModeExecChanged(diff['src_mode'], diff['dst_mode']):1248if not self.p4HasMoveCommand:1249p4_edit(dest)# with move: already open, writable1250 filesToChangeExecBit[dest] = diff['dst_mode']1251if not self.p4HasMoveCommand:1252 os.unlink(dest)1253 filesToDelete.add(src)1254 editedFiles.add(dest)1255else:1256die("unknown modifier%sfor%s"% (modifier, path))12571258 diffcmd ="git format-patch -k --stdout\"%s^\"..\"%s\""% (id,id)1259 patchcmd = diffcmd +" | git apply "1260 tryPatchCmd = patchcmd +"--check -"1261 applyPatchCmd = patchcmd +"--check --apply -"1262 patch_succeeded =True12631264if os.system(tryPatchCmd) !=0:1265 fixed_rcs_keywords =False1266 patch_succeeded =False1267print"Unfortunately applying the change failed!"12681269# Patch failed, maybe it's just RCS keyword woes. Look through1270# the patch to see if that's possible.1271ifgitConfig("git-p4.attemptRCSCleanup","--bool") =="true":1272file=None1273 pattern =None1274 kwfiles = {}1275forfilein editedFiles | filesToDelete:1276# did this file's delta contain RCS keywords?1277 pattern =p4_keywords_regexp_for_file(file)12781279if pattern:1280# this file is a possibility...look for RCS keywords.1281 regexp = re.compile(pattern, re.VERBOSE)1282for line inread_pipe_lines(["git","diff","%s^..%s"% (id,id),file]):1283if regexp.search(line):1284if verbose:1285print"got keyword match on%sin%sin%s"% (pattern, line,file)1286 kwfiles[file] = pattern1287break12881289forfilein kwfiles:1290if verbose:1291print"zapping%swith%s"% (line,pattern)1292 self.patchRCSKeywords(file, kwfiles[file])1293 fixed_rcs_keywords =True12941295if fixed_rcs_keywords:1296print"Retrying the patch with RCS keywords cleaned up"1297if os.system(tryPatchCmd) ==0:1298 patch_succeeded =True12991300if not patch_succeeded:1301for f in editedFiles:1302p4_revert(f)1303return False13041305#1306# Apply the patch for real, and do add/delete/+x handling.1307#1308system(applyPatchCmd)13091310for f in filesToAdd:1311p4_add(f)1312for f in filesToDelete:1313p4_revert(f)1314p4_delete(f)13151316# Set/clear executable bits1317for f in filesToChangeExecBit.keys():1318 mode = filesToChangeExecBit[f]1319setP4ExecBit(f, mode)13201321#1322# Build p4 change description, starting with the contents1323# of the git commit message.1324#1325 logMessage =extractLogMessageFromGitCommit(id)1326 logMessage = logMessage.strip()1327(logMessage, jobs) = self.separate_jobs_from_description(logMessage)13281329 template = self.prepareSubmitTemplate()1330 submitTemplate = self.prepareLogMessage(template, logMessage, jobs)13311332if self.preserveUser:1333 submitTemplate +="\n######## Actual user%s, modified after commit\n"% p4User13341335if self.checkAuthorship and not self.p4UserIsMe(p4User):1336 submitTemplate +="######## git author%sdoes not match your p4 account.\n"% gitEmail1337 submitTemplate +="######## Use option --preserve-user to modify authorship.\n"1338 submitTemplate +="######## Variable git-p4.skipUserNameCheck hides this message.\n"13391340 separatorLine ="######## everything below this line is just the diff #######\n"13411342# diff1343if os.environ.has_key("P4DIFF"):1344del(os.environ["P4DIFF"])1345 diff =""1346for editedFile in editedFiles:1347 diff +=p4_read_pipe(['diff','-du',1348wildcard_encode(editedFile)])13491350# new file diff1351 newdiff =""1352for newFile in filesToAdd:1353 newdiff +="==== new file ====\n"1354 newdiff +="--- /dev/null\n"1355 newdiff +="+++%s\n"% newFile1356 f =open(newFile,"r")1357for line in f.readlines():1358 newdiff +="+"+ line1359 f.close()13601361# change description file: submitTemplate, separatorLine, diff, newdiff1362(handle, fileName) = tempfile.mkstemp()1363 tmpFile = os.fdopen(handle,"w+")1364if self.isWindows:1365 submitTemplate = submitTemplate.replace("\n","\r\n")1366 separatorLine = separatorLine.replace("\n","\r\n")1367 newdiff = newdiff.replace("\n","\r\n")1368 tmpFile.write(submitTemplate + separatorLine + diff + newdiff)1369 tmpFile.close()13701371if self.prepare_p4_only:1372#1373# Leave the p4 tree prepared, and the submit template around1374# and let the user decide what to do next1375#1376print1377print"P4 workspace prepared for submission."1378print"To submit or revert, go to client workspace"1379print" "+ self.clientPath1380print1381print"To submit, use\"p4 submit\"to write a new description,"1382print"or\"p4 submit -i%s\"to use the one prepared by" \1383"\"git p4\"."% fileName1384print"You can delete the file\"%s\"when finished."% fileName13851386if self.preserveUser and p4User and not self.p4UserIsMe(p4User):1387print"To preserve change ownership by user%s, you must\n" \1388"do\"p4 change -f <change>\"after submitting and\n" \1389"edit the User field."1390if pureRenameCopy:1391print"After submitting, renamed files must be re-synced."1392print"Invoke\"p4 sync -f\"on each of these files:"1393for f in pureRenameCopy:1394print" "+ f13951396print1397print"To revert the changes, use\"p4 revert ...\", and delete"1398print"the submit template file\"%s\""% fileName1399if filesToAdd:1400print"Since the commit adds new files, they must be deleted:"1401for f in filesToAdd:1402print" "+ f1403print1404return True14051406#1407# Let the user edit the change description, then submit it.1408#1409if self.edit_template(fileName):1410# read the edited message and submit1411 ret =True1412 tmpFile =open(fileName,"rb")1413 message = tmpFile.read()1414 tmpFile.close()1415 submitTemplate = message[:message.index(separatorLine)]1416if self.isWindows:1417 submitTemplate = submitTemplate.replace("\r\n","\n")1418p4_write_pipe(['submit','-i'], submitTemplate)14191420if self.preserveUser:1421if p4User:1422# Get last changelist number. Cannot easily get it from1423# the submit command output as the output is1424# unmarshalled.1425 changelist = self.lastP4Changelist()1426 self.modifyChangelistUser(changelist, p4User)14271428# The rename/copy happened by applying a patch that created a1429# new file. This leaves it writable, which confuses p4.1430for f in pureRenameCopy:1431p4_sync(f,"-f")14321433else:1434# skip this patch1435 ret =False1436print"Submission cancelled, undoing p4 changes."1437for f in editedFiles:1438p4_revert(f)1439for f in filesToAdd:1440p4_revert(f)1441 os.remove(f)1442for f in filesToDelete:1443p4_revert(f)14441445 os.remove(fileName)1446return ret14471448# Export git tags as p4 labels. Create a p4 label and then tag1449# with that.1450defexportGitTags(self, gitTags):1451 validLabelRegexp =gitConfig("git-p4.labelExportRegexp")1452iflen(validLabelRegexp) ==0:1453 validLabelRegexp = defaultLabelRegexp1454 m = re.compile(validLabelRegexp)14551456for name in gitTags:14571458if not m.match(name):1459if verbose:1460print"tag%sdoes not match regexp%s"% (name, validLabelRegexp)1461continue14621463# Get the p4 commit this corresponds to1464 logMessage =extractLogMessageFromGitCommit(name)1465 values =extractSettingsGitLog(logMessage)14661467if not values.has_key('change'):1468# a tag pointing to something not sent to p4; ignore1469if verbose:1470print"git tag%sdoes not give a p4 commit"% name1471continue1472else:1473 changelist = values['change']14741475# Get the tag details.1476 inHeader =True1477 isAnnotated =False1478 body = []1479for l inread_pipe_lines(["git","cat-file","-p", name]):1480 l = l.strip()1481if inHeader:1482if re.match(r'tag\s+', l):1483 isAnnotated =True1484elif re.match(r'\s*$', l):1485 inHeader =False1486continue1487else:1488 body.append(l)14891490if not isAnnotated:1491 body = ["lightweight tag imported by git p4\n"]14921493# Create the label - use the same view as the client spec we are using1494 clientSpec =getClientSpec()14951496 labelTemplate ="Label:%s\n"% name1497 labelTemplate +="Description:\n"1498for b in body:1499 labelTemplate +="\t"+ b +"\n"1500 labelTemplate +="View:\n"1501for mapping in clientSpec.mappings:1502 labelTemplate +="\t%s\n"% mapping.depot_side.path15031504if self.dry_run:1505print"Would create p4 label%sfor tag"% name1506elif self.prepare_p4_only:1507print"Not creating p4 label%sfor tag due to option" \1508" --prepare-p4-only"% name1509else:1510p4_write_pipe(["label","-i"], labelTemplate)15111512# Use the label1513p4_system(["tag","-l", name] +1514["%s@%s"% (mapping.depot_side.path, changelist)for mapping in clientSpec.mappings])15151516if verbose:1517print"created p4 label for tag%s"% name15181519defrun(self, args):1520iflen(args) ==0:1521 self.master =currentGitBranch()1522iflen(self.master) ==0or notgitBranchExists("refs/heads/%s"% self.master):1523die("Detecting current git branch failed!")1524eliflen(args) ==1:1525 self.master = args[0]1526if notbranchExists(self.master):1527die("Branch%sdoes not exist"% self.master)1528else:1529return False15301531 allowSubmit =gitConfig("git-p4.allowSubmit")1532iflen(allowSubmit) >0and not self.master in allowSubmit.split(","):1533die("%sis not in git-p4.allowSubmit"% self.master)15341535[upstream, settings] =findUpstreamBranchPoint()1536 self.depotPath = settings['depot-paths'][0]1537iflen(self.origin) ==0:1538 self.origin = upstream15391540if self.preserveUser:1541if not self.canChangeChangelists():1542die("Cannot preserve user names without p4 super-user or admin permissions")15431544# if not set from the command line, try the config file1545if self.conflict_behavior is None:1546 val =gitConfig("git-p4.conflict")1547if val:1548if val not in self.conflict_behavior_choices:1549die("Invalid value '%s' for config git-p4.conflict"% val)1550else:1551 val ="ask"1552 self.conflict_behavior = val15531554if self.verbose:1555print"Origin branch is "+ self.origin15561557iflen(self.depotPath) ==0:1558print"Internal error: cannot locate perforce depot path from existing branches"1559 sys.exit(128)15601561 self.useClientSpec =False1562ifgitConfig("git-p4.useclientspec","--bool") =="true":1563 self.useClientSpec =True1564if self.useClientSpec:1565 self.clientSpecDirs =getClientSpec()15661567if self.useClientSpec:1568# all files are relative to the client spec1569 self.clientPath =getClientRoot()1570else:1571 self.clientPath =p4Where(self.depotPath)15721573if self.clientPath =="":1574die("Error: Cannot locate perforce checkout of%sin client view"% self.depotPath)15751576print"Perforce checkout for depot path%slocated at%s"% (self.depotPath, self.clientPath)1577 self.oldWorkingDirectory = os.getcwd()15781579# ensure the clientPath exists1580 new_client_dir =False1581if not os.path.exists(self.clientPath):1582 new_client_dir =True1583 os.makedirs(self.clientPath)15841585chdir(self.clientPath)1586if self.dry_run:1587print"Would synchronize p4 checkout in%s"% self.clientPath1588else:1589print"Synchronizing p4 checkout..."1590if new_client_dir:1591# old one was destroyed, and maybe nobody told p41592p4_sync("...","-f")1593else:1594p4_sync("...")1595 self.check()15961597 commits = []1598for line inread_pipe_lines("git rev-list --no-merges%s..%s"% (self.origin, self.master)):1599 commits.append(line.strip())1600 commits.reverse()16011602if self.preserveUser or(gitConfig("git-p4.skipUserNameCheck") =="true"):1603 self.checkAuthorship =False1604else:1605 self.checkAuthorship =True16061607if self.preserveUser:1608 self.checkValidP4Users(commits)16091610#1611# Build up a set of options to be passed to diff when1612# submitting each commit to p4.1613#1614if self.detectRenames:1615# command-line -M arg1616 self.diffOpts ="-M"1617else:1618# If not explicitly set check the config variable1619 detectRenames =gitConfig("git-p4.detectRenames")16201621if detectRenames.lower() =="false"or detectRenames =="":1622 self.diffOpts =""1623elif detectRenames.lower() =="true":1624 self.diffOpts ="-M"1625else:1626 self.diffOpts ="-M%s"% detectRenames16271628# no command-line arg for -C or --find-copies-harder, just1629# config variables1630 detectCopies =gitConfig("git-p4.detectCopies")1631if detectCopies.lower() =="false"or detectCopies =="":1632pass1633elif detectCopies.lower() =="true":1634 self.diffOpts +=" -C"1635else:1636 self.diffOpts +=" -C%s"% detectCopies16371638ifgitConfig("git-p4.detectCopiesHarder","--bool") =="true":1639 self.diffOpts +=" --find-copies-harder"16401641#1642# Apply the commits, one at a time. On failure, ask if should1643# continue to try the rest of the patches, or quit.1644#1645if self.dry_run:1646print"Would apply"1647 applied = []1648 last =len(commits) -11649for i, commit inenumerate(commits):1650if self.dry_run:1651print" ",read_pipe(["git","show","-s",1652"--format=format:%h%s", commit])1653 ok =True1654else:1655 ok = self.applyCommit(commit)1656if ok:1657 applied.append(commit)1658else:1659if self.prepare_p4_only and i < last:1660print"Processing only the first commit due to option" \1661" --prepare-p4-only"1662break1663if i < last:1664 quit =False1665while True:1666# prompt for what to do, or use the option/variable1667if self.conflict_behavior =="ask":1668print"What do you want to do?"1669 response =raw_input("[s]kip this commit but apply"1670" the rest, or [q]uit? ")1671if not response:1672continue1673elif self.conflict_behavior =="skip":1674 response ="s"1675elif self.conflict_behavior =="quit":1676 response ="q"1677else:1678die("Unknown conflict_behavior '%s'"%1679 self.conflict_behavior)16801681if response[0] =="s":1682print"Skipping this commit, but applying the rest"1683break1684if response[0] =="q":1685print"Quitting"1686 quit =True1687break1688if quit:1689break16901691chdir(self.oldWorkingDirectory)16921693if self.dry_run:1694pass1695elif self.prepare_p4_only:1696pass1697eliflen(commits) ==len(applied):1698print"All commits applied!"16991700 sync =P4Sync()1701if self.branch:1702 sync.branch = self.branch1703 sync.run([])17041705 rebase =P4Rebase()1706 rebase.rebase()17071708else:1709iflen(applied) ==0:1710print"No commits applied."1711else:1712print"Applied only the commits marked with '*':"1713for c in commits:1714if c in applied:1715 star ="*"1716else:1717 star =" "1718print star,read_pipe(["git","show","-s",1719"--format=format:%h%s", c])1720print"You will have to do 'git p4 sync' and rebase."17211722ifgitConfig("git-p4.exportLabels","--bool") =="true":1723 self.exportLabels =True17241725if self.exportLabels:1726 p4Labels =getP4Labels(self.depotPath)1727 gitTags =getGitTags()17281729 missingGitTags = gitTags - p4Labels1730 self.exportGitTags(missingGitTags)17311732# exit with error unless everything applied perfecly1733iflen(commits) !=len(applied):1734 sys.exit(1)17351736return True17371738classView(object):1739"""Represent a p4 view ("p4 help views"), and map files in a1740 repo according to the view."""17411742classPath(object):1743"""A depot or client path, possibly containing wildcards.1744 The only one supported is ... at the end, currently.1745 Initialize with the full path, with //depot or //client."""17461747def__init__(self, path, is_depot):1748 self.path = path1749 self.is_depot = is_depot1750 self.find_wildcards()1751# remember the prefix bit, useful for relative mappings1752 m = re.match("(//[^/]+/)", self.path)1753if not m:1754die("Path%sdoes not start with //prefix/"% self.path)1755 prefix = m.group(1)1756if not self.is_depot:1757# strip //client/ on client paths1758 self.path = self.path[len(prefix):]17591760deffind_wildcards(self):1761"""Make sure wildcards are valid, and set up internal1762 variables."""17631764 self.ends_triple_dot =False1765# There are three wildcards allowed in p4 views1766# (see "p4 help views"). This code knows how to1767# handle "..." (only at the end), but cannot deal with1768# "%%n" or "*". Only check the depot_side, as p4 should1769# validate that the client_side matches too.1770if re.search(r'%%[1-9]', self.path):1771die("Can't handle%%n wildcards in view:%s"% self.path)1772if self.path.find("*") >=0:1773die("Can't handle * wildcards in view:%s"% self.path)1774 triple_dot_index = self.path.find("...")1775if triple_dot_index >=0:1776if triple_dot_index !=len(self.path) -3:1777die("Can handle only single ... wildcard, at end:%s"%1778 self.path)1779 self.ends_triple_dot =True17801781defensure_compatible(self, other_path):1782"""Make sure the wildcards agree."""1783if self.ends_triple_dot != other_path.ends_triple_dot:1784die("Both paths must end with ... if either does;\n"+1785"paths:%s %s"% (self.path, other_path.path))17861787defmatch_wildcards(self, test_path):1788"""See if this test_path matches us, and fill in the value1789 of the wildcards if so. Returns a tuple of1790 (True|False, wildcards[]). For now, only the ... at end1791 is supported, so at most one wildcard."""1792if self.ends_triple_dot:1793 dotless = self.path[:-3]1794if test_path.startswith(dotless):1795 wildcard = test_path[len(dotless):]1796return(True, [ wildcard ])1797else:1798if test_path == self.path:1799return(True, [])1800return(False, [])18011802defmatch(self, test_path):1803"""Just return if it matches; don't bother with the wildcards."""1804 b, _ = self.match_wildcards(test_path)1805return b18061807deffill_in_wildcards(self, wildcards):1808"""Return the relative path, with the wildcards filled in1809 if there are any."""1810if self.ends_triple_dot:1811return self.path[:-3] + wildcards[0]1812else:1813return self.path18141815classMapping(object):1816def__init__(self, depot_side, client_side, overlay, exclude):1817# depot_side is without the trailing /... if it had one1818 self.depot_side = View.Path(depot_side, is_depot=True)1819 self.client_side = View.Path(client_side, is_depot=False)1820 self.overlay = overlay # started with "+"1821 self.exclude = exclude # started with "-"1822assert not(self.overlay and self.exclude)1823 self.depot_side.ensure_compatible(self.client_side)18241825def__str__(self):1826 c =" "1827if self.overlay:1828 c ="+"1829if self.exclude:1830 c ="-"1831return"View.Mapping:%s%s->%s"% \1832(c, self.depot_side.path, self.client_side.path)18331834defmap_depot_to_client(self, depot_path):1835"""Calculate the client path if using this mapping on the1836 given depot path; does not consider the effect of other1837 mappings in a view. Even excluded mappings are returned."""1838 matches, wildcards = self.depot_side.match_wildcards(depot_path)1839if not matches:1840return""1841 client_path = self.client_side.fill_in_wildcards(wildcards)1842return client_path18431844#1845# View methods1846#1847def__init__(self):1848 self.mappings = []18491850defappend(self, view_line):1851"""Parse a view line, splitting it into depot and client1852 sides. Append to self.mappings, preserving order."""18531854# Split the view line into exactly two words. P4 enforces1855# structure on these lines that simplifies this quite a bit.1856#1857# Either or both words may be double-quoted.1858# Single quotes do not matter.1859# Double-quote marks cannot occur inside the words.1860# A + or - prefix is also inside the quotes.1861# There are no quotes unless they contain a space.1862# The line is already white-space stripped.1863# The two words are separated by a single space.1864#1865if view_line[0] =='"':1866# First word is double quoted. Find its end.1867 close_quote_index = view_line.find('"',1)1868if close_quote_index <=0:1869die("No first-word closing quote found:%s"% view_line)1870 depot_side = view_line[1:close_quote_index]1871# skip closing quote and space1872 rhs_index = close_quote_index +1+11873else:1874 space_index = view_line.find(" ")1875if space_index <=0:1876die("No word-splitting space found:%s"% view_line)1877 depot_side = view_line[0:space_index]1878 rhs_index = space_index +118791880if view_line[rhs_index] =='"':1881# Second word is double quoted. Make sure there is a1882# double quote at the end too.1883if not view_line.endswith('"'):1884die("View line with rhs quote should end with one:%s"%1885 view_line)1886# skip the quotes1887 client_side = view_line[rhs_index+1:-1]1888else:1889 client_side = view_line[rhs_index:]18901891# prefix + means overlay on previous mapping1892 overlay =False1893if depot_side.startswith("+"):1894 overlay =True1895 depot_side = depot_side[1:]18961897# prefix - means exclude this path1898 exclude =False1899if depot_side.startswith("-"):1900 exclude =True1901 depot_side = depot_side[1:]19021903 m = View.Mapping(depot_side, client_side, overlay, exclude)1904 self.mappings.append(m)19051906defmap_in_client(self, depot_path):1907"""Return the relative location in the client where this1908 depot file should live. Returns "" if the file should1909 not be mapped in the client."""19101911 paths_filled = []1912 client_path =""19131914# look at later entries first1915for m in self.mappings[::-1]:19161917# see where will this path end up in the client1918 p = m.map_depot_to_client(depot_path)19191920if p =="":1921# Depot path does not belong in client. Must remember1922# this, as previous items should not cause files to1923# exist in this path either. Remember that the list is1924# being walked from the end, which has higher precedence.1925# Overlap mappings do not exclude previous mappings.1926if not m.overlay:1927 paths_filled.append(m.client_side)19281929else:1930# This mapping matched; no need to search any further.1931# But, the mapping could be rejected if the client path1932# has already been claimed by an earlier mapping (i.e.1933# one later in the list, which we are walking backwards).1934 already_mapped_in_client =False1935for f in paths_filled:1936# this is View.Path.match1937if f.match(p):1938 already_mapped_in_client =True1939break1940if not already_mapped_in_client:1941# Include this file, unless it is from a line that1942# explicitly said to exclude it.1943if not m.exclude:1944 client_path = p19451946# a match, even if rejected, always stops the search1947break19481949return client_path19501951classP4Sync(Command, P4UserMap):1952 delete_actions = ("delete","move/delete","purge")19531954def__init__(self):1955 Command.__init__(self)1956 P4UserMap.__init__(self)1957 self.options = [1958 optparse.make_option("--branch", dest="branch"),1959 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),1960 optparse.make_option("--changesfile", dest="changesFile"),1961 optparse.make_option("--silent", dest="silent", action="store_true"),1962 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),1963 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),1964 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",1965help="Import into refs/heads/ , not refs/remotes"),1966 optparse.make_option("--max-changes", dest="maxChanges"),1967 optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',1968help="Keep entire BRANCH/DIR/SUBDIR prefix during import"),1969 optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true',1970help="Only sync files that are included in the Perforce Client Spec")1971]1972 self.description ="""Imports from Perforce into a git repository.\n1973 example:1974 //depot/my/project/ -- to import the current head1975 //depot/my/project/@all -- to import everything1976 //depot/my/project/@1,6 -- to import only from revision 1 to 619771978 (a ... is not needed in the path p4 specification, it's added implicitly)"""19791980 self.usage +=" //depot/path[@revRange]"1981 self.silent =False1982 self.createdBranches =set()1983 self.committedChanges =set()1984 self.branch =""1985 self.detectBranches =False1986 self.detectLabels =False1987 self.importLabels =False1988 self.changesFile =""1989 self.syncWithOrigin =True1990 self.importIntoRemotes =True1991 self.maxChanges =""1992 self.keepRepoPath =False1993 self.depotPaths =None1994 self.p4BranchesInGit = []1995 self.cloneExclude = []1996 self.useClientSpec =False1997 self.useClientSpec_from_options =False1998 self.clientSpecDirs =None1999 self.tempBranches = []2000 self.tempBranchLocation ="git-p4-tmp"20012002ifgitConfig("git-p4.syncFromOrigin") =="false":2003 self.syncWithOrigin =False20042005# Force a checkpoint in fast-import and wait for it to finish2006defcheckpoint(self):2007 self.gitStream.write("checkpoint\n\n")2008 self.gitStream.write("progress checkpoint\n\n")2009 out = self.gitOutput.readline()2010if self.verbose:2011print"checkpoint finished: "+ out20122013defextractFilesFromCommit(self, commit):2014 self.cloneExclude = [re.sub(r"\.\.\.$","", path)2015for path in self.cloneExclude]2016 files = []2017 fnum =02018while commit.has_key("depotFile%s"% fnum):2019 path = commit["depotFile%s"% fnum]20202021if[p for p in self.cloneExclude2022ifp4PathStartsWith(path, p)]:2023 found =False2024else:2025 found = [p for p in self.depotPaths2026ifp4PathStartsWith(path, p)]2027if not found:2028 fnum = fnum +12029continue20302031file= {}2032file["path"] = path2033file["rev"] = commit["rev%s"% fnum]2034file["action"] = commit["action%s"% fnum]2035file["type"] = commit["type%s"% fnum]2036 files.append(file)2037 fnum = fnum +12038return files20392040defstripRepoPath(self, path, prefixes):2041"""When streaming files, this is called to map a p4 depot path2042 to where it should go in git. The prefixes are either2043 self.depotPaths, or self.branchPrefixes in the case of2044 branch detection."""20452046if self.useClientSpec:2047# branch detection moves files up a level (the branch name)2048# from what client spec interpretation gives2049 path = self.clientSpecDirs.map_in_client(path)2050if self.detectBranches:2051for b in self.knownBranches:2052if path.startswith(b +"/"):2053 path = path[len(b)+1:]20542055elif self.keepRepoPath:2056# Preserve everything in relative path name except leading2057# //depot/; just look at first prefix as they all should2058# be in the same depot.2059 depot = re.sub("^(//[^/]+/).*", r'\1', prefixes[0])2060ifp4PathStartsWith(path, depot):2061 path = path[len(depot):]20622063else:2064for p in prefixes:2065ifp4PathStartsWith(path, p):2066 path = path[len(p):]2067break20682069 path =wildcard_decode(path)2070return path20712072defsplitFilesIntoBranches(self, commit):2073"""Look at each depotFile in the commit to figure out to what2074 branch it belongs."""20752076 branches = {}2077 fnum =02078while commit.has_key("depotFile%s"% fnum):2079 path = commit["depotFile%s"% fnum]2080 found = [p for p in self.depotPaths2081ifp4PathStartsWith(path, p)]2082if not found:2083 fnum = fnum +12084continue20852086file= {}2087file["path"] = path2088file["rev"] = commit["rev%s"% fnum]2089file["action"] = commit["action%s"% fnum]2090file["type"] = commit["type%s"% fnum]2091 fnum = fnum +120922093# start with the full relative path where this file would2094# go in a p4 client2095if self.useClientSpec:2096 relPath = self.clientSpecDirs.map_in_client(path)2097else:2098 relPath = self.stripRepoPath(path, self.depotPaths)20992100for branch in self.knownBranches.keys():2101# add a trailing slash so that a commit into qt/4.2foo2102# doesn't end up in qt/4.2, e.g.2103if relPath.startswith(branch +"/"):2104if branch not in branches:2105 branches[branch] = []2106 branches[branch].append(file)2107break21082109return branches21102111# output one file from the P4 stream2112# - helper for streamP4Files21132114defstreamOneP4File(self,file, contents):2115 relPath = self.stripRepoPath(file['depotFile'], self.branchPrefixes)2116if verbose:2117 sys.stderr.write("%s\n"% relPath)21182119(type_base, type_mods) =split_p4_type(file["type"])21202121 git_mode ="100644"2122if"x"in type_mods:2123 git_mode ="100755"2124if type_base =="symlink":2125 git_mode ="120000"2126# p4 print on a symlink contains "target\n"; remove the newline2127 data =''.join(contents)2128 contents = [data[:-1]]21292130if type_base =="utf16":2131# p4 delivers different text in the python output to -G2132# than it does when using "print -o", or normal p4 client2133# operations. utf16 is converted to ascii or utf8, perhaps.2134# But ascii text saved as -t utf16 is completely mangled.2135# Invoke print -o to get the real contents.2136#2137# On windows, the newlines will always be mangled by print, so put2138# them back too. This is not needed to the cygwin windows version,2139# just the native "NT" type.2140#2141 text =p4_read_pipe(['print','-q','-o','-',file['depotFile']])2142ifp4_version_string().find("/NT") >=0:2143 text = text.replace("\r\n","\n")2144 contents = [ text ]21452146if type_base =="apple":2147# Apple filetype files will be streamed as a concatenation of2148# its appledouble header and the contents. This is useless2149# on both macs and non-macs. If using "print -q -o xx", it2150# will create "xx" with the data, and "%xx" with the header.2151# This is also not very useful.2152#2153# Ideally, someday, this script can learn how to generate2154# appledouble files directly and import those to git, but2155# non-mac machines can never find a use for apple filetype.2156print"\nIgnoring apple filetype file%s"%file['depotFile']2157return21582159# Note that we do not try to de-mangle keywords on utf16 files,2160# even though in theory somebody may want that.2161 pattern =p4_keywords_regexp_for_type(type_base, type_mods)2162if pattern:2163 regexp = re.compile(pattern, re.VERBOSE)2164 text =''.join(contents)2165 text = regexp.sub(r'$\1$', text)2166 contents = [ text ]21672168 self.gitStream.write("M%sinline%s\n"% (git_mode, relPath))21692170# total length...2171 length =02172for d in contents:2173 length = length +len(d)21742175 self.gitStream.write("data%d\n"% length)2176for d in contents:2177 self.gitStream.write(d)2178 self.gitStream.write("\n")21792180defstreamOneP4Deletion(self,file):2181 relPath = self.stripRepoPath(file['path'], self.branchPrefixes)2182if verbose:2183 sys.stderr.write("delete%s\n"% relPath)2184 self.gitStream.write("D%s\n"% relPath)21852186# handle another chunk of streaming data2187defstreamP4FilesCb(self, marshalled):21882189# catch p4 errors and complain2190 err =None2191if"code"in marshalled:2192if marshalled["code"] =="error":2193if"data"in marshalled:2194 err = marshalled["data"].rstrip()2195if err:2196 f =None2197if self.stream_have_file_info:2198if"depotFile"in self.stream_file:2199 f = self.stream_file["depotFile"]2200# force a failure in fast-import, else an empty2201# commit will be made2202 self.gitStream.write("\n")2203 self.gitStream.write("die-now\n")2204 self.gitStream.close()2205# ignore errors, but make sure it exits first2206 self.importProcess.wait()2207if f:2208die("Error from p4 print for%s:%s"% (f, err))2209else:2210die("Error from p4 print:%s"% err)22112212if marshalled.has_key('depotFile')and self.stream_have_file_info:2213# start of a new file - output the old one first2214 self.streamOneP4File(self.stream_file, self.stream_contents)2215 self.stream_file = {}2216 self.stream_contents = []2217 self.stream_have_file_info =False22182219# pick up the new file information... for the2220# 'data' field we need to append to our array2221for k in marshalled.keys():2222if k =='data':2223 self.stream_contents.append(marshalled['data'])2224else:2225 self.stream_file[k] = marshalled[k]22262227 self.stream_have_file_info =True22282229# Stream directly from "p4 files" into "git fast-import"2230defstreamP4Files(self, files):2231 filesForCommit = []2232 filesToRead = []2233 filesToDelete = []22342235for f in files:2236# if using a client spec, only add the files that have2237# a path in the client2238if self.clientSpecDirs:2239if self.clientSpecDirs.map_in_client(f['path']) =="":2240continue22412242 filesForCommit.append(f)2243if f['action']in self.delete_actions:2244 filesToDelete.append(f)2245else:2246 filesToRead.append(f)22472248# deleted files...2249for f in filesToDelete:2250 self.streamOneP4Deletion(f)22512252iflen(filesToRead) >0:2253 self.stream_file = {}2254 self.stream_contents = []2255 self.stream_have_file_info =False22562257# curry self argument2258defstreamP4FilesCbSelf(entry):2259 self.streamP4FilesCb(entry)22602261 fileArgs = ['%s#%s'% (f['path'], f['rev'])for f in filesToRead]22622263p4CmdList(["-x","-","print"],2264 stdin=fileArgs,2265 cb=streamP4FilesCbSelf)22662267# do the last chunk2268if self.stream_file.has_key('depotFile'):2269 self.streamOneP4File(self.stream_file, self.stream_contents)22702271defmake_email(self, userid):2272if userid in self.users:2273return self.users[userid]2274else:2275return"%s<a@b>"% userid22762277# Stream a p4 tag2278defstreamTag(self, gitStream, labelName, labelDetails, commit, epoch):2279if verbose:2280print"writing tag%sfor commit%s"% (labelName, commit)2281 gitStream.write("tag%s\n"% labelName)2282 gitStream.write("from%s\n"% commit)22832284if labelDetails.has_key('Owner'):2285 owner = labelDetails["Owner"]2286else:2287 owner =None22882289# Try to use the owner of the p4 label, or failing that,2290# the current p4 user id.2291if owner:2292 email = self.make_email(owner)2293else:2294 email = self.make_email(self.p4UserId())2295 tagger ="%s %s %s"% (email, epoch, self.tz)22962297 gitStream.write("tagger%s\n"% tagger)22982299print"labelDetails=",labelDetails2300if labelDetails.has_key('Description'):2301 description = labelDetails['Description']2302else:2303 description ='Label from git p4'23042305 gitStream.write("data%d\n"%len(description))2306 gitStream.write(description)2307 gitStream.write("\n")23082309defcommit(self, details, files, branch, parent =""):2310 epoch = details["time"]2311 author = details["user"]23122313if self.verbose:2314print"commit into%s"% branch23152316# start with reading files; if that fails, we should not2317# create a commit.2318 new_files = []2319for f in files:2320if[p for p in self.branchPrefixes ifp4PathStartsWith(f['path'], p)]:2321 new_files.append(f)2322else:2323 sys.stderr.write("Ignoring file outside of prefix:%s\n"% f['path'])23242325 self.gitStream.write("commit%s\n"% branch)2326# gitStream.write("mark :%s\n" % details["change"])2327 self.committedChanges.add(int(details["change"]))2328 committer =""2329if author not in self.users:2330 self.getUserMapFromPerforceServer()2331 committer ="%s %s %s"% (self.make_email(author), epoch, self.tz)23322333 self.gitStream.write("committer%s\n"% committer)23342335 self.gitStream.write("data <<EOT\n")2336 self.gitStream.write(details["desc"])2337 self.gitStream.write("\n[git-p4: depot-paths =\"%s\": change =%s"%2338(','.join(self.branchPrefixes), details["change"]))2339iflen(details['options']) >0:2340 self.gitStream.write(": options =%s"% details['options'])2341 self.gitStream.write("]\nEOT\n\n")23422343iflen(parent) >0:2344if self.verbose:2345print"parent%s"% parent2346 self.gitStream.write("from%s\n"% parent)23472348 self.streamP4Files(new_files)2349 self.gitStream.write("\n")23502351 change =int(details["change"])23522353if self.labels.has_key(change):2354 label = self.labels[change]2355 labelDetails = label[0]2356 labelRevisions = label[1]2357if self.verbose:2358print"Change%sis labelled%s"% (change, labelDetails)23592360 files =p4CmdList(["files"] + ["%s...@%s"% (p, change)2361for p in self.branchPrefixes])23622363iflen(files) ==len(labelRevisions):23642365 cleanedFiles = {}2366for info in files:2367if info["action"]in self.delete_actions:2368continue2369 cleanedFiles[info["depotFile"]] = info["rev"]23702371if cleanedFiles == labelRevisions:2372 self.streamTag(self.gitStream,'tag_%s'% labelDetails['label'], labelDetails, branch, epoch)23732374else:2375if not self.silent:2376print("Tag%sdoes not match with change%s: files do not match."2377% (labelDetails["label"], change))23782379else:2380if not self.silent:2381print("Tag%sdoes not match with change%s: file count is different."2382% (labelDetails["label"], change))23832384# Build a dictionary of changelists and labels, for "detect-labels" option.2385defgetLabels(self):2386 self.labels = {}23872388 l =p4CmdList(["labels"] + ["%s..."% p for p in self.depotPaths])2389iflen(l) >0and not self.silent:2390print"Finding files belonging to labels in%s"% `self.depotPaths`23912392for output in l:2393 label = output["label"]2394 revisions = {}2395 newestChange =02396if self.verbose:2397print"Querying files for label%s"% label2398forfileinp4CmdList(["files"] +2399["%s...@%s"% (p, label)2400for p in self.depotPaths]):2401 revisions[file["depotFile"]] =file["rev"]2402 change =int(file["change"])2403if change > newestChange:2404 newestChange = change24052406 self.labels[newestChange] = [output, revisions]24072408if self.verbose:2409print"Label changes:%s"% self.labels.keys()24102411# Import p4 labels as git tags. A direct mapping does not2412# exist, so assume that if all the files are at the same revision2413# then we can use that, or it's something more complicated we should2414# just ignore.2415defimportP4Labels(self, stream, p4Labels):2416if verbose:2417print"import p4 labels: "+' '.join(p4Labels)24182419 ignoredP4Labels =gitConfigList("git-p4.ignoredP4Labels")2420 validLabelRegexp =gitConfig("git-p4.labelImportRegexp")2421iflen(validLabelRegexp) ==0:2422 validLabelRegexp = defaultLabelRegexp2423 m = re.compile(validLabelRegexp)24242425for name in p4Labels:2426 commitFound =False24272428if not m.match(name):2429if verbose:2430print"label%sdoes not match regexp%s"% (name,validLabelRegexp)2431continue24322433if name in ignoredP4Labels:2434continue24352436 labelDetails =p4CmdList(['label',"-o", name])[0]24372438# get the most recent changelist for each file in this label2439 change =p4Cmd(["changes","-m","1"] + ["%s...@%s"% (p, name)2440for p in self.depotPaths])24412442if change.has_key('change'):2443# find the corresponding git commit; take the oldest commit2444 changelist =int(change['change'])2445 gitCommit =read_pipe(["git","rev-list","--max-count=1",2446"--reverse",":/\[git-p4:.*change =%d\]"% changelist])2447iflen(gitCommit) ==0:2448print"could not find git commit for changelist%d"% changelist2449else:2450 gitCommit = gitCommit.strip()2451 commitFound =True2452# Convert from p4 time format2453try:2454 tmwhen = time.strptime(labelDetails['Update'],"%Y/%m/%d%H:%M:%S")2455exceptValueError:2456print"Could not convert label time%s"% labelDetails['Update']2457 tmwhen =124582459 when =int(time.mktime(tmwhen))2460 self.streamTag(stream, name, labelDetails, gitCommit, when)2461if verbose:2462print"p4 label%smapped to git commit%s"% (name, gitCommit)2463else:2464if verbose:2465print"Label%shas no changelists - possibly deleted?"% name24662467if not commitFound:2468# We can't import this label; don't try again as it will get very2469# expensive repeatedly fetching all the files for labels that will2470# never be imported. If the label is moved in the future, the2471# ignore will need to be removed manually.2472system(["git","config","--add","git-p4.ignoredP4Labels", name])24732474defguessProjectName(self):2475for p in self.depotPaths:2476if p.endswith("/"):2477 p = p[:-1]2478 p = p[p.strip().rfind("/") +1:]2479if not p.endswith("/"):2480 p +="/"2481return p24822483defgetBranchMapping(self):2484 lostAndFoundBranches =set()24852486 user =gitConfig("git-p4.branchUser")2487iflen(user) >0:2488 command ="branches -u%s"% user2489else:2490 command ="branches"24912492for info inp4CmdList(command):2493 details =p4Cmd(["branch","-o", info["branch"]])2494 viewIdx =02495while details.has_key("View%s"% viewIdx):2496 paths = details["View%s"% viewIdx].split(" ")2497 viewIdx = viewIdx +12498# require standard //depot/foo/... //depot/bar/... mapping2499iflen(paths) !=2or not paths[0].endswith("/...")or not paths[1].endswith("/..."):2500continue2501 source = paths[0]2502 destination = paths[1]2503## HACK2504ifp4PathStartsWith(source, self.depotPaths[0])andp4PathStartsWith(destination, self.depotPaths[0]):2505 source = source[len(self.depotPaths[0]):-4]2506 destination = destination[len(self.depotPaths[0]):-4]25072508if destination in self.knownBranches:2509if not self.silent:2510print"p4 branch%sdefines a mapping from%sto%s"% (info["branch"], source, destination)2511print"but there exists another mapping from%sto%salready!"% (self.knownBranches[destination], destination)2512continue25132514 self.knownBranches[destination] = source25152516 lostAndFoundBranches.discard(destination)25172518if source not in self.knownBranches:2519 lostAndFoundBranches.add(source)25202521# Perforce does not strictly require branches to be defined, so we also2522# check git config for a branch list.2523#2524# Example of branch definition in git config file:2525# [git-p4]2526# branchList=main:branchA2527# branchList=main:branchB2528# branchList=branchA:branchC2529 configBranches =gitConfigList("git-p4.branchList")2530for branch in configBranches:2531if branch:2532(source, destination) = branch.split(":")2533 self.knownBranches[destination] = source25342535 lostAndFoundBranches.discard(destination)25362537if source not in self.knownBranches:2538 lostAndFoundBranches.add(source)253925402541for branch in lostAndFoundBranches:2542 self.knownBranches[branch] = branch25432544defgetBranchMappingFromGitBranches(self):2545 branches =p4BranchesInGit(self.importIntoRemotes)2546for branch in branches.keys():2547if branch =="master":2548 branch ="main"2549else:2550 branch = branch[len(self.projectName):]2551 self.knownBranches[branch] = branch25522553defupdateOptionDict(self, d):2554 option_keys = {}2555if self.keepRepoPath:2556 option_keys['keepRepoPath'] =125572558 d["options"] =' '.join(sorted(option_keys.keys()))25592560defreadOptions(self, d):2561 self.keepRepoPath = (d.has_key('options')2562and('keepRepoPath'in d['options']))25632564defgitRefForBranch(self, branch):2565if branch =="main":2566return self.refPrefix +"master"25672568iflen(branch) <=0:2569return branch25702571return self.refPrefix + self.projectName + branch25722573defgitCommitByP4Change(self, ref, change):2574if self.verbose:2575print"looking in ref "+ ref +" for change%susing bisect..."% change25762577 earliestCommit =""2578 latestCommit =parseRevision(ref)25792580while True:2581if self.verbose:2582print"trying: earliest%slatest%s"% (earliestCommit, latestCommit)2583 next =read_pipe("git rev-list --bisect%s %s"% (latestCommit, earliestCommit)).strip()2584iflen(next) ==0:2585if self.verbose:2586print"argh"2587return""2588 log =extractLogMessageFromGitCommit(next)2589 settings =extractSettingsGitLog(log)2590 currentChange =int(settings['change'])2591if self.verbose:2592print"current change%s"% currentChange25932594if currentChange == change:2595if self.verbose:2596print"found%s"% next2597return next25982599if currentChange < change:2600 earliestCommit ="^%s"% next2601else:2602 latestCommit ="%s"% next26032604return""26052606defimportNewBranch(self, branch, maxChange):2607# make fast-import flush all changes to disk and update the refs using the checkpoint2608# command so that we can try to find the branch parent in the git history2609 self.gitStream.write("checkpoint\n\n");2610 self.gitStream.flush();2611 branchPrefix = self.depotPaths[0] + branch +"/"2612range="@1,%s"% maxChange2613#print "prefix" + branchPrefix2614 changes =p4ChangesForPaths([branchPrefix],range)2615iflen(changes) <=0:2616return False2617 firstChange = changes[0]2618#print "first change in branch: %s" % firstChange2619 sourceBranch = self.knownBranches[branch]2620 sourceDepotPath = self.depotPaths[0] + sourceBranch2621 sourceRef = self.gitRefForBranch(sourceBranch)2622#print "source " + sourceBranch26232624 branchParentChange =int(p4Cmd(["changes","-m","1","%s...@1,%s"% (sourceDepotPath, firstChange)])["change"])2625#print "branch parent: %s" % branchParentChange2626 gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)2627iflen(gitParent) >0:2628 self.initialParents[self.gitRefForBranch(branch)] = gitParent2629#print "parent git commit: %s" % gitParent26302631 self.importChanges(changes)2632return True26332634defsearchParent(self, parent, branch, target):2635 parentFound =False2636for blob inread_pipe_lines(["git","rev-list","--reverse","--no-merges", parent]):2637 blob = blob.strip()2638iflen(read_pipe(["git","diff-tree", blob, target])) ==0:2639 parentFound =True2640if self.verbose:2641print"Found parent of%sin commit%s"% (branch, blob)2642break2643if parentFound:2644return blob2645else:2646return None26472648defimportChanges(self, changes):2649 cnt =12650for change in changes:2651 description =p4_describe(change)2652 self.updateOptionDict(description)26532654if not self.silent:2655 sys.stdout.write("\rImporting revision%s(%s%%)"% (change, cnt *100/len(changes)))2656 sys.stdout.flush()2657 cnt = cnt +126582659try:2660if self.detectBranches:2661 branches = self.splitFilesIntoBranches(description)2662for branch in branches.keys():2663## HACK --hwn2664 branchPrefix = self.depotPaths[0] + branch +"/"2665 self.branchPrefixes = [ branchPrefix ]26662667 parent =""26682669 filesForCommit = branches[branch]26702671if self.verbose:2672print"branch is%s"% branch26732674 self.updatedBranches.add(branch)26752676if branch not in self.createdBranches:2677 self.createdBranches.add(branch)2678 parent = self.knownBranches[branch]2679if parent == branch:2680 parent =""2681else:2682 fullBranch = self.projectName + branch2683if fullBranch not in self.p4BranchesInGit:2684if not self.silent:2685print("\nImporting new branch%s"% fullBranch);2686if self.importNewBranch(branch, change -1):2687 parent =""2688 self.p4BranchesInGit.append(fullBranch)2689if not self.silent:2690print("\nResuming with change%s"% change);26912692if self.verbose:2693print"parent determined through known branches:%s"% parent26942695 branch = self.gitRefForBranch(branch)2696 parent = self.gitRefForBranch(parent)26972698if self.verbose:2699print"looking for initial parent for%s; current parent is%s"% (branch, parent)27002701iflen(parent) ==0and branch in self.initialParents:2702 parent = self.initialParents[branch]2703del self.initialParents[branch]27042705 blob =None2706iflen(parent) >0:2707 tempBranch ="%s/%d"% (self.tempBranchLocation, change)2708if self.verbose:2709print"Creating temporary branch: "+ tempBranch2710 self.commit(description, filesForCommit, tempBranch)2711 self.tempBranches.append(tempBranch)2712 self.checkpoint()2713 blob = self.searchParent(parent, branch, tempBranch)2714if blob:2715 self.commit(description, filesForCommit, branch, blob)2716else:2717if self.verbose:2718print"Parent of%snot found. Committing into head of%s"% (branch, parent)2719 self.commit(description, filesForCommit, branch, parent)2720else:2721 files = self.extractFilesFromCommit(description)2722 self.commit(description, files, self.branch,2723 self.initialParent)2724# only needed once, to connect to the previous commit2725 self.initialParent =""2726exceptIOError:2727print self.gitError.read()2728 sys.exit(1)27292730defimportHeadRevision(self, revision):2731print"Doing initial import of%sfrom revision%sinto%s"% (' '.join(self.depotPaths), revision, self.branch)27322733 details = {}2734 details["user"] ="git perforce import user"2735 details["desc"] = ("Initial import of%sfrom the state at revision%s\n"2736% (' '.join(self.depotPaths), revision))2737 details["change"] = revision2738 newestRevision =027392740 fileCnt =02741 fileArgs = ["%s...%s"% (p,revision)for p in self.depotPaths]27422743for info inp4CmdList(["files"] + fileArgs):27442745if'code'in info and info['code'] =='error':2746 sys.stderr.write("p4 returned an error:%s\n"2747% info['data'])2748if info['data'].find("must refer to client") >=0:2749 sys.stderr.write("This particular p4 error is misleading.\n")2750 sys.stderr.write("Perhaps the depot path was misspelled.\n");2751 sys.stderr.write("Depot path:%s\n"%" ".join(self.depotPaths))2752 sys.exit(1)2753if'p4ExitCode'in info:2754 sys.stderr.write("p4 exitcode:%s\n"% info['p4ExitCode'])2755 sys.exit(1)275627572758 change =int(info["change"])2759if change > newestRevision:2760 newestRevision = change27612762if info["action"]in self.delete_actions:2763# don't increase the file cnt, otherwise details["depotFile123"] will have gaps!2764#fileCnt = fileCnt + 12765continue27662767for prop in["depotFile","rev","action","type"]:2768 details["%s%s"% (prop, fileCnt)] = info[prop]27692770 fileCnt = fileCnt +127712772 details["change"] = newestRevision27732774# Use time from top-most change so that all git p4 clones of2775# the same p4 repo have the same commit SHA1s.2776 res =p4_describe(newestRevision)2777 details["time"] = res["time"]27782779 self.updateOptionDict(details)2780try:2781 self.commit(details, self.extractFilesFromCommit(details), self.branch)2782exceptIOError:2783print"IO error with git fast-import. Is your git version recent enough?"2784print self.gitError.read()278527862787defrun(self, args):2788 self.depotPaths = []2789 self.changeRange =""2790 self.previousDepotPaths = []2791 self.hasOrigin =False27922793# map from branch depot path to parent branch2794 self.knownBranches = {}2795 self.initialParents = {}27962797if self.importIntoRemotes:2798 self.refPrefix ="refs/remotes/p4/"2799else:2800 self.refPrefix ="refs/heads/p4/"28012802if self.syncWithOrigin:2803 self.hasOrigin =originP4BranchesExist()2804if self.hasOrigin:2805if not self.silent:2806print'Syncing with origin first, using "git fetch origin"'2807system("git fetch origin")28082809 branch_arg_given =bool(self.branch)2810iflen(self.branch) ==0:2811 self.branch = self.refPrefix +"master"2812ifgitBranchExists("refs/heads/p4")and self.importIntoRemotes:2813system("git update-ref%srefs/heads/p4"% self.branch)2814system("git branch -D p4")28152816# accept either the command-line option, or the configuration variable2817if self.useClientSpec:2818# will use this after clone to set the variable2819 self.useClientSpec_from_options =True2820else:2821ifgitConfig("git-p4.useclientspec","--bool") =="true":2822 self.useClientSpec =True2823if self.useClientSpec:2824 self.clientSpecDirs =getClientSpec()28252826# TODO: should always look at previous commits,2827# merge with previous imports, if possible.2828if args == []:2829if self.hasOrigin:2830createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)28312832# branches holds mapping from branch name to sha12833 branches =p4BranchesInGit(self.importIntoRemotes)28342835# restrict to just this one, disabling detect-branches2836if branch_arg_given:2837 short = self.branch.split("/")[-1]2838if short in branches:2839 self.p4BranchesInGit = [ short ]2840else:2841 self.p4BranchesInGit = branches.keys()28422843iflen(self.p4BranchesInGit) >1:2844if not self.silent:2845print"Importing from/into multiple branches"2846 self.detectBranches =True2847for branch in branches.keys():2848 self.initialParents[self.refPrefix + branch] = \2849 branches[branch]28502851if self.verbose:2852print"branches:%s"% self.p4BranchesInGit28532854 p4Change =02855for branch in self.p4BranchesInGit:2856 logMsg =extractLogMessageFromGitCommit(self.refPrefix + branch)28572858 settings =extractSettingsGitLog(logMsg)28592860 self.readOptions(settings)2861if(settings.has_key('depot-paths')2862and settings.has_key('change')):2863 change =int(settings['change']) +12864 p4Change =max(p4Change, change)28652866 depotPaths =sorted(settings['depot-paths'])2867if self.previousDepotPaths == []:2868 self.previousDepotPaths = depotPaths2869else:2870 paths = []2871for(prev, cur)inzip(self.previousDepotPaths, depotPaths):2872 prev_list = prev.split("/")2873 cur_list = cur.split("/")2874for i inrange(0,min(len(cur_list),len(prev_list))):2875if cur_list[i] <> prev_list[i]:2876 i = i -12877break28782879 paths.append("/".join(cur_list[:i +1]))28802881 self.previousDepotPaths = paths28822883if p4Change >0:2884 self.depotPaths =sorted(self.previousDepotPaths)2885 self.changeRange ="@%s,#head"% p4Change2886if not self.silent and not self.detectBranches:2887print"Performing incremental import into%sgit branch"% self.branch28882889# accept multiple ref name abbreviations:2890# refs/foo/bar/branch -> use it exactly2891# p4/branch -> prepend refs/remotes/ or refs/heads/2892# branch -> prepend refs/remotes/p4/ or refs/heads/p4/2893if not self.branch.startswith("refs/"):2894if self.importIntoRemotes:2895 prepend ="refs/remotes/"2896else:2897 prepend ="refs/heads/"2898if not self.branch.startswith("p4/"):2899 prepend +="p4/"2900 self.branch = prepend + self.branch29012902iflen(args) ==0and self.depotPaths:2903if not self.silent:2904print"Depot paths:%s"%' '.join(self.depotPaths)2905else:2906if self.depotPaths and self.depotPaths != args:2907print("previous import used depot path%sand now%swas specified. "2908"This doesn't work!"% (' '.join(self.depotPaths),2909' '.join(args)))2910 sys.exit(1)29112912 self.depotPaths =sorted(args)29132914 revision =""2915 self.users = {}29162917# Make sure no revision specifiers are used when --changesfile2918# is specified.2919 bad_changesfile =False2920iflen(self.changesFile) >0:2921for p in self.depotPaths:2922if p.find("@") >=0or p.find("#") >=0:2923 bad_changesfile =True2924break2925if bad_changesfile:2926die("Option --changesfile is incompatible with revision specifiers")29272928 newPaths = []2929for p in self.depotPaths:2930if p.find("@") != -1:2931 atIdx = p.index("@")2932 self.changeRange = p[atIdx:]2933if self.changeRange =="@all":2934 self.changeRange =""2935elif','not in self.changeRange:2936 revision = self.changeRange2937 self.changeRange =""2938 p = p[:atIdx]2939elif p.find("#") != -1:2940 hashIdx = p.index("#")2941 revision = p[hashIdx:]2942 p = p[:hashIdx]2943elif self.previousDepotPaths == []:2944# pay attention to changesfile, if given, else import2945# the entire p4 tree at the head revision2946iflen(self.changesFile) ==0:2947 revision ="#head"29482949 p = re.sub("\.\.\.$","", p)2950if not p.endswith("/"):2951 p +="/"29522953 newPaths.append(p)29542955 self.depotPaths = newPaths29562957# --detect-branches may change this for each branch2958 self.branchPrefixes = self.depotPaths29592960 self.loadUserMapFromCache()2961 self.labels = {}2962if self.detectLabels:2963 self.getLabels();29642965if self.detectBranches:2966## FIXME - what's a P4 projectName ?2967 self.projectName = self.guessProjectName()29682969if self.hasOrigin:2970 self.getBranchMappingFromGitBranches()2971else:2972 self.getBranchMapping()2973if self.verbose:2974print"p4-git branches:%s"% self.p4BranchesInGit2975print"initial parents:%s"% self.initialParents2976for b in self.p4BranchesInGit:2977if b !="master":29782979## FIXME2980 b = b[len(self.projectName):]2981 self.createdBranches.add(b)29822983 self.tz ="%+03d%02d"% (- time.timezone /3600, ((- time.timezone %3600) /60))29842985 self.importProcess = subprocess.Popen(["git","fast-import"],2986 stdin=subprocess.PIPE,2987 stdout=subprocess.PIPE,2988 stderr=subprocess.PIPE);2989 self.gitOutput = self.importProcess.stdout2990 self.gitStream = self.importProcess.stdin2991 self.gitError = self.importProcess.stderr29922993if revision:2994 self.importHeadRevision(revision)2995else:2996 changes = []29972998iflen(self.changesFile) >0:2999 output =open(self.changesFile).readlines()3000 changeSet =set()3001for line in output:3002 changeSet.add(int(line))30033004for change in changeSet:3005 changes.append(change)30063007 changes.sort()3008else:3009# catch "git p4 sync" with no new branches, in a repo that3010# does not have any existing p4 branches3011iflen(args) ==0:3012if not self.p4BranchesInGit:3013die("No remote p4 branches. Perhaps you never did\"git p4 clone\"in here.")30143015# The default branch is master, unless --branch is used to3016# specify something else. Make sure it exists, or complain3017# nicely about how to use --branch.3018if not self.detectBranches:3019if notbranch_exists(self.branch):3020if branch_arg_given:3021die("Error: branch%sdoes not exist."% self.branch)3022else:3023die("Error: no branch%s; perhaps specify one with --branch."%3024 self.branch)30253026if self.verbose:3027print"Getting p4 changes for%s...%s"% (', '.join(self.depotPaths),3028 self.changeRange)3029 changes =p4ChangesForPaths(self.depotPaths, self.changeRange)30303031iflen(self.maxChanges) >0:3032 changes = changes[:min(int(self.maxChanges),len(changes))]30333034iflen(changes) ==0:3035if not self.silent:3036print"No changes to import!"3037else:3038if not self.silent and not self.detectBranches:3039print"Import destination:%s"% self.branch30403041 self.updatedBranches =set()30423043if not self.detectBranches:3044if args:3045# start a new branch3046 self.initialParent =""3047else:3048# build on a previous revision3049 self.initialParent =parseRevision(self.branch)30503051 self.importChanges(changes)30523053if not self.silent:3054print""3055iflen(self.updatedBranches) >0:3056 sys.stdout.write("Updated branches: ")3057for b in self.updatedBranches:3058 sys.stdout.write("%s"% b)3059 sys.stdout.write("\n")30603061ifgitConfig("git-p4.importLabels","--bool") =="true":3062 self.importLabels =True30633064if self.importLabels:3065 p4Labels =getP4Labels(self.depotPaths)3066 gitTags =getGitTags()30673068 missingP4Labels = p4Labels - gitTags3069 self.importP4Labels(self.gitStream, missingP4Labels)30703071 self.gitStream.close()3072if self.importProcess.wait() !=0:3073die("fast-import failed:%s"% self.gitError.read())3074 self.gitOutput.close()3075 self.gitError.close()30763077# Cleanup temporary branches created during import3078if self.tempBranches != []:3079for branch in self.tempBranches:3080read_pipe("git update-ref -d%s"% branch)3081 os.rmdir(os.path.join(os.environ.get("GIT_DIR",".git"), self.tempBranchLocation))30823083# Create a symbolic ref p4/HEAD pointing to p4/<branch> to allow3084# a convenient shortcut refname "p4".3085if self.importIntoRemotes:3086 head_ref = self.refPrefix +"HEAD"3087if notgitBranchExists(head_ref)andgitBranchExists(self.branch):3088system(["git","symbolic-ref", head_ref, self.branch])30893090return True30913092classP4Rebase(Command):3093def__init__(self):3094 Command.__init__(self)3095 self.options = [3096 optparse.make_option("--import-labels", dest="importLabels", action="store_true"),3097]3098 self.importLabels =False3099 self.description = ("Fetches the latest revision from perforce and "3100+"rebases the current work (branch) against it")31013102defrun(self, args):3103 sync =P4Sync()3104 sync.importLabels = self.importLabels3105 sync.run([])31063107return self.rebase()31083109defrebase(self):3110if os.system("git update-index --refresh") !=0:3111die("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.");3112iflen(read_pipe("git diff-index HEAD --")) >0:3113die("You have uncommited changes. Please commit them before rebasing or stash them away with git stash.");31143115[upstream, settings] =findUpstreamBranchPoint()3116iflen(upstream) ==0:3117die("Cannot find upstream branchpoint for rebase")31183119# the branchpoint may be p4/foo~3, so strip off the parent3120 upstream = re.sub("~[0-9]+$","", upstream)31213122print"Rebasing the current branch onto%s"% upstream3123 oldHead =read_pipe("git rev-parse HEAD").strip()3124system("git rebase%s"% upstream)3125system("git diff-tree --stat --summary -M%sHEAD"% oldHead)3126return True31273128classP4Clone(P4Sync):3129def__init__(self):3130 P4Sync.__init__(self)3131 self.description ="Creates a new git repository and imports from Perforce into it"3132 self.usage ="usage: %prog [options] //depot/path[@revRange]"3133 self.options += [3134 optparse.make_option("--destination", dest="cloneDestination",3135 action='store', default=None,3136help="where to leave result of the clone"),3137 optparse.make_option("-/", dest="cloneExclude",3138 action="append",type="string",3139help="exclude depot path"),3140 optparse.make_option("--bare", dest="cloneBare",3141 action="store_true", default=False),3142]3143 self.cloneDestination =None3144 self.needsGit =False3145 self.cloneBare =False31463147# This is required for the "append" cloneExclude action3148defensure_value(self, attr, value):3149if nothasattr(self, attr)orgetattr(self, attr)is None:3150setattr(self, attr, value)3151returngetattr(self, attr)31523153defdefaultDestination(self, args):3154## TODO: use common prefix of args?3155 depotPath = args[0]3156 depotDir = re.sub("(@[^@]*)$","", depotPath)3157 depotDir = re.sub("(#[^#]*)$","", depotDir)3158 depotDir = re.sub(r"\.\.\.$","", depotDir)3159 depotDir = re.sub(r"/$","", depotDir)3160return os.path.split(depotDir)[1]31613162defrun(self, args):3163iflen(args) <1:3164return False31653166if self.keepRepoPath and not self.cloneDestination:3167 sys.stderr.write("Must specify destination for --keep-path\n")3168 sys.exit(1)31693170 depotPaths = args31713172if not self.cloneDestination andlen(depotPaths) >1:3173 self.cloneDestination = depotPaths[-1]3174 depotPaths = depotPaths[:-1]31753176 self.cloneExclude = ["/"+p for p in self.cloneExclude]3177for p in depotPaths:3178if not p.startswith("//"):3179 sys.stderr.write('Depot paths must start with "//":%s\n'% p)3180return False31813182if not self.cloneDestination:3183 self.cloneDestination = self.defaultDestination(args)31843185print"Importing from%sinto%s"% (', '.join(depotPaths), self.cloneDestination)31863187if not os.path.exists(self.cloneDestination):3188 os.makedirs(self.cloneDestination)3189chdir(self.cloneDestination)31903191 init_cmd = ["git","init"]3192if self.cloneBare:3193 init_cmd.append("--bare")3194 subprocess.check_call(init_cmd)31953196if not P4Sync.run(self, depotPaths):3197return False31983199# create a master branch and check out a work tree3200ifgitBranchExists(self.branch):3201system(["git","branch","master", self.branch ])3202if not self.cloneBare:3203system(["git","checkout","-f"])3204else:3205print'Not checking out any branch, use ' \3206'"git checkout -q -b master <branch>"'32073208# auto-set this variable if invoked with --use-client-spec3209if self.useClientSpec_from_options:3210system("git config --bool git-p4.useclientspec true")32113212return True32133214classP4Branches(Command):3215def__init__(self):3216 Command.__init__(self)3217 self.options = [ ]3218 self.description = ("Shows the git branches that hold imports and their "3219+"corresponding perforce depot paths")3220 self.verbose =False32213222defrun(self, args):3223iforiginP4BranchesExist():3224createOrUpdateBranchesFromOrigin()32253226 cmdline ="git rev-parse --symbolic "3227 cmdline +=" --remotes"32283229for line inread_pipe_lines(cmdline):3230 line = line.strip()32313232if not line.startswith('p4/')or line =="p4/HEAD":3233continue3234 branch = line32353236 log =extractLogMessageFromGitCommit("refs/remotes/%s"% branch)3237 settings =extractSettingsGitLog(log)32383239print"%s<=%s(%s)"% (branch,",".join(settings["depot-paths"]), settings["change"])3240return True32413242classHelpFormatter(optparse.IndentedHelpFormatter):3243def__init__(self):3244 optparse.IndentedHelpFormatter.__init__(self)32453246defformat_description(self, description):3247if description:3248return description +"\n"3249else:3250return""32513252defprintUsage(commands):3253print"usage:%s<command> [options]"% sys.argv[0]3254print""3255print"valid commands:%s"%", ".join(commands)3256print""3257print"Try%s<command> --help for command specific help."% sys.argv[0]3258print""32593260commands = {3261"debug": P4Debug,3262"submit": P4Submit,3263"commit": P4Submit,3264"sync": P4Sync,3265"rebase": P4Rebase,3266"clone": P4Clone,3267"rollback": P4RollBack,3268"branches": P4Branches3269}327032713272defmain():3273iflen(sys.argv[1:]) ==0:3274printUsage(commands.keys())3275 sys.exit(2)32763277 cmdName = sys.argv[1]3278try:3279 klass = commands[cmdName]3280 cmd =klass()3281exceptKeyError:3282print"unknown command%s"% cmdName3283print""3284printUsage(commands.keys())3285 sys.exit(2)32863287 options = cmd.options3288 cmd.gitdir = os.environ.get("GIT_DIR",None)32893290 args = sys.argv[2:]32913292 options.append(optparse.make_option("--verbose","-v", dest="verbose", action="store_true"))3293if cmd.needsGit:3294 options.append(optparse.make_option("--git-dir", dest="gitdir"))32953296 parser = optparse.OptionParser(cmd.usage.replace("%prog","%prog "+ cmdName),3297 options,3298 description = cmd.description,3299 formatter =HelpFormatter())33003301(cmd, args) = parser.parse_args(sys.argv[2:], cmd);3302global verbose3303 verbose = cmd.verbose3304if cmd.needsGit:3305if cmd.gitdir ==None:3306 cmd.gitdir = os.path.abspath(".git")3307if notisValidGitDir(cmd.gitdir):3308 cmd.gitdir =read_pipe("git rev-parse --git-dir").strip()3309if os.path.exists(cmd.gitdir):3310 cdup =read_pipe("git rev-parse --show-cdup").strip()3311iflen(cdup) >0:3312chdir(cdup);33133314if notisValidGitDir(cmd.gitdir):3315ifisValidGitDir(cmd.gitdir +"/.git"):3316 cmd.gitdir +="/.git"3317else:3318die("fatal: cannot locate git repository at%s"% cmd.gitdir)33193320 os.environ["GIT_DIR"] = cmd.gitdir33213322if not cmd.run(args):3323 parser.print_help()3324 sys.exit(2)332533263327if __name__ =='__main__':3328main()