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

Re: [PATCH v6 1/2] documentation: add tutorial for first contribution

From
SZEDER Gábor <szeder.dev@gmail.com>
Date
Oct 18, 2019, 16:40 UTC
Message-ID
<20191018164027.GI29845@szeder.dev>
In-Reply-To
<20190517190701.49722-2-emilyshaffer@google.com>
On Fri, May 17, 2019 at 12:07:02PM -0700, Emily Shaffer wrote:
Show 76 quoted lines
> +=== Adding a New Command
> +
> +Lots of the subcommands are written as builtins, which means they are
> +implemented in C and compiled into the main `git` executable. Implementing the
> +very simple `psuh` command as a built-in will demonstrate the structure of the
> +codebase, the internal API, and the process of working together as a contributor
> +with the reviewers and maintainer to integrate this change into the system.
> +
> +Built-in subcommands are typically implemented in a function named "cmd_"
> +followed by the name of the subcommand, in a source file named after the
> +subcommand and contained within `builtin/`. So it makes sense to implement your
> +command in `builtin/psuh.c`. Create that file, and within it, write the entry
> +point for your command in a function matching the style and signature:
> +
> +----
> +int cmd_psuh(int argc, const char **argv, const char *prefix)
> +----
> +
> +We'll also need to add the declaration of psuh; open up `builtin.h`, find the
> +declaration for `cmd_push`, and add a new line for `psuh` immediately before it,
> +in order to keep the declarations sorted:
> +
> +----
> +int cmd_psuh(int argc, const char **argv, const char *prefix);
> +----
> +
> +Be sure to `#include "builtin.h"` in your `psuh.c`.
> +
> +Go ahead and add some throwaway printf to that function. This is a decent
> +starting point as we can now add build rules and register the command.
> +
> +NOTE: Your throwaway text, as well as much of the text you will be adding over
> +the course of this tutorial, is user-facing. That means it needs to be
> +localizable. Take a look at `po/README` under "Marking strings for translation".
> +Throughout the tutorial, we will mark strings for translation as necessary; you
> +should also do so when writing your user-facing commands in the future.
> +
> +----
> +int cmd_psuh(int argc, const char **argv, const char *prefix)
> +{
> +	printf(_("Pony saying hello goes here.\n"));
> +	return 0;
> +}
> +----
> +
> +Let's try to build it.  Open `Makefile`, find where `builtin/push.o` is added
> +to `BUILTIN_OBJS`, and add `builtin/psuh.o` in the same way next to it in
> +alphabetical order. Once you've done so, move to the top-level directory and
> +build simply with `make`. Also add the `DEVELOPER=1` variable to turn on
> +some additional warnings:
> +
> +----
> +$ echo DEVELOPER=1 >config.mak
> +$ make
> +----
> +
> +NOTE: When you are developing the Git project, it's preferred that you use the
> +`DEVELOPER` flag; if there's some reason it doesn't work for you, you can turn
> +it off, but it's a good idea to mention the problem to the mailing list.
> +
> +NOTE: The Git build is parallelizable. `-j#` is not included above but you can
> +use it as you prefer, here and elsewhere.
> +
> +Great, now your new command builds happily on its own. But nobody invokes it.
> +Let's change that.
> +
> +The list of commands lives in `git.c`. We can register a new command by adding
> +a `cmd_struct` to the `commands[]` array. `struct cmd_struct` takes a string
> +with the command name, a function pointer to the command implementation, and a
> +setup option flag. For now, let's keep mimicking `push`. Find the line where
> +`cmd_push` is registered, copy it, and modify it for `cmd_psuh`, placing the new
> +line in alphabetical order.
> +
> +The options are documented in `builtin.h` under "Adding a new built-in." Since
> +we hope to print some data about the user's current workspace context later,
> +we need a Git directory, so choose `RUN_SETUP` as your only option.
Just leaving a quick note here: an entry about the new command should
be added to 'command-list.txt' as well, so it will be included in the
list of available commands in 'git help -a' or even in 'git help'
and in completion, if the command is marked with the necessary
attributes.
 
Previous: Emily ShafferNext: Emily Shaffer
Message 41 of 44 in “documentation: add lab for first contribution”
  1. 0/1 documentation: add lab for first contributionEmily Shaffer via GitGitGadget, Apr 11, 2019
  2. 1/1 documentation: add lab for first contributionEmily Shaffer via GitGitGadget, Apr 11, 2019
  3. Junio C HamanoApr 12, 2019
  4. Emily ShafferApr 12, 2019
  5. Junio C HamanoApr 13, 2019
  6. Emily ShafferApr 15, 2019
  7. Josh SteadmonApr 11, 2019
  8. Junio C HamanoApr 12, 2019
  9. Emily ShafferApr 12, 2019
  10. 0/1 documentation: add lab for first contributionEmily Shaffer via GitGitGadget, Apr 16, 2019
  11. 1/1 documentation: add lab for first contributionEmily Shaffer via GitGitGadget, Apr 16, 2019
  12. Junio C HamanoApr 17, 2019
  13. Eric SunshineApr 17, 2019
  14. Junio C HamanoApr 18, 2019
  15. Emily ShafferApr 17, 2019
  16. Emily ShafferApr 16, 2019
  17. documentation: add lab for first contributionEmily Shaffer, Apr 19, 2019
  18. Junio C HamanoApr 21, 2019
  19. Emily ShafferApr 22, 2019
  20. documentation: add tutorial for first contributionEmily Shaffer, Apr 23, 2019
  21. Josh SteadmonApr 30, 2019
  22. Emily ShafferMay 2, 2019
  23. Phil HordMay 3, 2019
  24. Emily ShafferMay 7, 2019
  25. Jonathan TanMay 6, 2019
  26. Emily ShafferMay 7, 2019
  27. Jonathan TanMay 7, 2019
  28. Junio C HamanoMay 8, 2019
  29. 0/2 documentation: add lab for first contributionEmily Shaffer, May 7, 2019
  30. 1/2 documentation: add tutorial for first contributionEmily Shaffer, May 7, 2019
  31. Emily ShafferMay 7, 2019
  32. Junio C HamanoMay 8, 2019
  33. Emily ShafferMay 8, 2019
  34. Jonathan TanMay 8, 2019
  35. 2/2 documentation: add anchors to MyFirstContributionEmily Shaffer, May 7, 2019
  36. Junio C HamanoMay 8, 2019
  37. 0/2 documentation: add tutorial for first contributionEmily Shaffer, May 17, 2019
  38. 1/2 documentation: add tutorial for first contributionEmily Shaffer, May 17, 2019
  39. Christian CouderMay 26, 2019
  40. Emily ShafferMay 29, 2019
  41. SZEDER GáborOct 18, 2019
  42. Emily ShafferOct 18, 2019
  43. 2/2 documentation: add anchors to MyFirstContributionEmily Shaffer, May 17, 2019
  44. doc: add some nit fixes to MyFirstContributionEmily Shaffer, May 29, 2019

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.