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

[PATCH 09/10] Documentation: update api-builtin and api-setup

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Mar 2, 2008, 10:35 UTC
Message-ID
<20080302103519.GA9011@laptop>
In-Reply-To
<cover.1204453703.git.pclouds@gmail.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/technical/api-builtin.txt |   10 ++++
 Documentation/technical/api-setup.txt   |   91 ++++++++++++++++++++++++++++---
 2 files changed, 93 insertions(+), 8 deletions(-)
diff --git a/Documentation/technical/api-builtin.txt b/Documentation/technical/api-builtin.txt
index 52cdb4c..f0d988b 100644
--- a/Documentation/technical/api-builtin.txt
+++ b/Documentation/technical/api-builtin.txt
@@ -33,6 +33,10 @@ git:
 	If the standard output is connected to a tty, spawn a pager and
 	feed our output to it.
 
+`NEED_WORK_TREE`::
+
+	Make sure there is a work tree to work on.
+
 . Add `builtin-foo.o` to `BUILTIN_OBJS` in `Makefile`.
 
 Additionally, if `foo` is a new command, there are 3 more things to do:
@@ -59,5 +63,11 @@ to the subdirectory the command started from.  This allows you to
 convert a user-supplied pathname (typically relative to that directory)
 to a pathname relative to the top of the work tree.
 
+`NEED_WORK_TREE` also set `prefix` like `RUN_SETUP`. But it will `die()`
+if there is no work tree.
+
+If neither `NEED_WORK_TREE` nor `RUN_SETUP` is set, `prefix` is always `NULL`.
+No chdir(2) will be done.
+
 The return value from `cmd_foo()` becomes the exit status of the
 command.
diff --git a/Documentation/technical/api-setup.txt b/Documentation/technical/api-setup.txt
index 4f63a04..6f2defa 100644
--- a/Documentation/technical/api-setup.txt
+++ b/Documentation/technical/api-setup.txt
@@ -1,13 +1,88 @@
 setup API
 =========
 
-Talk about
+End-user point-of-view how the setup works
+------------------------------------------
 
-* setup_git_directory()
-* setup_git_directory_gently()
-* is_inside_git_dir()
-* is_inside_work_tree()
-* setup_work_tree()
-* get_pathspec()
+. 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.
 
-(Dscho)
+. 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.
+
+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. Note that `setup_git_directory()`
+  can only be called once.
+
+. 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)`.
+
+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.
+
+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.
+
+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()`
+
+Shell-based setup
+-----------------
+
+At startup, you need to `source git-sh-setup`. If your command does not always
+require a repository, set variable `NONGIT_OK` before sourcing git-sh-setup.
+
+If the command requires worktree, call `require_work_tree` (after
+git-sh-setup).
+
+Two variables `USAGE` and `LONG_USAGE` can be optionally set before sourcing
+git-sh-setup. They will be printed as help usage.
+
+To check if a repository is a bare repository, `is_bare_repository` can be
+used.
+
+There are several `git rev-parse` options to help shell scripts initialize
+their environment. Those are `--show-prefix`, `--show-cdup`, `--git-dir`,
+`--is-inside-git-dir`, `--is-inside-work-tree`, `--is-bare-repository` and
+`--parseopt`. Please refer to `git rev-parse` man page for more information.
-- 
1.5.4.2.281.g28d0e
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 31 of 32 in “"git read-tree -m" and the like require worktree”
  1. 01/10 "git read-tree -m" and the like require worktreeNguyễn Thái Ngọc Duy, Mar 2, 2008
  2. Johannes SchindelinMar 11, 2008
  3. Nguyen Thai Ngoc DuyMar 11, 2008
  4. Johannes SchindelinMar 11, 2008
  5. Nguyen Thai Ngoc DuyMar 11, 2008
  6. 02/10 Make sure setup_git_directory is called before accessing repositoryNguyễn Thái Ngọc Duy, Mar 2, 2008
  7. 03/10 Make get_git_dir() and 'git rev-parse --git-dir' absolute pathNguyễn Thái Ngọc Duy, Mar 2, 2008
  8. Johannes SchindelinMar 11, 2008
  9. Nguyen Thai Ngoc DuyMar 11, 2008
  10. Johannes SchindelinMar 11, 2008
  11. Nguyen Thai Ngoc DuyMar 11, 2008
  12. Johannes SchindelinMar 11, 2008
  13. 04/10 Make setup_work_tree() return new prefixNguyễn Thái Ngọc Duy, Mar 2, 2008
  14. Johannes SchindelinMar 11, 2008
  15. Nguyen Thai Ngoc DuyMar 11, 2008
  16. 05/10 http-push: Avoid calling setup_git_directory() twiceNguyễn Thái Ngọc Duy, Mar 2, 2008
  17. Johannes SchindelinMar 11, 2008
  18. Nguyen Thai Ngoc DuyMar 11, 2008
  19. Johannes SchindelinMar 11, 2008
  20. Nguyen Thai Ngoc DuyMar 11, 2008
  21. 06/10 Completely move out worktree setup from setup_git_directory_gently()Nguyễn Thái Ngọc Duy, Mar 2, 2008
  22. Johannes SchindelinMar 11, 2008
  23. Nguyen Thai Ngoc DuyMar 11, 2008
  24. Johannes SchindelinMar 11, 2008
  25. 07/10 builtin-archive: mark unused prefix "unused_prefix"Nguyễn Thái Ngọc Duy, Mar 2, 2008
  26. Johannes SchindelinMar 11, 2008
  27. Nguyen Thai Ngoc DuyMar 11, 2008
  28. Jay SoffianMar 11, 2008
  29. Nguyen Thai Ngoc DuyMar 11, 2008
  30. 08/10 Make setup_git_directory() auto-setup worktree if foundNguyễn Thái Ngọc Duy, Mar 2, 2008
  31. 09/10 Documentation: update api-builtin and api-setupNguyễn Thái Ngọc Duy, Mar 2, 2008
  32. 10/10 Additional tests to capture worktree special casesNguyễn Thái Ngọc Duy, Mar 2, 2008

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.