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

Re: Minor documentation problems [RFC PATCH]

From
Sean <seanlkml@sympatico.ca>
Date
Nov 2, 2006, 12:39 UTC
Message-ID
<BAYC1-PASMTP018DA61B5F35F9603DF8A8AEFF0@CEZ.ICE>
In-Reply-To
<vpqmz7a1694.fsf@ecrins.imag.fr>

On Thu, 02 Nov 2006 11:40:23 +0100 Matthieu Moy <Matthieu.Moy@imag.fr> wrote:

Show 7 quoted lines
> * http://www.kernel.org/pub/software/scm/cogito/docs/cg-commit.1.html
>   Mentions .git/config, but not ~/.gitconfig (which is indeed _the_
>   place where I think most people want to set their name and email).
> 
>   Side note: it can be interesting to have a command to do this.
>   For example, bzr has "bzr whoami 'me <myself@myisp.com>'", which
>   avoids having to learn the config file syntax.
This is the git version :

$ git repo-config user.email "myself@myisp.com" $ git repo-config user.name "me"

Unfortunately repo-config doesn't update ~/.gitconfig only the .git/config file.

The patch below adds a --global option to allow:

$ git repo-config --global user.email "myself@myisp.com" $ git repo-config --global user.name "me"

Although the syntax is a bit depressing, it would seem to be the path of least resistance.

The patch below always updates ~/.gitconfig but perhaps it should respect GIT_CONFIG and/or GIT_CONFIG_LOCAL environment variables.

Sean
diff --git a/builtin-repo-config.c b/builtin-repo-config.c
index f60cee1..8c2b58a 100644
--- a/builtin-repo-config.c
+++ b/builtin-repo-config.c
@@ -127,9 +127,20 @@ free_strings:
 	return ret;
 }
 
+static int set_config(int global, const char* key, const char* value,
+		const char* value_regex, int multi_replace)
+{
+	if (global)
+		return git_global_config_set_multivar(key, value,
+				value_regex, multi_replace);
+	else
+		return git_config_set_multivar(key, value,
+				value_regex, multi_replace);
+}
+
 int cmd_repo_config(int argc, const char **argv, const char *prefix)
 {
-	int nongit = 0;
+	int nongit = 0, global = 0;
 	setup_git_directory_gently(&nongit);
 
 	while (1 < argc) {
@@ -139,6 +150,8 @@ int cmd_repo_config(int argc, const char
 			type = T_BOOL;
 		else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
 			return git_config(show_all_config);
+		else if (!strcmp(argv[1], "--global"))
+			global = 1;
 		else
 			break;
 		argc--;
@@ -150,9 +163,9 @@ int cmd_repo_config(int argc, const char
 		return get_value(argv[1], NULL);
 	case 3:
 		if (!strcmp(argv[1], "--unset"))
-			return git_config_set(argv[2], NULL);
+			return set_config(global, argv[2], NULL, NULL, 0);
 		else if (!strcmp(argv[1], "--unset-all"))
-			return git_config_set_multivar(argv[2], NULL, NULL, 1);
+			return set_config(global, argv[2], NULL, NULL, 1);
 		else if (!strcmp(argv[1], "--get"))
 			return get_value(argv[2], NULL);
 		else if (!strcmp(argv[1], "--get-all")) {
@@ -165,12 +178,12 @@ int cmd_repo_config(int argc, const char
 			return get_value(argv[2], NULL);
 		} else
 
-			return git_config_set(argv[1], argv[2]);
+			return set_config(global, argv[1], argv[2], NULL, 0);
 	case 4:
 		if (!strcmp(argv[1], "--unset"))
-			return git_config_set_multivar(argv[2], NULL, argv[3], 0);
+			return set_config(global, argv[2], NULL, argv[3], 0);
 		else if (!strcmp(argv[1], "--unset-all"))
-			return git_config_set_multivar(argv[2], NULL, argv[3], 1);
+			return set_config(global, argv[2], NULL, argv[3], 1);
 		else if (!strcmp(argv[1], "--get"))
 			return get_value(argv[2], argv[3]);
 		else if (!strcmp(argv[1], "--get-all")) {
@@ -183,13 +196,13 @@ int cmd_repo_config(int argc, const char
 			return get_value(argv[2], argv[3]);
 		} else if (!strcmp(argv[1], "--replace-all"))
 
-			return git_config_set_multivar(argv[2], argv[3], NULL, 1);
+			return set_config(global, argv[2], argv[3], NULL, 1);
 		else
 
-			return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
+			return set_config(global, argv[1], argv[2], argv[3], 0);
 	case 5:
 		if (!strcmp(argv[1], "--replace-all"))
-			return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
+			return set_config(global, argv[2], argv[3], argv[4], 1);
 	case 1:
 	default:
 		usage(git_config_set_usage);
diff --git a/cache.h b/cache.h
index d0a1657..5f7c599 100644
--- a/cache.h
+++ b/cache.h
@@ -402,6 +402,8 @@ extern int git_config_int(const char *,
 extern int git_config_bool(const char *, const char *);
 extern int git_config_set(const char *, const char *);
 extern int git_config_set_multivar(const char *, const char *, const char *, int);
+extern int git_global_config_set(const char*, const char*);
+extern int git_global_config_set_multivar(const char*, const char*, const char*, int);
 extern int check_repository_format_version(const char *var, const char *value);
 
 #define MAX_GITNAME (1000)
diff --git a/config.c b/config.c
index e8f0caf..0393b65 100644
--- a/config.c
+++ b/config.c
@@ -529,22 +529,16 @@ int git_config_set(const char* key, cons
  * - the config file is removed and the lock file rename()d to it.
  *
  */
-int git_config_set_multivar(const char* key, const char* value,
+int git_config_file_set_multivar(char* config_filename,
+	const char* key, const char* value,
 	const char* value_regex, int multi_replace)
 {
 	int i, dot;
 	int fd = -1, in_fd;
 	int ret;
-	char* config_filename;
 	char* lock_file;
 	const char* last_dot = strrchr(key, '.');
 
-	config_filename = getenv("GIT_CONFIG");
-	if (!config_filename) {
-		config_filename = getenv("GIT_CONFIG_LOCAL");
-		if (!config_filename)
-			config_filename  = git_path("config");
-	}
 	config_filename = xstrdup(config_filename);
 	lock_file = xstrdup(mkpath("%s.lock", config_filename));
 
@@ -742,3 +736,36 @@ out_free:
 }
 
 
+int git_config_set_multivar(const char* key, const char* value,
+	const char* value_regex, int multi_replace)
+{
+	char* config_filename;
+	config_filename = getenv("GIT_CONFIG");
+	if (!config_filename) {
+		config_filename = getenv("GIT_CONFIG_LOCAL");
+		if (!config_filename)
+			config_filename  = git_path("config");
+	}
+	return git_config_file_set_multivar(config_filename, key, value,
+				value_regex, multi_replace);
+}
+
+int git_global_config_set_multivar(const char* key, const char* value,
+	const char* value_regex, int multi_replace)
+{
+	int ret = -1;
+	const char *home = getenv("HOME");
+	if (home) {
+		char * global = xstrdup(mkpath("%s/.gitconfig", home));
+		ret = git_config_file_set_multivar(global, key, value,
+				value_regex, multi_replace);
+		free(global);
+	}
+	return ret;
+}
+
+int git_global_config_set(const char* key, const char* value)
+{
+	return git_global_config_set_multivar(key, value, NULL, 0);
+}
Previous: Matthieu MoyNext: Matthieu Moy
Message 4 of 9 in “Minor documentation problems”
  1. Matthieu MoyNov 2, 2006
  2. Petr BaudisNov 2, 2006
  3. Matthieu MoyNov 2, 2006
  4. SeanNov 2, 2006
  5. Matthieu MoyNov 2, 2006
  6. Johannes SchindelinNov 2, 2006
  7. SeanNov 2, 2006
  8. Johannes SchindelinNov 2, 2006
  9. Add --global option to git-repo-config.Sean, Nov 2, 2006

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.