Skip to content

Commit 746bde3

Browse files
jayatheerthkulkarnigitster
authored andcommitted
cmd_psuh: Prefer repo_config for config lookup
This commit updates cmd_psuh to use repo_config and repo_config_get_string_tmp for retrieving the user.name config variable. This is a more robust and correct approach than using the global git_config functions because: git_config uses the global configuration, ignoring any repository-specific settings (e.g., in .git/config). repo_config loads the configuration specific to the repository, ensuring that the correct settings are used. repo_config_get_string_tmp retrieves configuration values relative to the repository, respecting any local overrides. This change ensures that cmd_psuh correctly reads the user.name setting that applies to the current repository, rather than relying on globalsettings that might be incorrect or misleading. It also demonstrates the correct way to access repository-specific configuration within Git commands. Signed-off-by: K Jayatheerth <[email protected]>
1 parent 7bb506d commit 746bde3

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

Documentation/MyFirstContribution.adoc

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -325,26 +325,48 @@ on the command line, including the name of our command. (If `prefix` is empty
325325
for you, try `cd Documentation/ && ../bin-wrappers/git psuh`). That's not so
326326
helpful. So what other context can we get?
327327

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

331331
----
332-
const char *cfg_name;
332+
#include "builtin.h"
333+
#include "gettext.h"
334+
#include "config.h"
335+
#include "repository.h"
333336
334-
...
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);
335346
336-
git_config(git_default_config, NULL);
337-
if (git_config_get_string_tmp("user.name", &cfg_name) > 0)
338-
printf(_("No name is found in config\n"));
339-
else
340-
printf(_("Your name: %s\n"), cfg_name);
347+
for (int i = 0; i < argc; i++) {
348+
printf("%d: %s\n", i, argv[i]);
349+
}
350+
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+
}
341363
----
342364

343-
`git_config()` will grab the configuration from config files known to Git and
344-
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
345367
a specific key ("user.name") and give you the value. There are a number of
346368
single-key lookup functions like this one; you can see them all (and more info
347-
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`.
348370

349371
You should see that the name printed matches the one you see when you run:
350372

@@ -377,7 +399,7 @@ status_init_config(&s, git_status_config);
377399
----
378400

379401
But as we drill down, we can find that `status_init_config()` wraps a call
380-
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.
381403

382404
Be sure to include the header to allow you to use `struct wt_status`:
383405

@@ -393,8 +415,8 @@ prepare it, and print its contents:
393415
394416
...
395417
396-
wt_status_prepare(the_repository, &status);
397-
git_config(git_default_config, &status);
418+
wt_status_prepare(repo, &status);
419+
repo_config(repo, git_default_config, &status);
398420
399421
...
400422

0 commit comments

Comments
 (0)