threads / discuss / 26774

Git fails to detect subcommand when hook is symlinked to a builtin

Subject: Git fails to detect subcommand when hook is symlinked to a builtin

## tl;dr

6 messages between Mar 18, 2011 and Mar 20, 2011.

replies: 5people: 3as markdown or json

Todd Zullinger· Mar 18, 2011, 15:14 UTC · lore

I'm not sure if this is a bug or more of a "don't do that" sort of thing. At fedorahosted.org, the git repositories are mounted on a filesystem with noexec. As such, we have all hooks symlinked elsewhere. For the post-update hook we used to link directly to git-update-server-info.

This worked until we upgraded the system to git-1.7.4.1 recently. A trace may help illustrate:

With hooks/post-update as a symlink to /usr/bin/git-update-server-info:

trace: run_command: 'hooks/post-update' 'refs/heads/master' trace: run_command: 'gc' '--auto' '--quiet' remote: trace: exec: 'git-refs/heads/master' remote: trace: run_command: 'git-refs/heads/master' remote: git: 'refs/heads/master' is not a git command. See 'git --help'.

With hooks/post-update as a symlink to /usr/share/git-core/templates/hooks/post-update.sample:

trace: run_command: 'hooks/post-update' 'refs/heads/master' remote: setup: git_dir: . remote: setup: worktree: (null) remote: setup: cwd: /home/fedora/tmz/tmp/iwhd.git remote: setup: prefix: (null) remote: trace: built-in: git 'update-server-info'

Is there any reasonable way to handle this case? Perhaps if the symlink were dereferenced (if no subcommand were found) and then re-checked for a subcommand? I admit that I did not delve too deeply into the code, but from the little I did I wasn't sure that any changes here wouldn't lead to worse troubles.

-- 
Todd        OpenPGP -> KeyID: 0xBEAF0CE3 | URL: www.pobox.com/~tmz/pgp
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Everyone needs to believe in something. I believe I'll have another
beer.
Junio C Hamano· Mar 19, 2011, 00:53 UTC · re: Todd Zullinger · lore

Re: Git fails to detect subcommand when hook is symlinked to a builtin

Todd Zullinger <tmz@pobox.com> writes:
Show 7 quoted lines
> I'm not sure if this is a bug or more of a "don't do that" sort of
> thing.  At fedorahosted.org, the git repositories are mounted on a
> filesystem with noexec.  As such, we have all hooks symlinked
> elsewhere.  For the post-update hook we used to link directly to
> git-update-server-info.
>
> This worked until we upgraded the system to git-1.7.4.1 recently.

I have a suspicion that we may end up saying "don't do that", but let's ponder a bit with some technical background first. The "git" wrapper and builtin commands are all the same binary, and as a consequence, their main() function needs to decide what to do based on argv[0]. If argv[0] is "git", we know argv[1] is the name of the subcommand, and if argv[0] is "git-foo", it might be the internal call from the "git" wrapper that was invoked with argv[] = { "git", "foo", ... }, but we don't expect that argv[0] to be things like "post-update".

But it is curious why the resulting behaviour changed recently. The symptom hints that the link target (git-update-server-info in your case) used to be given as argv[0] (which still wouldn't have been kosher---you are not supposed to use dashed form since 1.5.4 or so without adding $GIT_EXEC_PATH at the beginning of your $PATH, and that is why I said the first sentence in this reply), but we recently changed something and now we are getting "hooks/post-update" in argv[0].

I suspect the recent change to run hooks via run_command() interface has something to do with it. From what version did you update to 1.7.4.1?

Todd Zullinger· Mar 19, 2011, 01:14 UTC · re: Junio C Hamano · lore

Re: Git fails to detect subcommand when hook is symlinked to a builtin

Junio C Hamano wrote:
Show 9 quoted lines
> I have a suspicion that we may end up saying "don't do that", but
> let's ponder a bit with some technical background first.  The "git"
> wrapper and builtin commands are all the same binary, and as a
> consequence, their main() function needs to decide what to do based
> on argv[0].  If argv[0] is "git", we know argv[1] is the name of the
> subcommand, and if argv[0] is "git-foo", it might be the internal
> call from the "git" wrapper that was invoked with argv[] = { "git",
> "foo", ... }, but we don't expect that argv[0] to be things like
> "post-update".
I can certainly understand that. :)
Show 12 quoted lines
> But it is curious why the resulting behaviour changed recently.  The
> symptom hints that the link target (git-update-server-info in your
> case) used to be given as argv[0] (which still wouldn't have been
> kosher---you are not supposed to use dashed form since 1.5.4 or so
> without adding $GIT_EXEC_PATH at the beginning of your $PATH, and
> that is why I said the first sentence in this reply), but we
> recently changed something and now we are getting
> "hooks/post-update" in argv[0].
>
> I suspect the recent change to run hooks via run_command() interface
> has something to do with it.  From what version did you update to
> 1.7.4.1?
I should have mentioned it, but I failed to do so.  Sorry about that.

This was moving from 1.5.5.6 to 1.7.4.1. The packages for EPEL (extra stuff for RHEL/CentOS) lagged behind for quite some time. The fedorahosted.org system was using 1.5.5.6 for a long time with the symlinked post-update hook in place. But it's not necessarily anything that changed recently in git. It could have been changes all the way back in 1.6.0, which seems like an eternity in git time.

We already fixed up the repositories on fedorahosted for this, but I figured I should ask, in case this change was not intended and might catch other folks doing git hosting.

(The only other minor change that caught us was that some of the repos did not have core.bare = true and pushes to these oddly configured repositories failed. Clearly, that was a misconfiguration from the time the repos were setup. Otherwise, for moving from 1.5.5.6 to 1.7.4.1, things went quite smoothly. That's thanks in no small part to the great attention to detail you and the other folks on this list have. Thank you!)

-- 
Todd        OpenPGP -> KeyID: 0xBEAF0CE3 | URL: www.pobox.com/~tmz/pgp
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The United States is a nation of laws, badly written and randomly
enforced.
    -- Frank Zappa
Junio C Hamano· Mar 19, 2011, 19:40 UTC · re: Todd Zullinger · lore

Re: Git fails to detect subcommand when hook is symlinked to a builtin

Todd Zullinger <tmz@pobox.com> writes:
Show 7 quoted lines
>> I suspect the recent change to run hooks via run_command() interface
>> has something to do with it.  From what version did you update to
>> 1.7.4.1?
>
> I should have mentioned it, but I failed to do so.  Sorry about that.
>
> This was moving from 1.5.5.6 to 1.7.4.1.

Ok, that explains it. update-server-info wasn't a built-in, so didn't have to look at its name to decide what to do. It didn't have to spawn other git commands from GIT_EXEC_PATH so running the command directly without setting the environment variable happened to work.

But such a usage (this doesn't have anything to do with the symlinking) was deprecated way before 1.5.4 [*1*], so the answer is still "don't do that" ;-)

Thanks for a clarification.
[Reference]
*1* http://thread.gmane.org/gmane.comp.version-control.git/93511
Jonathan Nieder· Mar 20, 2011, 08:20 UTC · re: Junio C Hamano · lore

Re: Git fails to detect subcommand when hook is symlinked to a builtin

Junio C Hamano wrote:
> Ok, that explains it.  update-server-info wasn't a built-in, so didn't
> have to look at its name to decide what to do.  It didn't have to spawn
> other git commands from GIT_EXEC_PATH so running the command directly
> without setting the environment variable happened to work.
Nice detective work.
> But such a usage (this doesn't have anything to do with the symlinking)
> was deprecated way before 1.5.4 [*1*], so the answer is still "don't do
> that" ;-)
[...]
> *1* http://thread.gmane.org/gmane.comp.version-control.git/93511

After rereading some of that thread (oops), I feel a small clarification is in order. Yes, the dashed named git-update-server-info is not the name that that command would preferred to be called by, but if I understand correctly, this is not what you are talking about having being deprecated and removed. Instead, the rule is:

 If you set your $PATH to include $(git --exec-path), then you may still
 run traditional git-<command>s using that dashed name.  Otherwise, you
 may be lucky and find it working (which is probably a bug :)) but
 please, just say no.[1]
Rationale: in that thread, the change to $PATH was agreed on as a
long-term workaround for preserving scripts and muscle memory.

Regards, Jonathan

[1] In practice: the git wrapper adds GIT_EXEC_PATH to the $PATH and traditional commands rely on finding it there. For example, when git-<command> is a shell script, it will tend to begin by reading in some useful functions by running ". git-sh-setup", which relies on git-sh-setup lying on the $PATH.

Jonathan Nieder· Mar 20, 2011, 09:09 UTC · re: Jonathan Nieder · lore

Re: Git fails to detect subcommand when hook is symlinked to a builtin

Jonathan Nieder wrote:
> Instead, the rule is:

Sorry, that came out a muddle. Teemu Likonen explains the rule[1] better. And Linus explains the rationale[2]. The main reason I found this interesting enough to chime in is that I was considering proposing changing the beginning of git scripts to

 . "$(git --exec-path)/git-sh-setup"

to set a good example, which would be a regression in git's ability to complain when used in a fragile way.

So, Todd: thanks for a very useful reminder.

[1] http://thread.gmane.org/gmane.comp.version-control.git/94378 [2] http://thread.gmane.org/gmane.comp.version-control.git/93825

← back to recent threads