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

Re: builtin command's prefix question

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 5, 2007, 22:12 UTC
Message-ID
<7vlk88n648.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<fcaeb9bf0712050856t5d730779q82783fdb9876f41@mail.gmail.com>
"Nguyen Thai Ngoc Duy" <pclouds@gmail.com> writes:
> I have been looking at setup_git_directory_gently() lately. From my
> understanding, setup_git_directory* will return a string called
> "prefix" that is passed to builtin commands. What is the exact meaning
> of this prefix?

Some historical background is in order. For a long time, we only worked from the very top of the work tree and nowhere else. The path you get from the user on the command line was supposed to be relative to the top of the work tree, the user was supposed to be at the top of the work tree, no work from subdirectories.

This was limiting, so "setup" was introduced. The commands originally worked only from the top would chdir up to the top of the work tree if it was run from a subdirectory. This however needs adjustments to the paths we get from the user from the command line (or stdin for commands that take such). If we claim we work from a subdirectory, we should interpret path given by the user who is in a subdirectory as relative to that subdirectory. The way to do this adjustment is to prefix the subdirectory path to the path given by the user.

So, if you start a command from Documentation/ subdirectory, like this:
	$ cd Documentation
	$ git ls-files howto
internally, ls-files would:
 * Notice it was run from Documentation/ subdirectory;
 * cd up to the top level;
 * prefix "Documentation/" to the pathspec given by the user
   (i.e. "howto"), to form "Documentation/howto";
 * Act as before, except that it strips "Documentation/" from its usual
   output, to retain the illusion of working from that subdirectory.

And prefix is "Documentation/" (note the trailing slash) in such a case. If you run from the top, it is NULL to signal that there is no need to do any of these tricks.

> ... Correct me if I'm wrong. In early (read: no worktree)
> days, cwd was moved to working root directory and prefix contained
> relative path from working root directory to the original cwd. So it
> had a few implications:
>  1. A non-empty prefix indicates cwd has been moved
Correct (I do not know if we care, though)
>  2. If cwd is moved, it is moved to working root directory
Correct ("we always work from top of the tree internally" matters)
>  3. cwd+prefix should point to the current directory at the time the
> command was issued (let's call it "user cwd")
Correct.
Show 9 quoted lines
> Things change a bit since the rise of worktree:
>  - If GIT_DIR is set and GIT_WORK_TREE is not, prefix is relative to
> the to-be-set-up worktree, but cwd is not changed, so point 3 is gone.
>  - If GIT_DIR is not set and GiT_WORK_TREE is,
>   - and it is found that user cwd is inside a gitdir (bare repo), cwd
> has been moved and prefix is empty, cwd+prefix no longer point to user
> cwd
>   - for other cases, cwd may not be worktree (the real worktree will
> be setup in setup_work_tree or setup_git_directory)
The intention is:
 * If GIT_DIR is set but not GIT_WORK_TREE (nor core.worktree in
   config), you are either inside project.git/ directory of bare
   repository or at the toplevel of worktree-full directory.  This has
   been the traditional behaviour before GIT_WORK_TREE and we shouldn't
   break the existing setups that assume this behaviour.  So in that
   sense, with this combination:
   - If the repository is bare, the value of the prefix should not
     matter; the command that wants to look at prefix by definition
     wants to run from a subdirectory but there is no notion of
     "the user directory being a subdirectory of the top of the work
     tree" in a bare repository;
   - If the repository is not bare, the user directory _MUST_ be at the
     top of the work tree, as that is what the traditional behaviour is.
     Anything else would break existing setups.
     IOW, if you use GIT_DIR and still want to run from a subdirectory
     of the worktree, you must have either GIT_WORK_TREE or
     core.worktree to tell where the top of the worktree is, and if you
     don't, then you must be at the top.
   So the right thing to do in this case is not going anywhere and using
   prefix=NULL.
 * I would say it is a misconfiguration if GIT_DIR is not set and
   GIT_WORK_TREE is, as the sole purpose of GIT_WORK_TREE is so that you
   can work from a subdirectory when you set GIT_DIR.  I may be missing
   an obvious use case that this is useful, but I do not think of any.
   Dscho may be able to correct me on this, as he fixed up the original
   work tree series that was even messier quite a bit during the last
   round.
Previous: Nguyen Thai Ngoc DuyNext: Junio C Hamano
Message 2 of 7 in “builtin command's prefix question”
  1. Nguyen Thai Ngoc DuyDec 5, 2007
  2. Junio C HamanoDec 5, 2007
  3. Junio C HamanoDec 5, 2007
  4. Junio C HamanoDec 5, 2007
  5. Nguyen Thai Ngoc DuyDec 6, 2007
  6. Johannes SchindelinDec 6, 2007
  7. Junio C HamanoDec 6, 2007

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.