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

Re: Running 'git worktree add' in 'pre-commit' hook

From
CPCosmin Polifronie <oppturbv@gmail.com>
Date
May 15, 2019, 12:38 UTC
Message-ID
<CAJFQqNJsnkdwqk0+kCp6j5aL9BaXd=-JaXC9K5kSBSed+=V3xg@mail.gmail.com>
In-Reply-To
<CAGyf7-GtnCqfqPWVpBAELB4xAE6syn7+DfKXgfhL0UoMfvbn4A@mail.gmail.com>
Hello Bryan,

My project contains a Gradle build that is dependent on a configuration found in a file called build.gradle. What I need to do is run Gradle with a proper build.gradle file, meaning: if a new build.gradle is staged, I will use that for my Gradle build, if not I will use the latest approved commit's one (as in a commit that has already passed the pre-commit check). The problem is that build.gradle can be modified and not be staged, and Gradle cannot know that, it will use whatever is available on disk. I want to avoid that.

This is why I needed a copy of my current repo with the file versions already approved (already staged at a previous date). This is why I am using 'git worktree add'.

Now, I have found another solution using stashes, and those might work, but I am still curious about this situation.

On Wed, May 15, 2019 at 3:46 AM Bryan Turner <bturner@atlassian.com> wrote:
Show 46 quoted lines
>
> On Tue, May 14, 2019 at 7:53 AM Cosmin Polifronie <oppturbv@gmail.com> wrote:
> >
> > Hello! I am trying to run 'git worktree add <path> HEAD' in the
> > 'pre-commit' hook, more specifically in a Python script that is being
> > called from the hook. When doing so, I am greeted with the following
> > error:
> >
> > On Windows 10:
> > Preparing worktree (detached HEAD cbfef18)
> > fatal: Unable to create 'C:/Users/meh/Desktop/abc/.git/index.lock': No
> > such file or directory
> >
> > On Arch Linux:
> > Preparing worktree (detached HEAD cbfef18)
> > fatal: Unable to create '/home/cosmin/Downloads/abc/.git/index.lock':
> > Not a directory
> >
> > Is it forbidden to call this command from a hook? If yes, what kind of
> > alternatives do I have? I need to make a copy of the repo in its HEAD
> > state, process it and then decide if I will pass the current commit or
> > not.
>
> I can't speak to whether `git worktree add` should succeed or fail
> inside a `pre-commit` hook, but...
>
> Why do you need a new work tree, versus whatever working copy you're
> running `git commit` in? Is there a reason whatever validation needs
> to be done can't be done in the existing working copy? `HEAD` is the
> _previous, existing commit_, not the new, currently-being-created
> commit, so your validation in the new work tree, if you actually
> managed to create one, would be applied to the _latest existing
> commit_, not the new changes you're trying to commit. Even trying to
> copy the changes over wouldn't necessarily result in the same state,
> because there may be unstaged changes.
>
> What type of validation are you trying to do? I think the failure
> you're running into is an alarm bell indicating what you're trying to
> do may not make sense. However, without any insight into what "process
> it and then decide if I will pass the current commit or not" actually
> looks like, it's hard to offer you much help.
>
> Bryan
>
> >
> > Thanks! :)
Previous: Bryan TurnerNext: Duy Nguyen
Message 3 of 17 in “Running 'git worktree add' in 'pre-commit' hook”
  1. Cosmin PolifronieMay 14, 2019
  2. Bryan TurnerMay 15, 2019
  3. Cosmin PolifronieMay 15, 2019
  4. Duy NguyenMay 16, 2019
  5. Eric SunshineMay 16, 2019
  6. Eric SunshineMay 16, 2019
  7. Duy NguyenMay 16, 2019
  8. Eric SunshineMay 16, 2019
  9. Duy NguyenMay 16, 2019
  10. Eric SunshineMay 16, 2019
  11. Duy NguyenMay 16, 2019
  12. Eric SunshineMay 16, 2019
  13. Jeff KingMay 16, 2019
  14. Eric SunshineMay 16, 2019
  15. Jeff KingMay 17, 2019
  16. Eric SunshineMay 17, 2019
  17. Jeff KingMay 17, 2019

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.