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 branchExists(ref):
366 rev = read_pipe(["git", "rev-parse", "-q", "--verify", ref],
370 def extractLogMessageFromGitCommit(commit):
373 ## fixme: title is first line of commit, not 1st paragraph.
375 for log in read_pipe_lines("git cat-file commit %s" % commit):
384 def extractSettingsGitLog(log):
386 for line in log.split("\n"):
388 m = re.search (r"^ *\[git-p4: (.*)\]$", line)
392 assignments = m.group(1).split (':')
393 for a in assignments:
395 key = vals[0].strip()
396 val = ('='.join (vals[1:])).strip()
397 if val.endswith ('\"') and val.startswith('"'):
402 paths = values.get("depot-paths")
404 paths = values.get("depot-path")
406 values['depot-paths'] = paths.split(',')
409 def gitBranchExists(branch):
410 proc = subprocess.Popen(["git", "rev-parse", branch],
411 stderr=subprocess.PIPE, stdout=subprocess.PIPE);
412 return proc.wait() == 0;
415 def gitConfig(key, args = None): # set args to "--bool", for instance
416 if not _gitConfig.has_key(key):
419 argsFilter = "%s " % args
420 cmd = "git config %s%s" % (argsFilter, key)
421 _gitConfig[key] = read_pipe(cmd, ignore_error=True).strip()
422 return _gitConfig[key]
424 def gitConfigList(key):
425 if not _gitConfig.has_key(key):
426 _gitConfig[key] = read_pipe("git config --get-all %s" % key, ignore_error=True).strip().split(os.linesep)
427 return _gitConfig[key]
429 def p4BranchesInGit(branchesAreInRemotes = True):
432 cmdline = "git rev-parse --symbolic "
433 if branchesAreInRemotes:
434 cmdline += " --remotes"
436 cmdline += " --branches"
438 for line in read_pipe_lines(cmdline):
441 ## only import to p4/
442 if not line.startswith('p4/') or line == "p4/HEAD":
447 branch = re.sub ("^p4/", "", line)
449 branches[branch] = parseRevision(line)
452 def findUpstreamBranchPoint(head = "HEAD"):
453 branches = p4BranchesInGit()
454 # map from depot-path to branch name
455 branchByDepotPath = {}
456 for branch in branches.keys():
457 tip = branches[branch]
458 log = extractLogMessageFromGitCommit(tip)
459 settings = extractSettingsGitLog(log)
460 if settings.has_key("depot-paths"):
461 paths = ",".join(settings["depot-paths"])
462 branchByDepotPath[paths] = "remotes/p4/" + branch
466 while parent < 65535:
467 commit = head + "~%s" % parent
468 log = extractLogMessageFromGitCommit(commit)
469 settings = extractSettingsGitLog(log)
470 if settings.has_key("depot-paths"):
471 paths = ",".join(settings["depot-paths"])
472 if branchByDepotPath.has_key(paths):
473 return [branchByDepotPath[paths], settings]
477 return ["", settings]
479 def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True):
481 print ("Creating/updating branch(es) in %s based on origin branch(es)"
484 originPrefix = "origin/p4/"
486 for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
488 if (not line.startswith(originPrefix)) or line.endswith("HEAD"):
491 headName = line[len(originPrefix):]
492 remoteHead = localRefPrefix + headName
495 original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))
496 if (not original.has_key('depot-paths')
497 or not original.has_key('change')):
501 if not gitBranchExists(remoteHead):
503 print "creating %s" % remoteHead
506 settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))
507 if settings.has_key('change') > 0:
508 if settings['depot-paths'] == original['depot-paths']:
509 originP4Change = int(original['change'])
510 p4Change = int(settings['change'])
511 if originP4Change > p4Change:
512 print ("%s (%s) is newer than %s (%s). "
513 "Updating p4 branch from origin."
514 % (originHead, originP4Change,
515 remoteHead, p4Change))
518 print ("Ignoring: %s was imported from %s while "
519 "%s was imported from %s"
520 % (originHead, ','.join(original['depot-paths']),
521 remoteHead, ','.join(settings['depot-paths'])))
524 system("git update-ref %s %s" % (remoteHead, originHead))
526 def originP4BranchesExist():
527 return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
529 def p4ChangesForPaths(depotPaths, changeRange):
533 cmd += ["%s...%s" % (p, changeRange)]
534 output = p4_read_pipe_lines(cmd)
538 changeNum = int(line.split(" ")[1])
539 changes[changeNum] = True
541 changelist = changes.keys()
545 def p4PathStartsWith(path, prefix):
546 # This method tries to remedy a potential mixed-case issue:
548 # If UserA adds //depot/DirA/file1
549 # and UserB adds //depot/dira/file2
551 # we may or may not have a problem. If you have core.ignorecase=true,
552 # we treat DirA and dira as the same directory
553 ignorecase = gitConfig("core.ignorecase", "--bool") == "true"
555 return path.lower().startswith(prefix.lower())
556 return path.startswith(prefix)
559 """Look at the p4 client spec, create a View() object that contains
560 all the mappings, and return it."""
562 specList = p4CmdList("client -o")
563 if len(specList) != 1:
564 die('Output from "client -o" is %d lines, expecting 1' %
567 # dictionary of all client parameters
570 # just the keys that start with "View"
571 view_keys = [ k for k in entry.keys() if k.startswith("View") ]
576 # append the lines, in order, to the view
577 for view_num in range(len(view_keys)):
578 k = "View%d" % view_num
579 if k not in view_keys:
580 die("Expected view key %s missing" % k)
581 view.append(entry[k])
586 """Grab the client directory."""
588 output = p4CmdList("client -o")
590 die('Output from "client -o" is %d lines, expecting 1' % len(output))
593 if "Root" not in entry:
594 die('Client has no "Root"')
600 self.usage = "usage: %prog [options]"
605 self.userMapFromPerforceServer = False
607 def getUserCacheFilename(self):
608 home = os.environ.get("HOME", os.environ.get("USERPROFILE"))
609 return home + "/.gitp4-usercache.txt"
611 def getUserMapFromPerforceServer(self):
612 if self.userMapFromPerforceServer:
617 for output in p4CmdList("users"):
618 if not output.has_key("User"):
620 self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
621 self.emails[output["Email"]] = output["User"]
625 for (key, val) in self.users.items():
626 s += "%s\t%s\n" % (key.expandtabs(1), val.expandtabs(1))
628 open(self.getUserCacheFilename(), "wb").write(s)
629 self.userMapFromPerforceServer = True
631 def loadUserMapFromCache(self):
633 self.userMapFromPerforceServer = False
635 cache = open(self.getUserCacheFilename(), "rb")
636 lines = cache.readlines()
639 entry = line.strip().split("\t")
640 self.users[entry[0]] = entry[1]
642 self.getUserMapFromPerforceServer()
644 class P4Debug(Command):
646 Command.__init__(self)
648 optparse.make_option("--verbose", dest="verbose", action="store_true",
651 self.description = "A tool to debug the output of p4 -G."
652 self.needsGit = False
657 for output in p4CmdList(args):
658 print 'Element: %d' % j
663 class P4RollBack(Command):
665 Command.__init__(self)
667 optparse.make_option("--verbose", dest="verbose", action="store_true"),
668 optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true")
670 self.description = "A tool to debug the multi-branch import. Don't use :)"
672 self.rollbackLocalBranches = False
677 maxChange = int(args[0])
679 if "p4ExitCode" in p4Cmd("changes -m 1"):
680 die("Problems executing p4");
682 if self.rollbackLocalBranches:
683 refPrefix = "refs/heads/"
684 lines = read_pipe_lines("git rev-parse --symbolic --branches")
686 refPrefix = "refs/remotes/"
687 lines = read_pipe_lines("git rev-parse --symbolic --remotes")
690 if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"):
692 ref = refPrefix + line
693 log = extractLogMessageFromGitCommit(ref)
694 settings = extractSettingsGitLog(log)
696 depotPaths = settings['depot-paths']
697 change = settings['change']
701 if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange)
702 for p in depotPaths]))) == 0:
703 print "Branch %s did not exist at change %s, deleting." % (ref, maxChange)
704 system("git update-ref -d %s `git rev-parse %s`" % (ref, ref))
707 while change and int(change) > maxChange:
710 print "%s is at %s ; rewinding towards %s" % (ref, change, maxChange)
711 system("git update-ref %s \"%s^\"" % (ref, ref))
712 log = extractLogMessageFromGitCommit(ref)
713 settings = extractSettingsGitLog(log)
716 depotPaths = settings['depot-paths']
717 change = settings['change']
720 print "%s rewound to %s" % (ref, change)
724 class P4Submit(Command, P4UserMap):
726 Command.__init__(self)
727 P4UserMap.__init__(self)
729 optparse.make_option("--verbose", dest="verbose", action="store_true"),
730 optparse.make_option("--origin", dest="origin"),
731 optparse.make_option("-M", dest="detectRenames", action="store_true"),
732 # preserve the user, requires relevant p4 permissions
733 optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
735 self.description = "Submit changes from git to the perforce depot."
736 self.usage += " [name of git branch to submit into perforce depot]"
737 self.interactive = True
739 self.detectRenames = False
741 self.preserveUser = gitConfig("git-p4.preserveUser").lower() == "true"
742 self.isWindows = (platform.system() == "Windows")
743 self.myP4UserId = None
746 if len(p4CmdList("opened ...")) > 0:
747 die("You have files opened with perforce! Close them before starting the sync.")
749 # replaces everything between 'Description:' and the next P4 submit template field with the
751 def prepareLogMessage(self, template, message):
754 inDescriptionSection = False
756 for line in template.split("\n"):
757 if line.startswith("#"):
758 result += line + "\n"
761 if inDescriptionSection:
762 if line.startswith("Files:") or line.startswith("Jobs:"):
763 inDescriptionSection = False
767 if line.startswith("Description:"):
768 inDescriptionSection = True
770 for messageLine in message.split("\n"):
771 line += "\t" + messageLine + "\n"
773 result += line + "\n"
777 def p4UserForCommit(self,id):
778 # Return the tuple (perforce user,git email) for a given git commit id
779 self.getUserMapFromPerforceServer()
780 gitEmail = read_pipe("git log --max-count=1 --format='%%ae' %s" % id)
781 gitEmail = gitEmail.strip()
782 if not self.emails.has_key(gitEmail):
783 return (None,gitEmail)
785 return (self.emails[gitEmail],gitEmail)
787 def checkValidP4Users(self,commits):
788 # check if any git authors cannot be mapped to p4 users
790 (user,email) = self.p4UserForCommit(id)
792 msg = "Cannot find p4 user for email %s in commit %s." % (email, id)
793 if gitConfig('git-p4.allowMissingP4Users').lower() == "true":
796 die("Error: %s\nSet git-p4.allowMissingP4Users to true to allow this." % msg)
798 def lastP4Changelist(self):
799 # Get back the last changelist number submitted in this client spec. This
800 # then gets used to patch up the username in the change. If the same
801 # client spec is being used by multiple processes then this might go
803 results = p4CmdList("client -o") # find the current client
806 if r.has_key('Client'):
810 die("could not get client spec")
811 results = p4CmdList(["changes", "-c", client, "-m", "1"])
813 if r.has_key('change'):
815 die("Could not get changelist number for last submit - cannot patch up user details")
817 def modifyChangelistUser(self, changelist, newUser):
818 # fixup the user field of a changelist after it has been submitted.
819 changes = p4CmdList("change -o %s" % changelist)
820 if len(changes) != 1:
821 die("Bad output from p4 change modifying %s to user %s" %
822 (changelist, newUser))
825 if c['User'] == newUser: return # nothing to do
827 input = marshal.dumps(c)
829 result = p4CmdList("change -f -i", stdin=input)
831 if r.has_key('code'):
832 if r['code'] == 'error':
833 die("Could not modify user field of changelist %s to %s:%s" % (changelist, newUser, r['data']))
834 if r.has_key('data'):
835 print("Updated user field for changelist %s to %s" % (changelist, newUser))
837 die("Could not modify user field of changelist %s to %s" % (changelist, newUser))
839 def canChangeChangelists(self):
840 # check to see if we have p4 admin or super-user permissions, either of
841 # which are required to modify changelists.
842 results = p4CmdList("protects %s" % self.depotPath)
844 if r.has_key('perm'):
845 if r['perm'] == 'admin':
847 if r['perm'] == 'super':
853 return self.myP4UserId
855 results = p4CmdList("user -o")
857 if r.has_key('User'):
858 self.myP4UserId = r['User']
860 die("Could not find your p4 user id")
862 def p4UserIsMe(self, p4User):
863 # return True if the given p4 user is actually me
865 if not p4User or p4User != me:
870 def prepareSubmitTemplate(self):
871 # remove lines in the Files section that show changes to files outside the depot path we're committing into
873 inFilesSection = False
874 for line in p4_read_pipe_lines(['change', '-o']):
875 if line.endswith("\r\n"):
876 line = line[:-2] + "\n"
878 if line.startswith("\t"):
879 # path starts and ends with a tab
881 lastTab = path.rfind("\t")
883 path = path[:lastTab]
884 if not p4PathStartsWith(path, self.depotPath):
887 inFilesSection = False
889 if line.startswith("Files:"):
890 inFilesSection = True
896 def edit_template(self, template_file):
897 """Invoke the editor to let the user change the submission
898 message. Return true if okay to continue with the submit."""
900 # if configured to skip the editing part, just submit
901 if gitConfig("git-p4.skipSubmitEdit") == "true":
904 # look at the modification time, to check later if the user saved
906 mtime = os.stat(template_file).st_mtime
909 if os.environ.has_key("P4EDITOR"):
910 editor = os.environ.get("P4EDITOR")
912 editor = read_pipe("git var GIT_EDITOR").strip()
913 system(editor + " " + template_file)
915 # If the file was not saved, prompt to see if this patch should
916 # be skipped. But skip this verification step if configured so.
917 if gitConfig("git-p4.skipSubmitEditCheck") == "true":
920 # modification time updated means user saved the file
921 if os.stat(template_file).st_mtime > mtime:
925 response = raw_input("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
931 def applyCommit(self, id):
932 print "Applying %s" % (read_pipe("git log --max-count=1 --pretty=oneline %s" % id))
934 (p4User, gitEmail) = self.p4UserForCommit(id)
936 if not self.detectRenames:
937 # If not explicitly set check the config variable
938 self.detectRenames = gitConfig("git-p4.detectRenames")
940 if self.detectRenames.lower() == "false" or self.detectRenames == "":
942 elif self.detectRenames.lower() == "true":
945 diffOpts = "-M%s" % self.detectRenames
947 detectCopies = gitConfig("git-p4.detectCopies")
948 if detectCopies.lower() == "true":
950 elif detectCopies != "" and detectCopies.lower() != "false":
951 diffOpts += " -C%s" % detectCopies
953 if gitConfig("git-p4.detectCopiesHarder", "--bool") == "true":
954 diffOpts += " --find-copies-harder"
956 diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (diffOpts, id, id))
958 filesToDelete = set()
960 filesToChangeExecBit = {}
962 diff = parseDiffTreeEntry(line)
963 modifier = diff['status']
967 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
968 filesToChangeExecBit[path] = diff['dst_mode']
969 editedFiles.add(path)
970 elif modifier == "A":
972 filesToChangeExecBit[path] = diff['dst_mode']
973 if path in filesToDelete:
974 filesToDelete.remove(path)
975 elif modifier == "D":
976 filesToDelete.add(path)
977 if path in filesToAdd:
978 filesToAdd.remove(path)
979 elif modifier == "C":
980 src, dest = diff['src'], diff['dst']
981 p4_integrate(src, dest)
982 if diff['src_sha1'] != diff['dst_sha1']:
984 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
986 filesToChangeExecBit[dest] = diff['dst_mode']
988 editedFiles.add(dest)
989 elif modifier == "R":
990 src, dest = diff['src'], diff['dst']
991 p4_integrate(src, dest)
992 if diff['src_sha1'] != diff['dst_sha1']:
994 if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
996 filesToChangeExecBit[dest] = diff['dst_mode']
998 editedFiles.add(dest)
999 filesToDelete.add(src)
1001 die("unknown modifier %s for %s" % (modifier, path))
1003 diffcmd = "git format-patch -k --stdout \"%s^\"..\"%s\"" % (id, id)
1004 patchcmd = diffcmd + " | git apply "
1005 tryPatchCmd = patchcmd + "--check -"
1006 applyPatchCmd = patchcmd + "--check --apply -"
1008 if os.system(tryPatchCmd) != 0:
1009 print "Unfortunately applying the change failed!"
1010 print "What do you want to do?"
1012 while response != "s" and response != "a" and response != "w":
1013 response = raw_input("[s]kip this patch / [a]pply the patch forcibly "
1014 "and with .rej files / [w]rite the patch to a file (patch.txt) ")
1016 print "Skipping! Good luck with the next patches..."
1017 for f in editedFiles:
1019 for f in filesToAdd:
1022 elif response == "a":
1023 os.system(applyPatchCmd)
1024 if len(filesToAdd) > 0:
1025 print "You may also want to call p4 add on the following files:"
1026 print " ".join(filesToAdd)
1027 if len(filesToDelete):
1028 print "The following files should be scheduled for deletion with p4 delete:"
1029 print " ".join(filesToDelete)
1030 die("Please resolve and submit the conflict manually and "
1031 + "continue afterwards with git-p4 submit --continue")
1032 elif response == "w":
1033 system(diffcmd + " > patch.txt")
1034 print "Patch saved to patch.txt in %s !" % self.clientPath
1035 die("Please resolve and submit the conflict manually and "
1036 "continue afterwards with git-p4 submit --continue")
1038 system(applyPatchCmd)
1040 for f in filesToAdd:
1042 for f in filesToDelete:
1046 # Set/clear executable bits
1047 for f in filesToChangeExecBit.keys():
1048 mode = filesToChangeExecBit[f]
1049 setP4ExecBit(f, mode)
1051 logMessage = extractLogMessageFromGitCommit(id)
1052 logMessage = logMessage.strip()
1054 template = self.prepareSubmitTemplate()
1056 if self.interactive:
1057 submitTemplate = self.prepareLogMessage(template, logMessage)
1059 if self.preserveUser:
1060 submitTemplate = submitTemplate + ("\n######## Actual user %s, modified after commit\n" % p4User)
1062 if os.environ.has_key("P4DIFF"):
1063 del(os.environ["P4DIFF"])
1065 for editedFile in editedFiles:
1066 diff += p4_read_pipe(['diff', '-du', editedFile])
1069 for newFile in filesToAdd:
1070 newdiff += "==== new file ====\n"
1071 newdiff += "--- /dev/null\n"
1072 newdiff += "+++ %s\n" % newFile
1073 f = open(newFile, "r")
1074 for line in f.readlines():
1075 newdiff += "+" + line
1078 if self.checkAuthorship and not self.p4UserIsMe(p4User):
1079 submitTemplate += "######## git author %s does not match your p4 account.\n" % gitEmail
1080 submitTemplate += "######## Use git-p4 option --preserve-user to modify authorship\n"
1081 submitTemplate += "######## Use git-p4 config git-p4.skipUserNameCheck hides this message.\n"
1083 separatorLine = "######## everything below this line is just the diff #######\n"
1085 (handle, fileName) = tempfile.mkstemp()
1086 tmpFile = os.fdopen(handle, "w+")
1088 submitTemplate = submitTemplate.replace("\n", "\r\n")
1089 separatorLine = separatorLine.replace("\n", "\r\n")
1090 newdiff = newdiff.replace("\n", "\r\n")
1091 tmpFile.write(submitTemplate + separatorLine + diff + newdiff)
1094 if self.edit_template(fileName):
1095 # read the edited message and submit
1096 tmpFile = open(fileName, "rb")
1097 message = tmpFile.read()
1099 submitTemplate = message[:message.index(separatorLine)]
1101 submitTemplate = submitTemplate.replace("\r\n", "\n")
1102 p4_write_pipe(['submit', '-i'], submitTemplate)
1104 if self.preserveUser:
1106 # Get last changelist number. Cannot easily get it from
1107 # the submit command output as the output is
1109 changelist = self.lastP4Changelist()
1110 self.modifyChangelistUser(changelist, p4User)
1113 print "Submission cancelled, undoing p4 changes."
1114 for f in editedFiles:
1116 for f in filesToAdd:
1122 fileName = "submit.txt"
1123 file = open(fileName, "w+")
1124 file.write(self.prepareLogMessage(template, logMessage))
1126 print ("Perforce submit template written as %s. "
1127 + "Please review/edit and then use p4 submit -i < %s to submit directly!"
1128 % (fileName, fileName))
1130 def run(self, args):
1132 self.master = currentGitBranch()
1133 if len(self.master) == 0 or not gitBranchExists("refs/heads/%s" % self.master):
1134 die("Detecting current git branch failed!")
1135 elif len(args) == 1:
1136 self.master = args[0]
1137 if not branchExists(self.master):
1138 die("Branch %s does not exist" % self.master)
1142 allowSubmit = gitConfig("git-p4.allowSubmit")
1143 if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","):
1144 die("%s is not in git-p4.allowSubmit" % self.master)
1146 [upstream, settings] = findUpstreamBranchPoint()
1147 self.depotPath = settings['depot-paths'][0]
1148 if len(self.origin) == 0:
1149 self.origin = upstream
1151 if self.preserveUser:
1152 if not self.canChangeChangelists():
1153 die("Cannot preserve user names without p4 super-user or admin permissions")
1156 print "Origin branch is " + self.origin
1158 if len(self.depotPath) == 0:
1159 print "Internal error: cannot locate perforce depot path from existing branches"
1162 self.useClientSpec = False
1163 if gitConfig("git-p4.useclientspec", "--bool") == "true":
1164 self.useClientSpec = True
1165 if self.useClientSpec:
1166 self.clientSpecDirs = getClientSpec()
1168 if self.useClientSpec:
1169 # all files are relative to the client spec
1170 self.clientPath = getClientRoot()
1172 self.clientPath = p4Where(self.depotPath)
1174 if self.clientPath == "":
1175 die("Error: Cannot locate perforce checkout of %s in client view" % self.depotPath)
1177 print "Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath)
1178 self.oldWorkingDirectory = os.getcwd()
1180 # ensure the clientPath exists
1181 if not os.path.exists(self.clientPath):
1182 os.makedirs(self.clientPath)
1184 chdir(self.clientPath)
1185 print "Synchronizing p4 checkout..."
1190 for line in read_pipe_lines("git rev-list --no-merges %s..%s" % (self.origin, self.master)):
1191 commits.append(line.strip())
1194 if self.preserveUser or (gitConfig("git-p4.skipUserNameCheck") == "true"):
1195 self.checkAuthorship = False
1197 self.checkAuthorship = True
1199 if self.preserveUser:
1200 self.checkValidP4Users(commits)
1202 while len(commits) > 0:
1204 commits = commits[1:]
1205 self.applyCommit(commit)
1206 if not self.interactive:
1209 if len(commits) == 0:
1210 print "All changes applied!"
1211 chdir(self.oldWorkingDirectory)
1222 """Represent a p4 view ("p4 help views"), and map files in a
1223 repo according to the view."""
1226 """A depot or client path, possibly containing wildcards.
1227 The only one supported is ... at the end, currently.
1228 Initialize with the full path, with //depot or //client."""
1230 def __init__(self, path, is_depot):
1232 self.is_depot = is_depot
1233 self.find_wildcards()
1234 # remember the prefix bit, useful for relative mappings
1235 m = re.match("(//[^/]+/)", self.path)
1237 die("Path %s does not start with //prefix/" % self.path)
1239 if not self.is_depot:
1240 # strip //client/ on client paths
1241 self.path = self.path[len(prefix):]
1243 def find_wildcards(self):
1244 """Make sure wildcards are valid, and set up internal
1247 self.ends_triple_dot = False
1248 # There are three wildcards allowed in p4 views
1249 # (see "p4 help views"). This code knows how to
1250 # handle "..." (only at the end), but cannot deal with
1251 # "%%n" or "*". Only check the depot_side, as p4 should
1252 # validate that the client_side matches too.
1253 if re.search(r'%%[1-9]', self.path):
1254 die("Can't handle %%n wildcards in view: %s" % self.path)
1255 if self.path.find("*") >= 0:
1256 die("Can't handle * wildcards in view: %s" % self.path)
1257 triple_dot_index = self.path.find("...")
1258 if triple_dot_index >= 0:
1259 if not self.path.endswith("..."):
1260 die("Can handle ... wildcard only at end of path: %s" %
1262 self.ends_triple_dot = True
1264 def ensure_compatible(self, other_path):
1265 """Make sure the wildcards agree."""
1266 if self.ends_triple_dot != other_path.ends_triple_dot:
1267 die("Both paths must end with ... if either does;\n" +
1268 "paths: %s %s" % (self.path, other_path.path))
1270 def match_wildcards(self, test_path):
1271 """See if this test_path matches us, and fill in the value
1272 of the wildcards if so. Returns a tuple of
1273 (True|False, wildcards[]). For now, only the ... at end
1274 is supported, so at most one wildcard."""
1275 if self.ends_triple_dot:
1276 dotless = self.path[:-3]
1277 if test_path.startswith(dotless):
1278 wildcard = test_path[len(dotless):]
1279 return (True, [ wildcard ])
1281 if test_path == self.path:
1285 def match(self, test_path):
1286 """Just return if it matches; don't bother with the wildcards."""
1287 b, _ = self.match_wildcards(test_path)
1290 def fill_in_wildcards(self, wildcards):
1291 """Return the relative path, with the wildcards filled in
1292 if there are any."""
1293 if self.ends_triple_dot:
1294 return self.path[:-3] + wildcards[0]
1298 class Mapping(object):
1299 def __init__(self, depot_side, client_side, overlay, exclude):
1300 # depot_side is without the trailing /... if it had one
1301 self.depot_side = View.Path(depot_side, is_depot=True)
1302 self.client_side = View.Path(client_side, is_depot=False)
1303 self.overlay = overlay # started with "+"
1304 self.exclude = exclude # started with "-"
1305 assert not (self.overlay and self.exclude)
1306 self.depot_side.ensure_compatible(self.client_side)
1314 return "View.Mapping: %s%s -> %s" % \
1315 (c, self.depot_side, self.client_side)
1317 def map_depot_to_client(self, depot_path):
1318 """Calculate the client path if using this mapping on the
1319 given depot path; does not consider the effect of other
1320 mappings in a view. Even excluded mappings are returned."""
1321 matches, wildcards = self.depot_side.match_wildcards(depot_path)
1324 client_path = self.client_side.fill_in_wildcards(wildcards)
1333 def append(self, view_line):
1334 """Parse a view line, splitting it into depot and client
1335 sides. Append to self.mappings, preserving order."""
1337 # Split the view line into exactly two words. P4 enforces
1338 # structure on these lines that simplifies this quite a bit.
1340 # Either or both words may be double-quoted.
1341 # Single quotes do not matter.
1342 # Double-quote marks cannot occur inside the words.
1343 # A + or - prefix is also inside the quotes.
1344 # There are no quotes unless they contain a space.
1345 # The line is already white-space stripped.
1346 # The two words are separated by a single space.
1348 if view_line[0] == '"':
1349 # First word is double quoted. Find its end.
1350 close_quote_index = view_line.find('"', 1)
1351 if close_quote_index <= 0:
1352 die("No first-word closing quote found: %s" % view_line)
1353 depot_side = view_line[1:close_quote_index]
1354 # skip closing quote and space
1355 rhs_index = close_quote_index + 1 + 1
1357 space_index = view_line.find(" ")
1358 if space_index <= 0:
1359 die("No word-splitting space found: %s" % view_line)
1360 depot_side = view_line[0:space_index]
1361 rhs_index = space_index + 1
1363 if view_line[rhs_index] == '"':
1364 # Second word is double quoted. Make sure there is a
1365 # double quote at the end too.
1366 if not view_line.endswith('"'):
1367 die("View line with rhs quote should end with one: %s" %
1370 client_side = view_line[rhs_index+1:-1]
1372 client_side = view_line[rhs_index:]
1374 # prefix + means overlay on previous mapping
1376 if depot_side.startswith("+"):
1378 depot_side = depot_side[1:]
1380 # prefix - means exclude this path
1382 if depot_side.startswith("-"):
1384 depot_side = depot_side[1:]
1386 m = View.Mapping(depot_side, client_side, overlay, exclude)
1387 self.mappings.append(m)
1389 def map_in_client(self, depot_path):
1390 """Return the relative location in the client where this
1391 depot file should live. Returns "" if the file should
1392 not be mapped in the client."""
1397 # look at later entries first
1398 for m in self.mappings[::-1]:
1400 # see where will this path end up in the client
1401 p = m.map_depot_to_client(depot_path)
1404 # Depot path does not belong in client. Must remember
1405 # this, as previous items should not cause files to
1406 # exist in this path either. Remember that the list is
1407 # being walked from the end, which has higher precedence.
1408 # Overlap mappings do not exclude previous mappings.
1410 paths_filled.append(m.client_side)
1413 # This mapping matched; no need to search any further.
1414 # But, the mapping could be rejected if the client path
1415 # has already been claimed by an earlier mapping.
1416 already_mapped_in_client = False
1417 for f in paths_filled:
1418 # this is View.Path.match
1420 already_mapped_in_client = True
1422 if not already_mapped_in_client:
1423 # Include this file, unless it is from a line that
1424 # explicitly said to exclude it.
1428 # a match, even if rejected, always stops the search
1433 class P4Sync(Command, P4UserMap):
1434 delete_actions = ( "delete", "move/delete", "purge" )
1437 Command.__init__(self)
1438 P4UserMap.__init__(self)
1440 optparse.make_option("--branch", dest="branch"),
1441 optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
1442 optparse.make_option("--changesfile", dest="changesFile"),
1443 optparse.make_option("--silent", dest="silent", action="store_true"),
1444 optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
1445 optparse.make_option("--verbose", dest="verbose", action="store_true"),
1446 optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",
1447 help="Import into refs/heads/ , not refs/remotes"),
1448 optparse.make_option("--max-changes", dest="maxChanges"),
1449 optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',
1450 help="Keep entire BRANCH/DIR/SUBDIR prefix during import"),
1451 optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true',
1452 help="Only sync files that are included in the Perforce Client Spec")
1454 self.description = """Imports from Perforce into a git repository.\n
1456 //depot/my/project/ -- to import the current head
1457 //depot/my/project/@all -- to import everything
1458 //depot/my/project/@1,6 -- to import only from revision 1 to 6
1460 (a ... is not needed in the path p4 specification, it's added implicitly)"""
1462 self.usage += " //depot/path[@revRange]"
1464 self.createdBranches = set()
1465 self.committedChanges = set()
1467 self.detectBranches = False
1468 self.detectLabels = False
1469 self.changesFile = ""
1470 self.syncWithOrigin = True
1471 self.verbose = False
1472 self.importIntoRemotes = True
1473 self.maxChanges = ""
1474 self.isWindows = (platform.system() == "Windows")
1475 self.keepRepoPath = False
1476 self.depotPaths = None
1477 self.p4BranchesInGit = []
1478 self.cloneExclude = []
1479 self.useClientSpec = False
1480 self.useClientSpec_from_options = False
1481 self.clientSpecDirs = None
1483 if gitConfig("git-p4.syncFromOrigin") == "false":
1484 self.syncWithOrigin = False
1487 # P4 wildcards are not allowed in filenames. P4 complains
1488 # if you simply add them, but you can force it with "-f", in
1489 # which case it translates them into %xx encoding internally.
1490 # Search for and fix just these four characters. Do % last so
1491 # that fixing it does not inadvertently create new %-escapes.
1493 def wildcard_decode(self, path):
1494 # Cannot have * in a filename in windows; untested as to
1495 # what p4 would do in such a case.
1496 if not self.isWindows:
1497 path = path.replace("%2A", "*")
1498 path = path.replace("%23", "#") \
1499 .replace("%40", "@") \
1500 .replace("%25", "%")
1503 def extractFilesFromCommit(self, commit):
1504 self.cloneExclude = [re.sub(r"\.\.\.$", "", path)
1505 for path in self.cloneExclude]
1508 while commit.has_key("depotFile%s" % fnum):
1509 path = commit["depotFile%s" % fnum]
1511 if [p for p in self.cloneExclude
1512 if p4PathStartsWith(path, p)]:
1515 found = [p for p in self.depotPaths
1516 if p4PathStartsWith(path, p)]
1523 file["rev"] = commit["rev%s" % fnum]
1524 file["action"] = commit["action%s" % fnum]
1525 file["type"] = commit["type%s" % fnum]
1530 def stripRepoPath(self, path, prefixes):
1531 if self.useClientSpec:
1532 return self.clientSpecDirs.map_in_client(path)
1534 if self.keepRepoPath:
1535 prefixes = [re.sub("^(//[^/]+/).*", r'\1', prefixes[0])]
1538 if p4PathStartsWith(path, p):
1539 path = path[len(p):]
1543 def splitFilesIntoBranches(self, commit):
1546 while commit.has_key("depotFile%s" % fnum):
1547 path = commit["depotFile%s" % fnum]
1548 found = [p for p in self.depotPaths
1549 if p4PathStartsWith(path, p)]
1556 file["rev"] = commit["rev%s" % fnum]
1557 file["action"] = commit["action%s" % fnum]
1558 file["type"] = commit["type%s" % fnum]
1561 relPath = self.stripRepoPath(path, self.depotPaths)
1563 for branch in self.knownBranches.keys():
1565 # add a trailing slash so that a commit into qt/4.2foo doesn't end up in qt/4.2
1566 if relPath.startswith(branch + "/"):
1567 if branch not in branches:
1568 branches[branch] = []
1569 branches[branch].append(file)
1574 # output one file from the P4 stream
1575 # - helper for streamP4Files
1577 def streamOneP4File(self, file, contents):
1578 relPath = self.stripRepoPath(file['depotFile'], self.branchPrefixes)
1579 relPath = self.wildcard_decode(relPath)
1581 sys.stderr.write("%s\n" % relPath)
1583 (type_base, type_mods) = split_p4_type(file["type"])
1586 if "x" in type_mods:
1588 if type_base == "symlink":
1590 # p4 print on a symlink contains "target\n"; remove the newline
1591 data = ''.join(contents)
1592 contents = [data[:-1]]
1594 if type_base == "utf16":
1595 # p4 delivers different text in the python output to -G
1596 # than it does when using "print -o", or normal p4 client
1597 # operations. utf16 is converted to ascii or utf8, perhaps.
1598 # But ascii text saved as -t utf16 is completely mangled.
1599 # Invoke print -o to get the real contents.
1600 text = p4_read_pipe(['print', '-q', '-o', '-', file['depotFile']])
1603 if type_base == "apple":
1604 # Apple filetype files will be streamed as a concatenation of
1605 # its appledouble header and the contents. This is useless
1606 # on both macs and non-macs. If using "print -q -o xx", it
1607 # will create "xx" with the data, and "%xx" with the header.
1608 # This is also not very useful.
1610 # Ideally, someday, this script can learn how to generate
1611 # appledouble files directly and import those to git, but
1612 # non-mac machines can never find a use for apple filetype.
1613 print "\nIgnoring apple filetype file %s" % file['depotFile']
1616 # Perhaps windows wants unicode, utf16 newlines translated too;
1617 # but this is not doing it.
1618 if self.isWindows and type_base == "text":
1620 for data in contents:
1621 data = data.replace("\r\n", "\n")
1622 mangled.append(data)
1625 # Note that we do not try to de-mangle keywords on utf16 files,
1626 # even though in theory somebody may want that.
1627 if type_base in ("text", "unicode", "binary"):
1628 if "ko" in type_mods:
1629 text = ''.join(contents)
1630 text = re.sub(r'\$(Id|Header):[^$]*\$', r'$\1$', text)
1632 elif "k" in type_mods:
1633 text = ''.join(contents)
1634 text = re.sub(r'\$(Id|Header|Author|Date|DateTime|Change|File|Revision):[^$]*\$', r'$\1$', text)
1637 self.gitStream.write("M %s inline %s\n" % (git_mode, relPath))
1642 length = length + len(d)
1644 self.gitStream.write("data %d\n" % length)
1646 self.gitStream.write(d)
1647 self.gitStream.write("\n")
1649 def streamOneP4Deletion(self, file):
1650 relPath = self.stripRepoPath(file['path'], self.branchPrefixes)
1652 sys.stderr.write("delete %s\n" % relPath)
1653 self.gitStream.write("D %s\n" % relPath)
1655 # handle another chunk of streaming data
1656 def streamP4FilesCb(self, marshalled):
1658 if marshalled.has_key('depotFile') and self.stream_have_file_info:
1659 # start of a new file - output the old one first
1660 self.streamOneP4File(self.stream_file, self.stream_contents)
1661 self.stream_file = {}
1662 self.stream_contents = []
1663 self.stream_have_file_info = False
1665 # pick up the new file information... for the
1666 # 'data' field we need to append to our array
1667 for k in marshalled.keys():
1669 self.stream_contents.append(marshalled['data'])
1671 self.stream_file[k] = marshalled[k]
1673 self.stream_have_file_info = True
1675 # Stream directly from "p4 files" into "git fast-import"
1676 def streamP4Files(self, files):
1682 # if using a client spec, only add the files that have
1683 # a path in the client
1684 if self.clientSpecDirs:
1685 if self.clientSpecDirs.map_in_client(f['path']) == "":
1688 filesForCommit.append(f)
1689 if f['action'] in self.delete_actions:
1690 filesToDelete.append(f)
1692 filesToRead.append(f)
1695 for f in filesToDelete:
1696 self.streamOneP4Deletion(f)
1698 if len(filesToRead) > 0:
1699 self.stream_file = {}
1700 self.stream_contents = []
1701 self.stream_have_file_info = False
1703 # curry self argument
1704 def streamP4FilesCbSelf(entry):
1705 self.streamP4FilesCb(entry)
1707 fileArgs = ['%s#%s' % (f['path'], f['rev']) for f in filesToRead]
1709 p4CmdList(["-x", "-", "print"],
1711 cb=streamP4FilesCbSelf)
1714 if self.stream_file.has_key('depotFile'):
1715 self.streamOneP4File(self.stream_file, self.stream_contents)
1717 def commit(self, details, files, branch, branchPrefixes, parent = ""):
1718 epoch = details["time"]
1719 author = details["user"]
1720 self.branchPrefixes = branchPrefixes
1723 print "commit into %s" % branch
1725 # start with reading files; if that fails, we should not
1729 if [p for p in branchPrefixes if p4PathStartsWith(f['path'], p)]:
1730 new_files.append (f)
1732 sys.stderr.write("Ignoring file outside of prefix: %s\n" % f['path'])
1734 self.gitStream.write("commit %s\n" % branch)
1735 # gitStream.write("mark :%s\n" % details["change"])
1736 self.committedChanges.add(int(details["change"]))
1738 if author not in self.users:
1739 self.getUserMapFromPerforceServer()
1740 if author in self.users:
1741 committer = "%s %s %s" % (self.users[author], epoch, self.tz)
1743 committer = "%s <a@b> %s %s" % (author, epoch, self.tz)
1745 self.gitStream.write("committer %s\n" % committer)
1747 self.gitStream.write("data <<EOT\n")
1748 self.gitStream.write(details["desc"])
1749 self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s"
1750 % (','.join (branchPrefixes), details["change"]))
1751 if len(details['options']) > 0:
1752 self.gitStream.write(": options = %s" % details['options'])
1753 self.gitStream.write("]\nEOT\n\n")
1757 print "parent %s" % parent
1758 self.gitStream.write("from %s\n" % parent)
1760 self.streamP4Files(new_files)
1761 self.gitStream.write("\n")
1763 change = int(details["change"])
1765 if self.labels.has_key(change):
1766 label = self.labels[change]
1767 labelDetails = label[0]
1768 labelRevisions = label[1]
1770 print "Change %s is labelled %s" % (change, labelDetails)
1772 files = p4CmdList(["files"] + ["%s...@%s" % (p, change)
1773 for p in branchPrefixes])
1775 if len(files) == len(labelRevisions):
1779 if info["action"] in self.delete_actions:
1781 cleanedFiles[info["depotFile"]] = info["rev"]
1783 if cleanedFiles == labelRevisions:
1784 self.gitStream.write("tag tag_%s\n" % labelDetails["label"])
1785 self.gitStream.write("from %s\n" % branch)
1787 owner = labelDetails["Owner"]
1789 if author in self.users:
1790 tagger = "%s %s %s" % (self.users[owner], epoch, self.tz)
1792 tagger = "%s <a@b> %s %s" % (owner, epoch, self.tz)
1793 self.gitStream.write("tagger %s\n" % tagger)
1794 self.gitStream.write("data <<EOT\n")
1795 self.gitStream.write(labelDetails["Description"])
1796 self.gitStream.write("EOT\n\n")
1800 print ("Tag %s does not match with change %s: files do not match."
1801 % (labelDetails["label"], change))
1805 print ("Tag %s does not match with change %s: file count is different."
1806 % (labelDetails["label"], change))
1808 def getLabels(self):
1811 l = p4CmdList("labels %s..." % ' '.join (self.depotPaths))
1812 if len(l) > 0 and not self.silent:
1813 print "Finding files belonging to labels in %s" % `self.depotPaths`
1816 label = output["label"]
1820 print "Querying files for label %s" % label
1821 for file in p4CmdList(["files"] +
1822 ["%s...@%s" % (p, label)
1823 for p in self.depotPaths]):
1824 revisions[file["depotFile"]] = file["rev"]
1825 change = int(file["change"])
1826 if change > newestChange:
1827 newestChange = change
1829 self.labels[newestChange] = [output, revisions]
1832 print "Label changes: %s" % self.labels.keys()
1834 def guessProjectName(self):
1835 for p in self.depotPaths:
1838 p = p[p.strip().rfind("/") + 1:]
1839 if not p.endswith("/"):
1843 def getBranchMapping(self):
1844 lostAndFoundBranches = set()
1846 user = gitConfig("git-p4.branchUser")
1848 command = "branches -u %s" % user
1850 command = "branches"
1852 for info in p4CmdList(command):
1853 details = p4Cmd("branch -o %s" % info["branch"])
1855 while details.has_key("View%s" % viewIdx):
1856 paths = details["View%s" % viewIdx].split(" ")
1857 viewIdx = viewIdx + 1
1858 # require standard //depot/foo/... //depot/bar/... mapping
1859 if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."):
1862 destination = paths[1]
1864 if p4PathStartsWith(source, self.depotPaths[0]) and p4PathStartsWith(destination, self.depotPaths[0]):
1865 source = source[len(self.depotPaths[0]):-4]
1866 destination = destination[len(self.depotPaths[0]):-4]
1868 if destination in self.knownBranches:
1870 print "p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination)
1871 print "but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination)
1874 self.knownBranches[destination] = source
1876 lostAndFoundBranches.discard(destination)
1878 if source not in self.knownBranches:
1879 lostAndFoundBranches.add(source)
1881 # Perforce does not strictly require branches to be defined, so we also
1882 # check git config for a branch list.
1884 # Example of branch definition in git config file:
1886 # branchList=main:branchA
1887 # branchList=main:branchB
1888 # branchList=branchA:branchC
1889 configBranches = gitConfigList("git-p4.branchList")
1890 for branch in configBranches:
1892 (source, destination) = branch.split(":")
1893 self.knownBranches[destination] = source
1895 lostAndFoundBranches.discard(destination)
1897 if source not in self.knownBranches:
1898 lostAndFoundBranches.add(source)
1901 for branch in lostAndFoundBranches:
1902 self.knownBranches[branch] = branch
1904 def getBranchMappingFromGitBranches(self):
1905 branches = p4BranchesInGit(self.importIntoRemotes)
1906 for branch in branches.keys():
1907 if branch == "master":
1910 branch = branch[len(self.projectName):]
1911 self.knownBranches[branch] = branch
1913 def listExistingP4GitBranches(self):
1914 # branches holds mapping from name to commit
1915 branches = p4BranchesInGit(self.importIntoRemotes)
1916 self.p4BranchesInGit = branches.keys()
1917 for branch in branches.keys():
1918 self.initialParents[self.refPrefix + branch] = branches[branch]
1920 def updateOptionDict(self, d):
1922 if self.keepRepoPath:
1923 option_keys['keepRepoPath'] = 1
1925 d["options"] = ' '.join(sorted(option_keys.keys()))
1927 def readOptions(self, d):
1928 self.keepRepoPath = (d.has_key('options')
1929 and ('keepRepoPath' in d['options']))
1931 def gitRefForBranch(self, branch):
1932 if branch == "main":
1933 return self.refPrefix + "master"
1935 if len(branch) <= 0:
1938 return self.refPrefix + self.projectName + branch
1940 def gitCommitByP4Change(self, ref, change):
1942 print "looking in ref " + ref + " for change %s using bisect..." % change
1945 latestCommit = parseRevision(ref)
1949 print "trying: earliest %s latest %s" % (earliestCommit, latestCommit)
1950 next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip()
1955 log = extractLogMessageFromGitCommit(next)
1956 settings = extractSettingsGitLog(log)
1957 currentChange = int(settings['change'])
1959 print "current change %s" % currentChange
1961 if currentChange == change:
1963 print "found %s" % next
1966 if currentChange < change:
1967 earliestCommit = "^%s" % next
1969 latestCommit = "%s" % next
1973 def importNewBranch(self, branch, maxChange):
1974 # make fast-import flush all changes to disk and update the refs using the checkpoint
1975 # command so that we can try to find the branch parent in the git history
1976 self.gitStream.write("checkpoint\n\n");
1977 self.gitStream.flush();
1978 branchPrefix = self.depotPaths[0] + branch + "/"
1979 range = "@1,%s" % maxChange
1980 #print "prefix" + branchPrefix
1981 changes = p4ChangesForPaths([branchPrefix], range)
1982 if len(changes) <= 0:
1984 firstChange = changes[0]
1985 #print "first change in branch: %s" % firstChange
1986 sourceBranch = self.knownBranches[branch]
1987 sourceDepotPath = self.depotPaths[0] + sourceBranch
1988 sourceRef = self.gitRefForBranch(sourceBranch)
1989 #print "source " + sourceBranch
1991 branchParentChange = int(p4Cmd("changes -m 1 %s...@1,%s" % (sourceDepotPath, firstChange))["change"])
1992 #print "branch parent: %s" % branchParentChange
1993 gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)
1994 if len(gitParent) > 0:
1995 self.initialParents[self.gitRefForBranch(branch)] = gitParent
1996 #print "parent git commit: %s" % gitParent
1998 self.importChanges(changes)
2001 def importChanges(self, changes):
2003 for change in changes:
2004 description = p4Cmd("describe %s" % change)
2005 self.updateOptionDict(description)
2008 sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
2013 if self.detectBranches:
2014 branches = self.splitFilesIntoBranches(description)
2015 for branch in branches.keys():
2017 branchPrefix = self.depotPaths[0] + branch + "/"
2021 filesForCommit = branches[branch]
2024 print "branch is %s" % branch
2026 self.updatedBranches.add(branch)
2028 if branch not in self.createdBranches:
2029 self.createdBranches.add(branch)
2030 parent = self.knownBranches[branch]
2031 if parent == branch:
2034 fullBranch = self.projectName + branch
2035 if fullBranch not in self.p4BranchesInGit:
2037 print("\n Importing new branch %s" % fullBranch);
2038 if self.importNewBranch(branch, change - 1):
2040 self.p4BranchesInGit.append(fullBranch)
2042 print("\n Resuming with change %s" % change);
2045 print "parent determined through known branches: %s" % parent
2047 branch = self.gitRefForBranch(branch)
2048 parent = self.gitRefForBranch(parent)
2051 print "looking for initial parent for %s; current parent is %s" % (branch, parent)
2053 if len(parent) == 0 and branch in self.initialParents:
2054 parent = self.initialParents[branch]
2055 del self.initialParents[branch]
2057 self.commit(description, filesForCommit, branch, [branchPrefix], parent)
2059 files = self.extractFilesFromCommit(description)
2060 self.commit(description, files, self.branch, self.depotPaths,
2062 self.initialParent = ""
2064 print self.gitError.read()
2067 def importHeadRevision(self, revision):
2068 print "Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch)
2071 details["user"] = "git perforce import user"
2072 details["desc"] = ("Initial import of %s from the state at revision %s\n"
2073 % (' '.join(self.depotPaths), revision))
2074 details["change"] = revision
2078 fileArgs = ["%s...%s" % (p,revision) for p in self.depotPaths]
2080 for info in p4CmdList(["files"] + fileArgs):
2082 if 'code' in info and info['code'] == 'error':
2083 sys.stderr.write("p4 returned an error: %s\n"
2085 if info['data'].find("must refer to client") >= 0:
2086 sys.stderr.write("This particular p4 error is misleading.\n")
2087 sys.stderr.write("Perhaps the depot path was misspelled.\n");
2088 sys.stderr.write("Depot path: %s\n" % " ".join(self.depotPaths))
2090 if 'p4ExitCode' in info:
2091 sys.stderr.write("p4 exitcode: %s\n" % info['p4ExitCode'])
2095 change = int(info["change"])
2096 if change > newestRevision:
2097 newestRevision = change
2099 if info["action"] in self.delete_actions:
2100 # don't increase the file cnt, otherwise details["depotFile123"] will have gaps!
2101 #fileCnt = fileCnt + 1
2104 for prop in ["depotFile", "rev", "action", "type" ]:
2105 details["%s%s" % (prop, fileCnt)] = info[prop]
2107 fileCnt = fileCnt + 1
2109 details["change"] = newestRevision
2111 # Use time from top-most change so that all git-p4 clones of
2112 # the same p4 repo have the same commit SHA1s.
2113 res = p4CmdList("describe -s %d" % newestRevision)
2116 if r.has_key('time'):
2117 newestTime = int(r['time'])
2118 if newestTime is None:
2119 die("\"describe -s\" on newest change %d did not give a time")
2120 details["time"] = newestTime
2122 self.updateOptionDict(details)
2124 self.commit(details, self.extractFilesFromCommit(details), self.branch, self.depotPaths)
2126 print "IO error with git fast-import. Is your git version recent enough?"
2127 print self.gitError.read()
2130 def run(self, args):
2131 self.depotPaths = []
2132 self.changeRange = ""
2133 self.initialParent = ""
2134 self.previousDepotPaths = []
2136 # map from branch depot path to parent branch
2137 self.knownBranches = {}
2138 self.initialParents = {}
2139 self.hasOrigin = originP4BranchesExist()
2140 if not self.syncWithOrigin:
2141 self.hasOrigin = False
2143 if self.importIntoRemotes:
2144 self.refPrefix = "refs/remotes/p4/"
2146 self.refPrefix = "refs/heads/p4/"
2148 if self.syncWithOrigin and self.hasOrigin:
2150 print "Syncing with origin first by calling git fetch origin"
2151 system("git fetch origin")
2153 if len(self.branch) == 0:
2154 self.branch = self.refPrefix + "master"
2155 if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
2156 system("git update-ref %s refs/heads/p4" % self.branch)
2157 system("git branch -D p4");
2158 # create it /after/ importing, when master exists
2159 if not gitBranchExists(self.refPrefix + "HEAD") and self.importIntoRemotes and gitBranchExists(self.branch):
2160 system("git symbolic-ref %sHEAD %s" % (self.refPrefix, self.branch))
2162 # accept either the command-line option, or the configuration variable
2163 if self.useClientSpec:
2164 # will use this after clone to set the variable
2165 self.useClientSpec_from_options = True
2167 if gitConfig("git-p4.useclientspec", "--bool") == "true":
2168 self.useClientSpec = True
2169 if self.useClientSpec:
2170 self.clientSpecDirs = getClientSpec()
2172 # TODO: should always look at previous commits,
2173 # merge with previous imports, if possible.
2176 createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)
2177 self.listExistingP4GitBranches()
2179 if len(self.p4BranchesInGit) > 1:
2181 print "Importing from/into multiple branches"
2182 self.detectBranches = True
2185 print "branches: %s" % self.p4BranchesInGit
2188 for branch in self.p4BranchesInGit:
2189 logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch)
2191 settings = extractSettingsGitLog(logMsg)
2193 self.readOptions(settings)
2194 if (settings.has_key('depot-paths')
2195 and settings.has_key ('change')):
2196 change = int(settings['change']) + 1
2197 p4Change = max(p4Change, change)
2199 depotPaths = sorted(settings['depot-paths'])
2200 if self.previousDepotPaths == []:
2201 self.previousDepotPaths = depotPaths
2204 for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
2205 prev_list = prev.split("/")
2206 cur_list = cur.split("/")
2207 for i in range(0, min(len(cur_list), len(prev_list))):
2208 if cur_list[i] <> prev_list[i]:
2212 paths.append ("/".join(cur_list[:i + 1]))
2214 self.previousDepotPaths = paths
2217 self.depotPaths = sorted(self.previousDepotPaths)
2218 self.changeRange = "@%s,#head" % p4Change
2219 if not self.detectBranches:
2220 self.initialParent = parseRevision(self.branch)
2221 if not self.silent and not self.detectBranches:
2222 print "Performing incremental import into %s git branch" % self.branch
2224 if not self.branch.startswith("refs/"):
2225 self.branch = "refs/heads/" + self.branch
2227 if len(args) == 0 and self.depotPaths:
2229 print "Depot paths: %s" % ' '.join(self.depotPaths)
2231 if self.depotPaths and self.depotPaths != args:
2232 print ("previous import used depot path %s and now %s was specified. "
2233 "This doesn't work!" % (' '.join (self.depotPaths),
2237 self.depotPaths = sorted(args)
2242 # Make sure no revision specifiers are used when --changesfile
2244 bad_changesfile = False
2245 if len(self.changesFile) > 0:
2246 for p in self.depotPaths:
2247 if p.find("@") >= 0 or p.find("#") >= 0:
2248 bad_changesfile = True
2251 die("Option --changesfile is incompatible with revision specifiers")
2254 for p in self.depotPaths:
2255 if p.find("@") != -1:
2256 atIdx = p.index("@")
2257 self.changeRange = p[atIdx:]
2258 if self.changeRange == "@all":
2259 self.changeRange = ""
2260 elif ',' not in self.changeRange:
2261 revision = self.changeRange
2262 self.changeRange = ""
2264 elif p.find("#") != -1:
2265 hashIdx = p.index("#")
2266 revision = p[hashIdx:]
2268 elif self.previousDepotPaths == []:
2269 # pay attention to changesfile, if given, else import
2270 # the entire p4 tree at the head revision
2271 if len(self.changesFile) == 0:
2274 p = re.sub ("\.\.\.$", "", p)
2275 if not p.endswith("/"):
2280 self.depotPaths = newPaths
2283 self.loadUserMapFromCache()
2285 if self.detectLabels:
2288 if self.detectBranches:
2289 ## FIXME - what's a P4 projectName ?
2290 self.projectName = self.guessProjectName()
2293 self.getBranchMappingFromGitBranches()
2295 self.getBranchMapping()
2297 print "p4-git branches: %s" % self.p4BranchesInGit
2298 print "initial parents: %s" % self.initialParents
2299 for b in self.p4BranchesInGit:
2303 b = b[len(self.projectName):]
2304 self.createdBranches.add(b)
2306 self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
2308 importProcess = subprocess.Popen(["git", "fast-import"],
2309 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2310 stderr=subprocess.PIPE);
2311 self.gitOutput = importProcess.stdout
2312 self.gitStream = importProcess.stdin
2313 self.gitError = importProcess.stderr
2316 self.importHeadRevision(revision)
2320 if len(self.changesFile) > 0:
2321 output = open(self.changesFile).readlines()
2324 changeSet.add(int(line))
2326 for change in changeSet:
2327 changes.append(change)
2331 # catch "git-p4 sync" with no new branches, in a repo that
2332 # does not have any existing git-p4 branches
2333 if len(args) == 0 and not self.p4BranchesInGit:
2334 die("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.");
2336 print "Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
2338 changes = p4ChangesForPaths(self.depotPaths, self.changeRange)
2340 if len(self.maxChanges) > 0:
2341 changes = changes[:min(int(self.maxChanges), len(changes))]
2343 if len(changes) == 0:
2345 print "No changes to import!"
2348 if not self.silent and not self.detectBranches:
2349 print "Import destination: %s" % self.branch
2351 self.updatedBranches = set()
2353 self.importChanges(changes)
2357 if len(self.updatedBranches) > 0:
2358 sys.stdout.write("Updated branches: ")
2359 for b in self.updatedBranches:
2360 sys.stdout.write("%s " % b)
2361 sys.stdout.write("\n")
2363 self.gitStream.close()
2364 if importProcess.wait() != 0:
2365 die("fast-import failed: %s" % self.gitError.read())
2366 self.gitOutput.close()
2367 self.gitError.close()
2371 class P4Rebase(Command):
2373 Command.__init__(self)
2375 self.description = ("Fetches the latest revision from perforce and "
2376 + "rebases the current work (branch) against it")
2377 self.verbose = False
2379 def run(self, args):
2383 return self.rebase()
2386 if os.system("git update-index --refresh") != 0:
2387 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.");
2388 if len(read_pipe("git diff-index HEAD --")) > 0:
2389 die("You have uncommited changes. Please commit them before rebasing or stash them away with git stash.");
2391 [upstream, settings] = findUpstreamBranchPoint()
2392 if len(upstream) == 0:
2393 die("Cannot find upstream branchpoint for rebase")
2395 # the branchpoint may be p4/foo~3, so strip off the parent
2396 upstream = re.sub("~[0-9]+$", "", upstream)
2398 print "Rebasing the current branch onto %s" % upstream
2399 oldHead = read_pipe("git rev-parse HEAD").strip()
2400 system("git rebase %s" % upstream)
2401 system("git diff-tree --stat --summary -M %s HEAD" % oldHead)
2404 class P4Clone(P4Sync):
2406 P4Sync.__init__(self)
2407 self.description = "Creates a new git repository and imports from Perforce into it"
2408 self.usage = "usage: %prog [options] //depot/path[@revRange]"
2410 optparse.make_option("--destination", dest="cloneDestination",
2411 action='store', default=None,
2412 help="where to leave result of the clone"),
2413 optparse.make_option("-/", dest="cloneExclude",
2414 action="append", type="string",
2415 help="exclude depot path"),
2416 optparse.make_option("--bare", dest="cloneBare",
2417 action="store_true", default=False),
2419 self.cloneDestination = None
2420 self.needsGit = False
2421 self.cloneBare = False
2423 # This is required for the "append" cloneExclude action
2424 def ensure_value(self, attr, value):
2425 if not hasattr(self, attr) or getattr(self, attr) is None:
2426 setattr(self, attr, value)
2427 return getattr(self, attr)
2429 def defaultDestination(self, args):
2430 ## TODO: use common prefix of args?
2432 depotDir = re.sub("(@[^@]*)$", "", depotPath)
2433 depotDir = re.sub("(#[^#]*)$", "", depotDir)
2434 depotDir = re.sub(r"\.\.\.$", "", depotDir)
2435 depotDir = re.sub(r"/$", "", depotDir)
2436 return os.path.split(depotDir)[1]
2438 def run(self, args):
2442 if self.keepRepoPath and not self.cloneDestination:
2443 sys.stderr.write("Must specify destination for --keep-path\n")
2448 if not self.cloneDestination and len(depotPaths) > 1:
2449 self.cloneDestination = depotPaths[-1]
2450 depotPaths = depotPaths[:-1]
2452 self.cloneExclude = ["/"+p for p in self.cloneExclude]
2453 for p in depotPaths:
2454 if not p.startswith("//"):
2457 if not self.cloneDestination:
2458 self.cloneDestination = self.defaultDestination(args)
2460 print "Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination)
2462 if not os.path.exists(self.cloneDestination):
2463 os.makedirs(self.cloneDestination)
2464 chdir(self.cloneDestination)
2466 init_cmd = [ "git", "init" ]
2468 init_cmd.append("--bare")
2469 subprocess.check_call(init_cmd)
2471 if not P4Sync.run(self, depotPaths):
2473 if self.branch != "master":
2474 if self.importIntoRemotes:
2475 masterbranch = "refs/remotes/p4/master"
2477 masterbranch = "refs/heads/p4/master"
2478 if gitBranchExists(masterbranch):
2479 system("git branch master %s" % masterbranch)
2480 if not self.cloneBare:
2481 system("git checkout -f")
2483 print "Could not detect main branch. No checkout/master branch created."
2485 # auto-set this variable if invoked with --use-client-spec
2486 if self.useClientSpec_from_options:
2487 system("git config --bool git-p4.useclientspec true")
2491 class P4Branches(Command):
2493 Command.__init__(self)
2495 self.description = ("Shows the git branches that hold imports and their "
2496 + "corresponding perforce depot paths")
2497 self.verbose = False
2499 def run(self, args):
2500 if originP4BranchesExist():
2501 createOrUpdateBranchesFromOrigin()
2503 cmdline = "git rev-parse --symbolic "
2504 cmdline += " --remotes"
2506 for line in read_pipe_lines(cmdline):
2509 if not line.startswith('p4/') or line == "p4/HEAD":
2513 log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch)
2514 settings = extractSettingsGitLog(log)
2516 print "%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"])
2519 class HelpFormatter(optparse.IndentedHelpFormatter):
2521 optparse.IndentedHelpFormatter.__init__(self)
2523 def format_description(self, description):
2525 return description + "\n"
2529 def printUsage(commands):
2530 print "usage: %s <command> [options]" % sys.argv[0]
2532 print "valid commands: %s" % ", ".join(commands)
2534 print "Try %s <command> --help for command specific help." % sys.argv[0]
2539 "submit" : P4Submit,
2540 "commit" : P4Submit,
2542 "rebase" : P4Rebase,
2544 "rollback" : P4RollBack,
2545 "branches" : P4Branches
2550 if len(sys.argv[1:]) == 0:
2551 printUsage(commands.keys())
2555 cmdName = sys.argv[1]
2557 klass = commands[cmdName]
2560 print "unknown command %s" % cmdName
2562 printUsage(commands.keys())
2565 options = cmd.options
2566 cmd.gitdir = os.environ.get("GIT_DIR", None)
2570 if len(options) > 0:
2572 options.append(optparse.make_option("--git-dir", dest="gitdir"))
2574 parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
2576 description = cmd.description,
2577 formatter = HelpFormatter())
2579 (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
2581 verbose = cmd.verbose
2583 if cmd.gitdir == None:
2584 cmd.gitdir = os.path.abspath(".git")
2585 if not isValidGitDir(cmd.gitdir):
2586 cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
2587 if os.path.exists(cmd.gitdir):
2588 cdup = read_pipe("git rev-parse --show-cdup").strip()
2592 if not isValidGitDir(cmd.gitdir):
2593 if isValidGitDir(cmd.gitdir + "/.git"):
2594 cmd.gitdir += "/.git"
2596 die("fatal: cannot locate git repository at %s" % cmd.gitdir)
2598 os.environ["GIT_DIR"] = cmd.gitdir
2600 if not cmd.run(args):
2605 if __name__ == '__main__':