Skip to content

Conversation

@derrickstolee
Copy link

This feature was introduced in #736 but had a few pain points that were discovered when testing the full replacement of the existing post-index-change and post-command hooks in our internal environment:

  1. When using core.hooksPath to point to a directory within the worktree, the sentinel files were being written into the worktree. Write to $GITDIR/info/ instead.
  2. The existing internal post-index-change hook only signaled that work needed to be done if actually the worktree changed. Make the sentinel file logic more restrictive to only write in these cases.

Tests are added to confirm both of these behavior changes.

This change to use the 'info/' directory instead of the 'hooks/'
directory is important because the hooks dir could be redirected to the
source tree.

Modify the test in such a way that this code change would fail due to
being unable to write to the internal hooks directory.

Signed-off-by: Derrick Stolee <stolee@gmail.com>
Before making the post-index-change logic more advanced for the
postCommand.strategy=post-index-change feature, extract the code into a
new method that will be easier to update in the future.

Signed-off-by: Derrick Stolee <stolee@gmail.com>
The postCommand.strategy=post-index-change config was designed to
replace some internal hook usage. This internal hook was more
restrictive than the initial feature was designed. Specifically, it
ignored index updates that did not update the worktree. With the
existing implementation, this causes more work done in the post-command
hook than intended.

Update the code to write a sentinel file only when the worktree is
changed.

Signed-off-by: Derrick Stolee <stolee@gmail.com>
@derrickstolee derrickstolee requested review from dscho and mjcheetham May 7, 2025 14:15
@derrickstolee derrickstolee self-assigned this May 7, 2025
dscho
dscho previously approved these changes May 7, 2025
Copy link
Member

@dscho dscho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, and since the feature is young enough, I have no objection to that minor behavior change.

The previously-named 'post-index-change' value for
'postCommand.strategy' did the following two things:

 1. Avoid running the post-command hook unless the post-index-change
    hook _would_ run with a signal that the worktree changed.

 2. Avoid running any installed post-index-change hooks.

The additional restriction of the worktree change in (1) makes this name
less appropriate. Also, item (2) seems to be a side-effect that we
should avoid. We would like to allow both behaviors to exist.

Update the value and behavior, including the tests.

Signed-off-by: Derrick Stolee <stolee@gmail.com>
dscho
dscho previously approved these changes May 7, 2025
Now that we don't skip the post-index-change hook, we don't need to pass
the return value as an out-parameter in this method.

Signed-off-by: Derrick Stolee <stolee@gmail.com>
@dscho dscho enabled auto-merge May 7, 2025 17:48
@dscho dscho merged commit 107f41a into microsoft:vfs-2.49.0 May 7, 2025
62 checks passed
derrickstolee added a commit that referenced this pull request Nov 10, 2025
The postCommand.strategy=worktree-change config option allows for
skipping the post-command hook when the Git command doesn't change the
worktree. However, sometimes this config isn't loaded before the
post-command hook is invoked, causing the hook to run in cases where
we'd prefer it to not run.

Examples include: `git version` or `git <typo>`.

The tricky bit is that there are several places where we get here and
standard config mechanisms can't load due to not having a `gitdir` in
the repository struct. We fix this by:

1. Using the "load early config" mechanism also used by
`core.hooksPath`.
2. Skipping the lookup for the sentinel file when there isn't a `gitdir`
since we couldn't have written one without it.

The change is given in two commits: the first expands the tests with the
existing behavior and the second changes the behavior, showing the
impact on those tests.

* [X] This change only applies to microsoft/git specifics (post-command
hook)

See #736 and #748 for prior art in this space.
dscho pushed a commit that referenced this pull request Nov 10, 2025
The postCommand.strategy=worktree-change config option allows for
skipping the post-command hook when the Git command doesn't change the
worktree. However, sometimes this config isn't loaded before the
post-command hook is invoked, causing the hook to run in cases where
we'd prefer it to not run.

Examples include: `git version` or `git <typo>`.

The tricky bit is that there are several places where we get here and
standard config mechanisms can't load due to not having a `gitdir` in
the repository struct. We fix this by:

1. Using the "load early config" mechanism also used by
`core.hooksPath`.
2. Skipping the lookup for the sentinel file when there isn't a `gitdir`
since we couldn't have written one without it.

The change is given in two commits: the first expands the tests with the
existing behavior and the second changes the behavior, showing the
impact on those tests.

* [X] This change only applies to microsoft/git specifics (post-command
hook)

See #736 and #748 for prior art in this space.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants