Documentation / technical / api-builtin.txton commit Update draft release notes to 2.2 (fbecd99)
   1builtin API
   2===========
   3
   4Adding a new built-in
   5---------------------
   6
   7There are 4 things to do to add a built-in command implementation to
   8Git:
   9
  10. Define the implementation of the built-in command `foo` with
  11  signature:
  12
  13        int cmd_foo(int argc, const char **argv, const char *prefix);
  14
  15. Add the external declaration for the function to `builtin.h`.
  16
  17. Add the command to the `commands[]` table defined in `git.c`.
  18  The entry should look like:
  19
  20        { "foo", cmd_foo, <options> },
  21+
  22where options is the bitwise-or of:
  23
  24`RUN_SETUP`::
  25        If there is not a Git directory to work on, abort.  If there
  26        is a work tree, chdir to the top of it if the command was
  27        invoked in a subdirectory.  If there is no work tree, no
  28        chdir() is done.
  29
  30`RUN_SETUP_GENTLY`::
  31        If there is a Git directory, chdir as per RUN_SETUP, otherwise,
  32        don't chdir anywhere.
  33
  34`USE_PAGER`::
  35
  36        If the standard output is connected to a tty, spawn a pager and
  37        feed our output to it.
  38
  39`NEED_WORK_TREE`::
  40
  41        Make sure there is a work tree, i.e. the command cannot act
  42        on bare repositories.
  43        This only makes sense when `RUN_SETUP` is also set.
  44
  45. Add `builtin/foo.o` to `BUILTIN_OBJS` in `Makefile`.
  46
  47Additionally, if `foo` is a new command, there are 3 more things to do:
  48
  49. Add tests to `t/` directory.
  50
  51. Write documentation in `Documentation/git-foo.txt`.
  52
  53. Add an entry for `git-foo` to `command-list.txt`.
  54
  55. Add an entry for `/git-foo` to `.gitignore`.
  56
  57
  58How a built-in is called
  59------------------------
  60
  61The implementation `cmd_foo()` takes three parameters, `argc`, `argv,
  62and `prefix`.  The first two are similar to what `main()` of a
  63standalone command would be called with.
  64
  65When `RUN_SETUP` is specified in the `commands[]` table, and when you
  66were started from a subdirectory of the work tree, `cmd_foo()` is called
  67after chdir(2) to the top of the work tree, and `prefix` gets the path
  68to the subdirectory the command started from.  This allows you to
  69convert a user-supplied pathname (typically relative to that directory)
  70to a pathname relative to the top of the work tree.
  71
  72The return value from `cmd_foo()` becomes the exit status of the
  73command.