[v2,3/4] git-p4: add hook p4-pre-edit-changelist
diff mbox series

Message ID 1bdcdc4c18b9d8fa3cc5a9c846fb855182330d05.1580507895.git.gitgitgadget@gmail.com
State New
Headers show
  • git-p4: add hook p4-pre-edit-changelist
Related show

Commit Message

Philippe Blain via GitGitGadget Jan. 31, 2020, 9:58 p.m. UTC
From: Ben Keene <seraphire@gmail.com>

Add an additional hook to the git-p4 command to allow a hook to modify
the text of the changelist prior to displaying the p4editor command.

This hook will be called prior to checking for the flag

The hook is optional, if it does not exist, it will be skipped.

The hook takes a single parameter, the filename of the temporary file
that contains the P4 submit text.

The hook should return a zero exit code on success or a non-zero exit
code on failure.  If the hook returns a non-zero exit code, git-p4
will revert the P4 edits by calling p4_revert(f) on each file that was
flagged as edited and then it will return False so the calling method
may continue as it does in existing failure cases.

Signed-off-by: Ben Keene <seraphire@gmail.com>
 git-p4.py | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff mbox series

diff --git a/git-p4.py b/git-p4.py
index 4e481b3b55..61cfd1c1ae 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -2030,6 +2030,17 @@  def applyCommit(self, id):
+        # Run the pre-edit hook to allow programmatic update to the changelist
+        hooks_path = gitConfig("core.hooksPath")
+        if len(hooks_path) <= 0:
+            hooks_path = os.path.join(os.environ.get("GIT_DIR", ".git"), "hooks")
+        hook_file = os.path.join(hooks_path, "p4-pre-edit-changelist")
+        if os.path.isfile(hook_file) and os.access(hook_file, os.X_OK) and subprocess.call([hook_file, fileName]) != 0:
+            for f in editedFiles:
+                p4_revert(f)
+            return False
         if self.prepare_p4_only:
             # Leave the p4 tree prepared, and the submit template around