git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / Atom feed
* Running 'git worktree add' in 'pre-commit' hook
@ 2019-05-14 14:52 Cosmin Polifronie
  2019-05-15  0:46 ` Bryan Turner
  2019-05-16 11:25 ` Duy Nguyen
  0 siblings, 2 replies; 17+ messages in thread
From: Cosmin Polifronie @ 2019-05-14 14:52 UTC (permalink / raw)
  To: git

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.

Thanks! :)

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

* Re: Running 'git worktree add' in 'pre-commit' hook
  2019-05-14 14:52 Running 'git worktree add' in 'pre-commit' hook Cosmin Polifronie
@ 2019-05-15  0:46 ` Bryan Turner
  2019-05-15 12:38   ` Cosmin Polifronie
  2019-05-16 11:25 ` Duy Nguyen
  1 sibling, 1 reply; 17+ messages in thread
From: Bryan Turner @ 2019-05-15  0:46 UTC (permalink / raw)
  To: Cosmin Polifronie; +Cc: Git Users

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! :)

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

* Re: Running 'git worktree add' in 'pre-commit' hook
  2019-05-15  0:46 ` Bryan Turner
@ 2019-05-15 12:38   ` Cosmin Polifronie
  0 siblings, 0 replies; 17+ messages in thread
From: Cosmin Polifronie @ 2019-05-15 12:38 UTC (permalink / raw)
  To: Bryan Turner; +Cc: Git Users

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:
>
> 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! :)

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

* Re: Running 'git worktree add' in 'pre-commit' hook
  2019-05-14 14:52 Running 'git worktree add' in 'pre-commit' hook Cosmin Polifronie
  2019-05-15  0:46 ` Bryan Turner
@ 2019-05-16 11:25 ` Duy Nguyen
  2019-05-16 11:33   ` Eric Sunshine
                     ` (2 more replies)
  1 sibling, 3 replies; 17+ messages in thread
From: Duy Nguyen @ 2019-05-16 11:25 UTC (permalink / raw)
  To: Cosmin Polifronie; +Cc: Git Mailing List

On Tue, May 14, 2019 at 9:53 PM 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?

pre-commit hook sets GIT_INDEX_FILE to this "index.lock" so you have
the latest index content (which is not the same as from
$GIT_DIR/index). This variable will interfere with any commands that
work on a different worktree.

So you probably can still make it work by backing up $GIT_INDEX_FILE
(in case you need it), then unset it before you use "git worktree" (or
cd to it if you keep a permanent separate worktree for pre-commit
activities). To make sure you don't have similar problems, you
probably should do "env | grep GIT" from the hook and see if any other
variables are set.

> 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.
>
> Thanks! :)



-- 
Duy

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

* Re: Running 'git worktree add' in 'pre-commit' hook
  2019-05-16 11:25 ` Duy Nguyen
@ 2019-05-16 11:33   ` Eric Sunshine
  2019-05-16 11:39     ` Eric Sunshine
  2019-05-16 11:41     ` Duy Nguyen
  2019-05-16 11:59   ` Eric Sunshine
  2019-05-16 22:17   ` Jeff King
  2 siblings, 2 replies; 17+ messages in thread
From: Eric Sunshine @ 2019-05-16 11:33 UTC (permalink / raw)
  To: Duy Nguyen; +Cc: Cosmin Polifronie, Git Mailing List

On Thu, May 16, 2019 at 7:25 AM Duy Nguyen <pclouds@gmail.com> wrote:
> pre-commit hook sets GIT_INDEX_FILE to this "index.lock" so you have
> the latest index content (which is not the same as from
> $GIT_DIR/index). This variable will interfere with any commands that
> work on a different worktree.
>
> So you probably can still make it work by backing up $GIT_INDEX_FILE
> (in case you need it), then unset it before you use "git worktree" (or
> cd to it if you keep a permanent separate worktree for pre-commit
> activities). To make sure you don't have similar problems, you
> probably should do "env | grep GIT" from the hook and see if any other
> variables are set.

I researched this also and concluded that it's a bug in git-commit.
You run afoul of it in other situations, as well. For instance, say
you have your index file in a non-standard location:

    $ export GIT_INDEX_FILE=../storage/index
    $ git worktree add --detach other
    Preparing worktree (detached HEAD c9156d2)
    fatal: Unable to create '/.../foo/other/../storage/index.lock': No
such file or directory
    $

I think the correct fix is for git-commit to assign GIT_INDEX_FILE
with the absolute path to the index file, not a relative path.

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

* Re: Running 'git worktree add' in 'pre-commit' hook
  2019-05-16 11:33   ` Eric Sunshine
@ 2019-05-16 11:39     ` Eric Sunshine
  2019-05-16 11:44       ` Duy Nguyen
  2019-05-16 11:41     ` Duy Nguyen
  1 sibling, 1 reply; 17+ messages in thread
From: Eric Sunshine @ 2019-05-16 11:39 UTC (permalink / raw)
  To: Duy Nguyen; +Cc: Cosmin Polifronie, Git Mailing List

On Thu, May 16, 2019 at 7:33 AM Eric Sunshine <sunshine@sunshineco.com> wrote:
> I researched this also and concluded that it's a bug in git-commit.
> You run afoul of it in other situations, as well. For instance, say
> you have your index file in a non-standard location:
>
>     $ export GIT_INDEX_FILE=../storage/index
>     $ git worktree add --detach other
>     Preparing worktree (detached HEAD c9156d2)
>     fatal: Unable to create '/.../foo/other/../storage/index.lock': No
> such file or directory
>     $
>
> I think the correct fix is for git-commit to assign GIT_INDEX_FILE
> with the absolute path to the index file, not a relative path.

Thinking more clearly on it, a better fix might be for git-worktree to
deal with this itself, converting such a path to absolute before
cd'ing to the new worktree directory (or something -- I don't have the
code in front of me to provide a more concrete suggestion).

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

* Re: Running 'git worktree add' in 'pre-commit' hook
  2019-05-16 11:33   ` Eric Sunshine
  2019-05-16 11:39     ` Eric Sunshine
@ 2019-05-16 11:41     ` Duy Nguyen
  2019-05-16 12:14       ` Eric Sunshine
  1 sibling, 1 reply; 17+ messages in thread
From: Duy Nguyen @ 2019-05-16 11:41 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Cosmin Polifronie, Git Mailing List

On Thu, May 16, 2019 at 6:33 PM Eric Sunshine <sunshine@sunshineco.com> wrote:
> I researched this also and concluded that it's a bug in git-commit.
> You run afoul of it in other situations, as well. For instance, say
> you have your index file in a non-standard location:
>
>     $ export GIT_INDEX_FILE=../storage/index
>     $ git worktree add --detach other
>     Preparing worktree (detached HEAD c9156d2)
>     fatal: Unable to create '/.../foo/other/../storage/index.lock': No
> such file or directory
>     $
>
> I think the correct fix is for git-commit to assign GIT_INDEX_FILE
> with the absolute path to the index file, not a relative path.

Oh if it's relative $GIT_INDEX_FILE then I think its our environment.c
code that does not work so well when we chdir() away. I vaguely recall
something about this when discussing Jeff's chdir-notify series. But
it turns out that's about chdir() in run-command.c [1]. But the idea
is still the same, all variables are supposed to be relative to $CWD.
Whenever you move $CWD you should reparent all of them, including
$GIT_INDEX_FILE.

[1] https://public-inbox.org/git/CACsJy8CdqpNOw+zdMyugX-902Z=gLNij5_xcmE4jGLRBTqiO1g@mail.gmail.com/
-- 
Duy

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

* Re: Running 'git worktree add' in 'pre-commit' hook
  2019-05-16 11:39     ` Eric Sunshine
@ 2019-05-16 11:44       ` Duy Nguyen
  2019-05-16 12:05         ` Eric Sunshine
  0 siblings, 1 reply; 17+ messages in thread
From: Duy Nguyen @ 2019-05-16 11:44 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Cosmin Polifronie, Git Mailing List

On Thu, May 16, 2019 at 6:39 PM Eric Sunshine <sunshine@sunshineco.com> wrote:
>
> On Thu, May 16, 2019 at 7:33 AM Eric Sunshine <sunshine@sunshineco.com> wrote:
> > I researched this also and concluded that it's a bug in git-commit.
> > You run afoul of it in other situations, as well. For instance, say
> > you have your index file in a non-standard location:
> >
> >     $ export GIT_INDEX_FILE=../storage/index
> >     $ git worktree add --detach other
> >     Preparing worktree (detached HEAD c9156d2)
> >     fatal: Unable to create '/.../foo/other/../storage/index.lock': No
> > such file or directory
> >     $
> >
> > I think the correct fix is for git-commit to assign GIT_INDEX_FILE
> > with the absolute path to the index file, not a relative path.
>
> Thinking more clearly on it, a better fix might be for git-worktree to
> deal with this itself, converting such a path to absolute before
> cd'ing to the new worktree directory (or something -- I don't have the
> code in front of me to provide a more concrete suggestion).

Our mails crossed. But yeah it's basically the same thing.

Although I still doubt a good $GIT_INDEX_FILE would fix the problem.
Even if you manage to create a new worktree, when you cd there and
start using it, this $GIT_INDEX_FILE will still mess things up (unless
you do mean to ignore the worktree's index file and update one
provided by pre-commit hook).
-- 
Duy

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

* Re: Running 'git worktree add' in 'pre-commit' hook
  2019-05-16 11:25 ` Duy Nguyen
  2019-05-16 11:33   ` Eric Sunshine
@ 2019-05-16 11:59   ` Eric Sunshine
  2019-05-16 22:17   ` Jeff King
  2 siblings, 0 replies; 17+ messages in thread
From: Eric Sunshine @ 2019-05-16 11:59 UTC (permalink / raw)
  To: Duy Nguyen; +Cc: Cosmin Polifronie, Git Mailing List

On Thu, May 16, 2019 at 7:25 AM Duy Nguyen <pclouds@gmail.com> wrote:
> pre-commit hook sets GIT_INDEX_FILE to this "index.lock" so you have
> the latest index content (which is not the same as from
> $GIT_DIR/index). This variable will interfere with any commands that
> work on a different worktree.

I think that this is not quite accurate. The problem isn't that
git-commit is pointing GIT_INDEX_FILE at a temporary index; that works
fine. The problem is when it is using the normal .git/index file, in
which case the value it assigns to GIT_INDEX_FILE is relative.

For instance, with pre-commit hook:

    #!/bin/sh
    echo "GIT_INDEX_FILE=$GIT_INDEX_FILE"
    git worktree add --detach other

"git-worktree add" with a normal git-commit breaks due to the relative
location specified by GIT_INDEX_FILE:

    $ git add file
    $ git commit -m foo
    GIT_INDEX_FILE=.git/index
    Preparing worktree (detached HEAD 0857fef)
    fatal: Unable to create '/.../other/.git/index.lock': Not a directory
    $

whereas, the "git commit <file>" form succeeds just fine because it
assigns an absolute path for the temporary index:

    $ git commit -m foo file
    GIT_INDEX_FILE=/.../.git/next-index-23675.lock
    Preparing worktree (detached HEAD 0857fef)
    HEAD is now at 0857fef first
    [master 2fa5413] foo
    $

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

* Re: Running 'git worktree add' in 'pre-commit' hook
  2019-05-16 11:44       ` Duy Nguyen
@ 2019-05-16 12:05         ` Eric Sunshine
  0 siblings, 0 replies; 17+ messages in thread
From: Eric Sunshine @ 2019-05-16 12:05 UTC (permalink / raw)
  To: Duy Nguyen; +Cc: Cosmin Polifronie, Git Mailing List

On Thu, May 16, 2019 at 7:45 AM Duy Nguyen <pclouds@gmail.com> wrote:
> On Thu, May 16, 2019 at 6:39 PM Eric Sunshine <sunshine@sunshineco.com> wrote:
> > Thinking more clearly on it, a better fix might be for git-worktree to
> > deal with this itself, converting such a path to absolute before
> > cd'ing to the new worktree directory (or something -- I don't have the
> > code in front of me to provide a more concrete suggestion).
>
> Our mails crossed. But yeah it's basically the same thing.
>
> Although I still doubt a good $GIT_INDEX_FILE would fix the problem.
> Even if you manage to create a new worktree, when you cd there and
> start using it, this $GIT_INDEX_FILE will still mess things up (unless
> you do mean to ignore the worktree's index file and update one
> provided by pre-commit hook).

I was forgetting that each worktree has its own index. Your advice
that his hook should sanitize GIT_INDEX_FILE (and possibly other GIT_*
variables) is probably the only sane approach. I don't think it's
something we can handle in some automated fashion since we don't know
whence GIT_INDEX_FILE arose.

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

* Re: Running 'git worktree add' in 'pre-commit' hook
  2019-05-16 11:41     ` Duy Nguyen
@ 2019-05-16 12:14       ` Eric Sunshine
  2019-05-16 12:28         ` Duy Nguyen
  0 siblings, 1 reply; 17+ messages in thread
From: Eric Sunshine @ 2019-05-16 12:14 UTC (permalink / raw)
  To: Duy Nguyen; +Cc: Cosmin Polifronie, Git Mailing List

On Thu, May 16, 2019 at 7:42 AM Duy Nguyen <pclouds@gmail.com> wrote:
> On Thu, May 16, 2019 at 6:33 PM Eric Sunshine <sunshine@sunshineco.com> wrote:
> > You run afoul of it in other situations, as well. For instance, say
> > you have your index file in a non-standard location:
> >
> >     $ export GIT_INDEX_FILE=../storage/index
> >     $ git worktree add --detach other
> >     Preparing worktree (detached HEAD c9156d2)
> >     fatal: Unable to create '/.../foo/other/../storage/index.lock': No
> > such file or directory
> >     $
> >
> Oh if it's relative $GIT_INDEX_FILE then I think its our environment.c
> code that does not work so well when we chdir() away. I vaguely recall
> something about this when discussing Jeff's chdir-notify series. But
> it turns out that's about chdir() in run-command.c [1]. But the idea
> is still the same, all variables are supposed to be relative to $CWD.
> Whenever you move $CWD you should reparent all of them, including
> $GIT_INDEX_FILE.

builtin/worktree.c:add_worktree() is already assigning
new-worktree-specific values for GIT_DIR and GIT_WORK_TREE, so for the
specific example I showed above, the correct fix would be for
add_worktree() to remove GIT_INDEX_FILE from the environment before
invoking other Git commands, correct?

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

* Re: Running 'git worktree add' in 'pre-commit' hook
  2019-05-16 12:14       ` Eric Sunshine
@ 2019-05-16 12:28         ` Duy Nguyen
  0 siblings, 0 replies; 17+ messages in thread
From: Duy Nguyen @ 2019-05-16 12:28 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Cosmin Polifronie, Git Mailing List

On Thu, May 16, 2019 at 7:15 PM Eric Sunshine <sunshine@sunshineco.com> wrote:
>
> On Thu, May 16, 2019 at 7:42 AM Duy Nguyen <pclouds@gmail.com> wrote:
> > On Thu, May 16, 2019 at 6:33 PM Eric Sunshine <sunshine@sunshineco.com> wrote:
> > > You run afoul of it in other situations, as well. For instance, say
> > > you have your index file in a non-standard location:
> > >
> > >     $ export GIT_INDEX_FILE=../storage/index
> > >     $ git worktree add --detach other
> > >     Preparing worktree (detached HEAD c9156d2)
> > >     fatal: Unable to create '/.../foo/other/../storage/index.lock': No
> > > such file or directory
> > >     $
> > >
> > Oh if it's relative $GIT_INDEX_FILE then I think its our environment.c
> > code that does not work so well when we chdir() away. I vaguely recall
> > something about this when discussing Jeff's chdir-notify series. But
> > it turns out that's about chdir() in run-command.c [1]. But the idea
> > is still the same, all variables are supposed to be relative to $CWD.
> > Whenever you move $CWD you should reparent all of them, including
> > $GIT_INDEX_FILE.
>
> builtin/worktree.c:add_worktree() is already assigning
> new-worktree-specific values for GIT_DIR and GIT_WORK_TREE, so for the
> specific example I showed above, the correct fix would be for
> add_worktree() to remove GIT_INDEX_FILE from the environment before
> invoking other Git commands, correct?

I'll work on the assumption that all $GIT_* are for the _current_
worktree, not the one we're creating. If so, yes we need to sanitize
$GIT_*, maybe do it the same way submodules do (I think
prepare_submodule_repo_env() is the right one).
-- 
Duy

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

* Re: Running 'git worktree add' in 'pre-commit' hook
  2019-05-16 11:25 ` Duy Nguyen
  2019-05-16 11:33   ` Eric Sunshine
  2019-05-16 11:59   ` Eric Sunshine
@ 2019-05-16 22:17   ` Jeff King
  2019-05-16 23:16     ` Eric Sunshine
  2 siblings, 1 reply; 17+ messages in thread
From: Jeff King @ 2019-05-16 22:17 UTC (permalink / raw)
  To: Duy Nguyen; +Cc: Cosmin Polifronie, Git Mailing List

On Thu, May 16, 2019 at 06:25:24PM +0700, Duy Nguyen wrote:

> > Is it forbidden to call this command from a hook?
> 
> pre-commit hook sets GIT_INDEX_FILE to this "index.lock" so you have
> the latest index content (which is not the same as from
> $GIT_DIR/index). This variable will interfere with any commands that
> work on a different worktree.
> 
> So you probably can still make it work by backing up $GIT_INDEX_FILE
> (in case you need it), then unset it before you use "git worktree" (or
> cd to it if you keep a permanent separate worktree for pre-commit
> activities). To make sure you don't have similar problems, you
> probably should do "env | grep GIT" from the hook and see if any other
> variables are set.

If you're entering another repo from a hook, you're supposed to use:

  unset $(git rev-parse --local-env-vars)

I wondered if we'd need another similar mechanism for entering the
worktree of another repo, that would maybe clear fewer variables. But I
think it's actually the same: we really want to clear everything and let
our "cd" pick up the new repository path.

The case of actually _adding_ a new work tree (before we enter it) is
weirder, though. We definitely want to stay in the same repository, and
clearing all of that would not make sense. I do wonder if worktree-add
should be handling GIT_INDEX_FILE (ignoring it when we want to be
dealing with the index of the new worktree we added, and handling any
relative fixups if we chdir inside the worktree code).

-Peff

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

* Re: Running 'git worktree add' in 'pre-commit' hook
  2019-05-16 22:17   ` Jeff King
@ 2019-05-16 23:16     ` Eric Sunshine
  2019-05-17  0:19       ` Jeff King
  0 siblings, 1 reply; 17+ messages in thread
From: Eric Sunshine @ 2019-05-16 23:16 UTC (permalink / raw)
  To: Jeff King; +Cc: Duy Nguyen, Cosmin Polifronie, Git Mailing List

On Thu, May 16, 2019 at 6:17 PM Jeff King <peff@peff.net> wrote:
> On Thu, May 16, 2019 at 06:25:24PM +0700, Duy Nguyen wrote:
> > So you probably can still make it work by backing up $GIT_INDEX_FILE
> > (in case you need it), then unset it before you use "git worktree" (or
> > cd to it if you keep a permanent separate worktree for pre-commit
> > activities). [...]
>
> The case of actually _adding_ a new work tree (before we enter it) is
> weirder, though. We definitely want to stay in the same repository, and
> clearing all of that would not make sense. I do wonder if worktree-add
> should be handling GIT_INDEX_FILE (ignoring it when we want to be
> dealing with the index of the new worktree we added, and handling any
> relative fixups if we chdir inside the worktree code).

Ignoring GIT_INDEX_FILE was indeed the conclusion reached earlier in
this thread. Addressing your other point, "git worktree add" does
chdir() into the new worktree if a post-checkout hook exists since
that hook needs to run in the new worktree, not in the worktree in
which the "git worktree add" command itself was invoked. For the hook
invocation, it already sanitizes the environment of GIT_DIR and
GIT_WORK_TREE, and GIT_INDEX_FILE ought to be cleaned too. Is there
any existing code in Git for doing the relative fixups you mention for
other Git environment variables?

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

* Re: Running 'git worktree add' in 'pre-commit' hook
  2019-05-16 23:16     ` Eric Sunshine
@ 2019-05-17  0:19       ` Jeff King
  2019-05-17  1:02         ` Eric Sunshine
  0 siblings, 1 reply; 17+ messages in thread
From: Jeff King @ 2019-05-17  0:19 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Duy Nguyen, Cosmin Polifronie, Git Mailing List

On Thu, May 16, 2019 at 07:16:54PM -0400, Eric Sunshine wrote:

> On Thu, May 16, 2019 at 6:17 PM Jeff King <peff@peff.net> wrote:
> > On Thu, May 16, 2019 at 06:25:24PM +0700, Duy Nguyen wrote:
> > > So you probably can still make it work by backing up $GIT_INDEX_FILE
> > > (in case you need it), then unset it before you use "git worktree" (or
> > > cd to it if you keep a permanent separate worktree for pre-commit
> > > activities). [...]
> >
> > The case of actually _adding_ a new work tree (before we enter it) is
> > weirder, though. We definitely want to stay in the same repository, and
> > clearing all of that would not make sense. I do wonder if worktree-add
> > should be handling GIT_INDEX_FILE (ignoring it when we want to be
> > dealing with the index of the new worktree we added, and handling any
> > relative fixups if we chdir inside the worktree code).
> 
> Ignoring GIT_INDEX_FILE was indeed the conclusion reached earlier in
> this thread. Addressing your other point, "git worktree add" does
> chdir() into the new worktree if a post-checkout hook exists since
> that hook needs to run in the new worktree, not in the worktree in
> which the "git worktree add" command itself was invoked. For the hook
> invocation, it already sanitizes the environment of GIT_DIR and
> GIT_WORK_TREE, and GIT_INDEX_FILE ought to be cleaned too. Is there
> any existing code in Git for doing the relative fixups you mention for
> other Git environment variables?

You can assign local_repo_env to child_process.env (or push it
individually to env_array if you have to mix with other variables). See
git_connect() for an example.

-Peff

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

* Re: Running 'git worktree add' in 'pre-commit' hook
  2019-05-17  0:19       ` Jeff King
@ 2019-05-17  1:02         ` Eric Sunshine
  2019-05-17  1:19           ` Jeff King
  0 siblings, 1 reply; 17+ messages in thread
From: Eric Sunshine @ 2019-05-17  1:02 UTC (permalink / raw)
  To: Jeff King; +Cc: Duy Nguyen, Cosmin Polifronie, Git Mailing List

On Thu, May 16, 2019 at 8:19 PM Jeff King <peff@peff.net> wrote:
> On Thu, May 16, 2019 at 07:16:54PM -0400, Eric Sunshine wrote:
> > Is there
> > any existing code in Git for doing the relative fixups you mention for
> > other Git environment variables?
>
> You can assign local_repo_env to child_process.env (or push it
> individually to env_array if you have to mix with other variables). See
> git_connect() for an example.

I wasn't aware of 'local_repo_env', but can see how it could be
helpful as a basis for building machinery to adjust relative paths
contained in those environment variables prior to a chdir(). My
original question was if such machinery already exists, but I'm
guessing from your response that it doesn't.

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

* Re: Running 'git worktree add' in 'pre-commit' hook
  2019-05-17  1:02         ` Eric Sunshine
@ 2019-05-17  1:19           ` Jeff King
  0 siblings, 0 replies; 17+ messages in thread
From: Jeff King @ 2019-05-17  1:19 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Duy Nguyen, Cosmin Polifronie, Git Mailing List

On Thu, May 16, 2019 at 09:02:06PM -0400, Eric Sunshine wrote:

> On Thu, May 16, 2019 at 8:19 PM Jeff King <peff@peff.net> wrote:
> > On Thu, May 16, 2019 at 07:16:54PM -0400, Eric Sunshine wrote:
> > > Is there
> > > any existing code in Git for doing the relative fixups you mention for
> > > other Git environment variables?
> >
> > You can assign local_repo_env to child_process.env (or push it
> > individually to env_array if you have to mix with other variables). See
> > git_connect() for an example.
> 
> I wasn't aware of 'local_repo_env', but can see how it could be
> helpful as a basis for building machinery to adjust relative paths
> contained in those environment variables prior to a chdir(). My
> original question was if such machinery already exists, but I'm
> guessing from your response that it doesn't.

Oh, I see. I totally missed that you said "relative fixups" and not
"clearing". :)

There's the chdir_notify system. Any variables which need updating need
to register themselves, and then the chdir in the worktree code needs to
be done with chdir_notify(). See chdir-notify.h for details.

-Peff

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

end of thread, back to index

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-14 14:52 Running 'git worktree add' in 'pre-commit' hook Cosmin Polifronie
2019-05-15  0:46 ` Bryan Turner
2019-05-15 12:38   ` Cosmin Polifronie
2019-05-16 11:25 ` Duy Nguyen
2019-05-16 11:33   ` Eric Sunshine
2019-05-16 11:39     ` Eric Sunshine
2019-05-16 11:44       ` Duy Nguyen
2019-05-16 12:05         ` Eric Sunshine
2019-05-16 11:41     ` Duy Nguyen
2019-05-16 12:14       ` Eric Sunshine
2019-05-16 12:28         ` Duy Nguyen
2019-05-16 11:59   ` Eric Sunshine
2019-05-16 22:17   ` Jeff King
2019-05-16 23:16     ` Eric Sunshine
2019-05-17  0:19       ` Jeff King
2019-05-17  1:02         ` Eric Sunshine
2019-05-17  1:19           ` Jeff King

git@vger.kernel.org list mirror (unofficial, one of many)

Archives are clonable:
	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://ou63pmih66umazou.onion/inbox.comp.version-control.git
	nntp://czquwvybam4bgbro.onion/inbox.comp.version-control.git
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.version-control.git
	nntp://news.gmane.org/gmane.comp.version-control.git

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/ public-inbox