git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Chen Bin <chenbin.sh@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 1/1] add hook pre-p4-submit
Date: Thu, 26 Jul 2018 09:49:08 -0700	[thread overview]
Message-ID: <xmqq36w6rlh7.fsf@gitster-ct.c.googlers.com> (raw)
In-Reply-To: <20180726134138.12183-1-chenbin.sh@gmail.com> (Chen Bin's message of "Thu, 26 Jul 2018 23:41:38 +1000")

Chen Bin <chenbin.sh@gmail.com> writes:

> +Hook for submit
> +~~~~~~~~~~~~~~~
> +Hook `p4-pre-submit` is executed if it exists and is executable.

Just a lang nit, but "git grep pre-commit" shows me lines like

  Documentation/git-commit.txt:   This option bypasses the pre-commit and commit-msg hooks.

to tell me that the above is better phrased like

	The `p4-pre-submit` hook is executed...

The same comment applies to the patch title, the first sentence in
the log message and in-code ".description" field in git-py.py.

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

Isn't the last sentence redundant, saying only what the second
sentence already said?

> +
> +One usage scenario is to run unit tests in the hook.

Good thing to mention, I guess.

> +By default the hooks directory is `$GIT_DIR/hooks`, but that can be changed.
> +See githooks(5) manpage for details about hooks.

I do not think this is particularly a good change; it forces us to
maintain this sentence copied from githooks.txt every time the
original has to change, and documentation for no other command
duplicates it.

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

Hmmm, OK.

>  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."""

How do you plan to keep this in sync with the documentation?
Perhaps the last sentence of the new paragraph you added to
Documentation/githooks.txt should read

	See `git p4 --help` for details.

Then this hunk can be eliminated, I would think.

> @@ -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")
> +

Isn't the GIT_DIR available as self.gitdir at this point in the
code, prepared by main() before this method is run?

It may be a style thing, but I somehow would find it easier to
understand if you wrote it like so:

	hooks_path = gitConfig("...")
	if len(hooks_path) <= 0:
		hooks_path = os.path.join(self.gitdir, "hooks")
	hook_file = os.path.join(hooks_path, "p4-pre-submit")

The point being hooks_path always point at the directory that holds
hooks with or without configuration, and hook_file is set based on
that to the same "p4-pre-submit" without any potential of typo
getting in to make the two cases diverge.

> +        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" &&

"git p4" on the upstream side of a pipe?

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

Have each of these two on its own line, i.e.

	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"

What is this last "|| echo I always succeed" about?  

Do you want to make sure "git p4 submit" exit with non-zero exit
status *and* its output does not say "Would apply"?  The way to
write that would be

		test_must_fail git p4 submit --dry-run >out &&
		! grep "Would apply" out

Then "git p4 submit --dry-run" that exits with 0 even though its
presubmit hook exits with 1 would fail to meet the expectation that
the command must fail and would be caught as an error.  If it exits
with non-zero status, but still write "Would apply" to its standard
output stream, it would also be caught as an error as "grep" would
succeed.

> +	)
> +'
> +
>  test_expect_success 'submit from detached head' '
>  	test_when_finished cleanup_git &&
>  	git p4 clone --dest="$git" //depot &&


  reply	other threads:[~2018-07-26 16:49 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-23 11:27 [PATCH 1/1] add hook pre-p4-submit Chen Bin
2018-07-25  8:37 ` Luke Diamand
2018-07-25  9:14   ` Ævar Arnfjörð Bjarmason
2018-07-25 11:10     ` chen bin
2018-07-25 13:43   ` Chen Bin
2018-07-25 20:00     ` Eric Sunshine
2018-07-25 20:21       ` Junio C Hamano
2018-07-26  2:08       ` chen bin
2018-07-26  9:21         ` Eric Sunshine
2018-07-26 21:09           ` Luke Diamand
2018-07-27  1:31             ` chen bin
2018-07-26 13:41     ` Chen Bin
2018-07-26 16:49       ` Junio C Hamano [this message]
2018-07-26 21:20         ` Eric Sunshine
2018-07-27 11:22         ` [PATCH 1/1] Add the `p4-pre-submit` hook Chen Bin
2018-07-27 18:15           ` Eric Sunshine
2018-07-30 18:07           ` Junio C Hamano
2018-07-30 18:48             ` Luke Diamand
2018-07-30 21:01               ` Junio C Hamano
2018-07-31  8:46           ` SZEDER Gábor
2018-07-31 13:40             ` Luke Diamand
2018-07-31 14:40               ` Junio C Hamano
2018-07-31 19:54                 ` Luke Diamand
2018-08-01 14:57                   ` chen bin
2018-08-01 15:10                     ` Junio C Hamano
2018-08-01 15:54                       ` Chen Bin
2018-08-01 17:41                         ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqq36w6rlh7.fsf@gitster-ct.c.googlers.com \
    --to=gitster@pobox.com \
    --cc=chenbin.sh@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).