{"thread":{"id":"27134","subject":"[PATCH] git-p4: add option to preserve user names","startedAt":"2011-04-19T18:01:17Z","lastAt":"2011-04-20T00:10:38Z","messageCount":3,"participants":["Luke Diamand","Pete Wyckoff"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"166091","messageId":"1303236078-14011-1-git-send-email-luke@diamand.org","threadId":"27134","inReplyTo":null,"subject":"[PATCH] git-p4: add option to preserve user names","fromName":"Luke Diamand","fromEmail":"luke@diamand.org","sentAt":"2011-04-19T18:01:17Z","receivedAt":"2011-04-19T18:01:17Z","isPatch":true,"sender":{"key":"luke@diamand.org","avatar":"https://avatars.githubusercontent.com/u/5330967?v=4"},"body":"Patches from git passed into p4 end up with the committer\nbeing identified as the person who ran git-p4.\n\nWith \"submit --preserve-user\", git-p4 sets P4USER. If the\nsubmitter has sufficient p4 permissions, the p4 equivalent\nof the git email committer will be passed into perforce.\n\nLuke Diamand (1):\n  git-p4: add option to preserve user names\n\n contrib/fast-import/git-p4     |   33 +++++++++++++++++++++++++++++++++\n contrib/fast-import/git-p4.txt |    6 ++++++\n 2 files changed, 39 insertions(+), 0 deletions(-)\n"},{"id":"166092","messageId":"1303236078-14011-2-git-send-email-luke@diamand.org","threadId":"27134","inReplyTo":"1303236078-14011-1-git-send-email-luke@diamand.org","subject":"[PATCH] git-p4: add option to preserve user names","fromName":"Luke Diamand","fromEmail":"luke@diamand.org","sentAt":"2011-04-19T18:01:18Z","receivedAt":"2011-04-19T18:01:18Z","isPatch":true,"sender":{"key":"luke@diamand.org","avatar":"https://avatars.githubusercontent.com/u/5330967?v=4"},"body":"Patches from git passed into p4 end up with the committer\nbeing identified as the person who ran git-p4.\n\nWith \"submit --preserve-user\", git-p4 sets P4USER. If the\nsubmitter has sufficient p4 permissions, the p4 equivalent\nof the git email committer will be passed into perforce.\n\nSigned-off-by: Luke Diamand <luke@diamand.org>\n---\n contrib/fast-import/git-p4     |   33 +++++++++++++++++++++++++++++++++\n contrib/fast-import/git-p4.txt |    6 ++++++\n 2 files changed, 39 insertions(+), 0 deletions(-)\n\ndiff --git a/contrib/fast-import/git-p4 b/contrib/fast-import/git-p4\nindex 78e5b3a..7d66aa9 100755\n--- a/contrib/fast-import/git-p4\n+++ b/contrib/fast-import/git-p4\n@@ -561,6 +561,8 @@ class P4Submit(Command):\n                 optparse.make_option(\"--verbose\", dest=\"verbose\", action=\"store_true\"),\n                 optparse.make_option(\"--origin\", dest=\"origin\"),\n                 optparse.make_option(\"-M\", dest=\"detectRenames\", action=\"store_true\"),\n+                # preserve the user, assumes relevant p4 permissions available\n+                optparse.make_option(\"--preserve-user\", dest=\"preserveUser\", action=\"store_true\"),\n         ]\n         self.description = \"Submit changes from git to the perforce depot.\"\n         self.usage += \" [name of git branch to submit into perforce depot]\"\n@@ -568,6 +570,7 @@ class P4Submit(Command):\n         self.origin = \"\"\n         self.detectRenames = False\n         self.verbose = False\n+        self.preserveUser = False\n         self.isWindows = (platform.system() == \"Windows\")\n \n     def check(self):\n@@ -592,6 +595,11 @@ class P4Submit(Command):\n                 else:\n                     continue\n             else:\n+                if self.preserveUser:\n+\t\t    if self.p4user:\n+                        if line.startswith(\"User:\"):\n+                            line = \"User: %s\" % self.p4user\n+\n                 if line.startswith(\"Description:\"):\n                     inDescriptionSection = True\n                     line += \"\\n\"\n@@ -602,6 +610,18 @@ class P4Submit(Command):\n \n         return result\n \n+    def p4User(self,id):\n+        # Return the perforce user for a given git commit id\n+        git_email = read_pipe(\"git log --max-count=1 --format='%%ae' %s\" % id)\n+        git_email = git_email.strip()\n+        if not self.email_to_user.has_key(git_email):\n+            print(\"Cannot find perforce user for email %s in commit %s.\" %\n+                (git_email, id))\n+            print(\"Submitting changelist with default user - fixup later manually!\")\n+            return None\n+        else:\n+            return self.email_to_user[git_email]\n+\n     def prepareSubmitTemplate(self):\n         # remove lines in the Files section that show changes to files outside the depot path we're committing into\n         template = \"\"\n@@ -631,6 +651,12 @@ class P4Submit(Command):\n     def applyCommit(self, id):\n         print \"Applying %s\" % (read_pipe(\"git log --max-count=1 --pretty=oneline %s\" % id))\n \n+        if self.preserveUser:\n+            p4user = self.p4User(id)\n+            self.p4user = p4user\n+            if p4user:\n+                os.putenv('P4USER', p4user)\n+\n         if not self.detectRenames:\n             # If not explicitly set check the config variable\n             self.detectRenames = gitConfig(\"git-p4.detectRenames\").lower() == \"true\"\n@@ -847,6 +873,13 @@ class P4Submit(Command):\n         print \"Perforce checkout for depot path %s located at %s\" % (self.depotPath, self.clientPath)\n         self.oldWorkingDirectory = os.getcwd()\n \n+        if self.preserveUser:\n+\t    self.email_to_user = {}\n+\t    for output in p4CmdList(\"users\"):\n+\t        if not output.has_key(\"User\"):\n+\t            continue\n+\t        self.email_to_user[output[\"Email\"]] = output[\"User\"]\n+\n         chdir(self.clientPath)\n         print \"Synchronizing p4 checkout...\"\n         p4_system(\"sync ...\")\ndiff --git a/contrib/fast-import/git-p4.txt b/contrib/fast-import/git-p4.txt\nindex e09da44..7c3c794 100644\n--- a/contrib/fast-import/git-p4.txt\n+++ b/contrib/fast-import/git-p4.txt\n@@ -110,6 +110,12 @@ is not your current git branch you can also pass that as an argument:\n \n You can override the reference branch with the --origin=mysourcebranch option.\n \n+The Perforce changelists will be created with the user who ran git-p4. If you\n+use --preserve-user then git-p4 will attempt to create Perforce changelists\n+with the Perforce user corresponding to the git commit author. You need to\n+have sufficient permissions within Perforce, and the git users need to have\n+Perforce accounts.\n+\n If a submit fails you may have to \"p4 resolve\" and submit manually. You can\n continue importing the remaining changes with\n \n-- \n1.7.1\n"},{"id":"166113","messageId":"20110420001038.GB28768@arf.padd.com","threadId":"27134","inReplyTo":"1303236078-14011-2-git-send-email-luke@diamand.org","subject":"Re: [PATCH] git-p4: add option to preserve user names","fromName":"Pete Wyckoff","fromEmail":"pw@padd.com","sentAt":"2011-04-20T00:10:38Z","receivedAt":"2011-04-20T00:10:38Z","isPatch":true,"sender":{"key":"pw@padd.com","avatar":null},"body":"luke@diamand.org wrote on Tue, 19 Apr 2011 19:01 +0100:\n> Patches from git passed into p4 end up with the committer\n> being identified as the person who ran git-p4.\n> \n> With \"submit --preserve-user\", git-p4 sets P4USER. If the\n> submitter has sufficient p4 permissions, the p4 equivalent\n> of the git email committer will be passed into perforce.\n\nI like the idea.  Have often thought about using this approach\nmyself.  Some comments:\n\n1.  Can we have a .git/config option too?\n\n2.  p4 users output is already cached; you should use\n    the same loadUserMapFromCache() and friends, or refactor\n    to share the cache.\n\n3.  Andrew will convert your tabs into spaces later, maybe save him\n    the effort now.\n\n4.  Do you need both \"P4USER\" and \"User:\" for this to work?\n    Perhaps just use the \"User:\" one if so, it is a bit more\n    concise.\n\n5.  Document how \"p4 protect\" is used to give admin privileges\n    needed for this to work, but won't tell you if you have them.\n    I think it may be true that \"p4 typemap -o\" needs admin\n    priviliges, and is a way to test if you ever need that.\n\n6.  What happens if you try, and don't have admin?  Does it\n    silently just use you instead?  And your comment about\n    \"fixup later manually\": I've always wondered how actually\n    to do that with p4, can you advise?\n\n7.  Super-bonus points for a test case in t/t9800-git-p4.sh.  :)\n\n\t\t-- Pete\n"}]}