Skip to content

git-hooks: make pre-push validation work in linked worktrees - #13488

Open
ldanielmadariaga wants to merge 1 commit into
GoogleCloudPlatform:masterfrom
ldanielmadariaga:fix-pre-push-worktree
Open

ldanielmadariaga wants to merge 1 commit into
GoogleCloudPlatform:masterfrom
ldanielmadariaga:fix-pre-push-worktree

Conversation

@ldanielmadariaga

Copy link
Copy Markdown
Collaborator

Makes the pre-push hook work when pushing from a linked worktree (git worktree add).

Git exports GIT_DIR to hooks. In a linked worktree it points at .git/worktrees/<name> and GIT_WORK_TREE is unset, so git treats whatever directory it runs in as the top of the working tree. generate-proto.sh runs git rev-parse --show-toplevel from dev/tools/controllerbuilder and gets that directory back. validate-generated-files then fails, so every push from a worktree is rejected:

./generate-proto.sh: line 24: cd: <worktree>/dev/tools/controllerbuilder/dev/tools/controllerbuilder: No such file or directory

The hook now clears GIT_DIR and GIT_WORK_TREE, as githooks(5) recommends, so validation runs the same way as ./dev/tasks/validate-and-push --validate-only. Git doesn't export them in a normal checkout, so nothing changes there.

Tested:

  • In a scratch repo, checked what the hook sees from a normal checkout and from a linked worktree (including a push run from a subdirectory), before and after the change.
  • Pushed this branch from a linked worktree of this repo. The fixed hook ran the full validation (make fmt, go vet, validate-generated-files, unit-tests), and it passed.
NONE
Git exports GIT_DIR to hooks. In a linked worktree (`git worktree add`)
it points at .git/worktrees/<name> and GIT_WORK_TREE is unset, so git
treats whatever directory it runs in as the top of the working tree.
generate-proto.sh runs `git rev-parse --show-toplevel` from
dev/tools/controllerbuilder and gets that directory back, so
validate-generated-files fails and every push from a worktree is
rejected:

  ./generate-proto.sh: line 24: cd: <worktree>/dev/tools/controllerbuilder/dev/tools/controllerbuilder: No such file or directory

Clear GIT_DIR and GIT_WORK_TREE in the hook, as githooks(5) recommends,
so the validation runs the same way as
`./dev/tasks/validate-and-push --validate-only`. Git doesn't export
them in a normal checkout, so nothing changes there.

@barney-s barney-s left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

/lgtm
/approve

@cheftako cheftako left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

/lgtm
/approve

@google-oss-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: barney-s, cheftako

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

3 participants