git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/1] add hook pre-p4-submit

From
Chen Bin <chenbin.sh@gmail.com>
Date
Jul 26, 2018, 13:41 UTC
Message-ID
<20180726134138.12183-1-chenbin.sh@gmail.com>
In-Reply-To
<20180725134345.8631-1-chenbin.sh@gmail.com>

Hook pre-p4-submit is executed before git-p4 actually submits code. If the hook exits with non-zero value, submit process will abort.

Signed-off-by: Chen Bin <chenbin.sh@gmail.com>
---
 Documentation/git-p4.txt   | 12 ++++++++++++
 Documentation/githooks.txt |  7 +++++++
 git-p4.py                  | 21 ++++++++++++++++++++-
 t/t9800-git-p4-basic.sh    | 26 ++++++++++++++++++++++++++
 4 files changed, 65 insertions(+), 1 deletion(-)
diff --git a/Documentation/git-p4.txt b/Documentation/git-p4.txt
index f0de3b891..f5d428ddf 100644
--- a/Documentation/git-p4.txt
+++ b/Documentation/git-p4.txt
@@ -374,6 +374,18 @@ These options can be used to modify 'git p4 submit' behavior.
     been submitted. Implies --disable-rebase. Can also be set with
     git-p4.disableP4Sync. Sync with origin/master still goes ahead if possible.
 
+Hook for submit
+~~~~~~~~~~~~~~~
+Hook `p4-pre-submit` is executed if it exists and is executable.
+The hook takes no parameter and nothing from standard input. Exiting with
+non-zero status from this script prevents `git-p4 submit` from launching.
+So nothing is touched when it exits with non-zero status.
+
+One usage scenario is to run unit tests in the hook.
+
+By default the hooks directory is `$GIT_DIR/hooks`, but that can be changed.
+See githooks(5) manpage for details about hooks.
+
 Rebase options
 ~~~~~~~~~~~~~~
 These options can be used to modify 'git p4 rebase' behavior.
diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt
index e3c283a17..22fcabbe2 100644
--- a/Documentation/githooks.txt
+++ b/Documentation/githooks.txt
@@ -485,6 +485,13 @@ The exit status determines whether git will use the data from the
 hook to limit its search.  On error, it will fall back to verifying
 all files and folders.
 
+p4-pre-submit
+~~~~~~~~~~~~~
+
+This hook is invoked by `git-p4 submit`. It takes no parameter and nothing
+from standard input. Exiting with non-zero status from this script prevent
+`git-p4 submit` from launching. Run `git-p4 submit --help` for details.
+
 GIT
 ---
 Part of the linkgit:git[1] suite
diff --git a/git-p4.py b/git-p4.py
index b449db1cc..f147a2d2f 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -1494,7 +1494,17 @@ def __init__(self):
                 optparse.make_option("--disable-p4sync", dest="disable_p4sync", action="store_true",
                                      help="Skip Perforce sync of p4/master after submit or shelve"),
         ]
-        self.description = "Submit changes from git to the perforce depot."
+        self.description = """Submit changes from git to the perforce depot.\n
+    Hook `p4-pre-submit` is executed if it exists and is executable.
+    The hook takes no parameter and nothing from standard input. Exiting with
+    non-zero status from this script prevents `git-p4 submit` from launching.
+    So nothing is touched when it exits with non-zero status.
+
+    One usage scenario is to run unit tests in the hook.
+
+    By default the hooks directory is `$GIT_DIR/hooks`, but that can be changed.
+    See githooks(5) manpage for details about hooks."""
+
         self.usage += " [name of git branch to submit into perforce depot]"
         self.origin = ""
         self.detectRenames = False
@@ -2303,6 +2313,15 @@ def run(self, args):
             sys.exit("number of commits (%d) must match number of shelved changelist (%d)" %
                      (len(commits), num_shelves))
 
+        hooks_path = gitConfig("core.hooksPath")
+        if len(hooks_path) > 0:
+            hook_file = os.path.join(hooks_path, "p4-pre-submit")
+        else:
+            hook_file = os.path.join(read_pipe("git rev-parse --git-dir").strip(), "hooks", "p4-pre-submit")
+
+        if os.path.isfile(hook_file) and os.access(hook_file, os.X_OK) and subprocess.call([hook_file]) != 0:
+            sys.exit(1)
+
         #
         # Apply the commits, one at a time.  On failure, ask if should
         # continue to try the rest of the patches, or quit.
diff --git a/t/t9800-git-p4-basic.sh b/t/t9800-git-p4-basic.sh
index 4849edc4e..8457ff617 100755
--- a/t/t9800-git-p4-basic.sh
+++ b/t/t9800-git-p4-basic.sh
@@ -261,6 +261,32 @@ test_expect_success 'unresolvable host in P4PORT should display error' '
 	)
 '
 
+# Test following scenarios:
+#   - Without hook ".git/hooks/p4-pre-submit", submit should continue
+#   - With hook returning 0, submit should continue
+#   - With hook returning 1, submit should abort
+test_expect_success 'run hook p4-pre-submit before submit' '
+	test_when_finished cleanup_git &&
+	git p4 clone --dest="$git" //depot &&
+	(
+		cd "$git" &&
+		echo "hello world" >hello.txt &&
+		git add hello.txt &&
+		git commit -m "add hello.txt" &&
+		git config git-p4.skipSubmitEdit true &&
+		git p4 submit --dry-run | grep "Would apply" &&
+		mkdir -p .git/hooks &&
+		write_script .git/hooks/p4-pre-submit <<-\EOF &&
+		exit 0
+		EOF
+		git p4 submit --dry-run >out &&  grep "Would apply" out &&
+		write_script .git/hooks/p4-pre-submit <<-\EOF &&
+		exit 1
+		EOF
+		git p4 submit --dry-run >out && grep "Would apply" out || echo "Abort submit"
+	)
+'
+
 test_expect_success 'submit from detached head' '
 	test_when_finished cleanup_git &&
 	git p4 clone --dest="$git" //depot &&
-- 
2.18.0
Previous: chen binNext: Junio C Hamano
Message 12 of 27 in “add hook pre-p4-submit”
  1. 1/1 add hook pre-p4-submitChen Bin, Jul 23, 2018
  2. Luke DiamandJul 25, 2018
  3. Ævar Arnfjörð BjarmasonJul 25, 2018
  4. chen binJul 25, 2018
  5. 1/1 add hook pre-p4-submitChen Bin, Jul 25, 2018
  6. Eric SunshineJul 25, 2018
  7. Junio C HamanoJul 25, 2018
  8. chen binJul 26, 2018
  9. Eric SunshineJul 26, 2018
  10. Luke DiamandJul 26, 2018
  11. chen binJul 27, 2018
  12. 1/1 add hook pre-p4-submitChen Bin, Jul 26, 2018
  13. Junio C HamanoJul 26, 2018
  14. Eric SunshineJul 26, 2018
  15. 1/1 Add the `p4-pre-submit` hookChen Bin, Jul 27, 2018
  16. Eric SunshineJul 27, 2018
  17. Junio C HamanoJul 30, 2018
  18. Luke DiamandJul 30, 2018
  19. Junio C HamanoJul 30, 2018
  20. SZEDER GáborJul 31, 2018
  21. Luke DiamandJul 31, 2018
  22. Junio C HamanoJul 31, 2018
  23. Luke DiamandJul 31, 2018
  24. chen binAug 1, 2018
  25. Junio C HamanoAug 1, 2018
  26. 1/1 Add the `p4-pre-submit` hookChen Bin, Aug 1, 2018
  27. Junio C HamanoAug 1, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.