git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] commit: skip discarding the index if there is no pre-commit hook
@ 2017-08-10 18:54 Kevin Willford
  2017-08-10 19:16 ` Jeff King
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Kevin Willford @ 2017-08-10 18:54 UTC (permalink / raw)
  To: git; +Cc: peff, gitster, peartben, Kevin Willford

If there is not a pre-commit hook, there is no reason to discard
the index and reread it.

This change checks to presence of a pre-commit hook and then only
discards the index if there was one.

Signed-off-by: Kevin Willford <kewillf@microsoft.com>
---
 builtin/commit.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index e7a2cb6285..443949d87b 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -671,12 +671,22 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	const char *hook_arg2 = NULL;
 	int clean_message_contents = (cleanup_mode != CLEANUP_NONE);
 	int old_display_comment_prefix;
+	const char *precommit_hook = NULL;
 
 	/* This checks and barfs if author is badly specified */
 	determine_author_info(author_ident);
 
-	if (!no_verify && run_commit_hook(use_editor, index_file, "pre-commit", NULL))
-		return 0;
+
+	if (!no_verify) {
+		/*
+		 * Check to see if there is a pre-commit hook
+		 * If there not one we can skip discarding the index later on
+		 */
+		precommit_hook = find_hook("pre-commit");
+		if (precommit_hook &&
+		    run_commit_hook(use_editor, index_file, "pre-commit", NULL))
+			return 0;
+	}
 
 	if (squash_message) {
 		/*
@@ -940,12 +950,15 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 		return 0;
 	}
 
-	/*
-	 * Re-read the index as pre-commit hook could have updated it,
-	 * and write it out as a tree.  We must do this before we invoke
-	 * the editor and after we invoke run_status above.
-	 */
-	discard_cache();
+	if (!no_verify && precommit_hook) {
+		/*
+		 * Re-read the index as pre-commit hook could have updated it,
+		 * and write it out as a tree.  We must do this before we invoke
+		 * the editor and after we invoke run_status above.
+		 */
+		discard_cache();
+	}
+
 	read_cache_from(index_file);
 	if (update_main_cache_tree(0)) {
 		error(_("Error building trees"));
-- 
2.14.0.rc0.286.g44127d70e4


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] commit: skip discarding the index if there is no pre-commit hook
  2017-08-10 18:54 [PATCH] commit: skip discarding the index if there is no pre-commit hook Kevin Willford
@ 2017-08-10 19:16 ` Jeff King
  2017-08-10 22:22 ` Junio C Hamano
  2017-08-14 21:54 ` [PATCH v2] " Kevin Willford
  2 siblings, 0 replies; 8+ messages in thread
From: Jeff King @ 2017-08-10 19:16 UTC (permalink / raw)
  To: Kevin Willford; +Cc: git, gitster, peartben, Kevin Willford

On Thu, Aug 10, 2017 at 02:54:16PM -0400, Kevin Willford wrote:

> If there is not a pre-commit hook, there is no reason to discard
> the index and reread it.
> 
> This change checks to presence of a pre-commit hook and then only
> discards the index if there was one.

Sounds like a smart optimization.

> diff --git a/builtin/commit.c b/builtin/commit.c
> index e7a2cb6285..443949d87b 100644
> --- a/builtin/commit.c
> +++ b/builtin/commit.c
> @@ -671,12 +671,22 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
>  	const char *hook_arg2 = NULL;
>  	int clean_message_contents = (cleanup_mode != CLEANUP_NONE);
>  	int old_display_comment_prefix;
> +	const char *precommit_hook = NULL;

The return value from find_hook() points to storage that may later be
overwritten or even freed.  I know your patch doesn't actually look at
the contents of precommit_hook, but it seems like it's setting up a
potential bomb for somebody later.

Can we make this:

  int have_precommit_hook = 0;
  ...
  have_precommit_hook = !!find_hook("pre-commit");

? Though see below.

> -	if (!no_verify && run_commit_hook(use_editor, index_file, "pre-commit", NULL))
> -		return 0;
> +
> +	if (!no_verify) {
> +		/*
> +		 * Check to see if there is a pre-commit hook
> +		 * If there not one we can skip discarding the index later on
> +		 */
> +		precommit_hook = find_hook("pre-commit");
> +		if (precommit_hook &&
> +		    run_commit_hook(use_editor, index_file, "pre-commit", NULL))
> +			return 0;
> +	}

We'll find the hook again in run_commit_hook(), but it's not so
expensive that it's worth trying to pass down the hook path we found
(and if we switch to an integer flag we don't have the path anyway ;) ).

But note that we don't even care about precommit_hook here. We could
just call run_commit_hook() regardless. We only care later whether we
ran it or not. So we could drop this hunk and the precommit_hook
variable entirely, and...

> @@ -940,12 +950,15 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
>  		return 0;
>  	}
>  
> -	/*
> -	 * Re-read the index as pre-commit hook could have updated it,
> -	 * and write it out as a tree.  We must do this before we invoke
> -	 * the editor and after we invoke run_status above.
> -	 */
> -	discard_cache();
> +	if (!no_verify && precommit_hook) {
> +		/*
> +		 * Re-read the index as pre-commit hook could have updated it,
> +		 * and write it out as a tree.  We must do this before we invoke
> +		 * the editor and after we invoke run_status above.
> +		 */
> +		discard_cache();
> +	}

Just make this:

  if (!no_verify && find_hook("pre-commit"))
     ... discard cache ...

That is racy if somebody removes the hook while we're running (so is
your patch, but in the opposite direction). What we really want to know
is "did we run the hook" and annoyingly run_hook_ve() doesn't tell us
that.  So I think the most robust solution would be refactoring that to
pass out the information, and then use the flag here. But I'm not sure
it's actually worth worrying about such a race in practice.

-Peff

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] commit: skip discarding the index if there is no pre-commit hook
  2017-08-10 18:54 [PATCH] commit: skip discarding the index if there is no pre-commit hook Kevin Willford
  2017-08-10 19:16 ` Jeff King
@ 2017-08-10 22:22 ` Junio C Hamano
  2017-08-14 21:54 ` [PATCH v2] " Kevin Willford
  2 siblings, 0 replies; 8+ messages in thread
From: Junio C Hamano @ 2017-08-10 22:22 UTC (permalink / raw)
  To: Kevin Willford; +Cc: git, peff, peartben, Kevin Willford

Kevin Willford <kcwillford@gmail.com> writes:

> If there is not a pre-commit hook, there is no reason to discard
> the index and reread it.
>
> This change checks to presence of a pre-commit hook and then only
> discards the index if there was one.
>
> Signed-off-by: Kevin Willford <kewillf@microsoft.com>
> ---

Peff already has done a good job reviewing the patch text, and I
agree that this is a worthwhile optimization.

Could Microsoft folks all make sure that their signed-off-by lines
match their From: address (or leave an in-body From: to override
the From: address your MUA places in your messages)?

Thanks.

>  builtin/commit.c | 29 +++++++++++++++++++++--------
>  1 file changed, 21 insertions(+), 8 deletions(-)
>
> diff --git a/builtin/commit.c b/builtin/commit.c
> index e7a2cb6285..443949d87b 100644
> --- a/builtin/commit.c
> +++ b/builtin/commit.c
> @@ -671,12 +671,22 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
>  	const char *hook_arg2 = NULL;
>  	int clean_message_contents = (cleanup_mode != CLEANUP_NONE);
>  	int old_display_comment_prefix;
> +	const char *precommit_hook = NULL;
>  
>  	/* This checks and barfs if author is badly specified */
>  	determine_author_info(author_ident);
>  
> -	if (!no_verify && run_commit_hook(use_editor, index_file, "pre-commit", NULL))
> -		return 0;
> +
> +	if (!no_verify) {
> +		/*
> +		 * Check to see if there is a pre-commit hook
> +		 * If there not one we can skip discarding the index later on
> +		 */
> +		precommit_hook = find_hook("pre-commit");
> +		if (precommit_hook &&
> +		    run_commit_hook(use_editor, index_file, "pre-commit", NULL))
> +			return 0;
> +	}
>  
>  	if (squash_message) {
>  		/*
> @@ -940,12 +950,15 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
>  		return 0;
>  	}
>  
> -	/*
> -	 * Re-read the index as pre-commit hook could have updated it,
> -	 * and write it out as a tree.  We must do this before we invoke
> -	 * the editor and after we invoke run_status above.
> -	 */
> -	discard_cache();
> +	if (!no_verify && precommit_hook) {
> +		/*
> +		 * Re-read the index as pre-commit hook could have updated it,
> +		 * and write it out as a tree.  We must do this before we invoke
> +		 * the editor and after we invoke run_status above.
> +		 */
> +		discard_cache();
> +	}
> +
>  	read_cache_from(index_file);
>  	if (update_main_cache_tree(0)) {
>  		error(_("Error building trees"));

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2] commit: skip discarding the index if there is no pre-commit hook
  2017-08-10 18:54 [PATCH] commit: skip discarding the index if there is no pre-commit hook Kevin Willford
  2017-08-10 19:16 ` Jeff King
  2017-08-10 22:22 ` Junio C Hamano
@ 2017-08-14 21:54 ` Kevin Willford
  2017-08-14 22:13   ` Jeff King
  2 siblings, 1 reply; 8+ messages in thread
From: Kevin Willford @ 2017-08-14 21:54 UTC (permalink / raw)
  To: git; +Cc: peff, gitster, Kevin Willford

If there is not a pre-commit hook, there is no reason to discard
the index and reread it.

This change checks to presence of a pre-commit hook and then only
discards the index if there was one.

Signed-off-by: Kevin Willford <kewillf@microsoft.com>
---
 builtin/commit.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index e7a2cb6285..ab71b93518 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -940,12 +940,15 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 		return 0;
 	}
 
-	/*
-	 * Re-read the index as pre-commit hook could have updated it,
-	 * and write it out as a tree.  We must do this before we invoke
-	 * the editor and after we invoke run_status above.
-	 */
-	discard_cache();
+	if (!no_verify && find_hook("pre-commit")) {
+		/*
+		 * Re-read the index as pre-commit hook could have updated it,
+		 * and write it out as a tree.  We must do this before we invoke
+		 * the editor and after we invoke run_status above.
+		 */
+		discard_cache();
+	}
+
 	read_cache_from(index_file);
 	if (update_main_cache_tree(0)) {
 		error(_("Error building trees"));
-- 
2.14.1.481.g785c1dc9ae


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] commit: skip discarding the index if there is no pre-commit hook
  2017-08-14 21:54 ` [PATCH v2] " Kevin Willford
@ 2017-08-14 22:13   ` Jeff King
  2017-08-15  4:23     ` Kevin Willford
  0 siblings, 1 reply; 8+ messages in thread
From: Jeff King @ 2017-08-14 22:13 UTC (permalink / raw)
  To: Kevin Willford; +Cc: git, gitster

On Mon, Aug 14, 2017 at 03:54:25PM -0600, Kevin Willford wrote:

> If there is not a pre-commit hook, there is no reason to discard
> the index and reread it.
> 
> This change checks to presence of a pre-commit hook and then only
> discards the index if there was one.
> 
> Signed-off-by: Kevin Willford <kewillf@microsoft.com>
> ---
>  builtin/commit.c | 15 +++++++++------
>  1 file changed, 9 insertions(+), 6 deletions(-)

Thanks, this looks nice and simple.

> diff --git a/builtin/commit.c b/builtin/commit.c
> index e7a2cb6285..ab71b93518 100644
> --- a/builtin/commit.c
> +++ b/builtin/commit.c
> @@ -940,12 +940,15 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
>  		return 0;
>  	}
>  
> -	/*
> -	 * Re-read the index as pre-commit hook could have updated it,
> -	 * and write it out as a tree.  We must do this before we invoke
> -	 * the editor and after we invoke run_status above.
> -	 */
> -	discard_cache();
> +	if (!no_verify && find_hook("pre-commit")) {
> +		/*
> +		 * Re-read the index as pre-commit hook could have updated it,
> +		 * and write it out as a tree.  We must do this before we invoke
> +		 * the editor and after we invoke run_status above.
> +		 */
> +		discard_cache();
> +	}
> +
>  	read_cache_from(index_file);

This read_cache_from() should be a noop, right, because it immediately
sees istate->initialized is set? So it shouldn't matter that it is not
in the conditional with discard_cache(). Though if its only purpose is
to re-read the just-discarded contents, perhaps it makes sense to put it
there for readability.

-Peff

^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: [PATCH v2] commit: skip discarding the index if there is no pre-commit hook
  2017-08-14 22:13   ` Jeff King
@ 2017-08-15  4:23     ` Kevin Willford
  2017-08-15  4:53       ` Jeff King
  0 siblings, 1 reply; 8+ messages in thread
From: Kevin Willford @ 2017-08-15  4:23 UTC (permalink / raw)
  To: Jeff King; +Cc: git@vger.kernel.org, gitster@pobox.com

> On Mon, Aug 14, 2017 at 03:54:25PM -0600, Kevin Willford wrote:
> 
> > If there is not a pre-commit hook, there is no reason to discard
> > the index and reread it.
> >
> > This change checks to presence of a pre-commit hook and then only
> > discards the index if there was one.
> >
> > Signed-off-by: Kevin Willford <kewillf@microsoft.com>
> > ---
> >  builtin/commit.c | 15 +++++++++------
> >  1 file changed, 9 insertions(+), 6 deletions(-)
> 
> Thanks, this looks nice and simple.
> 
> > diff --git a/builtin/commit.c b/builtin/commit.c
> > index e7a2cb6285..ab71b93518 100644
> > --- a/builtin/commit.c
> > +++ b/builtin/commit.c
> > @@ -940,12 +940,15 @@ static int prepare_to_commit(const char *index_file,
> const char *prefix,
> >  		return 0;
> >  	}
> >
> > -	/*
> > -	 * Re-read the index as pre-commit hook could have updated it,
> > -	 * and write it out as a tree.  We must do this before we invoke
> > -	 * the editor and after we invoke run_status above.
> > -	 */
> > -	discard_cache();
> > +	if (!no_verify && find_hook("pre-commit")) {
> > +		/*
> > +		 * Re-read the index as pre-commit hook could have updated it,
> > +		 * and write it out as a tree.  We must do this before we invoke
> > +		 * the editor and after we invoke run_status above.
> > +		 */
> > +		discard_cache();
> > +	}
> > +
> >  	read_cache_from(index_file);
> 
> This read_cache_from() should be a noop, right, because it immediately
> sees istate->initialized is set? So it shouldn't matter that it is not
> in the conditional with discard_cache(). Though if its only purpose is
> to re-read the just-discarded contents, perhaps it makes sense to put it
> there for readability.
> 
> -Peff

I thought about that and didn't know if there were cases when this would be called
and the cache has not been loaded.  It didn't look like it since it is only called from 
cmd_commit and prepare_index is called before it.  Also if in the future this call would
be made when it had not read the index yet so thought it was safest just to leave
this as always being called since it is basically a noop if the istate->initialized is set.

Also based on this commit
https://github.com/git/git/commit/2888605c649ccd423232161186d72c0e6c458a48
it looked like the discard_cache was added independent of the read_cache_from call,
which made me think that the two were not tied together.

Kevin

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] commit: skip discarding the index if there is no pre-commit hook
  2017-08-15  4:23     ` Kevin Willford
@ 2017-08-15  4:53       ` Jeff King
  2017-08-15 18:05         ` Junio C Hamano
  0 siblings, 1 reply; 8+ messages in thread
From: Jeff King @ 2017-08-15  4:53 UTC (permalink / raw)
  To: Kevin Willford; +Cc: git@vger.kernel.org, gitster@pobox.com

On Tue, Aug 15, 2017 at 04:23:50AM +0000, Kevin Willford wrote:

> > This read_cache_from() should be a noop, right, because it immediately
> > sees istate->initialized is set? So it shouldn't matter that it is not
> > in the conditional with discard_cache(). Though if its only purpose is
> > to re-read the just-discarded contents, perhaps it makes sense to put it
> > there for readability.
> 
> I thought about that and didn't know if there were cases when this would be called
> and the cache has not been loaded.  It didn't look like it since it is only called from 
> cmd_commit and prepare_index is called before it.  Also if in the future this call would
> be made when it had not read the index yet so thought it was safest just to leave
> this as always being called since it is basically a noop if the istate->initialized is set.

Yeah, I agree it might be safer as you have it. And hopefully the
comment above the discard would point anybody in the right direction.

> Also based on this commit
> https://github.com/git/git/commit/2888605c649ccd423232161186d72c0e6c458a48
> it looked like the discard_cache was added independent of the read_cache_from call,
> which made me think that the two were not tied together.

Yeah, I tried to dig in the history and figure it out, but it was not
nearly as clear as I would have liked. :)

-Peff

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] commit: skip discarding the index if there is no pre-commit hook
  2017-08-15  4:53       ` Jeff King
@ 2017-08-15 18:05         ` Junio C Hamano
  0 siblings, 0 replies; 8+ messages in thread
From: Junio C Hamano @ 2017-08-15 18:05 UTC (permalink / raw)
  To: Jeff King; +Cc: Kevin Willford, git@vger.kernel.org

Jeff King <peff@peff.net> writes:

> On Tue, Aug 15, 2017 at 04:23:50AM +0000, Kevin Willford wrote:
>
>> > This read_cache_from() should be a noop, right, because it immediately
>> > sees istate->initialized is set? So it shouldn't matter that it is not
>> > in the conditional with discard_cache(). Though if its only purpose is
>> > to re-read the just-discarded contents, perhaps it makes sense to put it
>> > there for readability.
>> 
>> I thought about that and didn't know if there were cases when this would be called
>> and the cache has not been loaded.  It didn't look like it since it is only called from 
>> cmd_commit and prepare_index is called before it.  Also if in the future this call would
>> be made when it had not read the index yet so thought it was safest just to leave
>> this as always being called since it is basically a noop if the istate->initialized is set.
>
> Yeah, I agree it might be safer as you have it. And hopefully the
> comment above the discard would point anybody in the right direction.

I agree that it would not hurt to have it outside the conditional,
because read_cache_from() is a no-op when it is already populated.
I however do not think it is sensible to call prepare_to_commit()
without having populated the in-core index---in the function, nobody
conditionally reads the in-core index, expecting that the caller
might not have prepared the in-core index, when we need to do the
wt-status thing, for example.

>> Also based on this commit
>> https://github.com/git/git/commit/2888605c649ccd423232161186d72c0e6c458a48
>> it looked like the discard_cache was added independent of the read_cache_from call,
>> which made me think that the two were not tied together.

That one comes from the first commit of the C reimplementation,
f5bbc322 ("Port git commit to C.", 2007-11-08).

If I am reading the old code correctly, read_cache_from() is called
for entirely different reasons.  Back in the old code, when doing
"commit --patch", prepare_index() called interactive_add(), which
would return to us after updating the index file in the filesystem.
The caller of prepare_index(), which was cmd_commit(), needed to
read that off of the filesystem hence the call is there.

When not doing "--patch", prepare_index() called read_cache(), so
the read_cache_from() there was a no-op.

2888605c ("builtin-commit: fix partial-commit support", 2007-11-18)
was to fix the mistake of not re-reading the index from the file at
that point for non "--patch" cases.  Having read_cache_from() that
is most of the time no-op was not sufficient and needed additional
discard_cache() there.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2017-08-15 18:05 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-10 18:54 [PATCH] commit: skip discarding the index if there is no pre-commit hook Kevin Willford
2017-08-10 19:16 ` Jeff King
2017-08-10 22:22 ` Junio C Hamano
2017-08-14 21:54 ` [PATCH v2] " Kevin Willford
2017-08-14 22:13   ` Jeff King
2017-08-15  4:23     ` Kevin Willford
2017-08-15  4:53       ` Jeff King
2017-08-15 18:05         ` Junio C Hamano

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