Skip to content

Commit affb474

Browse files
luked99gitster
authored andcommitted
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 <[email protected]> Acked-by: Pete Wyckoff <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a37a8de commit affb474

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

contrib/fast-import/git-p4

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,26 @@ class Command:
563563
class P4UserMap:
564564
def __init__(self):
565565
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
566586

567587
def getUserCacheFilename(self):
568588
home = os.environ.get("HOME", os.environ.get("USERPROFILE"))
@@ -700,7 +720,6 @@ class P4Submit(Command, P4UserMap):
700720
self.verbose = False
701721
self.preserveUser = gitConfig("git-p4.preserveUser").lower() == "true"
702722
self.isWindows = (platform.system() == "Windows")
703-
self.myP4UserId = None
704723

705724
def check(self):
706725
if len(p4CmdList("opened ...")) > 0:
@@ -808,25 +827,6 @@ class P4Submit(Command, P4UserMap):
808827
return 1
809828
return 0
810829

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-
830830
def prepareSubmitTemplate(self):
831831
# remove lines in the Files section that show changes to files outside the depot path we're committing into
832832
template = ""
@@ -1664,6 +1664,12 @@ class P4Sync(Command, P4UserMap):
16641664
if self.stream_file.has_key('depotFile'):
16651665
self.streamOneP4File(self.stream_file, self.stream_contents)
16661666

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+
16671673
def commit(self, details, files, branch, branchPrefixes, parent = ""):
16681674
epoch = details["time"]
16691675
author = details["user"]
@@ -1687,10 +1693,7 @@ class P4Sync(Command, P4UserMap):
16871693
committer = ""
16881694
if author not in self.users:
16891695
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)
16941697

16951698
self.gitStream.write("committer %s\n" % committer)
16961699

@@ -1735,11 +1738,15 @@ class P4Sync(Command, P4UserMap):
17351738
self.gitStream.write("from %s\n" % branch)
17361739

17371740
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)
17411746
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+
17431750
self.gitStream.write("tagger %s\n" % tagger)
17441751

17451752
description = labelDetails["Description"]

0 commit comments

Comments
 (0)