Skip to content

Commit

Permalink
git-p4: warn if git authorship won't be retained
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Luke Diamand authored and Junio C Hamano committed May 13, 2011
1 parent ecdba36 commit 848de9c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
33 changes: 31 additions & 2 deletions contrib/fast-import/git-p4
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ class P4Submit(Command, P4UserMap):
self.verbose = False
self.preserveUser = gitConfig("git-p4.preserveUser").lower() == "true"
self.isWindows = (platform.system() == "Windows")
self.myP4UserId = None

def check(self):
if len(p4CmdList("opened ...")) > 0:
Expand Down Expand Up @@ -721,6 +722,25 @@ class P4Submit(Command, P4UserMap):
return 1
return 0

def p4UserId(self):
if self.myP4UserId:
return self.myP4UserId

results = p4CmdList("user -o")
for r in results:
if r.has_key('User'):
self.myP4UserId = r['User']
return r['User']
die("Could not find your p4 user id")

def p4UserIsMe(self, p4User):
# return True if the given p4 user is actually me
me = self.p4UserId()
if not p4User or p4User != me:
return False
else:
return True

def prepareSubmitTemplate(self):
# remove lines in the Files section that show changes to files outside the depot path we're committing into
template = ""
Expand Down Expand Up @@ -750,8 +770,7 @@ class P4Submit(Command, P4UserMap):
def applyCommit(self, id):
print "Applying %s" % (read_pipe("git log --max-count=1 --pretty=oneline %s" % id))

if self.preserveUser:
(p4User, gitEmail) = self.p4UserForCommit(id)
(p4User, gitEmail) = self.p4UserForCommit(id)

if not self.detectRenames:
# If not explicitly set check the config variable
Expand Down Expand Up @@ -890,6 +909,11 @@ class P4Submit(Command, P4UserMap):
newdiff += "+" + line
f.close()

if self.checkAuthorship and not self.p4UserIsMe(p4User):
submitTemplate += "######## git author %s does not match your p4 account.\n" % gitEmail
submitTemplate += "######## Use git-p4 option --preserve-user to modify authorship\n"
submitTemplate += "######## Use git-p4 config git-p4.skipUserNameCheck hides this message.\n"

separatorLine = "######## everything below this line is just the diff #######\n"

[handle, fileName] = tempfile.mkstemp()
Expand Down Expand Up @@ -1001,6 +1025,11 @@ class P4Submit(Command, P4UserMap):
commits.append(line.strip())
commits.reverse()

if self.preserveUser or (gitConfig("git-p4.skipUserNameCheck") == "true"):
self.checkAuthorship = False
else:
self.checkAuthorship = True

if self.preserveUser:
self.checkValidP4Users(commits)

Expand Down
7 changes: 7 additions & 0 deletions contrib/fast-import/git-p4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ stop. With allowMissingPerforceUsers set to true, git-p4 will use the
current user (i.e. the behavior without --preserve-user) and carry on with
the perforce commit.

git-p4.skipUserNameCheck

git config [--global] git-p4.skipUserNameCheck false

When submitting, git-p4 checks that the git commits are authored by the current
p4 user, and warns if they are not. This disables the check.

Implementation Details...
=========================

Expand Down
41 changes: 41 additions & 0 deletions t/t9800-git-p4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ p4_check_commit_author() {
fi
}

make_change_by_user() {
file=$1 name=$2 email=$3 &&
echo "username: a change by $name" >>"$file" &&
git add "$file" &&
git commit --author "$name <$email>" -m "a change by $name"
}

# Test username support, submitting as user 'alice'
test_expect_success 'preserve users' '
p4_add_user alice Alice &&
Expand Down Expand Up @@ -213,6 +220,40 @@ test_expect_success 'preserve user where author is unknown to p4' '
rm -rf "$git" && mkdir "$git"
'

# If we're *not* using --preserve-user, git-p4 should warn if we're submitting
# changes that are not all ours.
# Test: user in p4 and user unknown to p4.
# Test: warning disabled and user is the same.
test_expect_success 'not preserving user with mixed authorship' '
"$GITP4" clone --dest="$git" //depot &&
(
cd "$git" &&
git config git-p4.skipSubmitEditCheck true &&
p4_add_user derek Derek &&
make_change_by_user usernamefile3 Derek derek@localhost &&
P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
grep "git author derek@localhost does not match" actual &&
make_change_by_user usernamefile3 Charlie charlie@localhost &&
P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
grep "git author charlie@localhost does not match" actual &&
make_change_by_user usernamefile3 alice alice@localhost &&
P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
! grep "git author.*does not match" actual &&
git config git-p4.skipUserNameCheck true &&
make_change_by_user usernamefile3 Charlie charlie@localhost &&
P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
! grep "git author.*does not match" actual &&
p4_check_commit_author usernamefile3 alice
) &&
rm -rf "$git" && mkdir "$git"
'


test_expect_success 'shutdown' '
pid=`pgrep -f p4d` &&
test -n "$pid" &&
Expand Down

0 comments on commit 848de9c

Please sign in to comment.