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

Re: [PATCH 9/9] Documentation: update api-builtin and api-setup

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Apr 5, 2011, 08:07 UTC
Message-ID
<20110405080729.GA25921@elie>
In-Reply-To
<20080227164045.GA28142@laptop>
Hi,
On 2008-02-27, Nguyễn Thái Ngọc Duy wrote:
>  Documentation/technical/api-builtin.txt |   10 ++++
>  Documentation/technical/api-setup.txt   |   70 +++++++++++++++++++++++++++----
>  2 files changed, 72 insertions(+), 8 deletions(-)
I'm curious --- did anything ever come of this patch?
Show 8 quoted lines
> --- a/Documentation/technical/api-setup.txt
> +++ b/Documentation/technical/api-setup.txt
> @@ -1,13 +1,67 @@
>  setup API
>  =========
>  
> +End-user point-of-view how the setup works
> +------------------------------------------

I suppose this is scattered through git(1) and git-config(1) now. Maybe it would make sense to centralized in git-repository-layout(5) or gitcli(7) and put cross-references elsewhere.

Show 14 quoted lines
> +. If you have `GIT_DIR` exported, then no discovery is attempted.
> +  We use the `GIT_DIR` you set it, and the repository lives
> +  there.  `$GIT_DIR/config` is the repository config.
> +
> +. Otherwise we do the usual discovery going up to find the
> +  repository.
> +
> +. If you have `GIT_WORK_TREE` exported, or otherwise if the
> +  config has `core.worktree`, that's where your worktree is.
> +  If these variables point to an invalid place, you have no worktree.
> +
> +. Otherwise, if you have `GIT_DIR` exported, you do not have a
> +  worktree.  Else one level above your `$GIT_DIR` is the toplevel
> +  of your worktree.
E.g., like this (but simpler somehow):
	Locating the repository
	-----------------------
	Typically git is run from within a work tree with a `.git`
	subdirectory at its top level or within a `<project>.git` directory
	associated to a bare repository.  To find the toplevel directory,
	git will repeatedly chdir("..") and at each level check for (in
	order of preference):
	 a) a subdirectory named ".git" with readable "objects" and
	    "refs" subdirectories and a readable "HEAD" file;
	 b) a file named ".git";
	 c) readable "objects" and "refs" subdirectories and a
	    readable "HEAD" file.
	Each is interpreted differently:
	 a) a .git subdirectory is the repository and the directory
	    containing it is the top of the work tree.
	 b) a .git file is used to locate the repository (see
	    "Platform-independent .git symlink" below) and the
	    directory containing a .git file is the top of the
	    work tree.
	 c) an ancestory of the starting cwd containing objects/,
	    refs/, and HEAD is treated as a bare repository.
	    There is no work tree in that case.
	Limiting the repository search
	------------------------------
	Git will not chdir("..") into a directory named in the
	GIT_CEILING_DIRECTORIES environment variable, nor will it
	chdir("..") into a directory on a different filesystem unless
	the GIT_DISCOVERY_ACROSS_FILESYSTEM variable is set.
	Unusual repository layouts
	--------------------------
	If the GIT_DIR environment variable is set, it will be used
	and no repository search performed.  The work tree defaults
	to ".".
	The GIT_WORK_TREE environment variable can be used to
	specify the top level of the work tree.  It can even be used
	from within a bare repository, to set up a temporary work
	tree.
	The "core.worktree" configuration variable in a repository
	can be used to specify the top level of the work tree,
	relative to the repository root.  This can be useful in
	situations in which the work tree must be completely pristine
	(so that it is not possible to put a .git symlink or .git file
	in the top level).
	Some files usually found in the repository can be placed
	elsewhere if requested through the environment.
	 * GIT_CONFIG - .git/config
	 * GIT_OBJECT_DIRECTORY - .git/objects
	 * GIT_INDEX_FILE - .git/index
	 * GIT_GRAFT_FILE - .git/info/grafts
Show 19 quoted lines
> +
> +Repository setup
> +----------------
> +
> +At startup:
> +
> +. If the command always need a repository, call
> +  `setup_git_directory()`. It will ensure you have a valid
> +  repository. It will `die()` otherwise. If a worktree is detected,
> +  it will be setup automatically.
> +
> +. If the command can optionally run in a repository, use
> +  `setup_git_directory_gently(&nongit_ok)`,which is similar
> +  to `setup_git_directory()` except it won't `die()`
> +  but sets `nongit_ok` to true if run outside a repository.
> +  No chdir(2) is done.
> +
> +. If you don't want worktree setup at all, but always need a git repository,
> +  you can use `setup_git_directory_gently(NULL)`.
Simpler advice might be:
 . If the command is builtin and always needs a repository,
   use the RUN_SETUP flag in the builtin table.
 . If the command is builtin and can benefit from a repository,
   use RUN_SETUP_GENTLY.
 . If you have to run a repository search later, call
   "setup_git_directory_gently" or the shortcut "setup_git_directory"
   (which means "setup_git_directory_gently(NULL)").
> +
> +Do not access git repository (even indirectly like `git_config()`) before
> +calling one of these functions. Otherwise you may encounter `die()` if git
> +fails to automatically find/setup a repository.
 . If you try to access the git repository (even indirectly like
   `git_config()`) before calling setup_git_directory_gently then git
   will look in the wrong place.
 . When changing the value of the GIT_DIR environment variable, call
   set_git_dir.  setup_git_directory_gently does this already.
Show 11 quoted lines
> +
> +Working directory setup
> +-----------------------
> +
> +If `setup_git_directory()` is used, worktree can be optionally setup already.
> +To check if work tree has been setup, use `get_git_work_tree()`. The return
> +value is `NULL` if no work tree is setup or work tree directory otherwise.
> +
> +If you need a working directory, use `setup_work_tree()`. It will
> +move current directory to top-level working directory and return
> +a prefix. It will `die()` if unable to setup working directory.

Did we ever figure out what happens/should happen when the requested worktree is not an ancestor of cwd?

Show 13 quoted lines
> +
> +Miscellanous functions
> +----------------------
> +
> +To know where `$GIT_DIR` is, use `get_git_dir()`. It will always return
> +an absolute path. To know where `$GIT_WORK_TREE` is, use
> +`get_git_work_tree()`. To check if you are inside a worktree or a git
> +repository, use `is_inside_work_tree()` or `is_inside_git_dir()` respectively.
> +There functions may be not valid until `setup_git_directory*()` is called.
> +
> +When working with pathspecs and prefix, you can use `get_pathspec()`
> +to auto prepend a given prefix to pathspecs. Other helpful functions
> +are `prefix_path()`, `prefix_filename()`
What do prefix_* do when there is no worktree?
Show 14 quoted lines
> -Talk about
> -
> +
> -* setup_git_directory()
> -* setup_git_directory_gently()
> -* is_inside_git_dir()
> -* is_inside_work_tree()
> -* setup_work_tree()
> -* get_pathspec()
> -
> -(Dscho)
> -- 
> 1.5.4.2.281.g28d0e
> 
Previous: Nguyễn Thái Ngọc DuyNext: Nguyen Thai Ngoc Duy
Message 23 of 24 in “"git read-tree -m" and the like require worktree”
  1. 1/9 "git read-tree -m" and the like require worktreeNguyễn Thái Ngọc Duy, Feb 27, 2008
  2. Junio C HamanoFeb 28, 2008
  3. Nguyen Thai Ngoc DuyFeb 28, 2008
  4. 2/9 Make sure setup_git_directory is called before accessing repositoryNguyễn Thái Ngọc Duy, Feb 27, 2008
  5. 3/9 Make get_git_dir() and 'git rev-parse --git-dir' absolute pathNguyễn Thái Ngọc Duy, Feb 27, 2008
  6. 4/9 Make setup_work_tree() return new prefixNguyễn Thái Ngọc Duy, Feb 27, 2008
  7. Johannes SchindelinFeb 28, 2008
  8. Nguyen Thai Ngoc DuyFeb 28, 2008
  9. Johannes SchindelinFeb 28, 2008
  10. 5/9 http-push: Avoid calling setup_git_directory() twiceNguyễn Thái Ngọc Duy, Feb 27, 2008
  11. Junio C HamanoFeb 28, 2008
  12. Nguyen Thai Ngoc DuyFeb 28, 2008
  13. 6/9 Completely move out worktree setup from setup_git_directory_gently()Nguyễn Thái Ngọc Duy, Feb 27, 2008
  14. Junio C HamanoFeb 28, 2008
  15. Nguyen Thai Ngoc DuyFeb 28, 2008
  16. Junio C HamanoFeb 28, 2008
  17. Nguyen Thai Ngoc DuyFeb 28, 2008
  18. Johannes SchindelinFeb 28, 2008
  19. Nguyen Thai Ngoc DuyFeb 28, 2008
  20. 7/9 builtin-archive: mark unused prefix "unused_prefix"Nguyễn Thái Ngọc Duy, Feb 27, 2008
  21. 8/9 Make setup_git_directory() auto-setup worktree if foundNguyễn Thái Ngọc Duy, Feb 27, 2008
  22. 9/9 Documentation: update api-builtin and api-setupNguyễn Thái Ngọc Duy, Feb 27, 2008
  23. Jonathan NiederApr 5, 2011
  24. Nguyen Thai Ngoc DuyApr 5, 2011

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.