3 # git-p4.py -- A tool for bidirectional operation between a Perforce depot and git.
5 # Author: Simon Hausmann <simon@lst.de>
6 # Copyright: 2007 Simon Hausmann <simon@lst.de>
8 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
11 import optparse, sys, os, marshal, subprocess, shelve
12 import tempfile, getopt, os.path, time, platform
18 def p4_build_cmd(cmd):
19 """Build a suitable p4 command line.
21 This consolidates building and returning a p4 command line into one
22 location. It means that hooking into the environment, or other configuration
23 can be done more easily.
27 user = gitConfig("git-p4.user")
29 real_cmd += ["-u",user]
31 password = gitConfig("git-p4.password")
33 real_cmd += ["-P", password]
35 port = gitConfig("git-p4.port")
37 real_cmd += ["-p", port]
39 host = gitConfig("git-p4.host")
41 real_cmd += ["-h", host]
43 client = gitConfig("git-p4.client")
45 real_cmd += ["-c", client]
48 if isinstance(cmd,basestring):
49 real_cmd = ' '.join(real_cmd) + ' ' + cmd
55 # P4 uses the PWD environment variable rather than getcwd(). Since we're
56 # not using the shell, we have to set it ourselves. This path could
57 # be relative, so go there first, then figure out where we ended up.
59 os.environ['PWD'] = os.getcwd()
65 sys.stderr.write(msg + "\n")
68 def write_pipe(c, stdin):
70 sys.stderr.write('Writing pipe: %s\n' % str(c))
72 expand = isinstance(c,basestring)
73 p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand)
75 val = pipe.write(stdin)
78 die('Command failed: %s' % str(c))
82 def p4_write_pipe(c, stdin):
83 real_cmd = p4_build_cmd(c)
84 return write_pipe(real_cmd, stdin)
86 def read_pipe(c, ignore_error=False):
88 sys.stderr.write('Reading pipe: %s\n' % str(c))
90 expand = isinstance(c,basestring)
91 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
94 if p.wait() and not ignore_error:
95 die('Command failed: %s' % str(c))
99 def p4_read_pipe(c, ignore_error=False):
100 real_cmd = p4_build_cmd(c)
101 return read_pipe(real_cmd, ignore_error)
103 def read_pipe_lines(c):
105 sys.stderr.write('Reading pipe: %s\n' % str(c))
107 expand = isinstance(c, basestring)
108 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
110 val = pipe.readlines()
111 if pipe.close() or p.wait():
112 die('Command failed: %s' % str(c))
116 def p4_read_pipe_lines(c):
117 """Specifically invoke p4 on the command supplied. """
118 real_cmd = p4_build_cmd(c)
119 return read_pipe_lines(real_cmd)
122 expand = isinstance(cmd,basestring)
124 sys.stderr.write("executing %s\n" % str(cmd))
125 subprocess.check_call(cmd, shell=expand)
128 """Specifically invoke p4 as the system command. """
129 real_cmd = p4_build_cmd(cmd)
130 expand = isinstance(real_cmd, basestring)
131 subprocess.check_call(real_cmd, shell=expand)
133 def p4_integrate(src, dest):
134 p4_system(["integrate", "-Dt", src, dest])
137 p4_system(["sync", path])
140 p4_system(["add", f])
143 p4_system(["delete", f])
146 p4_system(["edit", f])
149 p4_system(["revert", f])
151 def p4_reopen(type, file):
152 p4_system(["reopen", "-t", type, file])
155 # Canonicalize the p4 type and return a tuple of the
156 # base type, plus any modifiers. See "p4 help filetypes"
157 # for a list and explanation.
159 def split_p4_type(p4type):
161 p4_filetypes_historical = {
162 "ctempobj": "binary+Sw",
168 "tempobj": "binary+FSw",
169 "ubinary": "binary+F",
170 "uresource": "resource+F",
171 "uxbinary": "binary+Fx",
172 "xbinary": "binary+x",
174 "xtempobj": "binary+Swx",
176 "xunicode": "unicode+x",
179 if p4type in p4_filetypes_historical:
180 p4type = p4_filetypes_historical[p4type]
182 s = p4type.split("+")
190 def setP4ExecBit(file, mode):
191 # Reopens an already open file and changes the execute bit to match
192 # the execute bit setting in the passed in mode.
196 if not isModeExec(mode):
197 p4Type = getP4OpenedType(file)
198 p4Type = re.sub('^([cku]?)x(.*)', '\\1\\2', p4Type)
199 p4Type = re.sub('(.*?\+.*?)x(.*?)', '\\1\\2', p4Type)
200 if p4Type[-1] == "+":
201 p4Type = p4Type[0:-1]
203 p4_reopen(p4Type, file)
205 def getP4OpenedType(file):
206 # Returns the perforce file type for the given file.
208 result = p4_read_pipe(["opened", file])
209 match = re.match(".*\((.+)\)\r?$", result)
211 return match.group(1)
213 die("Could not determine file type for %s (result: '%s')" % (file, result))
215 def diffTreePattern():
216 # This is a simple generator for the diff tree regex pattern. This could be
217 # a class variable if this and parseDiffTreeEntry were a part of a class.
218 pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)')
222 def parseDiffTreeEntry(entry):
223 """Parses a single diff tree entry into its component elements.
225 See git-diff-tree(1) manpage for details about the format of the diff
226 output. This method returns a dictionary with the following elements:
228 src_mode - The mode of the source file
229 dst_mode - The mode of the destination file
230 src_sha1 - The sha1 for the source file
231 dst_sha1 - The sha1 fr the destination file
232 status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc)
233 status_score - The score for the status (applicable for 'C' and 'R'
234 statuses). This is None if there is no score.
235 src - The path for the source file.
236 dst - The path for the destination file. This is only present for
237 copy or renames. If it is not present, this is None.
239 If the pattern is not matched, None is returned."""
241 match = diffTreePattern().next().match(entry)
244 'src_mode': match.group(1),
245 'dst_mode': match.group(2),
246 'src_sha1': match.group(3),
247 'dst_sha1': match.group(4),
248 'status': match.group(5),
249 'status_score': match.group(6),
250 'src': match.group(7),
251 'dst': match.group(10)
255 def isModeExec(mode):
256 # Returns True if the given git mode represents an executable file,
258 return mode[-3:] == "755"
260 def isModeExecChanged(src_mode, dst_mode):
261 return isModeExec(src_mode) != isModeExec(dst_mode)
263 def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None):
265 if isinstance(cmd,basestring):
272 cmd = p4_build_cmd(cmd)
274 sys.stderr.write("Opening pipe: %s\n" % str(cmd))
276 # Use a temporary file to avoid deadlocks without
277 # subprocess.communicate(), which would put another copy
278 # of stdout into memory.
280 if stdin is not None:
281 stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode)
282 if isinstance(stdin,basestring):
283 stdin_file.write(stdin)
286 stdin_file.write(i + '\n')
290 p4 = subprocess.Popen(cmd,
293 stdout=subprocess.PIPE)
298 entry = marshal.load(p4.stdout)
308 entry["p4ExitCode"] = exitCode
314 list = p4CmdList(cmd)
320 def p4Where(depotPath):
321 if not depotPath.endswith("/"):
323 depotPath = depotPath + "..."
324 outputList = p4CmdList(["where", depotPath])
326 for entry in outputList:
327 if "depotFile" in entry:
328 if entry["depotFile"] == depotPath:
331 elif "data" in entry:
332 data = entry.get("data")
333 space = data.find(" ")
334 if data[:space] == depotPath:
339 if output["code"] == "error":
343 clientPath = output.get("path")
344 elif "data" in output:
345 data = output.get("data")
346 lastSpace = data.rfind(" ")
347 clientPath = data[lastSpace + 1:]
349 if clientPath.endswith("..."):
350 clientPath = clientPath[:-3]
353 def currentGitBranch():
354 return read_pipe("git name-rev HEAD").split(" ")[1].strip()
356 def isValidGitDir(path):
357 if (os.path.exists(path + "/HEAD")
358 and os.path.exists(path + "/refs") and os.path.exists(path + "/objects")):
362 def parseRevision(ref):
363 return read_pipe("git rev-parse %s" % ref).strip()
365 def extractLogMessageFromGitCommit(commit):
368 ## fixme: title is first line of commit, not 1st paragraph.
370 for log in read_pipe_lines("git cat-file commit %s" % commit):
379 def extractSettingsGitLog(log):
381 for line in log.split("\n"):
383 m = re.search (r"^ *\[git-p4: (.*)\]$", line)
387 assignments = m.group(1).split (':')
388 for a in assignments:
390 key = vals[0].strip()
391 val = ('='.join (vals[1:])).strip()
392 if val.endswith ('\"') and val.startswith('"'):
397 paths = values.get("depot-paths")
399 paths = values.get("depot-path")
401 values['depot-paths'] = paths.split(',')
404 def gitBranchExists(branch):
405 proc = subprocess.Popen(["git", "rev-parse", branch],
406 stderr=subprocess.PIPE, stdout=subprocess.PIPE);
407 return proc.wait() == 0;
410 def gitConfig(key, args = None): # set args to "--bool", for instance
411 if not _gitConfig.has_key(key):
414 argsFilter = "%s " % args
415 cmd = "git config %s%s" % (argsFilter, key)
416 _gitConfig[key] = read_pipe(cmd, ignore_error=True).strip()
417 return _gitConfig[key]
419 def gitConfigList(key):
420 if not _gitConfig.has_key(key):
421 _gitConfig[key] = read_pipe("git config --get-all %s" % key, ignore_error=True).strip().split(os.linesep)
422 return _gitConfig[key]
424 def p4BranchesInGit(branchesAreInRemotes = True):
427 cmdline = "git rev-parse --symbolic "
428 if branchesAreInRemotes:
429 cmdline += " --remotes"
431 cmdline += " --branches"
433 for line in read_pipe_lines(cmdline):
436 ## only import to p4/
437 if not line.startswith('p4/') or line == "p4/HEAD":
442 branch = re.sub ("^p4/", "", line)
444 branches[branch] = parseRevision(line)
447 def findUpstreamBranchPoint(head = "HEAD"):
448 branches = p4BranchesInGit()
449 # map from depot-path to branch name
450 branchByDepotPath = {}
451 for branch in branches.keys():
452 tip = branches[branch]
453 log = extractLogMessageFromGitCommit(tip)
454 settings = extractSettingsGitLog(log)
455 if settings.has_key("depot-paths"):
456 paths = ",".join(settings["depot-paths"])
457 branchByDepotPath[paths] = "remotes/p4/" + branch
461 while parent < 65535:
462 commit = head + "~%s" % parent
463 log = extractLogMessageFromGitCommit(commit)
464 settings = extractSettingsGitLog(log)
465 if settings.has_key("depot-paths"):
466 paths = ",".join(settings["depot-paths"])
467 if branchByDepotPath.has_key(paths):
468 return [branchByDepotPath[paths], settings]
472 return ["", settings]
474 def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True):
476 print ("Creating/updating branch(es) in %s based on origin branch(es)"
479 originPrefix = "origin/p4/"
481 for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
483 if (not line.startswith(originPrefix)) or line.endswith("HEAD"):
486 headName = line[len(originPrefix):]
487 remoteHead = localRefPrefix + headName
490 original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))
491 if (not original.has_key('depot-paths')
492 or not original.has_key('change')):
496 if not gitBranchExists(remoteHead):
498 print "creating %s" % remoteHead
501 settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))
502 if settings.has_key('change') > 0:
503 if settings['depot-paths'] == original['depot-paths']:
504 originP4Change = int(original['change'])
505 p4Change = int(settings['change'])
506 if originP4Change > p4Change:
507 print ("%s (%s) is newer than %s (%s). "
508 "Updating p4 branch from origin."
509 % (originHead, originP4Change,
510 remoteHead, p4Change))
513 print ("Ignoring: %s was imported from %s while "
514 "%s was imported from %s"
515 % (originHead, ','.join(original['depot-paths']),
516 remoteHead, ','.join(settings['depot-paths'])))
519 system("git update-ref %s %s" % (remoteHead, originHead))
521 def originP4BranchesExist():
522 return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
524 def p4ChangesForPaths(depotPaths, changeRange):
528 cmd += ["%s...%s" % (p, changeRange)]
529 output = p4_read_pipe_lines(cmd)
533 changeNum = int(line.split(" ")[1])
534 changes[changeNum] = True
536 changelist = changes.keys()
540 def p4PathStartsWith(path, prefix):
541 # This method tries to remedy a potential mixed-case issue:
543 # If UserA adds //depot/DirA/file1
544 # and UserB adds //depot/dira/file2
546 # we may or may not have a problem. If you have core.ignorecase=true,
547 # we treat DirA and dira as the same directory
548 ignorecase = gitConfig("core.ignorecase", "--bool") == "true"
550 return path.lower().startswith(prefix.lower())
551 return path.startswith(prefix)
555 self.usage = "usage: %prog [options]"
560 self.userMapFromPerforceServer = False
562 def getUserCacheFilename(self):
563 home = os.environ.get("HOME", os.environ.get("USERPROFILE"))
564 return home + "/.gitp4-usercache.txt"
566 def getUserMapFromPerforceServer(self):
567 if self.userMapFromPerforceServer:
572 for output in p4CmdList("users"):
573 if not output.has_key("User"):
575 self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
576 self.emails[output["Email"]] = output["User"]
580 for (key, val) in self.users.items():
581 s += "%s\t%s\n" % (key.expandtabs(1), val.expandtabs(1))
583 open(self.getUserCacheFilename(), "wb").write(s)
584 self.userMapFromPerforceServer = True
586 def loadUserMapFromCache(self):
588 self.userMapFromPerforceServer = False
590 cache = open(self.getUserCacheFilename(), "rb")
591 lines = cache.readlines()
594 entry = line.strip().split("\t")
595 self.users[entry[0]] = entry[1]
597 self.getUserMapFromPerforceServer()
599 class P4Debug(Command):
601 Command.__init__(self)
603 optparse.make_option("--verbose", dest="verbose", action="store_true",
606 self.description = "A tool to debug the output of p4 -G."
607 self.needsGit = False
612 for output in p4CmdList(args):
613 print 'Element: %d' % j
618 class P4RollBack(Command):
620 Command.__init__(self)
622 optparse.make_option("--verbose", dest="verbose", action="store_true"),
623 optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true")
625 self.description = "A tool to debug the multi-branch import. Don't use :)"
627 self.rollbackLocalBranches = False
632 maxChange = int(args[0])
634 if "p4ExitCode" in p4Cmd("changes -m 1"):
635 die("Problems executing p4");
637 if self.rollbackLocalBranches:
638 refPrefix = "refs/heads/"
639 lines = read_pipe_lines("git rev-parse --symbolic --branches")
641 refPrefix = "refs/remotes/"
642 lines = read_pipe_lines("git rev-parse --symbolic --remotes")
645 if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"):
647 ref = refPrefix + line
648 log = extractLogMessageFromGitCommit(ref)
649 settings = extractSettingsGitLog(log)
651 depotPaths = settings['depot-paths']
652 change = settings['change']
656 if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange)
657 for p in depotPaths]))) == 0:
658 print "Branch %s did not exist at change %s, deleting." % (ref, maxChange)
659 system("git update-ref -d %s `git rev-parse %s`" % (ref, ref))
662 while change and int(change) > maxChange:
665 print "%s is at %s ; rewinding towards %s" % (ref, change, maxChange)
666 system("git update-ref %s \"%s^\"" % (ref, ref))
667 log = extractLogMessageFromGitCommit(ref)
668 settings = extractSettingsGitLog(log)
671 depotPaths = settings['depot-paths']
672 change = settings['change']
675 print "%s rewound to %s" % (ref, change)
679 class P4Submit(Command, P4UserMap):
681 Command.__init__(self)
682 P4UserMap.__init__(self)
684 optparse.make_option("--verbose", dest="verbose", action="store_true"),
685 optparse.make_option("--origin", dest="origin"),
686 optparse.make_option("-M", dest="detectRenames", action="store_true"),
687 # preserve the user, requires relevant p4 permissions
688 optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
690 self.description = "Submit changes from git to the perforce depot."
691 self.usage += " [name of git branch to submit into perforce depot]"
692 self.interactive = True
694 self.detectRenames = False
696 self.preserveUser = gitConfig("git-p4.preserveUser").lower() == "true"
697 self.isWindows = (platform.system() == "Windows")
698 self.myP4UserId = None
701 if len(p4CmdList("opened ...")) > 0:
702 die("You have files opened with perforce! Close them before starting the sync.")
704 # replaces everything between 'Description:' and the next P4 submit template field with the
706 def prepareLogMessage(self, template, message):
709 inDescriptionSection = False
711 for line in template.split("\n"):
712 if line.startswith("#"):
713 result += line + "\n"
716 if inDescriptionSection:
717 if line.startswith("Files:") or line.startswith("Jobs:"):
718 inDescriptionSection = False
722 if line.startswith("Description:"):
723 inDescriptionSection = True
725 for messageLine in message.split("\n"):
726 line += "\t" + messageLine + "\n"
728 result += line + "\n"
732 def p4UserForCommit(self,id):
733 # Return the tuple (perforce user,git email) for a given git commit id
734 self.getUserMapFromPerforceServer()
735 gitEmail = read_pipe("git log --max-count=1 --format='%%ae' %s" % id)
736 gitEmail = gitEmail.strip()
737 if not self.emails.has_key(gitEmail):
738 return (None,gitEmail)
740 return (self.emails[gitEmail],gitEmail)
742 def checkValidP4Users(self,commits):
743 # check if any git authors cannot be mapped to p4 users
745 (user,email) = self.p4UserForCommit(id)
747 msg = "Cannot find p4 user for email %s in commit %s." % (email, id)
748 if gitConfig('git-p4.allowMissingP4Users').lower() == "true":
751 die("Error: %s\nSet git-p4.allowMissingP4Users to true to allow this." % msg)
753 def lastP4Changelist(self):
754 # Get back the last changelist number submitted in this client spec. This
755 # then gets used to patch up the username in the change. If the same
756 # client spec is being used by multiple processes then this might go
758 results = p4CmdList("client -o") # find the current client
761 if r.has_key('Client'):
765 die("could not get client spec")
766 results = p4CmdList(["changes", "-c", client, "-m", "1"])
768 if r.has_key('change'):
770 die("Could not get changelist number for last submit - cannot patch up user details")
772 def modifyChangelistUser(self, changelist, newUser):
773 # fixup the user field of a changelist after it has been submitted.
774 changes = p4CmdList("change -o %s" % changelist)
775 if len(changes) != 1:
776 die("Bad output from p4 change modifying %s to user %s" %
777 (changelist, newUser))
780 if c['User'] == newUser: return # nothing to do
782 input = marshal.dumps(c)
784 result = p4CmdList("change -f -i", stdin=input)
786 if r.has_key('code'):
787 if r['code'] == 'error':
788 die("Could not modify user field of changelist %s to %s:%s" % (changelist, newUser, r['data']))
789 if r.has_key('data'):
790 print("Updated user field for changelist %s to %s" % (changelist, newUser))
792 die("Could not modify user field of changelist %s to %s" % (changelist, newUser))
794 def canChangeChangelists(self):
795 # check to see if we have p4 admin or super-user permissions, either of
796 # which are required to modify changelists.
797 results = p4CmdList("protects %s" % self.depotPath)
799 if r.has_key('perm'):
800 if r['perm'] == 'admin':
802 if r['perm'] == 'super':
808 return self.myP4UserId
810 results = p4CmdList("user -o")
812 if r.has_key('User'):
813 self.myP4UserId = r['User']
815 die("Could not find your p4 user id")
817 def p4UserIsMe(self, p4User):
818 # return True if the given p4 user is actually me
820 if not p4User or p4User != me:
825 def prepareSubmitTemplate(self):
826 # remove lines in the Files section that show changes to files outside the depot path we're committing into
828 inFilesSection = False
829 for line in p4_read_pipe_lines(['change', '-o']):
830 if line.endswith("\r\n"):
831 line = line[:-2] + "\n"
833 if line.startswith("\t"):
834 # path starts and ends with a tab
836 lastTab = path.rfind("\t")
838 path = path[:lastTab]
839 if not p4PathStartsWith(path, self.depotPath):
842 inFilesSection = False
844 if line.startswith("Files:"):
845 inFilesSection = True
851 def edit_template(self, template_file):
852 """Invoke the editor to let the user change the submission
853 message. Return true if okay to continue with the submit."""
855 # if configured to skip the editing part, just submit
856 if gitConfig("git-p4.skipSubmitEdit") == "true":
859 # look at the modification time, to check later if the user saved
861 mtime = os.stat(template_file).st_mtime
864 if os.environ.has_key("P4EDITOR"):
865 editor = os.environ.get("P4EDITOR")
867 editor = read_pipe("git var GIT_EDITOR").strip()
868 system(editor + " " + template_file)
870 # If the file was not saved, prompt to see if this patch should
871 # be skipped. But skip this verification step if configured so.
872 if gitConfig("git-p4.skipSubmitEditCheck") == "true":
875 # modification time updated means user saved the file
876 if os.stat(template_file).st_mtime > mtime:
880 response = raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
886 def applyCommit(self, id):
887 print "Applying %s" % (read_pipe("git log --max-count=1 --pretty=oneline %s" % id))
889 (p4User, gitEmail) = self.p4UserForCommit(id)
891 if not self.detectRenames:
892 # If not explicitly set check the config variable
893 self.detectRenames = gitConfig("git-p4.detectRenames")
895 if self.detectRenames.lower() == "false" or self.detectRenames == "":
897 elif self.detectRenames.lower() == "true":
900 diffOpts = "-M%s" % self.detectRenames
902 detectCopies = gitConfig("git-p4.detectCopies")
903 if detectCopies.lower() == "true":
905 elif detectCopies != "" and detectCopies.lower() != "false":
906 diffOpts += " -C%s" % detectCopies
908 if gitConfig("git-p4.detectCopiesHarder", "--bool") == "true":
909 diffOpts += " --find-copies-harder"
911 diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (diffOpts, id, id))
913 filesToDelete = set()
915 filesToChangeExecBit = {}
917 diff = parseDiffTreeEntry(line)
918 modifier = diff['status']
922 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
923 filesToChangeExecBit[path] = diff['dst_mode']
924 editedFiles.add(path)
925 elif modifier == "A":
927 filesToChangeExecBit[path] = diff['dst_mode']
928 if path in filesToDelete:
929 filesToDelete.remove(path)
930 elif modifier == "D":
931 filesToDelete.add(path)
932 if path in filesToAdd:
933 filesToAdd.remove(path)
934 elif modifier == "C":
935 src, dest = diff['src'], diff['dst']
936 p4_integrate(src, dest)
937 if diff['src_sha1'] != diff['dst_sha1']:
939 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
941 filesToChangeExecBit[dest] = diff['dst_mode']
943 editedFiles.add(dest)
944 elif modifier == "R":
945 src, dest = diff['src'], diff['dst']
946 p4_integrate(src, dest)
947 if diff['src_sha1'] != diff['dst_sha1']:
949 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
951 filesToChangeExecBit[dest] = diff['dst_mode']
953 editedFiles.add(dest)
954 filesToDelete.add(src)
956 die("unknown modifier %s for %s" % (modifier, path))
958 diffcmd = "git format-patch -k --stdout \"%s^\"..\"%s\"" % (id, id)
959 patchcmd = diffcmd + " | git apply "
960 tryPatchCmd = patchcmd + "--check -"
961 applyPatchCmd = patchcmd + "--check --apply -"
963 if os.system(tryPatchCmd) != 0:
964 print "Unfortunately applying the change failed!"
965 print "What do you want to do?"
967 while response != "s" and response != "a" and response != "w":
968 response = raw_input("[s]kip this patch / [a]pply the patch forcibly "
969 "and with .rej files / [w]rite the patch to a file (patch.txt) ")
971 print "Skipping! Good luck with the next patches..."
972 for f in editedFiles:
977 elif response == "a":
978 os.system(applyPatchCmd)
979 if len(filesToAdd) > 0:
980 print "You may also want to call p4 add on the following files:"
981 print " ".join(filesToAdd)
982 if len(filesToDelete):
983 print "The following files should be scheduled for deletion with p4 delete:"
984 print " ".join(filesToDelete)
985 die("Please resolve and submit the conflict manually and "
986 + "continue afterwards with git-p4 submit --continue")
987 elif response == "w":
988 system(diffcmd + " > patch.txt")
989 print "Patch saved to patch.txt in %s !" % self.clientPath
990 die("Please resolve and submit the conflict manually and "
991 "continue afterwards with git-p4 submit --continue")
993 system(applyPatchCmd)
997 for f in filesToDelete:
1001 # Set/clear executable bits
1002 for f in filesToChangeExecBit.keys():
1003 mode = filesToChangeExecBit[f]
1004 setP4ExecBit(f, mode)
1006 logMessage = extractLogMessageFromGitCommit(id)
1007 logMessage = logMessage.strip()
1009 template = self.prepareSubmitTemplate()
1011 if self.interactive:
1012 submitTemplate = self.prepareLogMessage(template, logMessage)
1014 if self.preserveUser:
1015 submitTemplate = submitTemplate + ("\n######## Actual user %s, modified after commit\n" % p4User)
1017 if os.environ.has_key("P4DIFF"):
1018 del(os.environ["P4DIFF"])
1020 for editedFile in editedFiles:
1021 diff += p4_read_pipe(['diff', '-du', editedFile])
1024 for newFile in filesToAdd:
1025 newdiff += "==== new file ====\n"
1026 newdiff += "--- /dev/null\n"
1027 newdiff += "+++ %s\n" % newFile
1028 f = open(newFile, "r")
1029 for line in f.readlines():
1030 newdiff += "+" + line
1033 if self.checkAuthorship and not self.p4UserIsMe(p4User):
1034 submitTemplate += "######## git author %s does not match your p4 account.\n" % gitEmail
1035 submitTemplate += "######## Use git-p4 option --preserve-user to modify authorship\n"
1036 submitTemplate += "######## Use git-p4 config git-p4.skipUserNameCheck hides this message.\n"
1038 separatorLine = "######## everything below this line is just the diff #######\n"
1040 (handle, fileName) = tempfile.mkstemp()
1041 tmpFile = os.fdopen(handle, "w+")
1043 submitTemplate = submitTemplate.replace("\n", "\r\n")
1044 separatorLine = separatorLine.replace("\n", "\r\n")
1045 newdiff = newdiff.replace("\n", "\r\n")
1046 tmpFile.write(submitTemplate + separatorLine + diff + newdiff)
1049 if self.edit_template(fileName):
1050 # read the edited message and submit
1051 tmpFile = open(fileName, "rb")
1052 message = tmpFile.read()
1054 submitTemplate = message[:message.index(separatorLine)]
1056 submitTemplate = submitTemplate.replace("\r\n", "\n")
1057 p4_write_pipe(['submit', '-i'], submitTemplate)
1059 if self.preserveUser:
1061 # Get last changelist number. Cannot easily get it from
1062 # the submit command output as the output is
1064 changelist = self.lastP4Changelist()
1065 self.modifyChangelistUser(changelist, p4User)
1068 print "Submission cancelled, undoing p4 changes."
1069 for f in editedFiles:
1071 for f in filesToAdd:
1077 fileName = "submit.txt"
1078 file = open(fileName, "w+")
1079 file.write(self.prepareLogMessage(template, logMessage))
1081 print ("Perforce submit template written as %s. "
1082 + "Please review/edit and then use p4 submit -i < %s to submit directly!"
1083 % (fileName, fileName))
1085 def run(self, args):
1087 self.master = currentGitBranch()
1088 if len(self.master) == 0 or not gitBranchExists("refs/heads/%s" % self.master):
1089 die("Detecting current git branch failed!")
1090 elif len(args) == 1:
1091 self.master = args[0]
1095 allowSubmit = gitConfig("git-p4.allowSubmit")
1096 if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","):
1097 die("%s is not in git-p4.allowSubmit" % self.master)
1099 [upstream, settings] = findUpstreamBranchPoint()
1100 self.depotPath = settings['depot-paths'][0]
1101 if len(self.origin) == 0:
1102 self.origin = upstream
1104 if self.preserveUser:
1105 if not self.canChangeChangelists():
1106 die("Cannot preserve user names without p4 super-user or admin permissions")
1109 print "Origin branch is " + self.origin
1111 if len(self.depotPath) == 0:
1112 print "Internal error: cannot locate perforce depot path from existing branches"
1115 self.clientPath = p4Where(self.depotPath)
1117 if len(self.clientPath) == 0:
1118 print "Error: Cannot locate perforce checkout of %s in client view" % self.depotPath
1121 print "Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath)
1122 self.oldWorkingDirectory = os.getcwd()
1124 # ensure the clientPath exists
1125 if not os.path.exists(self.clientPath):
1126 os.makedirs(self.clientPath)
1128 chdir(self.clientPath)
1129 print "Synchronizing p4 checkout..."
1134 for line in read_pipe_lines("git rev-list --no-merges %s..%s" % (self.origin, self.master)):
1135 commits.append(line.strip())
1138 if self.preserveUser or (gitConfig("git-p4.skipUserNameCheck") == "true"):
1139 self.checkAuthorship = False
1141 self.checkAuthorship = True
1143 if self.preserveUser:
1144 self.checkValidP4Users(commits)
1146 while len(commits) > 0:
1148 commits = commits[1:]
1149 self.applyCommit(commit)
1150 if not self.interactive:
1153 if len(commits) == 0:
1154 print "All changes applied!"
1155 chdir(self.oldWorkingDirectory)
1165 class P4Sync(Command, P4UserMap):
1166 delete_actions = ( "delete", "move/delete", "purge" )
1169 Command.__init__(self)
1170 P4UserMap.__init__(self)
1172 optparse.make_option("--branch", dest="branch"),
1173 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
1174 optparse.make_option("--changesfile", dest="changesFile"),
1175 optparse.make_option("--silent", dest="silent", action="store_true"),
1176 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
1177 optparse.make_option("--verbose", dest="verbose", action="store_true"),
1178 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",
1179 help="Import into refs/heads/ , not refs/remotes"),
1180 optparse.make_option("--max-changes", dest="maxChanges"),
1181 optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',
1182 help="Keep entire BRANCH/DIR/SUBDIR prefix during import"),
1183 optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true',
1184 help="Only sync files that are included in the Perforce Client Spec")
1186 self.description = """Imports from Perforce into a git repository.\n
1188 //depot/my/project/ -- to import the current head
1189 //depot/my/project/@all -- to import everything
1190 //depot/my/project/@1,6 -- to import only from revision 1 to 6
1192 (a ... is not needed in the path p4 specification, it's added implicitly)"""
1194 self.usage += " //depot/path[@revRange]"
1196 self.createdBranches = set()
1197 self.committedChanges = set()
1199 self.detectBranches = False
1200 self.detectLabels = False
1201 self.changesFile = ""
1202 self.syncWithOrigin = True
1203 self.verbose = False
1204 self.importIntoRemotes = True
1205 self.maxChanges = ""
1206 self.isWindows = (platform.system() == "Windows")
1207 self.keepRepoPath = False
1208 self.depotPaths = None
1209 self.p4BranchesInGit = []
1210 self.cloneExclude = []
1211 self.useClientSpec = False
1212 self.clientSpecDirs = []
1214 if gitConfig("git-p4.syncFromOrigin") == "false":
1215 self.syncWithOrigin = False
1218 # P4 wildcards are not allowed in filenames. P4 complains
1219 # if you simply add them, but you can force it with "-f", in
1220 # which case it translates them into %xx encoding internally.
1221 # Search for and fix just these four characters. Do % last so
1222 # that fixing it does not inadvertently create new %-escapes.
1224 def wildcard_decode(self, path):
1225 # Cannot have * in a filename in windows; untested as to
1226 # what p4 would do in such a case.
1227 if not self.isWindows:
1228 path = path.replace("%2A", "*")
1229 path = path.replace("%23", "#") \
1230 .replace("%40", "@") \
1231 .replace("%25", "%")
1234 def extractFilesFromCommit(self, commit):
1235 self.cloneExclude = [re.sub(r"\.\.\.$", "", path)
1236 for path in self.cloneExclude]
1239 while commit.has_key("depotFile%s" % fnum):
1240 path = commit["depotFile%s" % fnum]
1242 if [p for p in self.cloneExclude
1243 if p4PathStartsWith(path, p)]:
1246 found = [p for p in self.depotPaths
1247 if p4PathStartsWith(path, p)]
1254 file["rev"] = commit["rev%s" % fnum]
1255 file["action"] = commit["action%s" % fnum]
1256 file["type"] = commit["type%s" % fnum]
1261 def stripRepoPath(self, path, prefixes):
1262 if self.useClientSpec:
1264 # if using the client spec, we use the output directory
1265 # specified in the client. For example, a view
1266 # //depot/foo/branch/... //client/branch/foo/...
1267 # will end up putting all foo/branch files into
1269 for val in self.clientSpecDirs:
1270 if path.startswith(val[0]):
1271 # replace the depot path with the client path
1272 path = path.replace(val[0], val[1][1])
1273 # now strip out the client (//client/...)
1274 path = re.sub("^(//[^/]+/)", '', path)
1275 # the rest is all path
1278 if self.keepRepoPath:
1279 prefixes = [re.sub("^(//[^/]+/).*", r'\1', prefixes[0])]
1282 if p4PathStartsWith(path, p):
1283 path = path[len(p):]
1287 def splitFilesIntoBranches(self, commit):
1290 while commit.has_key("depotFile%s" % fnum):
1291 path = commit["depotFile%s" % fnum]
1292 found = [p for p in self.depotPaths
1293 if p4PathStartsWith(path, p)]
1300 file["rev"] = commit["rev%s" % fnum]
1301 file["action"] = commit["action%s" % fnum]
1302 file["type"] = commit["type%s" % fnum]
1305 relPath = self.stripRepoPath(path, self.depotPaths)
1307 for branch in self.knownBranches.keys():
1309 # add a trailing slash so that a commit into qt/4.2foo doesn't end up in qt/4.2
1310 if relPath.startswith(branch + "/"):
1311 if branch not in branches:
1312 branches[branch] = []
1313 branches[branch].append(file)
1318 # output one file from the P4 stream
1319 # - helper for streamP4Files
1321 def streamOneP4File(self, file, contents):
1322 relPath = self.stripRepoPath(file['depotFile'], self.branchPrefixes)
1323 relPath = self.wildcard_decode(relPath)
1325 sys.stderr.write("%s\n" % relPath)
1327 (type_base, type_mods) = split_p4_type(file["type"])
1330 if "x" in type_mods:
1332 if type_base == "symlink":
1334 # p4 print on a symlink contains "target\n"; remove the newline
1335 data = ''.join(contents)
1336 contents = [data[:-1]]
1338 if type_base == "utf16":
1339 # p4 delivers different text in the python output to -G
1340 # than it does when using "print -o", or normal p4 client
1341 # operations. utf16 is converted to ascii or utf8, perhaps.
1342 # But ascii text saved as -t utf16 is completely mangled.
1343 # Invoke print -o to get the real contents.
1344 text = p4_read_pipe(['print', '-q', '-o', '-', file['depotFile']])
1347 if type_base == "apple":
1348 # Apple filetype files will be streamed as a concatenation of
1349 # its appledouble header and the contents. This is useless
1350 # on both macs and non-macs. If using "print -q -o xx", it
1351 # will create "xx" with the data, and "%xx" with the header.
1352 # This is also not very useful.
1354 # Ideally, someday, this script can learn how to generate
1355 # appledouble files directly and import those to git, but
1356 # non-mac machines can never find a use for apple filetype.
1357 print "\nIgnoring apple filetype file %s" % file['depotFile']
1360 # Perhaps windows wants unicode, utf16 newlines translated too;
1361 # but this is not doing it.
1362 if self.isWindows and type_base == "text":
1364 for data in contents:
1365 data = data.replace("\r\n", "\n")
1366 mangled.append(data)
1369 # Note that we do not try to de-mangle keywords on utf16 files,
1370 # even though in theory somebody may want that.
1371 if type_base in ("text", "unicode", "binary"):
1372 if "ko" in type_mods:
1373 text = ''.join(contents)
1374 text = re.sub(r'\$(Id|Header):[^$]*\$', r'$\1$', text)
1376 elif "k" in type_mods:
1377 text = ''.join(contents)
1378 text = re.sub(r'\$(Id|Header|Author|Date|DateTime|Change|File|Revision):[^$]*\$', r'$\1$', text)
1381 self.gitStream.write("M %s inline %s\n" % (git_mode, relPath))
1386 length = length + len(d)
1388 self.gitStream.write("data %d\n" % length)
1390 self.gitStream.write(d)
1391 self.gitStream.write("\n")
1393 def streamOneP4Deletion(self, file):
1394 relPath = self.stripRepoPath(file['path'], self.branchPrefixes)
1396 sys.stderr.write("delete %s\n" % relPath)
1397 self.gitStream.write("D %s\n" % relPath)
1399 # handle another chunk of streaming data
1400 def streamP4FilesCb(self, marshalled):
1402 if marshalled.has_key('depotFile') and self.stream_have_file_info:
1403 # start of a new file - output the old one first
1404 self.streamOneP4File(self.stream_file, self.stream_contents)
1405 self.stream_file = {}
1406 self.stream_contents = []
1407 self.stream_have_file_info = False
1409 # pick up the new file information... for the
1410 # 'data' field we need to append to our array
1411 for k in marshalled.keys():
1413 self.stream_contents.append(marshalled['data'])
1415 self.stream_file[k] = marshalled[k]
1417 self.stream_have_file_info = True
1419 # Stream directly from "p4 files" into "git fast-import"
1420 def streamP4Files(self, files):
1427 for val in self.clientSpecDirs:
1428 if f['path'].startswith(val[0]):
1434 filesForCommit.append(f)
1435 if f['action'] in self.delete_actions:
1436 filesToDelete.append(f)
1438 filesToRead.append(f)
1441 for f in filesToDelete:
1442 self.streamOneP4Deletion(f)
1444 if len(filesToRead) > 0:
1445 self.stream_file = {}
1446 self.stream_contents = []
1447 self.stream_have_file_info = False
1449 # curry self argument
1450 def streamP4FilesCbSelf(entry):
1451 self.streamP4FilesCb(entry)
1453 fileArgs = ['%s#%s' % (f['path'], f['rev']) for f in filesToRead]
1455 p4CmdList(["-x", "-", "print"],
1457 cb=streamP4FilesCbSelf)
1460 if self.stream_file.has_key('depotFile'):
1461 self.streamOneP4File(self.stream_file, self.stream_contents)
1463 def commit(self, details, files, branch, branchPrefixes, parent = ""):
1464 epoch = details["time"]
1465 author = details["user"]
1466 self.branchPrefixes = branchPrefixes
1469 print "commit into %s" % branch
1471 # start with reading files; if that fails, we should not
1475 if [p for p in branchPrefixes if p4PathStartsWith(f['path'], p)]:
1476 new_files.append (f)
1478 sys.stderr.write("Ignoring file outside of prefix: %s\n" % f['path'])
1480 self.gitStream.write("commit %s\n" % branch)
1481 # gitStream.write("mark :%s\n" % details["change"])
1482 self.committedChanges.add(int(details["change"]))
1484 if author not in self.users:
1485 self.getUserMapFromPerforceServer()
1486 if author in self.users:
1487 committer = "%s %s %s" % (self.users[author], epoch, self.tz)
1489 committer = "%s <a@b> %s %s" % (author, epoch, self.tz)
1491 self.gitStream.write("committer %s\n" % committer)
1493 self.gitStream.write("data <<EOT\n")
1494 self.gitStream.write(details["desc"])
1495 self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s"
1496 % (','.join (branchPrefixes), details["change"]))
1497 if len(details['options']) > 0:
1498 self.gitStream.write(": options = %s" % details['options'])
1499 self.gitStream.write("]\nEOT\n\n")
1503 print "parent %s" % parent
1504 self.gitStream.write("from %s\n" % parent)
1506 self.streamP4Files(new_files)
1507 self.gitStream.write("\n")
1509 change = int(details["change"])
1511 if self.labels.has_key(change):
1512 label = self.labels[change]
1513 labelDetails = label[0]
1514 labelRevisions = label[1]
1516 print "Change %s is labelled %s" % (change, labelDetails)
1518 files = p4CmdList(["files"] + ["%s...@%s" % (p, change)
1519 for p in branchPrefixes])
1521 if len(files) == len(labelRevisions):
1525 if info["action"] in self.delete_actions:
1527 cleanedFiles[info["depotFile"]] = info["rev"]
1529 if cleanedFiles == labelRevisions:
1530 self.gitStream.write("tag tag_%s\n" % labelDetails["label"])
1531 self.gitStream.write("from %s\n" % branch)
1533 owner = labelDetails["Owner"]
1535 if author in self.users:
1536 tagger = "%s %s %s" % (self.users[owner], epoch, self.tz)
1538 tagger = "%s <a@b> %s %s" % (owner, epoch, self.tz)
1539 self.gitStream.write("tagger %s\n" % tagger)
1540 self.gitStream.write("data <<EOT\n")
1541 self.gitStream.write(labelDetails["Description"])
1542 self.gitStream.write("EOT\n\n")
1546 print ("Tag %s does not match with change %s: files do not match."
1547 % (labelDetails["label"], change))
1551 print ("Tag %s does not match with change %s: file count is different."
1552 % (labelDetails["label"], change))
1554 def getLabels(self):
1557 l = p4CmdList("labels %s..." % ' '.join (self.depotPaths))
1558 if len(l) > 0 and not self.silent:
1559 print "Finding files belonging to labels in %s" % `self.depotPaths`
1562 label = output["label"]
1566 print "Querying files for label %s" % label
1567 for file in p4CmdList(["files"] +
1568 ["%s...@%s" % (p, label)
1569 for p in self.depotPaths]):
1570 revisions[file["depotFile"]] = file["rev"]
1571 change = int(file["change"])
1572 if change > newestChange:
1573 newestChange = change
1575 self.labels[newestChange] = [output, revisions]
1578 print "Label changes: %s" % self.labels.keys()
1580 def guessProjectName(self):
1581 for p in self.depotPaths:
1584 p = p[p.strip().rfind("/") + 1:]
1585 if not p.endswith("/"):
1589 def getBranchMapping(self):
1590 lostAndFoundBranches = set()
1592 user = gitConfig("git-p4.branchUser")
1594 command = "branches -u %s" % user
1596 command = "branches"
1598 for info in p4CmdList(command):
1599 details = p4Cmd("branch -o %s" % info["branch"])
1601 while details.has_key("View%s" % viewIdx):
1602 paths = details["View%s" % viewIdx].split(" ")
1603 viewIdx = viewIdx + 1
1604 # require standard //depot/foo/... //depot/bar/... mapping
1605 if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."):
1608 destination = paths[1]
1610 if p4PathStartsWith(source, self.depotPaths[0]) and p4PathStartsWith(destination, self.depotPaths[0]):
1611 source = source[len(self.depotPaths[0]):-4]
1612 destination = destination[len(self.depotPaths[0]):-4]
1614 if destination in self.knownBranches:
1616 print "p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination)
1617 print "but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination)
1620 self.knownBranches[destination] = source
1622 lostAndFoundBranches.discard(destination)
1624 if source not in self.knownBranches:
1625 lostAndFoundBranches.add(source)
1627 # Perforce does not strictly require branches to be defined, so we also
1628 # check git config for a branch list.
1630 # Example of branch definition in git config file:
1632 # branchList=main:branchA
1633 # branchList=main:branchB
1634 # branchList=branchA:branchC
1635 configBranches = gitConfigList("git-p4.branchList")
1636 for branch in configBranches:
1638 (source, destination) = branch.split(":")
1639 self.knownBranches[destination] = source
1641 lostAndFoundBranches.discard(destination)
1643 if source not in self.knownBranches:
1644 lostAndFoundBranches.add(source)
1647 for branch in lostAndFoundBranches:
1648 self.knownBranches[branch] = branch
1650 def getBranchMappingFromGitBranches(self):
1651 branches = p4BranchesInGit(self.importIntoRemotes)
1652 for branch in branches.keys():
1653 if branch == "master":
1656 branch = branch[len(self.projectName):]
1657 self.knownBranches[branch] = branch
1659 def listExistingP4GitBranches(self):
1660 # branches holds mapping from name to commit
1661 branches = p4BranchesInGit(self.importIntoRemotes)
1662 self.p4BranchesInGit = branches.keys()
1663 for branch in branches.keys():
1664 self.initialParents[self.refPrefix + branch] = branches[branch]
1666 def updateOptionDict(self, d):
1668 if self.keepRepoPath:
1669 option_keys['keepRepoPath'] = 1
1671 d["options"] = ' '.join(sorted(option_keys.keys()))
1673 def readOptions(self, d):
1674 self.keepRepoPath = (d.has_key('options')
1675 and ('keepRepoPath' in d['options']))
1677 def gitRefForBranch(self, branch):
1678 if branch == "main":
1679 return self.refPrefix + "master"
1681 if len(branch) <= 0:
1684 return self.refPrefix + self.projectName + branch
1686 def gitCommitByP4Change(self, ref, change):
1688 print "looking in ref " + ref + " for change %s using bisect..." % change
1691 latestCommit = parseRevision(ref)
1695 print "trying: earliest %s latest %s" % (earliestCommit, latestCommit)
1696 next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip()
1701 log = extractLogMessageFromGitCommit(next)
1702 settings = extractSettingsGitLog(log)
1703 currentChange = int(settings['change'])
1705 print "current change %s" % currentChange
1707 if currentChange == change:
1709 print "found %s" % next
1712 if currentChange < change:
1713 earliestCommit = "^%s" % next
1715 latestCommit = "%s" % next
1719 def importNewBranch(self, branch, maxChange):
1720 # make fast-import flush all changes to disk and update the refs using the checkpoint
1721 # command so that we can try to find the branch parent in the git history
1722 self.gitStream.write("checkpoint\n\n");
1723 self.gitStream.flush();
1724 branchPrefix = self.depotPaths[0] + branch + "/"
1725 range = "@1,%s" % maxChange
1726 #print "prefix" + branchPrefix
1727 changes = p4ChangesForPaths([branchPrefix], range)
1728 if len(changes) <= 0:
1730 firstChange = changes[0]
1731 #print "first change in branch: %s" % firstChange
1732 sourceBranch = self.knownBranches[branch]
1733 sourceDepotPath = self.depotPaths[0] + sourceBranch
1734 sourceRef = self.gitRefForBranch(sourceBranch)
1735 #print "source " + sourceBranch
1737 branchParentChange = int(p4Cmd("changes -m 1 %s...@1,%s" % (sourceDepotPath, firstChange))["change"])
1738 #print "branch parent: %s" % branchParentChange
1739 gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)
1740 if len(gitParent) > 0:
1741 self.initialParents[self.gitRefForBranch(branch)] = gitParent
1742 #print "parent git commit: %s" % gitParent
1744 self.importChanges(changes)
1747 def importChanges(self, changes):
1749 for change in changes:
1750 description = p4Cmd("describe %s" % change)
1751 self.updateOptionDict(description)
1754 sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
1759 if self.detectBranches:
1760 branches = self.splitFilesIntoBranches(description)
1761 for branch in branches.keys():
1763 branchPrefix = self.depotPaths[0] + branch + "/"
1767 filesForCommit = branches[branch]
1770 print "branch is %s" % branch
1772 self.updatedBranches.add(branch)
1774 if branch not in self.createdBranches:
1775 self.createdBranches.add(branch)
1776 parent = self.knownBranches[branch]
1777 if parent == branch:
1780 fullBranch = self.projectName + branch
1781 if fullBranch not in self.p4BranchesInGit:
1783 print("\n Importing new branch %s" % fullBranch);
1784 if self.importNewBranch(branch, change - 1):
1786 self.p4BranchesInGit.append(fullBranch)
1788 print("\n Resuming with change %s" % change);
1791 print "parent determined through known branches: %s" % parent
1793 branch = self.gitRefForBranch(branch)
1794 parent = self.gitRefForBranch(parent)
1797 print "looking for initial parent for %s; current parent is %s" % (branch, parent)
1799 if len(parent) == 0 and branch in self.initialParents:
1800 parent = self.initialParents[branch]
1801 del self.initialParents[branch]
1803 self.commit(description, filesForCommit, branch, [branchPrefix], parent)
1805 files = self.extractFilesFromCommit(description)
1806 self.commit(description, files, self.branch, self.depotPaths,
1808 self.initialParent = ""
1810 print self.gitError.read()
1813 def importHeadRevision(self, revision):
1814 print "Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch)
1817 details["user"] = "git perforce import user"
1818 details["desc"] = ("Initial import of %s from the state at revision %s\n"
1819 % (' '.join(self.depotPaths), revision))
1820 details["change"] = revision
1824 fileArgs = ["%s...%s" % (p,revision) for p in self.depotPaths]
1826 for info in p4CmdList(["files"] + fileArgs):
1828 if 'code' in info and info['code'] == 'error':
1829 sys.stderr.write("p4 returned an error: %s\n"
1831 if info['data'].find("must refer to client") >= 0:
1832 sys.stderr.write("This particular p4 error is misleading.\n")
1833 sys.stderr.write("Perhaps the depot path was misspelled.\n");
1834 sys.stderr.write("Depot path: %s\n" % " ".join(self.depotPaths))
1836 if 'p4ExitCode' in info:
1837 sys.stderr.write("p4 exitcode: %s\n" % info['p4ExitCode'])
1841 change = int(info["change"])
1842 if change > newestRevision:
1843 newestRevision = change
1845 if info["action"] in self.delete_actions:
1846 # don't increase the file cnt, otherwise details["depotFile123"] will have gaps!
1847 #fileCnt = fileCnt + 1
1850 for prop in ["depotFile", "rev", "action", "type" ]:
1851 details["%s%s" % (prop, fileCnt)] = info[prop]
1853 fileCnt = fileCnt + 1
1855 details["change"] = newestRevision
1857 # Use time from top-most change so that all git-p4 clones of
1858 # the same p4 repo have the same commit SHA1s.
1859 res = p4CmdList("describe -s %d" % newestRevision)
1862 if r.has_key('time'):
1863 newestTime = int(r['time'])
1864 if newestTime is None:
1865 die("\"describe -s\" on newest change %d did not give a time")
1866 details["time"] = newestTime
1868 self.updateOptionDict(details)
1870 self.commit(details, self.extractFilesFromCommit(details), self.branch, self.depotPaths)
1872 print "IO error with git fast-import. Is your git version recent enough?"
1873 print self.gitError.read()
1876 def getClientSpec(self):
1877 specList = p4CmdList( "client -o" )
1879 for entry in specList:
1880 for k,v in entry.iteritems():
1881 if k.startswith("View"):
1883 # p4 has these %%1 to %%9 arguments in specs to
1884 # reorder paths; which we can't handle (yet :)
1885 if re.match('%%\d', v) != None:
1886 print "Sorry, can't handle %%n arguments in client specs"
1889 if v.startswith('"'):
1893 index = v.find("...")
1895 # save the "client view"; i.e the RHS of the view
1896 # line that tells the client where to put the
1897 # files for this view.
1898 cv = v[index+3:].strip() # +3 to remove previous '...'
1900 # if the client view doesn't end with a
1901 # ... wildcard, then we're going to mess up the
1902 # output directory, so fail gracefully.
1903 if not cv.endswith('...'):
1904 print 'Sorry, client view in "%s" needs to end with wildcard' % (k)
1908 # now save the view; +index means included, -index
1909 # means it should be filtered out.
1911 if v.startswith("-"):
1917 temp[v] = (include, cv)
1919 self.clientSpecDirs = temp.items()
1920 self.clientSpecDirs.sort( lambda x, y: abs( y[1][0] ) - abs( x[1][0] ) )
1922 def run(self, args):
1923 self.depotPaths = []
1924 self.changeRange = ""
1925 self.initialParent = ""
1926 self.previousDepotPaths = []
1928 # map from branch depot path to parent branch
1929 self.knownBranches = {}
1930 self.initialParents = {}
1931 self.hasOrigin = originP4BranchesExist()
1932 if not self.syncWithOrigin:
1933 self.hasOrigin = False
1935 if self.importIntoRemotes:
1936 self.refPrefix = "refs/remotes/p4/"
1938 self.refPrefix = "refs/heads/p4/"
1940 if self.syncWithOrigin and self.hasOrigin:
1942 print "Syncing with origin first by calling git fetch origin"
1943 system("git fetch origin")
1945 if len(self.branch) == 0:
1946 self.branch = self.refPrefix + "master"
1947 if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
1948 system("git update-ref %s refs/heads/p4" % self.branch)
1949 system("git branch -D p4");
1950 # create it /after/ importing, when master exists
1951 if not gitBranchExists(self.refPrefix + "HEAD") and self.importIntoRemotes and gitBranchExists(self.branch):
1952 system("git symbolic-ref %sHEAD %s" % (self.refPrefix, self.branch))
1954 if self.useClientSpec or gitConfig("git-p4.useclientspec") == "true":
1955 self.getClientSpec()
1957 # TODO: should always look at previous commits,
1958 # merge with previous imports, if possible.
1961 createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)
1962 self.listExistingP4GitBranches()
1964 if len(self.p4BranchesInGit) > 1:
1966 print "Importing from/into multiple branches"
1967 self.detectBranches = True
1970 print "branches: %s" % self.p4BranchesInGit
1973 for branch in self.p4BranchesInGit:
1974 logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch)
1976 settings = extractSettingsGitLog(logMsg)
1978 self.readOptions(settings)
1979 if (settings.has_key('depot-paths')
1980 and settings.has_key ('change')):
1981 change = int(settings['change']) + 1
1982 p4Change = max(p4Change, change)
1984 depotPaths = sorted(settings['depot-paths'])
1985 if self.previousDepotPaths == []:
1986 self.previousDepotPaths = depotPaths
1989 for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
1990 prev_list = prev.split("/")
1991 cur_list = cur.split("/")
1992 for i in range(0, min(len(cur_list), len(prev_list))):
1993 if cur_list[i] <> prev_list[i]:
1997 paths.append ("/".join(cur_list[:i + 1]))
1999 self.previousDepotPaths = paths
2002 self.depotPaths = sorted(self.previousDepotPaths)
2003 self.changeRange = "@%s,#head" % p4Change
2004 if not self.detectBranches:
2005 self.initialParent = parseRevision(self.branch)
2006 if not self.silent and not self.detectBranches:
2007 print "Performing incremental import into %s git branch" % self.branch
2009 if not self.branch.startswith("refs/"):
2010 self.branch = "refs/heads/" + self.branch
2012 if len(args) == 0 and self.depotPaths:
2014 print "Depot paths: %s" % ' '.join(self.depotPaths)
2016 if self.depotPaths and self.depotPaths != args:
2017 print ("previous import used depot path %s and now %s was specified. "
2018 "This doesn't work!" % (' '.join (self.depotPaths),
2022 self.depotPaths = sorted(args)
2028 for p in self.depotPaths:
2029 if p.find("@") != -1:
2030 atIdx = p.index("@")
2031 self.changeRange = p[atIdx:]
2032 if self.changeRange == "@all":
2033 self.changeRange = ""
2034 elif ',' not in self.changeRange:
2035 revision = self.changeRange
2036 self.changeRange = ""
2038 elif p.find("#") != -1:
2039 hashIdx = p.index("#")
2040 revision = p[hashIdx:]
2042 elif self.previousDepotPaths == []:
2045 p = re.sub ("\.\.\.$", "", p)
2046 if not p.endswith("/"):
2051 self.depotPaths = newPaths
2054 self.loadUserMapFromCache()
2056 if self.detectLabels:
2059 if self.detectBranches:
2060 ## FIXME - what's a P4 projectName ?
2061 self.projectName = self.guessProjectName()
2064 self.getBranchMappingFromGitBranches()
2066 self.getBranchMapping()
2068 print "p4-git branches: %s" % self.p4BranchesInGit
2069 print "initial parents: %s" % self.initialParents
2070 for b in self.p4BranchesInGit:
2074 b = b[len(self.projectName):]
2075 self.createdBranches.add(b)
2077 self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
2079 importProcess = subprocess.Popen(["git", "fast-import"],
2080 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2081 stderr=subprocess.PIPE);
2082 self.gitOutput = importProcess.stdout
2083 self.gitStream = importProcess.stdin
2084 self.gitError = importProcess.stderr
2087 self.importHeadRevision(revision)
2091 if len(self.changesFile) > 0:
2092 output = open(self.changesFile).readlines()
2095 changeSet.add(int(line))
2097 for change in changeSet:
2098 changes.append(change)
2102 # catch "git-p4 sync" with no new branches, in a repo that
2103 # does not have any existing git-p4 branches
2104 if len(args) == 0 and not self.p4BranchesInGit:
2105 die("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.");
2107 print "Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
2109 changes = p4ChangesForPaths(self.depotPaths, self.changeRange)
2111 if len(self.maxChanges) > 0:
2112 changes = changes[:min(int(self.maxChanges), len(changes))]
2114 if len(changes) == 0:
2116 print "No changes to import!"
2119 if not self.silent and not self.detectBranches:
2120 print "Import destination: %s" % self.branch
2122 self.updatedBranches = set()
2124 self.importChanges(changes)
2128 if len(self.updatedBranches) > 0:
2129 sys.stdout.write("Updated branches: ")
2130 for b in self.updatedBranches:
2131 sys.stdout.write("%s " % b)
2132 sys.stdout.write("\n")
2134 self.gitStream.close()
2135 if importProcess.wait() != 0:
2136 die("fast-import failed: %s" % self.gitError.read())
2137 self.gitOutput.close()
2138 self.gitError.close()
2142 class P4Rebase(Command):
2144 Command.__init__(self)
2146 self.description = ("Fetches the latest revision from perforce and "
2147 + "rebases the current work (branch) against it")
2148 self.verbose = False
2150 def run(self, args):
2154 return self.rebase()
2157 if os.system("git update-index --refresh") != 0:
2158 die("Some files in your working directory are modified and different than what is in your index. You can use git update-index <filename> to bring the index up-to-date or stash away all your changes with git stash.");
2159 if len(read_pipe("git diff-index HEAD --")) > 0:
2160 die("You have uncommited changes. Please commit them before rebasing or stash them away with git stash.");
2162 [upstream, settings] = findUpstreamBranchPoint()
2163 if len(upstream) == 0:
2164 die("Cannot find upstream branchpoint for rebase")
2166 # the branchpoint may be p4/foo~3, so strip off the parent
2167 upstream = re.sub("~[0-9]+$", "", upstream)
2169 print "Rebasing the current branch onto %s" % upstream
2170 oldHead = read_pipe("git rev-parse HEAD").strip()
2171 system("git rebase %s" % upstream)
2172 system("git diff-tree --stat --summary -M %s HEAD" % oldHead)
2175 class P4Clone(P4Sync):
2177 P4Sync.__init__(self)
2178 self.description = "Creates a new git repository and imports from Perforce into it"
2179 self.usage = "usage: %prog [options] //depot/path[@revRange]"
2181 optparse.make_option("--destination", dest="cloneDestination",
2182 action='store', default=None,
2183 help="where to leave result of the clone"),
2184 optparse.make_option("-/", dest="cloneExclude",
2185 action="append", type="string",
2186 help="exclude depot path"),
2187 optparse.make_option("--bare", dest="cloneBare",
2188 action="store_true", default=False),
2190 self.cloneDestination = None
2191 self.needsGit = False
2192 self.cloneBare = False
2194 # This is required for the "append" cloneExclude action
2195 def ensure_value(self, attr, value):
2196 if not hasattr(self, attr) or getattr(self, attr) is None:
2197 setattr(self, attr, value)
2198 return getattr(self, attr)
2200 def defaultDestination(self, args):
2201 ## TODO: use common prefix of args?
2203 depotDir = re.sub("(@[^@]*)$", "", depotPath)
2204 depotDir = re.sub("(#[^#]*)$", "", depotDir)
2205 depotDir = re.sub(r"\.\.\.$", "", depotDir)
2206 depotDir = re.sub(r"/$", "", depotDir)
2207 return os.path.split(depotDir)[1]
2209 def run(self, args):
2213 if self.keepRepoPath and not self.cloneDestination:
2214 sys.stderr.write("Must specify destination for --keep-path\n")
2219 if not self.cloneDestination and len(depotPaths) > 1:
2220 self.cloneDestination = depotPaths[-1]
2221 depotPaths = depotPaths[:-1]
2223 self.cloneExclude = ["/"+p for p in self.cloneExclude]
2224 for p in depotPaths:
2225 if not p.startswith("//"):
2228 if not self.cloneDestination:
2229 self.cloneDestination = self.defaultDestination(args)
2231 print "Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination)
2233 if not os.path.exists(self.cloneDestination):
2234 os.makedirs(self.cloneDestination)
2235 chdir(self.cloneDestination)
2237 init_cmd = [ "git", "init" ]
2239 init_cmd.append("--bare")
2240 subprocess.check_call(init_cmd)
2242 if not P4Sync.run(self, depotPaths):
2244 if self.branch != "master":
2245 if self.importIntoRemotes:
2246 masterbranch = "refs/remotes/p4/master"
2248 masterbranch = "refs/heads/p4/master"
2249 if gitBranchExists(masterbranch):
2250 system("git branch master %s" % masterbranch)
2251 if not self.cloneBare:
2252 system("git checkout -f")
2254 print "Could not detect main branch. No checkout/master branch created."
2258 class P4Branches(Command):
2260 Command.__init__(self)
2262 self.description = ("Shows the git branches that hold imports and their "
2263 + "corresponding perforce depot paths")
2264 self.verbose = False
2266 def run(self, args):
2267 if originP4BranchesExist():
2268 createOrUpdateBranchesFromOrigin()
2270 cmdline = "git rev-parse --symbolic "
2271 cmdline += " --remotes"
2273 for line in read_pipe_lines(cmdline):
2276 if not line.startswith('p4/') or line == "p4/HEAD":
2280 log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch)
2281 settings = extractSettingsGitLog(log)
2283 print "%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"])
2286 class HelpFormatter(optparse.IndentedHelpFormatter):
2288 optparse.IndentedHelpFormatter.__init__(self)
2290 def format_description(self, description):
2292 return description + "\n"
2296 def printUsage(commands):
2297 print "usage: %s <command> [options]" % sys.argv[0]
2299 print "valid commands: %s" % ", ".join(commands)
2301 print "Try %s <command> --help for command specific help." % sys.argv[0]
2306 "submit" : P4Submit,
2307 "commit" : P4Submit,
2309 "rebase" : P4Rebase,
2311 "rollback" : P4RollBack,
2312 "branches" : P4Branches
2317 if len(sys.argv[1:]) == 0:
2318 printUsage(commands.keys())
2322 cmdName = sys.argv[1]
2324 klass = commands[cmdName]
2327 print "unknown command %s" % cmdName
2329 printUsage(commands.keys())
2332 options = cmd.options
2333 cmd.gitdir = os.environ.get("GIT_DIR", None)
2337 if len(options) > 0:
2338 options.append(optparse.make_option("--git-dir", dest="gitdir"))
2340 parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
2342 description = cmd.description,
2343 formatter = HelpFormatter())
2345 (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
2347 verbose = cmd.verbose
2349 if cmd.gitdir == None:
2350 cmd.gitdir = os.path.abspath(".git")
2351 if not isValidGitDir(cmd.gitdir):
2352 cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
2353 if os.path.exists(cmd.gitdir):
2354 cdup = read_pipe("git rev-parse --show-cdup").strip()
2358 if not isValidGitDir(cmd.gitdir):
2359 if isValidGitDir(cmd.gitdir + "/.git"):
2360 cmd.gitdir += "/.git"
2362 die("fatal: cannot locate git repository at %s" % cmd.gitdir)
2364 os.environ["GIT_DIR"] = cmd.gitdir
2366 if not cmd.run(args):
2370 if __name__ == '__main__':