git-p4: importing labels should cope with missing owner

In p4, the Owner field is optional. If it is missing, construct something sensible rather than crashing. Signed-off-by: Luke Diamand <luke@diamand.org> Acked-by: Pete Wyckoff <pw@padd.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Luke Diamand committed Jan 19, 2012 at 09:52 UTC affb474f6a683f3a314bb24d39ef3b43d83e5cfe
1 file changed +35 -28
contrib/fast-import/git-p4
+35 -28
@@ -563,6 +563,26 @@ class Command:
563 class P4UserMap:
564 def __init__(self):
565 self.userMapFromPerforceServer = False
566 + self.myP4UserId = None
567 +
568 + def p4UserId(self):
569 + if self.myP4UserId:
570 + return self.myP4UserId
571 +
572 + results = p4CmdList("user -o")
573 + for r in results:
574 + if r.has_key('User'):
575 + self.myP4UserId = r['User']
576 + return r['User']
577 + die("Could not find your p4 user id")
578 +
579 + def p4UserIsMe(self, p4User):
580 + # return True if the given p4 user is actually me
581 + me = self.p4UserId()
582 + if not p4User or p4User != me:
583 + return False
584 + else:
585 + return True
586
587 def getUserCacheFilename(self):
588 home = os.environ.get("HOME", os.environ.get("USERPROFILE"))
@@ -700,7 +720,6 @@ class P4Submit(Command, P4UserMap):
720 self.verbose = False
721 self.preserveUser = gitConfig("git-p4.preserveUser").lower() == "true"
722 self.isWindows = (platform.system() == "Windows")
703 - self.myP4UserId = None
723
724 def check(self):
725 if len(p4CmdList("opened ...")) > 0:
@@ -808,25 +827,6 @@ class P4Submit(Command, P4UserMap):
827 return 1
828 return 0
829
811 - def p4UserId(self):
812 - if self.myP4UserId:
813 - return self.myP4UserId
814 -
815 - results = p4CmdList("user -o")
816 - for r in results:
817 - if r.has_key('User'):
818 - self.myP4UserId = r['User']
819 - return r['User']
820 - die("Could not find your p4 user id")
821 -
822 - def p4UserIsMe(self, p4User):
823 - # return True if the given p4 user is actually me
824 - me = self.p4UserId()
825 - if not p4User or p4User != me:
826 - return False
827 - else:
828 - return True
829 -
830 def prepareSubmitTemplate(self):
831 # remove lines in the Files section that show changes to files outside the depot path we're committing into
832 template = ""
@@ -1664,6 +1664,12 @@ class P4Sync(Command, P4UserMap):
1664 if self.stream_file.has_key('depotFile'):
1665 self.streamOneP4File(self.stream_file, self.stream_contents)
1666
1667 + def make_email(self, userid):
1668 + if userid in self.users:
1669 + return self.users[userid]
1670 + else:
1671 + return "%s <a@b>" % userid
1672 +
1673 def commit(self, details, files, branch, branchPrefixes, parent = ""):
1674 epoch = details["time"]
1675 author = details["user"]
@@ -1687,10 +1693,7 @@ class P4Sync(Command, P4UserMap):
1693 committer = ""
1694 if author not in self.users:
1695 self.getUserMapFromPerforceServer()
1690 - if author in self.users:
1691 - committer = "%s %s %s" % (self.users[author], epoch, self.tz)
1692 - else:
1693 - committer = "%s <a@b> %s %s" % (author, epoch, self.tz)
1696 + committer = "%s %s %s" % (self.make_email(author), epoch, self.tz)
1697
1698 self.gitStream.write("committer %s\n" % committer)
1699
@@ -1735,11 +1738,15 @@ class P4Sync(Command, P4UserMap):
1738 self.gitStream.write("from %s\n" % branch)
1739
1740 owner = labelDetails["Owner"]
1738 - tagger = ""
1739 - if author in self.users:
1740 - tagger = "%s %s %s" % (self.users[owner], epoch, self.tz)
1741 +
1742 + # Try to use the owner of the p4 label, or failing that,
1743 + # the current p4 user id.
1744 + if owner:
1745 + email = self.make_email(owner)
1746 else:
1742 - tagger = "%s <a@b> %s %s" % (owner, epoch, self.tz)
1747 + email = self.make_email(self.p4UserId())
1748 + tagger = "%s %s %s" % (email, epoch, self.tz)
1749 +
1750 self.gitStream.write("tagger %s\n" % tagger)
1751
1752 description = labelDetails["Description"]