[PATCH] git-p4: add option to preserve user names

DORMANTno replies

3 messages, 2 authors, 2016-06-15 · open the first message on its own page

[PATCH] git-p4: add option to preserve user names

From: Luke Diamand <hidden>
Date: 2016-06-15 22:51:04

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.

Luke Diamand (1):
  git-p4: add option to preserve user names

 contrib/fast-import/git-p4     |   33 +++++++++++++++++++++++++++++++++
 contrib/fast-import/git-p4.txt |    6 ++++++
 2 files changed, 39 insertions(+), 0 deletions(-)

[PATCH] git-p4: add option to preserve user names

From: Luke Diamand <hidden>
Date: 2016-06-15 22:51:04

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 <redacted>
---
 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

Re: [PATCH] git-p4: add option to preserve user names

From: Pete Wyckoff <hidden>
Date: 2016-06-15 22:51:04

luke@diamand.org wrote on Tue, 19 Apr 2011 19:01 +0100:
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.
I like the idea.  Have often thought about using this approach
myself.  Some comments:

1.  Can we have a .git/config option too?

2.  p4 users output is already cached; you should use
    the same loadUserMapFromCache() and friends, or refactor
    to share the cache.

3.  Andrew will convert your tabs into spaces later, maybe save him
    the effort now.

4.  Do you need both "P4USER" and "User:" for this to work?
    Perhaps just use the "User:" one if so, it is a bit more
    concise.

5.  Document how "p4 protect" is used to give admin privileges
    needed for this to work, but won't tell you if you have them.
    I think it may be true that "p4 typemap -o" needs admin
    priviliges, and is a way to test if you ever need that.

6.  What happens if you try, and don't have admin?  Does it
    silently just use you instead?  And your comment about
    "fixup later manually": I've always wondered how actually
    to do that with p4, can you advise?

7.  Super-bonus points for a test case in t/t9800-git-p4.sh.  :)

		-- Pete
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help