git p4: refactor diffOpts calculation

P4Submit.applyCommit() To avoid recalculating the same diffOpts for each commit, move it out of applyCommit() and into the top-level run(). Also fix a bug in that code which interpreted the value of detectRenames as a string rather than as a boolean. [pw: fix documentation, rearrange code a bit] Signed-off-by: Gary Gibbons <ggibbons@perforce.com> Signed-off-by: Pete Wyckoff <pw@padd.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Gary Gibbons committed Jul 4, 2012 at 09:40 UTC 84cb00036fb59cf0ff305dcc27551f0519b5098c
2 files changed +38 -24
Documentation/git-p4.txt
+6 -4
@@ -255,7 +255,7 @@ These options can be used to modify 'git p4 submit' behavior.
255 p4. By default, this is the most recent p4 commit reachable
256 from 'HEAD'.
257
258 --M[<n>]::
258 +-M::
259 Detect renames. See linkgit:git-diff[1]. Renames will be
260 represented in p4 using explicit 'move' operations. There
261 is no corresponding option to detect copies, but there are
@@ -465,13 +465,15 @@ git-p4.useClientSpec::
465 Submit variables
466 ~~~~~~~~~~~~~~~~
467 git-p4.detectRenames::
468 - Detect renames. See linkgit:git-diff[1].
468 + Detect renames. See linkgit:git-diff[1]. This can be true,
469 + false, or a score as expected by 'git diff -M'.
470
471 git-p4.detectCopies::
471 - Detect copies. See linkgit:git-diff[1].
472 + Detect copies. See linkgit:git-diff[1]. This can be true,
473 + false, or a score as expected by 'git diff -C'.
474
475 git-p4.detectCopiesHarder::
474 - Detect copies harder. See linkgit:git-diff[1].
476 + Detect copies harder. See linkgit:git-diff[1]. A boolean.
477
478 git-p4.preserveUser::
479 On submit, re-author changes to reflect the git author,
git-p4.py
+32 -20
@@ -1046,27 +1046,8 @@ class P4Submit(Command, P4UserMap):
1046
1047 (p4User, gitEmail) = self.p4UserForCommit(id)
1048
1049 - if not self.detectRenames:
1050 - # If not explicitly set check the config variable
1051 - self.detectRenames = gitConfig("git-p4.detectRenames")
1052 -
1053 - if self.detectRenames.lower() == "false" or self.detectRenames == "":
1054 - diffOpts = ""
1055 - elif self.detectRenames.lower() == "true":
1056 - diffOpts = "-M"
1057 - else:
1058 - diffOpts = "-M%s" % self.detectRenames
1059 -
1060 - detectCopies = gitConfig("git-p4.detectCopies")
1061 - if detectCopies.lower() == "true":
1062 - diffOpts += " -C"
1063 - elif detectCopies != "" and detectCopies.lower() != "false":
1064 - diffOpts += " -C%s" % detectCopies
1049
1066 - if gitConfig("git-p4.detectCopiesHarder", "--bool") == "true":
1067 - diffOpts += " --find-copies-harder"
1068 -
1069 - diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (diffOpts, id, id))
1050 + diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (self.diffOpts, id, id))
1051 filesToAdd = set()
1052 filesToDelete = set()
1053 editedFiles = set()
@@ -1433,6 +1414,37 @@ class P4Submit(Command, P4UserMap):
1414 if self.preserveUser:
1415 self.checkValidP4Users(commits)
1416
1417 + #
1418 + # Build up a set of options to be passed to diff when
1419 + # submitting each commit to p4.
1420 + #
1421 + if self.detectRenames:
1422 + # command-line -M arg
1423 + self.diffOpts = "-M"
1424 + else:
1425 + # If not explicitly set check the config variable
1426 + detectRenames = gitConfig("git-p4.detectRenames")
1427 +
1428 + if detectRenames.lower() == "false" or detectRenames == "":
1429 + self.diffOpts = ""
1430 + elif detectRenames.lower() == "true":
1431 + self.diffOpts = "-M"
1432 + else:
1433 + self.diffOpts = "-M%s" % detectRenames
1434 +
1435 + # no command-line arg for -C or --find-copies-harder, just
1436 + # config variables
1437 + detectCopies = gitConfig("git-p4.detectCopies")
1438 + if detectCopies.lower() == "false" or detectCopies == "":
1439 + pass
1440 + elif detectCopies.lower() == "true":
1441 + self.diffOpts += " -C"
1442 + else:
1443 + self.diffOpts += " -C%s" % detectCopies
1444 +
1445 + if gitConfig("git-p4.detectCopiesHarder", "--bool") == "true":
1446 + self.diffOpts += " --find-copies-harder"
1447 +
1448 while len(commits) > 0:
1449 commit = commits[0]
1450 commits = commits[1:]