Skip to content

Commit 38ecf75

Browse files
seraphiregitster
authored andcommitted
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>
1 parent cd1e0dc commit 38ecf75

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

Documentation/git-p4.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,41 @@ It can be bypassed with the `--no-verify` command line option.
387387

388388
One usage scenario is to run unit tests in the hook.
389389

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+
390425

391426
Rebase options
392427
~~~~~~~~~~~~~~

Documentation/githooks.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,52 @@ The exit status determines whether git will use the data from the
515515
hook to limit its search. On error, it will fall back to verifying
516516
all files and folders.
517517

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+
518564
p4-pre-submit
519565
~~~~~~~~~~~~~
520566

git-p4.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1589,7 +1589,7 @@ def __init__(self):
15891589
optparse.make_option("--disable-p4sync", dest="disable_p4sync", action="store_true",
15901590
help="Skip Perforce sync of p4/master after submit or shelve"),
15911591
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"),
15931593
]
15941594
self.description = """Submit changes from git to the perforce depot.\n
15951595
The `p4-pre-submit` hook is executed if it exists and is executable. It
@@ -1598,6 +1598,28 @@ def __init__(self):
15981598
from this script prevents `git-p4 submit` from launching.
15991599
16001600
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.
16011623
"""
16021624

16031625
self.usage += " [name of git branch to submit into perforce depot]"
@@ -2105,6 +2127,10 @@ def applyCommit(self, id):
21052127
submitted = False
21062128

21072129
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
21082134

21092135
if self.prepare_p4_only:
21102136
#
@@ -2144,6 +2170,12 @@ def applyCommit(self, id):
21442170
return True
21452171

21462172
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+
21472179
# read the edited message and submit
21482180
tmpFile = open(fileName, "rb")
21492181
message = tmpFile.read()
@@ -2181,6 +2213,7 @@ def applyCommit(self, id):
21812213

21822214
submitted = True
21832215

2216+
run_git_hook("p4-post-changelist")
21842217
finally:
21852218
# Revert changes if we skip this patch
21862219
if not submitted or self.shelve:

0 commit comments

Comments
 (0)