Skip to content

Commit 82e1b3f

Browse files
committed
Merge branch 'kj/my-first-contribution-updates' into seen
* kj/my-first-contribution-updates: cmd_psuh: Prefer repo_config for config lookup Docs: Add cmd_psuh with repo and UNUSED removal Docs: Correct cmd_psuh and Explain UNUSED macro Remove unused git-mentoring mailing list
2 parents 18a2350 + 746bde3 commit 82e1b3f

File tree

1 file changed

+62
-27
lines changed

1 file changed

+62
-27
lines changed

Documentation/MyFirstContribution.adoc

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ the list by sending an email to <[email protected]>
4040
The https://lore.kernel.org/git[archive] of this mailing list is
4141
available to view in a browser.
4242

43-
==== https://groups.google.com/forum/#!forum/git-mentoring[[email protected]]
44-
45-
This mailing list is targeted to new contributors and was created as a place to
46-
post questions and receive answers outside of the public eye of the main list.
47-
Veteran contributors who are especially interested in helping mentor newcomers
48-
are present on the list. In order to avoid search indexers, group membership is
49-
required to view messages; anyone can join and no approval is required.
50-
5143
==== https://web.libera.chat/#git-devel[#git-devel] on Libera Chat
5244

5345
This IRC channel is for conversations between Git contributors. If someone is
@@ -150,15 +142,23 @@ command in `builtin/psuh.c`. Create that file, and within it, write the entry
150142
point for your command in a function matching the style and signature:
151143

152144
----
153-
int cmd_psuh(int argc, const char **argv, const char *prefix)
145+
int cmd_psuh(int argc, const char **argv, const char *prefix, struct repository *repo)
146+
----
147+
148+
We also use the UNUSED macro to make sure we don't recieve compiler warnings
149+
for unused arguments from the function cmd_psuh.
150+
151+
----
152+
int cmd_psuh(int argc UNUSED, const char **argv UNUSED,
153+
const char *prefix UNUSED, struct repository *repo UNUSED)
154154
----
155155

156156
We'll also need to add the declaration of psuh; open up `builtin.h`, find the
157157
declaration for `cmd_pull`, and add a new line for `psuh` immediately before it,
158158
in order to keep the declarations alphabetically sorted:
159159

160160
----
161-
int cmd_psuh(int argc, const char **argv, const char *prefix);
161+
int cmd_psuh(int argc, const char **argv, const char *prefix, struct repository *repo);
162162
----
163163

164164
Be sure to `#include "builtin.h"` in your `psuh.c`. You'll also need to
@@ -174,7 +174,8 @@ Throughout the tutorial, we will mark strings for translation as necessary; you
174174
should also do so when writing your user-facing commands in the future.
175175

176176
----
177-
int cmd_psuh(int argc, const char **argv, const char *prefix)
177+
int cmd_psuh(int argc UNUSED, const char **argv UNUSED,
178+
const char *prefix UNUSED, struct repository *repo UNUSED)
178179
{
179180
printf(_("Pony saying hello goes here.\n"));
180181
return 0;
@@ -287,10 +288,14 @@ on the reference implementation linked at the top of this document.
287288
It's probably useful to do at least something besides printing out a string.
288289
Let's start by having a look at everything we get.
289290

290-
Modify your `cmd_psuh` implementation to dump the args you're passed, keeping
291+
Modify your `cmd_psuh` implementation to dump the args you're passed
292+
and removing the UNUSED macro from them, keeping
291293
existing `printf()` calls in place:
292294

293295
----
296+
int cmd_psuh(int argc, const char **argv,
297+
const char *prefix, struct repository *repo)
298+
{
294299
int i;
295300
296301
...
@@ -305,33 +310,63 @@ existing `printf()` calls in place:
305310
printf(_("Your current working directory:\n<top-level>%s%s\n"),
306311
prefix ? "/" : "", prefix ? prefix : "");
307312
313+
if (repo && repo->gitdir) {
314+
printf(_("Git directory: %s\n"), repo->gitdir);
315+
} else {
316+
printf(_("No Git directory found.\n"));
317+
}
318+
319+
...
320+
}
308321
----
309322

310323
Build and try it. As you may expect, there's pretty much just whatever we give
311324
on the command line, including the name of our command. (If `prefix` is empty
312325
for you, try `cd Documentation/ && ../bin-wrappers/git psuh`). That's not so
313326
helpful. So what other context can we get?
314327

315-
Add a line to `#include "config.h"`. Then, add the following bits to the
316-
function body:
328+
Add a line to `#include "config.h"` and `#include "repository.h"`.
329+
Then, add the following bits to the function body:
317330

318331
----
319-
const char *cfg_name;
332+
#include "builtin.h"
333+
#include "gettext.h"
334+
#include "config.h"
335+
#include "repository.h"
320336
321-
...
337+
int cmd_psuh(int argc, const char **argv,
338+
const char *prefix, struct repository *repo)
339+
{
340+
const char *cfg_name;
341+
342+
printf(Q_("Your args (there is %d):\n",
343+
"Your args (there are %d):\n",
344+
argc),
345+
argc);
346+
347+
for (int i = 0; i < argc; i++) {
348+
printf("%d: %s\n", i, argv[i]);
349+
}
322350
323-
git_config(git_default_config, NULL);
324-
if (git_config_get_string_tmp("user.name", &cfg_name) > 0)
325-
printf(_("No name is found in config\n"));
326-
else
327-
printf(_("Your name: %s\n"), cfg_name);
351+
printf(_("Your current working directory:\n<top-level>%s%s\n"),
352+
prefix ? "/" : "", prefix ? prefix : "");
353+
354+
repo_config(repo, git_default_config, NULL);
355+
356+
if (repo_config_get_string_tmp(repo, "user.name", &cfg_name))
357+
printf(_("No name is found in config\n"));
358+
else
359+
printf(_("Your name: %s\n"), cfg_name);
360+
361+
return 0;
362+
}
328363
----
329364

330-
`git_config()` will grab the configuration from config files known to Git and
331-
apply standard precedence rules. `git_config_get_string_tmp()` will look up
365+
`repo_config()` will grab the configuration from config files known to Git and
366+
apply standard precedence rules. `repo_config_get_string_tmp()` will look up
332367
a specific key ("user.name") and give you the value. There are a number of
333368
single-key lookup functions like this one; you can see them all (and more info
334-
about how to use `git_config()`) in `Documentation/technical/api-config.adoc`.
369+
about how to use `repo_config()`) in `Documentation/technical/api-config.adoc`.
335370

336371
You should see that the name printed matches the one you see when you run:
337372

@@ -364,7 +399,7 @@ status_init_config(&s, git_status_config);
364399
----
365400

366401
But as we drill down, we can find that `status_init_config()` wraps a call
367-
to `git_config()`. Let's modify the code we wrote in the previous commit.
402+
to `repo_config()`. Let's modify the code we wrote in the previous commit.
368403

369404
Be sure to include the header to allow you to use `struct wt_status`:
370405

@@ -380,8 +415,8 @@ prepare it, and print its contents:
380415
381416
...
382417
383-
wt_status_prepare(the_repository, &status);
384-
git_config(git_default_config, &status);
418+
wt_status_prepare(repo, &status);
419+
repo_config(repo, git_default_config, &status);
385420
386421
...
387422

0 commit comments

Comments
 (0)