@matheusbsbs / matheus-bagliano / commits / 38ecf75244
Fork 0

git-p4: add p4 submit hooks

The git command "commit" supports a number of hooks that support changing the behavior of the commit command. The git-p4.py program only has one existing hook, "p4-pre-submit". This command occurs early in the process. There are no hooks in the process flow for modifying the P4 changelist text programmatically. Adds 3 new hooks to git-p4.py to the submit option. The new hooks are: * p4-prepare-changelist - Execute this hook after the changelist file has been created. The hook will be executed even if the --prepare-p4-only option is set. This hook ignores the --no-verify option in keeping with the existing behavior of git commit. * p4-changelist - Execute this hook after the user has edited the changelist. Do not execute this hook if the user has selected the --prepare-p4-only option. This hook will honor the --no-verify, following the conventions of git commit. * p4-post-changelist - Execute this hook after the P4 submission process has completed successfully. This hook takes no parameters and is executed regardless of the --no-verify option. It's return value will not be checked. The calls to the new hooks: p4-prepare-changelist, p4-changelist, and p4-post-changelist should all be called inside the try-finally block. Signed-off-by: Ben Keene <seraphire@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ben Keene committed Feb 14, 2020 at 14:44 UTC 38ecf75244c4cab93ecb201afff8980c29f56ce8
3 files changed +115 -1
Documentation/git-p4.txt
+35
@@ -387,6 +387,41 @@ It can be bypassed with the `--no-verify` command line option.
387
388 One usage scenario is to run unit tests in the hook.
389
390 +p4-prepare-changelist
391 +~~~~~~~~~~~~~~~~~~~~~
392 +
393 +The `p4-prepare-changelist` hook is executed right after preparing
394 +the default changelist message and before the editor is started.
395 +It takes one parameter, the name of the file that contains the
396 +changelist text. Exiting with a non-zero status from the script
397 +will abort the process.
398 +
399 +The purpose of the hook is to edit the message file in place,
400 +and it is not supressed by the `--no-verify` option. This hook
401 +is called even if `--prepare-p4-only` is set.
402 +
403 +p4-changelist
404 +~~~~~~~~~~~~~
405 +
406 +The `p4-changelist` hook is executed after the changelist
407 +message has been edited by the user. It can be bypassed with the
408 +`--no-verify` option. It takes a single parameter, the name
409 +of the file that holds the proposed changelist text. Exiting
410 +with a non-zero status causes the command to abort.
411 +
412 +The hook is allowed to edit the changelist file and can be used
413 +to normalize the text into some project standard format. It can
414 +also be used to refuse the Submit after inspect the message file.
415 +
416 +p4-post-changelist
417 +~~~~~~~~~~~~~~~~~~
418 +
419 +The `p4-post-changelist` hook is invoked after the submit has
420 +successfully occured in P4. It takes no parameters and is meant
421 +primarily for notification and cannot affect the outcome of the
422 +git p4 submit action.
423 +
424 +
425
426 Rebase options
427 ~~~~~~~~~~~~~~
Documentation/githooks.txt
+46
@@ -515,6 +515,52 @@ The exit status determines whether git will use the data from the
515 hook to limit its search. On error, it will fall back to verifying
516 all files and folders.
517
518 +p4-changelist
519 +~~~~~~~~~~~~~
520 +
521 +This hook is invoked by `git-p4 submit`.
522 +
523 +The `p4-changelist` hook is executed after the changelist
524 +message has been edited by the user. It can be bypassed with the
525 +`--no-verify` option. It takes a single parameter, the name
526 +of the file that holds the proposed changelist text. Exiting
527 +with a non-zero status causes the command to abort.
528 +
529 +The hook is allowed to edit the changelist file and can be used
530 +to normalize the text into some project standard format. It can
531 +also be used to refuse the Submit after inspect the message file.
532 +
533 +Run `git-p4 submit --help` for details.
534 +
535 +p4-prepare-changelist
536 +~~~~~~~~~~~~~~~~~~~~~
537 +
538 +This hook is invoked by `git-p4 submit`.
539 +
540 +The `p4-prepare-changelist` hook is executed right after preparing
541 +the default changelist message and before the editor is started.
542 +It takes one parameter, the name of the file that contains the
543 +changelist text. Exiting with a non-zero status from the script
544 +will abort the process.
545 +
546 +The purpose of the hook is to edit the message file in place,
547 +and it is not supressed by the `--no-verify` option. This hook
548 +is called even if `--prepare-p4-only` is set.
549 +
550 +Run `git-p4 submit --help` for details.
551 +
552 +p4-post-changelist
553 +~~~~~~~~~~~~~~~~~~
554 +
555 +This hook is invoked by `git-p4 submit`.
556 +
557 +The `p4-post-changelist` hook is invoked after the submit has
558 +successfully occured in P4. It takes no parameters and is meant
559 +primarily for notification and cannot affect the outcome of the
560 +git p4 submit action.
561 +
562 +Run `git-p4 submit --help` for details.
563 +
564 p4-pre-submit
565 ~~~~~~~~~~~~~
566
git-p4.py
+34 -1
@@ -1589,7 +1589,7 @@ class P4Submit(Command, P4UserMap):
1589 optparse.make_option("--disable-p4sync", dest="disable_p4sync", action="store_true",
1590 help="Skip Perforce sync of p4/master after submit or shelve"),
1591 optparse.make_option("--no-verify", dest="no_verify", action="store_true",
1592 - help="Bypass p4-pre-submit"),
1592 + help="Bypass p4-pre-submit and p4-changelist hooks"),
1593 ]
1594 self.description = """Submit changes from git to the perforce depot.\n
1595 The `p4-pre-submit` hook is executed if it exists and is executable. It
@@ -1598,6 +1598,28 @@ class P4Submit(Command, P4UserMap):
1598 from this script prevents `git-p4 submit` from launching.
1599
1600 One usage scenario is to run unit tests in the hook.
1601 +
1602 + The `p4-prepare-changelist` hook is executed right after preparing the default
1603 + changelist message and before the editor is started. It takes one parameter,
1604 + the name of the file that contains the changelist text. Exiting with a non-zero
1605 + status from the script will abort the process.
1606 +
1607 + The purpose of the hook is to edit the message file in place, and it is not
1608 + supressed by the `--no-verify` option. This hook is called even if
1609 + `--prepare-p4-only` is set.
1610 +
1611 + The `p4-changelist` hook is executed after the changelist message has been
1612 + edited by the user. It can be bypassed with the `--no-verify` option. It
1613 + takes a single parameter, the name of the file that holds the proposed
1614 + changelist text. Exiting with a non-zero status causes the command to abort.
1615 +
1616 + The hook is allowed to edit the changelist file and can be used to normalize
1617 + the text into some project standard format. It can also be used to refuse the
1618 + Submit after inspect the message file.
1619 +
1620 + The `p4-post-changelist` hook is invoked after the submit has successfully
1621 + occured in P4. It takes no parameters and is meant primarily for notification
1622 + and cannot affect the outcome of the git p4 submit action.
1623 """
1624
1625 self.usage += " [name of git branch to submit into perforce depot]"
@@ -2105,6 +2127,10 @@ class P4Submit(Command, P4UserMap):
2127 submitted = False
2128
2129 try:
2130 + # Allow the hook to edit the changelist text before presenting it
2131 + # to the user.
2132 + if not run_git_hook("p4-prepare-changelist", [fileName]):
2133 + return False
2134
2135 if self.prepare_p4_only:
2136 #
@@ -2144,6 +2170,12 @@ class P4Submit(Command, P4UserMap):
2170 return True
2171
2172 if self.edit_template(fileName):
2173 + if not self.no_verify:
2174 + if not run_git_hook("p4-changelist", [fileName]):
2175 + print("The p4-changelist hook failed.")
2176 + sys.stdout.flush()
2177 + return False
2178 +
2179 # read the edited message and submit
2180 tmpFile = open(fileName, "rb")
2181 message = tmpFile.read()
@@ -2181,6 +2213,7 @@ class P4Submit(Command, P4UserMap):
2213
2214 submitted = True
2215
2216 + run_git_hook("p4-post-changelist")
2217 finally:
2218 # Revert changes if we skip this patch
2219 if not submitted or self.shelve: