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
, popen2
, subprocess
, shelve
12 import tempfile
, getopt
, sha
, os
.path
, time
, platform
23 sys
.stderr
.write(msg
+ "\n")
26 def write_pipe(c
, str):
28 sys
.stderr
.write('Writing pipe: %s\n' % c
)
30 pipe
= os
.popen(c
, 'w')
33 die('Command failed: %s' % c
)
37 def read_pipe(c
, ignore_error
=False):
39 sys
.stderr
.write('Reading pipe: %s\n' % c
)
41 pipe
= os
.popen(c
, 'rb')
43 if pipe
.close() and not ignore_error
:
44 die('Command failed: %s' % c
)
49 def read_pipe_lines(c
):
51 sys
.stderr
.write('Reading pipe: %s\n' % c
)
52 ## todo: check return status
53 pipe
= os
.popen(c
, 'rb')
54 val
= pipe
.readlines()
56 die('Command failed: %s' % c
)
62 sys
.stderr
.write("executing %s\n" % cmd
)
63 if os
.system(cmd
) != 0:
64 die("command failed: %s" % cmd
)
67 """Determine if a Perforce 'kind' should have execute permission
69 'p4 help filetypes' gives a list of the types. If it starts with 'x',
70 or x follows one of a few letters. Otherwise, if there is an 'x' after
71 a plus sign, it is also executable"""
72 return (re
.search(r
"(^[cku]?x)|\+.*x", kind
) != None)
74 def setP4ExecBit(file, mode
):
75 # Reopens an already open file and changes the execute bit to match
76 # the execute bit setting in the passed in mode.
80 if not isModeExec(mode
):
81 p4Type
= getP4OpenedType(file)
82 p4Type
= re
.sub('^([cku]?)x(.*)', '\\1\\2', p4Type
)
83 p4Type
= re
.sub('(.*?\+.*?)x(.*?)', '\\1\\2', p4Type
)
87 system("p4 reopen -t %s %s" % (p4Type
, file))
89 def getP4OpenedType(file):
90 # Returns the perforce file type for the given file.
92 result
= read_pipe("p4 opened %s" % file)
93 match
= re
.match(".*\((.+)\)$", result
)
97 die("Could not determine file type for %s" % file)
99 def diffTreePattern():
100 # This is a simple generator for the diff tree regex pattern. This could be
101 # a class variable if this and parseDiffTreeEntry were a part of a class.
102 pattern
= re
.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)')
106 def parseDiffTreeEntry(entry
):
107 """Parses a single diff tree entry into its component elements.
109 See git-diff-tree(1) manpage for details about the format of the diff
110 output. This method returns a dictionary with the following elements:
112 src_mode - The mode of the source file
113 dst_mode - The mode of the destination file
114 src_sha1 - The sha1 for the source file
115 dst_sha1 - The sha1 fr the destination file
116 status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc)
117 status_score - The score for the status (applicable for 'C' and 'R'
118 statuses). This is None if there is no score.
119 src - The path for the source file.
120 dst - The path for the destination file. This is only present for
121 copy or renames. If it is not present, this is None.
123 If the pattern is not matched, None is returned."""
125 match
= diffTreePattern().next().match(entry
)
128 'src_mode': match
.group(1),
129 'dst_mode': match
.group(2),
130 'src_sha1': match
.group(3),
131 'dst_sha1': match
.group(4),
132 'status': match
.group(5),
133 'status_score': match
.group(6),
134 'src': match
.group(7),
135 'dst': match
.group(10)
139 def isModeExec(mode
):
140 # Returns True if the given git mode represents an executable file,
142 return mode
[-3:] == "755"
144 def isModeExecChanged(src_mode
, dst_mode
):
145 return isModeExec(src_mode
) != isModeExec(dst_mode
)
147 def p4CmdList(cmd
, stdin
=None, stdin_mode
='w+b'):
148 cmd
= "p4 -G %s" % cmd
150 sys
.stderr
.write("Opening pipe: %s\n" % cmd
)
152 # Use a temporary file to avoid deadlocks without
153 # subprocess.communicate(), which would put another copy
154 # of stdout into memory.
156 if stdin
is not None:
157 stdin_file
= tempfile
.TemporaryFile(prefix
='p4-stdin', mode
=stdin_mode
)
158 stdin_file
.write(stdin
)
162 p4
= subprocess
.Popen(cmd
, shell
=True,
164 stdout
=subprocess
.PIPE
)
169 entry
= marshal
.load(p4
.stdout
)
176 entry
["p4ExitCode"] = exitCode
182 list = p4CmdList(cmd
)
188 def p4Where(depotPath
):
189 if not depotPath
.endswith("/"):
191 output
= p4Cmd("where %s..." % depotPath
)
192 if output
["code"] == "error":
196 clientPath
= output
.get("path")
197 elif "data" in output
:
198 data
= output
.get("data")
199 lastSpace
= data
.rfind(" ")
200 clientPath
= data
[lastSpace
+ 1:]
202 if clientPath
.endswith("..."):
203 clientPath
= clientPath
[:-3]
206 def currentGitBranch():
207 return read_pipe("git name-rev HEAD").split(" ")[1].strip()
209 def isValidGitDir(path
):
210 if (os
.path
.exists(path
+ "/HEAD")
211 and os
.path
.exists(path
+ "/refs") and os
.path
.exists(path
+ "/objects")):
215 def parseRevision(ref
):
216 return read_pipe("git rev-parse %s" % ref
).strip()
218 def extractLogMessageFromGitCommit(commit
):
221 ## fixme: title is first line of commit, not 1st paragraph.
223 for log
in read_pipe_lines("git cat-file commit %s" % commit
):
232 def extractSettingsGitLog(log
):
234 for line
in log
.split("\n"):
236 m
= re
.search (r
"^ *\[git-p4: (.*)\]$", line
)
240 assignments
= m
.group(1).split (':')
241 for a
in assignments
:
243 key
= vals
[0].strip()
244 val
= ('='.join (vals
[1:])).strip()
245 if val
.endswith ('\"') and val
.startswith('"'):
250 paths
= values
.get("depot-paths")
252 paths
= values
.get("depot-path")
254 values
['depot-paths'] = paths
.split(',')
257 def gitBranchExists(branch
):
258 proc
= subprocess
.Popen(["git", "rev-parse", branch
],
259 stderr
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
);
260 return proc
.wait() == 0;
263 return read_pipe("git config %s" % key
, ignore_error
=True).strip()
265 def p4BranchesInGit(branchesAreInRemotes
= True):
268 cmdline
= "git rev-parse --symbolic "
269 if branchesAreInRemotes
:
270 cmdline
+= " --remotes"
272 cmdline
+= " --branches"
274 for line
in read_pipe_lines(cmdline
):
277 ## only import to p4/
278 if not line
.startswith('p4/') or line
== "p4/HEAD":
283 branch
= re
.sub ("^p4/", "", line
)
285 branches
[branch
] = parseRevision(line
)
288 def findUpstreamBranchPoint(head
= "HEAD"):
289 branches
= p4BranchesInGit()
290 # map from depot-path to branch name
291 branchByDepotPath
= {}
292 for branch
in branches
.keys():
293 tip
= branches
[branch
]
294 log
= extractLogMessageFromGitCommit(tip
)
295 settings
= extractSettingsGitLog(log
)
296 if settings
.has_key("depot-paths"):
297 paths
= ",".join(settings
["depot-paths"])
298 branchByDepotPath
[paths
] = "remotes/p4/" + branch
302 while parent
< 65535:
303 commit
= head
+ "~%s" % parent
304 log
= extractLogMessageFromGitCommit(commit
)
305 settings
= extractSettingsGitLog(log
)
306 if settings
.has_key("depot-paths"):
307 paths
= ",".join(settings
["depot-paths"])
308 if branchByDepotPath
.has_key(paths
):
309 return [branchByDepotPath
[paths
], settings
]
313 return ["", settings
]
315 def createOrUpdateBranchesFromOrigin(localRefPrefix
= "refs/remotes/p4/", silent
=True):
317 print ("Creating/updating branch(es) in %s based on origin branch(es)"
320 originPrefix
= "origin/p4/"
322 for line
in read_pipe_lines("git rev-parse --symbolic --remotes"):
324 if (not line
.startswith(originPrefix
)) or line
.endswith("HEAD"):
327 headName
= line
[len(originPrefix
):]
328 remoteHead
= localRefPrefix
+ headName
331 original
= extractSettingsGitLog(extractLogMessageFromGitCommit(originHead
))
332 if (not original
.has_key('depot-paths')
333 or not original
.has_key('change')):
337 if not gitBranchExists(remoteHead
):
339 print "creating %s" % remoteHead
342 settings
= extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead
))
343 if settings
.has_key('change') > 0:
344 if settings
['depot-paths'] == original
['depot-paths']:
345 originP4Change
= int(original
['change'])
346 p4Change
= int(settings
['change'])
347 if originP4Change
> p4Change
:
348 print ("%s (%s) is newer than %s (%s). "
349 "Updating p4 branch from origin."
350 % (originHead
, originP4Change
,
351 remoteHead
, p4Change
))
354 print ("Ignoring: %s was imported from %s while "
355 "%s was imported from %s"
356 % (originHead
, ','.join(original
['depot-paths']),
357 remoteHead
, ','.join(settings
['depot-paths'])))
360 system("git update-ref %s %s" % (remoteHead
, originHead
))
362 def originP4BranchesExist():
363 return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
365 def p4ChangesForPaths(depotPaths
, changeRange
):
367 output
= read_pipe_lines("p4 changes " + ' '.join (["%s...%s" % (p
, changeRange
)
368 for p
in depotPaths
]))
372 changeNum
= line
.split(" ")[1]
373 changes
.append(int(changeNum
))
380 self
.usage
= "usage: %prog [options]"
383 class P4Debug(Command
):
385 Command
.__init
__(self
)
387 optparse
.make_option("--verbose", dest
="verbose", action
="store_true",
390 self
.description
= "A tool to debug the output of p4 -G."
391 self
.needsGit
= False
396 for output
in p4CmdList(" ".join(args
)):
397 print 'Element: %d' % j
402 class P4RollBack(Command
):
404 Command
.__init
__(self
)
406 optparse
.make_option("--verbose", dest
="verbose", action
="store_true"),
407 optparse
.make_option("--local", dest
="rollbackLocalBranches", action
="store_true")
409 self
.description
= "A tool to debug the multi-branch import. Don't use :)"
411 self
.rollbackLocalBranches
= False
416 maxChange
= int(args
[0])
418 if "p4ExitCode" in p4Cmd("changes -m 1"):
419 die("Problems executing p4");
421 if self
.rollbackLocalBranches
:
422 refPrefix
= "refs/heads/"
423 lines
= read_pipe_lines("git rev-parse --symbolic --branches")
425 refPrefix
= "refs/remotes/"
426 lines
= read_pipe_lines("git rev-parse --symbolic --remotes")
429 if self
.rollbackLocalBranches
or (line
.startswith("p4/") and line
!= "p4/HEAD\n"):
431 ref
= refPrefix
+ line
432 log
= extractLogMessageFromGitCommit(ref
)
433 settings
= extractSettingsGitLog(log
)
435 depotPaths
= settings
['depot-paths']
436 change
= settings
['change']
440 if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p
, maxChange
)
441 for p
in depotPaths
]))) == 0:
442 print "Branch %s did not exist at change %s, deleting." % (ref
, maxChange
)
443 system("git update-ref -d %s `git rev-parse %s`" % (ref
, ref
))
446 while change
and int(change
) > maxChange
:
449 print "%s is at %s ; rewinding towards %s" % (ref
, change
, maxChange
)
450 system("git update-ref %s \"%s^\"" % (ref
, ref
))
451 log
= extractLogMessageFromGitCommit(ref
)
452 settings
= extractSettingsGitLog(log
)
455 depotPaths
= settings
['depot-paths']
456 change
= settings
['change']
459 print "%s rewound to %s" % (ref
, change
)
463 class P4Submit(Command
):
465 Command
.__init
__(self
)
467 optparse
.make_option("--verbose", dest
="verbose", action
="store_true"),
468 optparse
.make_option("--origin", dest
="origin"),
469 optparse
.make_option("-M", dest
="detectRename", action
="store_true"),
471 self
.description
= "Submit changes from git to the perforce depot."
472 self
.usage
+= " [name of git branch to submit into perforce depot]"
473 self
.interactive
= True
475 self
.detectRename
= False
477 self
.isWindows
= (platform
.system() == "Windows")
480 if len(p4CmdList("opened ...")) > 0:
481 die("You have files opened with perforce! Close them before starting the sync.")
483 # replaces everything between 'Description:' and the next P4 submit template field with the
485 def prepareLogMessage(self
, template
, message
):
488 inDescriptionSection
= False
490 for line
in template
.split("\n"):
491 if line
.startswith("#"):
492 result
+= line
+ "\n"
495 if inDescriptionSection
:
496 if line
.startswith("Files:"):
497 inDescriptionSection
= False
501 if line
.startswith("Description:"):
502 inDescriptionSection
= True
504 for messageLine
in message
.split("\n"):
505 line
+= "\t" + messageLine
+ "\n"
507 result
+= line
+ "\n"
511 def prepareSubmitTemplate(self
):
512 # remove lines in the Files section that show changes to files outside the depot path we're committing into
514 inFilesSection
= False
515 for line
in read_pipe_lines("p4 change -o"):
517 if line
.startswith("\t"):
518 # path starts and ends with a tab
520 lastTab
= path
.rfind("\t")
522 path
= path
[:lastTab
]
523 if not path
.startswith(self
.depotPath
):
526 inFilesSection
= False
528 if line
.startswith("Files:"):
529 inFilesSection
= True
535 def applyCommit(self
, id):
536 print "Applying %s" % (read_pipe("git log --max-count=1 --pretty=oneline %s" % id))
537 diffOpts
= ("", "-M")[self
.detectRename
]
538 diff
= read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (diffOpts
, id, id))
540 filesToDelete
= set()
542 filesToChangeExecBit
= {}
544 diff
= parseDiffTreeEntry(line
)
545 modifier
= diff
['status']
548 system("p4 edit \"%s\"" % path
)
549 if isModeExecChanged(diff
['src_mode'], diff
['dst_mode']):
550 filesToChangeExecBit
[path
] = diff
['dst_mode']
551 editedFiles
.add(path
)
552 elif modifier
== "A":
554 filesToChangeExecBit
[path
] = diff
['dst_mode']
555 if path
in filesToDelete
:
556 filesToDelete
.remove(path
)
557 elif modifier
== "D":
558 filesToDelete
.add(path
)
559 if path
in filesToAdd
:
560 filesToAdd
.remove(path
)
561 elif modifier
== "R":
562 src
, dest
= diff
['src'], diff
['dst']
563 system("p4 integrate -Dt \"%s\" \"%s\"" % (src
, dest
))
564 system("p4 edit \"%s\"" % (dest
))
565 if isModeExecChanged(diff
['src_mode'], diff
['dst_mode']):
566 filesToChangeExecBit
[dest
] = diff
['dst_mode']
568 editedFiles
.add(dest
)
569 filesToDelete
.add(src
)
571 die("unknown modifier %s for %s" % (modifier
, path
))
573 diffcmd
= "git format-patch -k --stdout \"%s^\"..\"%s\"" % (id, id)
574 patchcmd
= diffcmd
+ " | git apply "
575 tryPatchCmd
= patchcmd
+ "--check -"
576 applyPatchCmd
= patchcmd
+ "--check --apply -"
578 if os
.system(tryPatchCmd
) != 0:
579 print "Unfortunately applying the change failed!"
580 print "What do you want to do?"
582 while response
!= "s" and response
!= "a" and response
!= "w":
583 response
= raw_input("[s]kip this patch / [a]pply the patch forcibly "
584 "and with .rej files / [w]rite the patch to a file (patch.txt) ")
586 print "Skipping! Good luck with the next patches..."
587 for f
in editedFiles
:
588 system("p4 revert \"%s\"" % f
);
592 elif response
== "a":
593 os
.system(applyPatchCmd
)
594 if len(filesToAdd
) > 0:
595 print "You may also want to call p4 add on the following files:"
596 print " ".join(filesToAdd
)
597 if len(filesToDelete
):
598 print "The following files should be scheduled for deletion with p4 delete:"
599 print " ".join(filesToDelete
)
600 die("Please resolve and submit the conflict manually and "
601 + "continue afterwards with git-p4 submit --continue")
602 elif response
== "w":
603 system(diffcmd
+ " > patch.txt")
604 print "Patch saved to patch.txt in %s !" % self
.clientPath
605 die("Please resolve and submit the conflict manually and "
606 "continue afterwards with git-p4 submit --continue")
608 system(applyPatchCmd
)
611 system("p4 add \"%s\"" % f
)
612 for f
in filesToDelete
:
613 system("p4 revert \"%s\"" % f
)
614 system("p4 delete \"%s\"" % f
)
616 # Set/clear executable bits
617 for f
in filesToChangeExecBit
.keys():
618 mode
= filesToChangeExecBit
[f
]
619 setP4ExecBit(f
, mode
)
621 logMessage
= extractLogMessageFromGitCommit(id)
623 logMessage
= logMessage
.replace("\n", "\r\n")
624 logMessage
= logMessage
.strip()
626 template
= self
.prepareSubmitTemplate()
629 submitTemplate
= self
.prepareLogMessage(template
, logMessage
)
630 diff
= read_pipe("p4 diff -du ...")
632 for newFile
in filesToAdd
:
633 diff
+= "==== new file ====\n"
634 diff
+= "--- /dev/null\n"
635 diff
+= "+++ %s\n" % newFile
636 f
= open(newFile
, "r")
637 for line
in f
.readlines():
641 separatorLine
= "######## everything below this line is just the diff #######"
642 if platform
.system() == "Windows":
643 separatorLine
+= "\r"
644 separatorLine
+= "\n"
646 [handle
, fileName
] = tempfile
.mkstemp()
647 tmpFile
= os
.fdopen(handle
, "w+")
648 tmpFile
.write(submitTemplate
+ separatorLine
+ diff
)
651 if platform
.system() == "Windows":
652 defaultEditor
= "notepad"
653 editor
= os
.environ
.get("EDITOR", defaultEditor
);
654 system(editor
+ " " + fileName
)
655 tmpFile
= open(fileName
, "rb")
656 message
= tmpFile
.read()
659 submitTemplate
= message
[:message
.index(separatorLine
)]
661 submitTemplate
= submitTemplate
.replace("\r\n", "\n")
663 write_pipe("p4 submit -i", submitTemplate
)
665 fileName
= "submit.txt"
666 file = open(fileName
, "w+")
667 file.write(self
.prepareLogMessage(template
, logMessage
))
669 print ("Perforce submit template written as %s. "
670 + "Please review/edit and then use p4 submit -i < %s to submit directly!"
671 % (fileName
, fileName
))
675 self
.master
= currentGitBranch()
676 if len(self
.master
) == 0 or not gitBranchExists("refs/heads/%s" % self
.master
):
677 die("Detecting current git branch failed!")
679 self
.master
= args
[0]
683 [upstream
, settings
] = findUpstreamBranchPoint()
684 self
.depotPath
= settings
['depot-paths'][0]
685 if len(self
.origin
) == 0:
686 self
.origin
= upstream
689 print "Origin branch is " + self
.origin
691 if len(self
.depotPath
) == 0:
692 print "Internal error: cannot locate perforce depot path from existing branches"
695 self
.clientPath
= p4Where(self
.depotPath
)
697 if len(self
.clientPath
) == 0:
698 print "Error: Cannot locate perforce checkout of %s in client view" % self
.depotPath
701 print "Perforce checkout for depot path %s located at %s" % (self
.depotPath
, self
.clientPath
)
702 self
.oldWorkingDirectory
= os
.getcwd()
704 os
.chdir(self
.clientPath
)
705 print "Syncronizing p4 checkout..."
706 system("p4 sync ...")
711 for line
in read_pipe_lines("git rev-list --no-merges %s..%s" % (self
.origin
, self
.master
)):
712 commits
.append(line
.strip())
715 while len(commits
) > 0:
717 commits
= commits
[1:]
718 self
.applyCommit(commit
)
719 if not self
.interactive
:
722 if len(commits
) == 0:
723 print "All changes applied!"
724 os
.chdir(self
.oldWorkingDirectory
)
734 class P4Sync(Command
):
736 Command
.__init
__(self
)
738 optparse
.make_option("--branch", dest
="branch"),
739 optparse
.make_option("--detect-branches", dest
="detectBranches", action
="store_true"),
740 optparse
.make_option("--changesfile", dest
="changesFile"),
741 optparse
.make_option("--silent", dest
="silent", action
="store_true"),
742 optparse
.make_option("--detect-labels", dest
="detectLabels", action
="store_true"),
743 optparse
.make_option("--verbose", dest
="verbose", action
="store_true"),
744 optparse
.make_option("--import-local", dest
="importIntoRemotes", action
="store_false",
745 help="Import into refs/heads/ , not refs/remotes"),
746 optparse
.make_option("--max-changes", dest
="maxChanges"),
747 optparse
.make_option("--keep-path", dest
="keepRepoPath", action
='store_true',
748 help="Keep entire BRANCH/DIR/SUBDIR prefix during import")
750 self
.description
= """Imports from Perforce into a git repository.\n
752 //depot/my/project/ -- to import the current head
753 //depot/my/project/@all -- to import everything
754 //depot/my/project/@1,6 -- to import only from revision 1 to 6
756 (a ... is not needed in the path p4 specification, it's added implicitly)"""
758 self
.usage
+= " //depot/path[@revRange]"
760 self
.createdBranches
= Set()
761 self
.committedChanges
= Set()
763 self
.detectBranches
= False
764 self
.detectLabels
= False
765 self
.changesFile
= ""
766 self
.syncWithOrigin
= True
768 self
.importIntoRemotes
= True
770 self
.isWindows
= (platform
.system() == "Windows")
771 self
.keepRepoPath
= False
772 self
.depotPaths
= None
773 self
.p4BranchesInGit
= []
774 self
.cloneExclude
= []
776 if gitConfig("git-p4.syncFromOrigin") == "false":
777 self
.syncWithOrigin
= False
779 def extractFilesFromCommit(self
, commit
):
780 self
.cloneExclude
= [re
.sub(r
"\.\.\.$", "", path
)
781 for path
in self
.cloneExclude
]
784 while commit
.has_key("depotFile%s" % fnum
):
785 path
= commit
["depotFile%s" % fnum
]
787 if [p
for p
in self
.cloneExclude
788 if path
.startswith (p
)]:
791 found
= [p
for p
in self
.depotPaths
792 if path
.startswith (p
)]
799 file["rev"] = commit
["rev%s" % fnum
]
800 file["action"] = commit
["action%s" % fnum
]
801 file["type"] = commit
["type%s" % fnum
]
806 def stripRepoPath(self
, path
, prefixes
):
807 if self
.keepRepoPath
:
808 prefixes
= [re
.sub("^(//[^/]+/).*", r
'\1', prefixes
[0])]
811 if path
.startswith(p
):
816 def splitFilesIntoBranches(self
, commit
):
819 while commit
.has_key("depotFile%s" % fnum
):
820 path
= commit
["depotFile%s" % fnum
]
821 found
= [p
for p
in self
.depotPaths
822 if path
.startswith (p
)]
829 file["rev"] = commit
["rev%s" % fnum
]
830 file["action"] = commit
["action%s" % fnum
]
831 file["type"] = commit
["type%s" % fnum
]
834 relPath
= self
.stripRepoPath(path
, self
.depotPaths
)
836 for branch
in self
.knownBranches
.keys():
838 # add a trailing slash so that a commit into qt/4.2foo doesn't end up in qt/4.2
839 if relPath
.startswith(branch
+ "/"):
840 if branch
not in branches
:
841 branches
[branch
] = []
842 branches
[branch
].append(file)
847 ## Should move this out, doesn't use SELF.
848 def readP4Files(self
, files
):
849 files
= [f
for f
in files
850 if f
['action'] != 'delete']
855 filedata
= p4CmdList('-x - print',
856 stdin
='\n'.join(['%s#%s' % (f
['path'], f
['rev'])
859 if "p4ExitCode" in filedata
[0]:
860 die("Problems executing p4. Error: [%d]."
861 % (filedata
[0]['p4ExitCode']));
865 while j
< len(filedata
):
869 while j
< len(filedata
) and filedata
[j
]['code'] in ('text', 'unicode', 'binary'):
870 tmp
= filedata
[j
]['data']
871 if stat
['type'] in ('text+ko', 'unicode+ko', 'binary+ko'):
872 tmp
= re
.sub(r
'(?i)\$(Id|Header):[^$]*\$',r
'$\1$', tmp
)
873 elif stat
['type'] in ('text+k', 'ktext', 'kxtext', 'unicode+k', 'binary+k'):
874 tmp
= re
.sub(r
'(?i)\$(Id|Header|Author|Date|DateTime|Change|File|Revision):[^$]*\$',r
'$\1$', tmp
)
879 if not stat
.has_key('depotFile'):
880 sys
.stderr
.write("p4 print fails with: %s\n" % repr(stat
))
883 contents
[stat
['depotFile']] = text
886 assert not f
.has_key('data')
887 f
['data'] = contents
[f
['path']]
889 def commit(self
, details
, files
, branch
, branchPrefixes
, parent
= ""):
890 epoch
= details
["time"]
891 author
= details
["user"]
894 print "commit into %s" % branch
896 # start with reading files; if that fails, we should not
900 if [p
for p
in branchPrefixes
if f
['path'].startswith(p
)]:
903 sys
.stderr
.write("Ignoring file outside of prefix: %s\n" % path
)
905 self
.readP4Files(files
)
910 self
.gitStream
.write("commit %s\n" % branch
)
911 # gitStream.write("mark :%s\n" % details["change"])
912 self
.committedChanges
.add(int(details
["change"]))
914 if author
not in self
.users
:
915 self
.getUserMapFromPerforceServer()
916 if author
in self
.users
:
917 committer
= "%s %s %s" % (self
.users
[author
], epoch
, self
.tz
)
919 committer
= "%s <a@b> %s %s" % (author
, epoch
, self
.tz
)
921 self
.gitStream
.write("committer %s\n" % committer
)
923 self
.gitStream
.write("data <<EOT\n")
924 self
.gitStream
.write(details
["desc"])
925 self
.gitStream
.write("\n[git-p4: depot-paths = \"%s\": change = %s"
926 % (','.join (branchPrefixes
), details
["change"]))
927 if len(details
['options']) > 0:
928 self
.gitStream
.write(": options = %s" % details
['options'])
929 self
.gitStream
.write("]\nEOT\n\n")
933 print "parent %s" % parent
934 self
.gitStream
.write("from %s\n" % parent
)
937 if file["type"] == "apple":
938 print "\nfile %s is a strange apple file that forks. Ignoring!" % file['path']
941 relPath
= self
.stripRepoPath(file['path'], branchPrefixes
)
942 if file["action"] == "delete":
943 self
.gitStream
.write("D %s\n" % relPath
)
948 if isP4Exec(file["type"]):
950 elif file["type"] == "symlink":
952 # p4 print on a symlink contains "target\n", so strip it off
955 if self
.isWindows
and file["type"].endswith("text"):
956 data
= data
.replace("\r\n", "\n")
958 self
.gitStream
.write("M %s inline %s\n" % (mode
, relPath
))
959 self
.gitStream
.write("data %s\n" % len(data
))
960 self
.gitStream
.write(data
)
961 self
.gitStream
.write("\n")
963 self
.gitStream
.write("\n")
965 change
= int(details
["change"])
967 if self
.labels
.has_key(change
):
968 label
= self
.labels
[change
]
969 labelDetails
= label
[0]
970 labelRevisions
= label
[1]
972 print "Change %s is labelled %s" % (change
, labelDetails
)
974 files
= p4CmdList("files " + ' '.join (["%s...@%s" % (p
, change
)
975 for p
in branchPrefixes
]))
977 if len(files
) == len(labelRevisions
):
981 if info
["action"] == "delete":
983 cleanedFiles
[info
["depotFile"]] = info
["rev"]
985 if cleanedFiles
== labelRevisions
:
986 self
.gitStream
.write("tag tag_%s\n" % labelDetails
["label"])
987 self
.gitStream
.write("from %s\n" % branch
)
989 owner
= labelDetails
["Owner"]
991 if author
in self
.users
:
992 tagger
= "%s %s %s" % (self
.users
[owner
], epoch
, self
.tz
)
994 tagger
= "%s <a@b> %s %s" % (owner
, epoch
, self
.tz
)
995 self
.gitStream
.write("tagger %s\n" % tagger
)
996 self
.gitStream
.write("data <<EOT\n")
997 self
.gitStream
.write(labelDetails
["Description"])
998 self
.gitStream
.write("EOT\n\n")
1002 print ("Tag %s does not match with change %s: files do not match."
1003 % (labelDetails
["label"], change
))
1007 print ("Tag %s does not match with change %s: file count is different."
1008 % (labelDetails
["label"], change
))
1010 def getUserCacheFilename(self
):
1011 home
= os
.environ
.get("HOME", os
.environ
.get("USERPROFILE"))
1012 return home
+ "/.gitp4-usercache.txt"
1014 def getUserMapFromPerforceServer(self
):
1015 if self
.userMapFromPerforceServer
:
1019 for output
in p4CmdList("users"):
1020 if not output
.has_key("User"):
1022 self
.users
[output
["User"]] = output
["FullName"] + " <" + output
["Email"] + ">"
1026 for (key
, val
) in self
.users
.items():
1027 s
+= "%s\t%s\n" % (key
, val
)
1029 open(self
.getUserCacheFilename(), "wb").write(s
)
1030 self
.userMapFromPerforceServer
= True
1032 def loadUserMapFromCache(self
):
1034 self
.userMapFromPerforceServer
= False
1036 cache
= open(self
.getUserCacheFilename(), "rb")
1037 lines
= cache
.readlines()
1040 entry
= line
.strip().split("\t")
1041 self
.users
[entry
[0]] = entry
[1]
1043 self
.getUserMapFromPerforceServer()
1045 def getLabels(self
):
1048 l
= p4CmdList("labels %s..." % ' '.join (self
.depotPaths
))
1049 if len(l
) > 0 and not self
.silent
:
1050 print "Finding files belonging to labels in %s" % `self
.depotPaths`
1053 label
= output
["label"]
1057 print "Querying files for label %s" % label
1058 for file in p4CmdList("files "
1059 + ' '.join (["%s...@%s" % (p
, label
)
1060 for p
in self
.depotPaths
])):
1061 revisions
[file["depotFile"]] = file["rev"]
1062 change
= int(file["change"])
1063 if change
> newestChange
:
1064 newestChange
= change
1066 self
.labels
[newestChange
] = [output
, revisions
]
1069 print "Label changes: %s" % self
.labels
.keys()
1071 def guessProjectName(self
):
1072 for p
in self
.depotPaths
:
1075 p
= p
[p
.strip().rfind("/") + 1:]
1076 if not p
.endswith("/"):
1080 def getBranchMapping(self
):
1081 lostAndFoundBranches
= set()
1083 for info
in p4CmdList("branches"):
1084 details
= p4Cmd("branch -o %s" % info
["branch"])
1086 while details
.has_key("View%s" % viewIdx
):
1087 paths
= details
["View%s" % viewIdx
].split(" ")
1088 viewIdx
= viewIdx
+ 1
1089 # require standard //depot/foo/... //depot/bar/... mapping
1090 if len(paths
) != 2 or not paths
[0].endswith("/...") or not paths
[1].endswith("/..."):
1093 destination
= paths
[1]
1095 if source
.startswith(self
.depotPaths
[0]) and destination
.startswith(self
.depotPaths
[0]):
1096 source
= source
[len(self
.depotPaths
[0]):-4]
1097 destination
= destination
[len(self
.depotPaths
[0]):-4]
1099 if destination
in self
.knownBranches
:
1101 print "p4 branch %s defines a mapping from %s to %s" % (info
["branch"], source
, destination
)
1102 print "but there exists another mapping from %s to %s already!" % (self
.knownBranches
[destination
], destination
)
1105 self
.knownBranches
[destination
] = source
1107 lostAndFoundBranches
.discard(destination
)
1109 if source
not in self
.knownBranches
:
1110 lostAndFoundBranches
.add(source
)
1113 for branch
in lostAndFoundBranches
:
1114 self
.knownBranches
[branch
] = branch
1116 def getBranchMappingFromGitBranches(self
):
1117 branches
= p4BranchesInGit(self
.importIntoRemotes
)
1118 for branch
in branches
.keys():
1119 if branch
== "master":
1122 branch
= branch
[len(self
.projectName
):]
1123 self
.knownBranches
[branch
] = branch
1125 def listExistingP4GitBranches(self
):
1126 # branches holds mapping from name to commit
1127 branches
= p4BranchesInGit(self
.importIntoRemotes
)
1128 self
.p4BranchesInGit
= branches
.keys()
1129 for branch
in branches
.keys():
1130 self
.initialParents
[self
.refPrefix
+ branch
] = branches
[branch
]
1132 def updateOptionDict(self
, d
):
1134 if self
.keepRepoPath
:
1135 option_keys
['keepRepoPath'] = 1
1137 d
["options"] = ' '.join(sorted(option_keys
.keys()))
1139 def readOptions(self
, d
):
1140 self
.keepRepoPath
= (d
.has_key('options')
1141 and ('keepRepoPath' in d
['options']))
1143 def gitRefForBranch(self
, branch
):
1144 if branch
== "main":
1145 return self
.refPrefix
+ "master"
1147 if len(branch
) <= 0:
1150 return self
.refPrefix
+ self
.projectName
+ branch
1152 def gitCommitByP4Change(self
, ref
, change
):
1154 print "looking in ref " + ref
+ " for change %s using bisect..." % change
1157 latestCommit
= parseRevision(ref
)
1161 print "trying: earliest %s latest %s" % (earliestCommit
, latestCommit
)
1162 next
= read_pipe("git rev-list --bisect %s %s" % (latestCommit
, earliestCommit
)).strip()
1167 log
= extractLogMessageFromGitCommit(next
)
1168 settings
= extractSettingsGitLog(log
)
1169 currentChange
= int(settings
['change'])
1171 print "current change %s" % currentChange
1173 if currentChange
== change
:
1175 print "found %s" % next
1178 if currentChange
< change
:
1179 earliestCommit
= "^%s" % next
1181 latestCommit
= "%s" % next
1185 def importNewBranch(self
, branch
, maxChange
):
1186 # make fast-import flush all changes to disk and update the refs using the checkpoint
1187 # command so that we can try to find the branch parent in the git history
1188 self
.gitStream
.write("checkpoint\n\n");
1189 self
.gitStream
.flush();
1190 branchPrefix
= self
.depotPaths
[0] + branch
+ "/"
1191 range = "@1,%s" % maxChange
1192 #print "prefix" + branchPrefix
1193 changes
= p4ChangesForPaths([branchPrefix
], range)
1194 if len(changes
) <= 0:
1196 firstChange
= changes
[0]
1197 #print "first change in branch: %s" % firstChange
1198 sourceBranch
= self
.knownBranches
[branch
]
1199 sourceDepotPath
= self
.depotPaths
[0] + sourceBranch
1200 sourceRef
= self
.gitRefForBranch(sourceBranch
)
1201 #print "source " + sourceBranch
1203 branchParentChange
= int(p4Cmd("changes -m 1 %s...@1,%s" % (sourceDepotPath
, firstChange
))["change"])
1204 #print "branch parent: %s" % branchParentChange
1205 gitParent
= self
.gitCommitByP4Change(sourceRef
, branchParentChange
)
1206 if len(gitParent
) > 0:
1207 self
.initialParents
[self
.gitRefForBranch(branch
)] = gitParent
1208 #print "parent git commit: %s" % gitParent
1210 self
.importChanges(changes
)
1213 def importChanges(self
, changes
):
1215 for change
in changes
:
1216 description
= p4Cmd("describe %s" % change
)
1217 self
.updateOptionDict(description
)
1220 sys
.stdout
.write("\rImporting revision %s (%s%%)" % (change
, cnt
* 100 / len(changes
)))
1225 if self
.detectBranches
:
1226 branches
= self
.splitFilesIntoBranches(description
)
1227 for branch
in branches
.keys():
1229 branchPrefix
= self
.depotPaths
[0] + branch
+ "/"
1233 filesForCommit
= branches
[branch
]
1236 print "branch is %s" % branch
1238 self
.updatedBranches
.add(branch
)
1240 if branch
not in self
.createdBranches
:
1241 self
.createdBranches
.add(branch
)
1242 parent
= self
.knownBranches
[branch
]
1243 if parent
== branch
:
1246 fullBranch
= self
.projectName
+ branch
1247 if fullBranch
not in self
.p4BranchesInGit
:
1249 print("\n Importing new branch %s" % fullBranch
);
1250 if self
.importNewBranch(branch
, change
- 1):
1252 self
.p4BranchesInGit
.append(fullBranch
)
1254 print("\n Resuming with change %s" % change
);
1257 print "parent determined through known branches: %s" % parent
1259 branch
= self
.gitRefForBranch(branch
)
1260 parent
= self
.gitRefForBranch(parent
)
1263 print "looking for initial parent for %s; current parent is %s" % (branch
, parent
)
1265 if len(parent
) == 0 and branch
in self
.initialParents
:
1266 parent
= self
.initialParents
[branch
]
1267 del self
.initialParents
[branch
]
1269 self
.commit(description
, filesForCommit
, branch
, [branchPrefix
], parent
)
1271 files
= self
.extractFilesFromCommit(description
)
1272 self
.commit(description
, files
, self
.branch
, self
.depotPaths
,
1274 self
.initialParent
= ""
1276 print self
.gitError
.read()
1279 def importHeadRevision(self
, revision
):
1280 print "Doing initial import of %s from revision %s into %s" % (' '.join(self
.depotPaths
), revision
, self
.branch
)
1282 details
= { "user" : "git perforce import user", "time" : int(time
.time()) }
1283 details
["desc"] = ("Initial import of %s from the state at revision %s"
1284 % (' '.join(self
.depotPaths
), revision
))
1285 details
["change"] = revision
1289 for info
in p4CmdList("files "
1290 + ' '.join(["%s...%s"
1292 for p
in self
.depotPaths
])):
1294 if info
['code'] == 'error':
1295 sys
.stderr
.write("p4 returned an error: %s\n"
1300 change
= int(info
["change"])
1301 if change
> newestRevision
:
1302 newestRevision
= change
1304 if info
["action"] == "delete":
1305 # don't increase the file cnt, otherwise details["depotFile123"] will have gaps!
1306 #fileCnt = fileCnt + 1
1309 for prop
in ["depotFile", "rev", "action", "type" ]:
1310 details
["%s%s" % (prop
, fileCnt
)] = info
[prop
]
1312 fileCnt
= fileCnt
+ 1
1314 details
["change"] = newestRevision
1315 self
.updateOptionDict(details
)
1317 self
.commit(details
, self
.extractFilesFromCommit(details
), self
.branch
, self
.depotPaths
)
1319 print "IO error with git fast-import. Is your git version recent enough?"
1320 print self
.gitError
.read()
1323 def run(self
, args
):
1324 self
.depotPaths
= []
1325 self
.changeRange
= ""
1326 self
.initialParent
= ""
1327 self
.previousDepotPaths
= []
1329 # map from branch depot path to parent branch
1330 self
.knownBranches
= {}
1331 self
.initialParents
= {}
1332 self
.hasOrigin
= originP4BranchesExist()
1333 if not self
.syncWithOrigin
:
1334 self
.hasOrigin
= False
1336 if self
.importIntoRemotes
:
1337 self
.refPrefix
= "refs/remotes/p4/"
1339 self
.refPrefix
= "refs/heads/p4/"
1341 if self
.syncWithOrigin
and self
.hasOrigin
:
1343 print "Syncing with origin first by calling git fetch origin"
1344 system("git fetch origin")
1346 if len(self
.branch
) == 0:
1347 self
.branch
= self
.refPrefix
+ "master"
1348 if gitBranchExists("refs/heads/p4") and self
.importIntoRemotes
:
1349 system("git update-ref %s refs/heads/p4" % self
.branch
)
1350 system("git branch -D p4");
1351 # create it /after/ importing, when master exists
1352 if not gitBranchExists(self
.refPrefix
+ "HEAD") and self
.importIntoRemotes
and gitBranchExists(self
.branch
):
1353 system("git symbolic-ref %sHEAD %s" % (self
.refPrefix
, self
.branch
))
1355 # TODO: should always look at previous commits,
1356 # merge with previous imports, if possible.
1359 createOrUpdateBranchesFromOrigin(self
.refPrefix
, self
.silent
)
1360 self
.listExistingP4GitBranches()
1362 if len(self
.p4BranchesInGit
) > 1:
1364 print "Importing from/into multiple branches"
1365 self
.detectBranches
= True
1368 print "branches: %s" % self
.p4BranchesInGit
1371 for branch
in self
.p4BranchesInGit
:
1372 logMsg
= extractLogMessageFromGitCommit(self
.refPrefix
+ branch
)
1374 settings
= extractSettingsGitLog(logMsg
)
1376 self
.readOptions(settings
)
1377 if (settings
.has_key('depot-paths')
1378 and settings
.has_key ('change')):
1379 change
= int(settings
['change']) + 1
1380 p4Change
= max(p4Change
, change
)
1382 depotPaths
= sorted(settings
['depot-paths'])
1383 if self
.previousDepotPaths
== []:
1384 self
.previousDepotPaths
= depotPaths
1387 for (prev
, cur
) in zip(self
.previousDepotPaths
, depotPaths
):
1388 for i
in range(0, min(len(cur
), len(prev
))):
1389 if cur
[i
] <> prev
[i
]:
1393 paths
.append (cur
[:i
+ 1])
1395 self
.previousDepotPaths
= paths
1398 self
.depotPaths
= sorted(self
.previousDepotPaths
)
1399 self
.changeRange
= "@%s,#head" % p4Change
1400 if not self
.detectBranches
:
1401 self
.initialParent
= parseRevision(self
.branch
)
1402 if not self
.silent
and not self
.detectBranches
:
1403 print "Performing incremental import into %s git branch" % self
.branch
1405 if not self
.branch
.startswith("refs/"):
1406 self
.branch
= "refs/heads/" + self
.branch
1408 if len(args
) == 0 and self
.depotPaths
:
1410 print "Depot paths: %s" % ' '.join(self
.depotPaths
)
1412 if self
.depotPaths
and self
.depotPaths
!= args
:
1413 print ("previous import used depot path %s and now %s was specified. "
1414 "This doesn't work!" % (' '.join (self
.depotPaths
),
1418 self
.depotPaths
= sorted(args
)
1424 for p
in self
.depotPaths
:
1425 if p
.find("@") != -1:
1426 atIdx
= p
.index("@")
1427 self
.changeRange
= p
[atIdx
:]
1428 if self
.changeRange
== "@all":
1429 self
.changeRange
= ""
1430 elif ',' not in self
.changeRange
:
1431 revision
= self
.changeRange
1432 self
.changeRange
= ""
1434 elif p
.find("#") != -1:
1435 hashIdx
= p
.index("#")
1436 revision
= p
[hashIdx
:]
1438 elif self
.previousDepotPaths
== []:
1441 p
= re
.sub ("\.\.\.$", "", p
)
1442 if not p
.endswith("/"):
1447 self
.depotPaths
= newPaths
1450 self
.loadUserMapFromCache()
1452 if self
.detectLabels
:
1455 if self
.detectBranches
:
1456 ## FIXME - what's a P4 projectName ?
1457 self
.projectName
= self
.guessProjectName()
1460 self
.getBranchMappingFromGitBranches()
1462 self
.getBranchMapping()
1464 print "p4-git branches: %s" % self
.p4BranchesInGit
1465 print "initial parents: %s" % self
.initialParents
1466 for b
in self
.p4BranchesInGit
:
1470 b
= b
[len(self
.projectName
):]
1471 self
.createdBranches
.add(b
)
1473 self
.tz
= "%+03d%02d" % (- time
.timezone
/ 3600, ((- time
.timezone
% 3600) / 60))
1475 importProcess
= subprocess
.Popen(["git", "fast-import"],
1476 stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
,
1477 stderr
=subprocess
.PIPE
);
1478 self
.gitOutput
= importProcess
.stdout
1479 self
.gitStream
= importProcess
.stdin
1480 self
.gitError
= importProcess
.stderr
1483 self
.importHeadRevision(revision
)
1487 if len(self
.changesFile
) > 0:
1488 output
= open(self
.changesFile
).readlines()
1491 changeSet
.add(int(line
))
1493 for change
in changeSet
:
1494 changes
.append(change
)
1499 print "Getting p4 changes for %s...%s" % (', '.join(self
.depotPaths
),
1501 changes
= p4ChangesForPaths(self
.depotPaths
, self
.changeRange
)
1503 if len(self
.maxChanges
) > 0:
1504 changes
= changes
[:min(int(self
.maxChanges
), len(changes
))]
1506 if len(changes
) == 0:
1508 print "No changes to import!"
1511 if not self
.silent
and not self
.detectBranches
:
1512 print "Import destination: %s" % self
.branch
1514 self
.updatedBranches
= set()
1516 self
.importChanges(changes
)
1520 if len(self
.updatedBranches
) > 0:
1521 sys
.stdout
.write("Updated branches: ")
1522 for b
in self
.updatedBranches
:
1523 sys
.stdout
.write("%s " % b
)
1524 sys
.stdout
.write("\n")
1526 self
.gitStream
.close()
1527 if importProcess
.wait() != 0:
1528 die("fast-import failed: %s" % self
.gitError
.read())
1529 self
.gitOutput
.close()
1530 self
.gitError
.close()
1534 class P4Rebase(Command
):
1536 Command
.__init
__(self
)
1538 self
.description
= ("Fetches the latest revision from perforce and "
1539 + "rebases the current work (branch) against it")
1540 self
.verbose
= False
1542 def run(self
, args
):
1546 return self
.rebase()
1549 if os
.system("git update-index --refresh") != 0:
1550 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.");
1551 if len(read_pipe("git diff-index HEAD --")) > 0:
1552 die("You have uncommited changes. Please commit them before rebasing or stash them away with git stash.");
1554 [upstream
, settings
] = findUpstreamBranchPoint()
1555 if len(upstream
) == 0:
1556 die("Cannot find upstream branchpoint for rebase")
1558 # the branchpoint may be p4/foo~3, so strip off the parent
1559 upstream
= re
.sub("~[0-9]+$", "", upstream
)
1561 print "Rebasing the current branch onto %s" % upstream
1562 oldHead
= read_pipe("git rev-parse HEAD").strip()
1563 system("git rebase %s" % upstream
)
1564 system("git diff-tree --stat --summary -M %s HEAD" % oldHead
)
1567 class P4Clone(P4Sync
):
1569 P4Sync
.__init
__(self
)
1570 self
.description
= "Creates a new git repository and imports from Perforce into it"
1571 self
.usage
= "usage: %prog [options] //depot/path[@revRange]"
1573 optparse
.make_option("--destination", dest
="cloneDestination",
1574 action
='store', default
=None,
1575 help="where to leave result of the clone"),
1576 optparse
.make_option("-/", dest
="cloneExclude",
1577 action
="append", type="string",
1578 help="exclude depot path")
1580 self
.cloneDestination
= None
1581 self
.needsGit
= False
1583 # This is required for the "append" cloneExclude action
1584 def ensure_value(self
, attr
, value
):
1585 if not hasattr(self
, attr
) or getattr(self
, attr
) is None:
1586 setattr(self
, attr
, value
)
1587 return getattr(self
, attr
)
1589 def defaultDestination(self
, args
):
1590 ## TODO: use common prefix of args?
1592 depotDir
= re
.sub("(@[^@]*)$", "", depotPath
)
1593 depotDir
= re
.sub("(#[^#]*)$", "", depotDir
)
1594 depotDir
= re
.sub(r
"\.\.\.$", "", depotDir
)
1595 depotDir
= re
.sub(r
"/$", "", depotDir
)
1596 return os
.path
.split(depotDir
)[1]
1598 def run(self
, args
):
1602 if self
.keepRepoPath
and not self
.cloneDestination
:
1603 sys
.stderr
.write("Must specify destination for --keep-path\n")
1608 if not self
.cloneDestination
and len(depotPaths
) > 1:
1609 self
.cloneDestination
= depotPaths
[-1]
1610 depotPaths
= depotPaths
[:-1]
1612 self
.cloneExclude
= ["/"+p
for p
in self
.cloneExclude
]
1613 for p
in depotPaths
:
1614 if not p
.startswith("//"):
1617 if not self
.cloneDestination
:
1618 self
.cloneDestination
= self
.defaultDestination(args
)
1620 print "Importing from %s into %s" % (', '.join(depotPaths
), self
.cloneDestination
)
1621 if not os
.path
.exists(self
.cloneDestination
):
1622 os
.makedirs(self
.cloneDestination
)
1623 os
.chdir(self
.cloneDestination
)
1625 self
.gitdir
= os
.getcwd() + "/.git"
1626 if not P4Sync
.run(self
, depotPaths
):
1628 if self
.branch
!= "master":
1629 if gitBranchExists("refs/remotes/p4/master"):
1630 system("git branch master refs/remotes/p4/master")
1631 system("git checkout -f")
1633 print "Could not detect main branch. No checkout/master branch created."
1637 class P4Branches(Command
):
1639 Command
.__init
__(self
)
1641 self
.description
= ("Shows the git branches that hold imports and their "
1642 + "corresponding perforce depot paths")
1643 self
.verbose
= False
1645 def run(self
, args
):
1646 if originP4BranchesExist():
1647 createOrUpdateBranchesFromOrigin()
1649 cmdline
= "git rev-parse --symbolic "
1650 cmdline
+= " --remotes"
1652 for line
in read_pipe_lines(cmdline
):
1655 if not line
.startswith('p4/') or line
== "p4/HEAD":
1659 log
= extractLogMessageFromGitCommit("refs/remotes/%s" % branch
)
1660 settings
= extractSettingsGitLog(log
)
1662 print "%s <= %s (%s)" % (branch
, ",".join(settings
["depot-paths"]), settings
["change"])
1665 class HelpFormatter(optparse
.IndentedHelpFormatter
):
1667 optparse
.IndentedHelpFormatter
.__init
__(self
)
1669 def format_description(self
, description
):
1671 return description
+ "\n"
1675 def printUsage(commands
):
1676 print "usage: %s <command> [options]" % sys
.argv
[0]
1678 print "valid commands: %s" % ", ".join(commands
)
1680 print "Try %s <command> --help for command specific help." % sys
.argv
[0]
1685 "submit" : P4Submit
,
1686 "commit" : P4Submit
,
1688 "rebase" : P4Rebase
,
1690 "rollback" : P4RollBack
,
1691 "branches" : P4Branches
1696 if len(sys
.argv
[1:]) == 0:
1697 printUsage(commands
.keys())
1701 cmdName
= sys
.argv
[1]
1703 klass
= commands
[cmdName
]
1706 print "unknown command %s" % cmdName
1708 printUsage(commands
.keys())
1711 options
= cmd
.options
1712 cmd
.gitdir
= os
.environ
.get("GIT_DIR", None)
1716 if len(options
) > 0:
1717 options
.append(optparse
.make_option("--git-dir", dest
="gitdir"))
1719 parser
= optparse
.OptionParser(cmd
.usage
.replace("%prog", "%prog " + cmdName
),
1721 description
= cmd
.description
,
1722 formatter
= HelpFormatter())
1724 (cmd
, args
) = parser
.parse_args(sys
.argv
[2:], cmd
);
1726 verbose
= cmd
.verbose
1728 if cmd
.gitdir
== None:
1729 cmd
.gitdir
= os
.path
.abspath(".git")
1730 if not isValidGitDir(cmd
.gitdir
):
1731 cmd
.gitdir
= read_pipe("git rev-parse --git-dir").strip()
1732 if os
.path
.exists(cmd
.gitdir
):
1733 cdup
= read_pipe("git rev-parse --show-cdup").strip()
1737 if not isValidGitDir(cmd
.gitdir
):
1738 if isValidGitDir(cmd
.gitdir
+ "/.git"):
1739 cmd
.gitdir
+= "/.git"
1741 die("fatal: cannot locate git repository at %s" % cmd
.gitdir
)
1743 os
.environ
["GIT_DIR"] = cmd
.gitdir
1745 if not cmd
.run(args
):
1749 if __name__
== '__main__':