git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luke Diamand <luke@diamand.org>
To: git@vger.kernel.org
To: git@vger.kernel.org
Cc: Luke Diamand <luke@diamand.org>, Pete Wyckoff <pw@padd.com>
Subject: [PATCH] git-p4: add option to preserve user names
Date: Tue, 19 Apr 2011 19:01:18 +0100	[thread overview]
Message-ID: <1303236078-14011-2-git-send-email-luke@diamand.org> (raw)
In-Reply-To: <1303236078-14011-1-git-send-email-luke@diamand.org>

Patches from git passed into p4 end up with the committer
being identified as the person who ran git-p4.

With "submit --preserve-user", git-p4 sets P4USER. If the
submitter has sufficient p4 permissions, the p4 equivalent
of the git email committer will be passed into perforce.

Signed-off-by: Luke Diamand <luke@diamand.org>
---
 contrib/fast-import/git-p4     |   33 +++++++++++++++++++++++++++++++++
 contrib/fast-import/git-p4.txt |    6 ++++++
 2 files changed, 39 insertions(+), 0 deletions(-)

diff --git a/contrib/fast-import/git-p4 b/contrib/fast-import/git-p4
index 78e5b3a..7d66aa9 100755
--- a/contrib/fast-import/git-p4
+++ b/contrib/fast-import/git-p4
@@ -561,6 +561,8 @@ class P4Submit(Command):
                 optparse.make_option("--verbose", dest="verbose", action="store_true"),
                 optparse.make_option("--origin", dest="origin"),
                 optparse.make_option("-M", dest="detectRenames", action="store_true"),
+                # preserve the user, assumes relevant p4 permissions available
+                optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
         ]
         self.description = "Submit changes from git to the perforce depot."
         self.usage += " [name of git branch to submit into perforce depot]"
@@ -568,6 +570,7 @@ class P4Submit(Command):
         self.origin = ""
         self.detectRenames = False
         self.verbose = False
+        self.preserveUser = False
         self.isWindows = (platform.system() == "Windows")
 
     def check(self):
@@ -592,6 +595,11 @@ class P4Submit(Command):
                 else:
                     continue
             else:
+                if self.preserveUser:
+		    if self.p4user:
+                        if line.startswith("User:"):
+                            line = "User: %s" % self.p4user
+
                 if line.startswith("Description:"):
                     inDescriptionSection = True
                     line += "\n"
@@ -602,6 +610,18 @@ class P4Submit(Command):
 
         return result
 
+    def p4User(self,id):
+        # Return the perforce user for a given git commit id
+        git_email = read_pipe("git log --max-count=1 --format='%%ae' %s" % id)
+        git_email = git_email.strip()
+        if not self.email_to_user.has_key(git_email):
+            print("Cannot find perforce user for email %s in commit %s." %
+                (git_email, id))
+            print("Submitting changelist with default user - fixup later manually!")
+            return None
+        else:
+            return self.email_to_user[git_email]
+
     def prepareSubmitTemplate(self):
         # remove lines in the Files section that show changes to files outside the depot path we're committing into
         template = ""
@@ -631,6 +651,12 @@ class P4Submit(Command):
     def applyCommit(self, id):
         print "Applying %s" % (read_pipe("git log --max-count=1 --pretty=oneline %s" % id))
 
+        if self.preserveUser:
+            p4user = self.p4User(id)
+            self.p4user = p4user
+            if p4user:
+                os.putenv('P4USER', p4user)
+
         if not self.detectRenames:
             # If not explicitly set check the config variable
             self.detectRenames = gitConfig("git-p4.detectRenames").lower() == "true"
@@ -847,6 +873,13 @@ class P4Submit(Command):
         print "Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath)
         self.oldWorkingDirectory = os.getcwd()
 
+        if self.preserveUser:
+	    self.email_to_user = {}
+	    for output in p4CmdList("users"):
+	        if not output.has_key("User"):
+	            continue
+	        self.email_to_user[output["Email"]] = output["User"]
+
         chdir(self.clientPath)
         print "Synchronizing p4 checkout..."
         p4_system("sync ...")
diff --git a/contrib/fast-import/git-p4.txt b/contrib/fast-import/git-p4.txt
index e09da44..7c3c794 100644
--- a/contrib/fast-import/git-p4.txt
+++ b/contrib/fast-import/git-p4.txt
@@ -110,6 +110,12 @@ is not your current git branch you can also pass that as an argument:
 
 You can override the reference branch with the --origin=mysourcebranch option.
 
+The Perforce changelists will be created with the user who ran git-p4. If you
+use --preserve-user then git-p4 will attempt to create Perforce changelists
+with the Perforce user corresponding to the git commit author. You need to
+have sufficient permissions within Perforce, and the git users need to have
+Perforce accounts.
+
 If a submit fails you may have to "p4 resolve" and submit manually. You can
 continue importing the remaining changes with
 
-- 
1.7.1

  reply	other threads:[~2011-04-19 18:02 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-19 18:01 [PATCH] git-p4: add option to preserve user names Luke Diamand
2011-04-19 18:01 ` Luke Diamand [this message]
2011-04-20  0:10   ` Pete Wyckoff
  -- strict thread matches above, loose matches on Subject: below --
2011-04-21 19:50 [PATCH/RFC v2] " Luke Diamand
2011-04-21 19:50 ` [PATCH] " Luke Diamand
2011-04-23 11:54   ` Pete Wyckoff

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1303236078-14011-2-git-send-email-luke@diamand.org \
    --to=luke@diamand.org \
    --cc=git@vger.kernel.org \
    --cc=pw@padd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).