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

[PATCH 4/4] cmd_psuh: Prefer repo_config for config lookup

From
K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
Date
Apr 16, 2025, 06:14 UTC
Message-ID
<20250416061450.25695-5-jayatheerthkulkarni2005@gmail.com>
In-Reply-To
<20250416061450.25695-1-jayatheerthkulkarni2005@gmail.com>

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 <jayatheerthkulkarni2005@gmail.com>
---
 Documentation/MyFirstContribution.adoc | 52 ++++++++++++++++++--------
 1 file changed, 37 insertions(+), 15 deletions(-)
diff --git a/Documentation/MyFirstContribution.adoc b/Documentation/MyFirstContribution.adoc
index ed6dcc1fc6..688240ce8b 100644
--- a/Documentation/MyFirstContribution.adoc
+++ b/Documentation/MyFirstContribution.adoc
@@ -325,26 +325,48 @@ on the command line, including the name of our command. (If `prefix` is empty
 for you, try `cd Documentation/ && ../bin-wrappers/git psuh`). That's not so
 helpful. So what other context can we get?
 
-Add a line to `#include "config.h"`. Then, add the following bits to the
-function body:
+Add a line to `#include "config.h"` and `#include "repository.h"`. 
+Then, add the following bits to the function body:
 
 ----
-	const char *cfg_name;
+#include "builtin.h"
+#include "gettext.h"
+#include "config.h"
+#include "repository.h"  
 
-...
+int cmd_psuh(int argc, const char **argv, 
+			const char *prefix, struct repository *repo)
+{
+    const char *cfg_name;
+
+    printf(Q_("Your args (there is %d):\n",
+              "Your args (there are %d):\n",
+              argc),
+           argc);
 
-	git_config(git_default_config, NULL);
-	if (git_config_get_string_tmp("user.name", &cfg_name) > 0)
-		printf(_("No name is found in config\n"));
-	else
-		printf(_("Your name: %s\n"), cfg_name);
+    for (int i = 0; i < argc; i++) {
+        printf("%d: %s\n", i, argv[i]);
+    }
+
+    printf(_("Your current working directory:\n<top-level>%s%s\n"),
+           prefix ? "/" : "", prefix ? prefix : "");
+
+    repo_config(repo, git_default_config, NULL);
+
+    if (repo_config_get_string_tmp(repo, "user.name", &cfg_name))
+        printf(_("No name is found in config\n"));
+    else
+        printf(_("Your name: %s\n"), cfg_name);
+
+    return 0;
+}
 ----
 
-`git_config()` will grab the configuration from config files known to Git and
-apply standard precedence rules. `git_config_get_string_tmp()` will look up
+`repo_config()` will grab the configuration from config files known to Git and
+apply standard precedence rules. `repo_config_get_string_tmp()` will look up
 a specific key ("user.name") and give you the value. There are a number of
 single-key lookup functions like this one; you can see them all (and more info
-about how to use `git_config()`) in `Documentation/technical/api-config.adoc`.
+about how to use `repo_config()`) in `Documentation/technical/api-config.adoc`.
 
 You should see that the name printed matches the one you see when you run:
 
@@ -377,7 +399,7 @@ status_init_config(&s, git_status_config);
 ----
 
 But as we drill down, we can find that `status_init_config()` wraps a call
-to `git_config()`. Let's modify the code we wrote in the previous commit.
+to `repo_config()`. Let's modify the code we wrote in the previous commit.
 
 Be sure to include the header to allow you to use `struct wt_status`:
 
@@ -393,8 +415,8 @@ prepare it, and print its contents:
 
 ...
 
-	wt_status_prepare(the_repository, &status);
-	git_config(git_default_config, &status);
+	wt_status_prepare(repo, &status);
+	repo_config(repo, git_default_config, &status);
 
 ...
 
-- 
2.49.GIT
Previous: K JayatheerthNext: Emily Shaffer
Message 24 of 37 in “update MyFirstContribution with current code base”
  1. 0/4 update MyFirstContribution with current code baseK Jayatheerth, Apr 16, 2025
  2. 1/4 Remove unused git-mentoring mailing listK Jayatheerth, Apr 16, 2025
  3. 2/4 Docs: Correct cmd_psuh and Explain UNUSED macroK Jayatheerth, Apr 16, 2025
  4. Emily ShafferMay 16, 2025
  5. 3/4 Docs: Add cmd_psuh with repo and UNUSED removalK Jayatheerth, Apr 16, 2025
  6. Emily ShafferMay 16, 2025
  7. 1/3 docs: remove unused mentoring mailing list referenceK Jayatheerth, May 16, 2025
  8. 2/3 docs: clarify cmd_psuh signature and explain UNUSED macroK Jayatheerth, May 16, 2025
  9. Junio C HamanoMay 17, 2025
  10. Junio C HamanoMay 17, 2025
  11. 0/3 Update MyFirstContribution.adoc to follow modern practicesK Jayatheerth, May 18, 2025
  12. 1/3 docs: remove unused mentoring mailing list referenceK Jayatheerth, May 18, 2025
  13. 2/3 docs: clarify cmd_psuh signature and explain UNUSED macroK Jayatheerth, May 18, 2025
  14. 3/3 docs: replace git_config to repo_configK Jayatheerth, May 18, 2025
  15. JAYATHEERTH KMay 18, 2025
  16. 3/3 docs: replace git_config to repo_configK Jayatheerth, May 16, 2025
  17. Junio C HamanoMay 17, 2025
  18. Emily ShafferMay 16, 2025
  19. Junio C HamanoMay 17, 2025
  20. 0/3 Update MyFirstContribution.adoc to follow modern practicesK Jayatheerth, May 17, 2025
  21. 1/3 docs: remove unused mentoring mailing list referenceK Jayatheerth, May 17, 2025
  22. 2/3 docs: clarify cmd_psuh signature and explain UNUSED macroK Jayatheerth, May 17, 2025
  23. 3/3 docs: replace git_config to repo_configK Jayatheerth, May 17, 2025
  24. 4/4 cmd_psuh: Prefer repo_config for config lookupK Jayatheerth, Apr 16, 2025
  25. Emily ShafferMay 16, 2025
  26. JAYATHEERTH KMay 16, 2025
  27. Junio C HamanoApr 16, 2025
  28. JAYATHEERTH KApr 16, 2025
  29. JAYATHEERTH KMay 13, 2025
  30. Junio C HamanoMay 14, 2025
  31. JAYATHEERTH KMay 14, 2025
  32. Emily ShafferMay 15, 2025
  33. JAYATHEERTH KMay 16, 2025
  34. Junio C HamanoMay 16, 2025
  35. JAYATHEERTH KMay 16, 2025
  36. D. Ben KnobleMay 20, 2025
  37. Junio C HamanoMay 17, 2025

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.