git-p4: warn if git authorship won't be retained
If the git commits you are submitting contain changes made by other people, the authorship will not be retained. Change git-p4 to warn of this and to note that --preserve-user can be used to solve the problem (if you have suitable permissions). The warning can be disabled. Add a test case and update documentation. Signed-off-by: Luke Diamand <luke@diamand.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Luke Diamand committed
May 13, 2011 at 20:46 UTC
848de9c3831f7e476ea58e3ee426aa63fb8f6870
3 files changed
+79
-2
contrib/fast-import/git-p4
+31
-2
@@ -614,6 +614,7 @@ class P4Submit(Command, P4UserMap):
614
self.verbose = False
615
self.preserveUser = gitConfig("git-p4.preserveUser").lower() == "true"
616
self.isWindows = (platform.system() == "Windows")
617
+ self.myP4UserId = None
618
619
def check(self):
620
if len(p4CmdList("opened ...")) > 0:
@@ -721,6 +722,25 @@ class P4Submit(Command, P4UserMap):
722
return 1
723
return 0
724
725
+ def p4UserId(self):
726
+ if self.myP4UserId:
727
+ return self.myP4UserId
728
+
729
+ results = p4CmdList("user -o")
730
+ for r in results:
731
+ if r.has_key('User'):
732
+ self.myP4UserId = r['User']
733
+ return r['User']
734
+ die("Could not find your p4 user id")
735
+
736
+ def p4UserIsMe(self, p4User):
737
+ # return True if the given p4 user is actually me
738
+ me = self.p4UserId()
739
+ if not p4User or p4User != me:
740
+ return False
741
+ else:
742
+ return True
743
+
744
def prepareSubmitTemplate(self):
745
# remove lines in the Files section that show changes to files outside the depot path we're committing into
746
template = ""
@@ -750,8 +770,7 @@ class P4Submit(Command, P4UserMap):
770
def applyCommit(self, id):
771
print "Applying %s" % (read_pipe("git log --max-count=1 --pretty=oneline %s" % id))
772
753
- if self.preserveUser:
754
- (p4User, gitEmail) = self.p4UserForCommit(id)
773
+ (p4User, gitEmail) = self.p4UserForCommit(id)
774
775
if not self.detectRenames:
776
# If not explicitly set check the config variable
@@ -890,6 +909,11 @@ class P4Submit(Command, P4UserMap):
909
newdiff += "+" + line
910
f.close()
911
912
+ if self.checkAuthorship and not self.p4UserIsMe(p4User):
913
+ submitTemplate += "######## git author %s does not match your p4 account.\n" % gitEmail
914
+ submitTemplate += "######## Use git-p4 option --preserve-user to modify authorship\n"
915
+ submitTemplate += "######## Use git-p4 config git-p4.skipUserNameCheck hides this message.\n"
916
+
917
separatorLine = "######## everything below this line is just the diff #######\n"
918
919
[handle, fileName] = tempfile.mkstemp()
@@ -1001,6 +1025,11 @@ class P4Submit(Command, P4UserMap):
1025
commits.append(line.strip())
1026
commits.reverse()
1027
1028
+ if self.preserveUser or (gitConfig("git-p4.skipUserNameCheck") == "true"):
1029
+ self.checkAuthorship = False
1030
+ else:
1031
+ self.checkAuthorship = True
1032
+
1033
if self.preserveUser:
1034
self.checkValidP4Users(commits)
1035
contrib/fast-import/git-p4.txt
+7
@@ -225,6 +225,13 @@ stop. With allowMissingPerforceUsers set to true, git-p4 will use the
225
current user (i.e. the behavior without --preserve-user) and carry on with
226
the perforce commit.
227
228
+git-p4.skipUserNameCheck
229
+
230
+ git config [--global] git-p4.skipUserNameCheck false
231
+
232
+When submitting, git-p4 checks that the git commits are authored by the current
233
+p4 user, and warns if they are not. This disables the check.
234
+
235
Implementation Details...
236
=========================
237
t/t9800-git-p4.sh
+41
@@ -160,6 +160,13 @@ p4_check_commit_author() {
160
fi
161
}
162
163
+make_change_by_user() {
164
+ file=$1 name=$2 email=$3 &&
165
+ echo "username: a change by $name" >>"$file" &&
166
+ git add "$file" &&
167
+ git commit --author "$name <$email>" -m "a change by $name"
168
+}
169
+
170
# Test username support, submitting as user 'alice'
171
test_expect_success 'preserve users' '
172
p4_add_user alice Alice &&
@@ -213,6 +220,40 @@ test_expect_success 'preserve user where author is unknown to p4' '
220
rm -rf "$git" && mkdir "$git"
221
'
222
223
+# If we're *not* using --preserve-user, git-p4 should warn if we're submitting
224
+# changes that are not all ours.
225
+# Test: user in p4 and user unknown to p4.
226
+# Test: warning disabled and user is the same.
227
+test_expect_success 'not preserving user with mixed authorship' '
228
+ "$GITP4" clone --dest="$git" //depot &&
229
+ (
230
+ cd "$git" &&
231
+ git config git-p4.skipSubmitEditCheck true &&
232
+ p4_add_user derek Derek &&
233
+
234
+ make_change_by_user usernamefile3 Derek derek@localhost &&
235
+ P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
236
+ grep "git author derek@localhost does not match" actual &&
237
+
238
+ make_change_by_user usernamefile3 Charlie charlie@localhost &&
239
+ P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
240
+ grep "git author charlie@localhost does not match" actual &&
241
+
242
+ make_change_by_user usernamefile3 alice alice@localhost &&
243
+ P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
244
+ ! grep "git author.*does not match" actual &&
245
+
246
+ git config git-p4.skipUserNameCheck true &&
247
+ make_change_by_user usernamefile3 Charlie charlie@localhost &&
248
+ P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
249
+ ! grep "git author.*does not match" actual &&
250
+
251
+ p4_check_commit_author usernamefile3 alice
252
+ ) &&
253
+ rm -rf "$git" && mkdir "$git"
254
+'
255
+
256
+
257
test_expect_success 'shutdown' '
258
pid=`pgrep -f p4d` &&
259
test -n "$pid" &&